Jump to content

lua and GetMaterialShader


shadmar
 Share

Recommended Posts

How to do this in the editor, I only get a crash when calling GetMaterialShader ?

 

function object:Init()
       object:SetKey("material", "rock1.mat")
       material=self:GetKey("material", "rock1.mat")
  	 shader=GetMaterialShader(material)   --    =====> Crashes EXCESS VIOLATION
  	 SetShaderFloat(shader,"wavescale",2)
end

HP Omen - 16GB - i7 - Nvidia GTX 1060 6GB

Link to comment
Share on other sites

Here is my "openwater_low" lua w.i.p. script - you most likely want to use SetShaderVec4 in le2.5+, however.

It lets the user choose a model to draw the effect on ... not sure how instance safe it is though ...

 

require("scripts/class")
local class=CreateClass(...)
function class:InitDialog(grid)
self.super:InitDialog(grid)
group1 = grid:AddGroup("Model")
group1:AddProperty("usemodel", PROPERTY_FILE, "GMF Files (*.gmf):gmf", "Model File", "Model Files")
group1:AddProperty("waterdepth", PROPERTY_FLOAT, "2|0.0,20","Water depth")
group1:AddProperty("depthcolor",PROPERTY_COLOR,"176,176,227,89","Water Depth color")
group1:AddProperty("fogcolor",PROPERTY_COLOR,"99,99,130,32","Water Fog color")
group1:AddProperty("fogrange",PROPERTY_VEC2,"","Water Fog range")
group1:AddProperty("ambientintensity", PROPERTY_FLOAT, "2|0.0,4","Intensity")
group1:Expand(1)
end
function class:CreateObject(model)
local object = self.super:CreateObject(model)
object.model = model
 object.waterdepth = 6.0
object.fogcolor   = Vec4(0.39, 0.39, 0.51, .125)
 object.fogrange   = Vec2(0.0, 400.0)
object.depthcolor = Vec4(0.69, 0.69, 0.89, .35)
 object.ambientintensity = 1.0
 --Use the invisible material so the model is not visible, but can still be picked
object.model:Paint(LoadMaterial("abstract::invisible.mat"), 0)
function object:SetKey(key,value)
 if key=="usemodel" then
  if fw~=nil then
SetWorld(fw.transparency.world)
	-- does leak into other instances (one shader instance)
	--
	if self.useModel == nil then
	  self.useModel = LoadMesh("abstract::"..value )  
	end
if self.useModel ~= nil then
 self.useModel:SetPosition(object.model:GetPosition(0))
 self.useModel:SetRotation(object.model:GetRotation(0))
 self.useModel:SetScale(object.model:GetScale(0))
end
SetWorld(fw.main.world)
  end
 elseif key=="fogcolor" then
  color=string.Explode(value,",")
  r=tonumber(color[1])
  g=tonumber(color[2])
  b=tonumber(color[3])
  a=tonumber(color[4])
  if r==nil then r=255 end
  if g==nil then g=255 end
  if b==nil then b=255 end
  if a==nil then a=255 end
  self.fogcolor = Vec4(r/255.0,g/255.0,b/255.0,a/255.0)
 elseif key=="fogrange" then
  self.fogrange=StringToVec2(value)
 elseif key=="depthcolor" then
  color=string.Explode(value,",")
  r=tonumber(color[1])
  g=tonumber(color[2])
  b=tonumber(color[3])
  a=tonumber(color[4])
  if r==nil then r=255 end
  if g==nil then g=255 end
  if b==nil then b=255 end
  if a==nil then a=255 end
  self.depthcolor = Vec4(r/255.0,g/255.0,b/255.0,a/255.0)
elseif key=="ambientintensity" then
  self.ambientintensity = tonumber(value)
elseif key=="waterdepth" then
  self.waterdepth = tonumber(value)
 else
  return self.super:SetKey(key,value)
 end
 return 1
end
function object:GetKey(key,value)
 if key=='fogcolor' then
  return (self.fogcolor.x*255.0)..','..(self.fogcolor.y*255.0)..','..(self.fogcolor.z*255.0)..','..(self.fogcolor.w*255.0)
 elseif key=="fogrange" then
  return self.fogrange.x..","..self.fogrange.y
 elseif key=='depthcolor' then
  return (self.depthcolor.x*255.0)..','..(self.depthcolor.y*255.0)..','..(self.depthcolor.z*255.0)..','..(self.depthcolor.w*255.0)
 elseif key=='ambientintensity' then
  return self.ambientintensity
 elseif key=='waterdepth' then
  return self.waterdepth
 elseif key=='usemodel' then
  return self.useModel
 else
  return self.super:GetKey(key,value)
 end
 return value
end
function object:Refresh()
 if fw~=nil then
  SetWorld(fw.transparency.world)
  local mat=LoadMaterial("abstract::effect_oceanplane_low.mat")
  local shader=GetMaterialShader(mat)
  SetShaderFloat(shader,"waterdepth", self.waterdepth)
  SetShaderFloat(shader, "fogcolorr",self.fogcolor.x)
  SetShaderFloat(shader, "fogcolorg",self.fogcolor.y)
  SetShaderFloat(shader, "fogcolorb",self.fogcolor.z)
  SetShaderFloat(shader, "fogcolora",self.fogcolor.w)
  SetShaderFloat(shader, "fograngex",self.fogrange.x)
  SetShaderFloat(shader, "fograngey",self.fogrange.y)
  SetShaderFloat(shader, "depthcolorr",self.depthcolor.x)
  SetShaderFloat(shader, "depthcolorg",self.depthcolor.y)
  SetShaderFloat(shader, "depthcolorb",self.depthcolor.z)
  SetShaderFloat(shader, "depthcolora",self.depthcolor.w)
  SetShaderFloat(shader, "lightambient",self.ambientintensity)
  --Use the invisible material and hide the model
  self.model:Paint(LoadMaterial("abstract::invisible.mat"), 0)
  self.model:Hide()
  if self.useModel ~= nil then
self.useModel:Paint(mat)
self.useModel:UpdateLocalAABB()
self.useModel:UpdateAABB()
self.useModel:SetColor(self.model.color)
  end
  --Switch back to the main world
  SetWorld(fw.main.world)
 end
end
function object:UpdateMatrix()
 if fw~=nil then
  if object.useModel ~= nil then
self.useModel:SetPosition(object.model:GetPosition(0))
self.useModel:SetRotation(object.model:GetRotation(0))
self.useModel:SetScale(object.model:GetScale(0))
  end
 end
end
function object:UnlockKeys()
 self.super:UnlockKeys()
 self:Refresh()
end
function object:Free(model)
 local model,entity
 for model,entity in pairs(self.class.instances) do
  if entity~=self then
entity:Refresh()
break
  end
 end
 if self.useModel ~= nil then
  self.useModel:Free()
  self.useModel=nil
 end
 --self.super:Free()
 --Notify("Inside free")
end

end

 

PS:

i think it would be a good idea to have "effect_ambient" and "effect_sundirection" as a global with the dynamic "night&day" emitting these shader values but couldnt take a look into it further.

 

hth

  • Upvote 1

AMD 64 X2 Dual 5k - 4GB - XFX GForce9800GT - nv196.21 - WinXP Sp3

zBrush4R2 - Silo2Pro - Unwrap3DPro - Gile - MaPZone2.5

 

adv_banner-april2012_720x150_tex01.png

 

Xxploration FPS in progress ...

Link to comment
Share on other sites

      material=self:GetKey("material", "rock1.mat")
      shader=GetMaterialShader(material)   --	=====> Crashes EXCESS VIOLATION

 

"material" - is not a material but material name (path).

 

Try this:

      path=self:GetKey("material")
      material = LoadMaterial(path)
      shader=GetMaterialShader(material)

 

And don't forget to use "abstract::" for paths. It will be more secure.

  • Upvote 1
Link to comment
Share on other sites

You mean the existng one? You have to add a texture slot in the mat file and then blend it in the shader using GLSL. But I haven't tried a diffuse one, just an extra normalmap like this :

 

normal += texture2D(texture7,modelvertex.xz*scale).xyz * 2.0 - 1.0; 

 

(on both refraction and reflection normals)

HP Omen - 16GB - i7 - Nvidia GTX 1060 6GB

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...