Community Forum

Set custom texture for shader (runtime texture changing)

Forum Overview >> Scripting

Created15.07.2018 03:01

Martin Fabik (Unknown) 15.07.2018 03:01

I'm trying to create s shader with a custom texture that can be modified in runtime by LUA script. LUA will load the texture's image filename and then set the parameter of shader to this loaded value. I know a function setShaderParameter, but this function expects floats as the parameter. Is there a function to manipulate shader textures? Or I cannot change the texture by runtime?

Or does anybody knows any other way how to set texture in runtime?

Martin FabĂ­k

PS: I don't need switching textures by shader - I know this. I need to set the path to file with texture to display (I don't know texture before loading a mod - texture can be in another mod, etc...).

Gtx | Andy (GtX_Andy) 29.07.2018 02:58
Hey Martin,
Hope you have been well ;-)

I am not sure what you are changing but how about making the 'shape' in blender or maya and apply multiple materials to it.
You could then set a different texture for each material and change it in runtime with something like this..


-- LOAD--

self.objectToChange = Utils.indexToObject(id, getUserAttribute(id, "index"));

self.materials = {};
for i = 1, getNumMaterials(self.objectToChange) do
self.materials[i] = getMaterial(self.objectToChange, i-1);


setMaterial(self.objectToChange, self.materials[materialId], 0);


The other option if you are only changing the diffuse is to make use of maybe the 'tileAndMirrorShader'.
You can then apply the 2, 4, 6, 8 textures to one diffuse and then offset the UV position in runTime using something like this..

-- xTrans and yTrans 0 or 1 (0.5, 0.5 is UV scale for 4 images on one diffuse)
setShaderParameter(self.indexToChange, "RDT", 1, 1, 20, 20, false);
setShaderParameter(self.indexToChange, "mirrorScaleAndOffsetUV", 0.5, 0.5, xTrans, yTrans, false);
setShaderParameter(self.indexToChange, "atlasInvSizeAndOffsetUV", 1, 1, 0, 0, false);

I am not sure if any of this helps but just some thoughts.


Note: Log in to post. Create a new account here.