Hello
I’m trying to pass a custom world matrix 4x4 from c# to a shader using set matrix and setTRS, but it doesn’t work as expected, I looked for similar implementation on the forum and can’t find thread done any differently.
The function is from a class called within the start() of a monobehavior
//initialization
Matrix4x4 positionMatrix = Matrix4x4.identity;
//code inside the init function that does a bunch of stuff and pass to shader
//debug world matrix in shader
positionMatrix.SetTRS(
new Vector3(32,0,32), //position
Quaternion.Euler(
0,
0,
0
), //rotation
new Vector3(64,1,64) //scale
);
Debug.Log(positionMatrix);
directPass.SetMatrix("_PosMat", positionMatrix);
//end debug
The log look correct, I don’t really know what’s going on, the shader just stop functionning except if the setTRS is commented.
Anybody has idea of what’s going on?
Here the shader code relevant snippet
CGPROGRAM
#pragma vertex vertexProgram
#pragma fragment fragmentProcessing
#include "UnityCG.cginc"
#include "ShaderTools.cginc"
struct meshData
{
float4 vertex : POSITION;
float2 uv : TEXCOORD1;
fixed4 color : COLOR;
fixed3 normal : NORMAL;
};
struct rasterData
{
//float2 uv : TEXCOORD0;
float4 vertex : POSITION;
float4 wpos : TEXCOORD1;
fixed4 color : COLOR;
fixed3 wnormals : NORMAL;
};
float4x4 _PosMat;
rasterData vertexProgram (meshData input)
{
rasterData output;
// output.wpos = mul(unity_ObjectToWorld, input.vertex); //world position
output.wpos = mul ( _PosMat, input.vertex );//world position
output.vertex = unwrap(input.uv, input.vertex.w); //screen position
//output.vertex = UnityObjectToClipPos(input.vertex); //screen position
output.wnormals =UnityObjectToWorldNormal(input.normal); //normal to world normal
output.color = float4(input.uv, 0,1);// v.color;
return output;
}
sampler2D _Atlas;
float4 _MainLight;
float4 _Origin;
My hypothesis is that matrix 4x4 don’t work in start() before the unity render has been initialize, BUT I think it’s unlikely, passing other data works just fine (though, unlike matrix4x4 they have equivalent parameter block, even though I’m not using the parameter block), also the matrix as identity works correctly, it’s just after the TRS that it fails. Is there a BUG in setTRS to setMatrix?