Thats it, im making a shader and I have to cancel the rotation of my mesh, it need to be always 0,0,0.
Scale and Position can be accessible from the inspector.
I have tried many shader around the web but none gave me the results, Nordeus GitHub have one similar but it also cancel the position of the object.
this is the nordeous one that cancel position and rotation
// The position parameter is stored in the normal
float4 wp = mul(UNITY_MATRIX_V, input.pos);
float3 position = half3(wp.x,wp.y,wp.z);
// Construct our orientation axes
float3 turnDir = _WorldSpaceCameraPos.xyz - position;
float3 y = float3(0, 1, 0);
float3 x = normalize(cross(turnDir, y));
float3 z = normalize(cross(y, x));
// Construct the transposed model matrix
float4x4 modelMatrix = float4x4
(
x.x, y.x, z.x, position.x,
x.y, y.y, z.y, position.y,
x.z, y.z, z.z, position.z,
0, 0, 0, 1
);
// Calculate the position
float4 worldPos = mul(modelMatrix, input.pos);
output.pos = mul(UNITY_MATRIX_VP, worldPos);
// Texture coordinates remain the same
output.texcoord = input.texcoord.xy;
output.color = input.color;
return output;
Curious, why not just have a script on the object that prevents changes to rotation? This would be better in most cases than added more GPU overhead, especially if you implement it with the ECS Job system.
If you also want to cancel the scale, this is really trivial to do, and super cheap. If you need to keep scale that gets more complex and expensive, though still doable. The one big caveat here is if your meshes are getting batched none of this will work and it does need to be done with a C# script as the rotation is all getting baked into the mesh.
That’s because that is getting the object position offset from specially encoded vertex data rather than using the original matrix’s position. Note that comment in line 1:
It’s also overriding the rotation with a new rotation rather than canceling it entirely.
For cancelling the rotation and scale, you just need to do this near the start of the vertex shader:
Those length() calls are relatively expensive, but really not that bad.
If you need object normals, you’ll also need to update the unity_WorldToObject matrix as well. For no scale it’s the same code as above, just applied to unity_WorldToObject. For scale preserving it’d be:
Because that’ll be way more expensive. For a past project I had maybe 50~100 objects resetting their rotation in a semi-jobified manner (predated real jobs), and that came up as the #2 most expensive operation in the entire game and was preventing us from hitting our framerate targets for PSVR. We had to limit the number of objects we modified the rotation of in C# and moved the rest to shader based. Setting the rotation on a transform makes Unity calculate the internal transform matrix, and I suspect there’s no fast-pass for zero rotation, so it’s still doing all of the calculations for that even though the resulting matrix is going to be nearly an identity matrix with the position offset. Technically the script in question was aiming objects toward the camera rather than cancelling the rotation, but similar idea.
As you say, that’s to create an aim direction and avoid setting rotation altogether though, while this user is trying to negate changes in rotation that have already happened and thus matrix recalc is happening regardless. Also, the system Unity has made does a lot of behind the scenes optimizations that you wouldn’t have been able to achieve with a classical job system without engine source access and considerable work no? Not to mention multi-threading.
Yes is expensive and im canceling the rotation using c# right now. The deal is that my game is CPU bounded and i barely use the GPU, so im trying to put all i can inside shaders now.
The main is to create a fake particle effect (i have to draw around 20 particles that right now cost me 4 draw call per particle in diferent positions) those draw call (80) are eating more than 5 fps + the 2Ms of particles effect Modules. So now im saving in Maya around 80 quad mesh inside one mesh and inserted a fake id inside the tangent of the vertexs, later inside my shaders i animate the vertex by the tangent id. it works nicely, looks like a very expensive particle effect but is one mesh, one draw call, one set pass and diferent positions. They also are billboard rotated to the camera, the issue is that if the designer change the fake particle rotation inside the inspector the billboard effect is broke, so i need it to be 0,0,0 if is posible inside the shader. I saw this technique on EA for Battlefield Particles and old Nordeus Tech Talk optimization techcniques.
Ofcorse if it do not work i just leave it with the c# code but i trust bgolus god, right now the cpu cost (rotation cancel) is lot less than having 80 draw calls as it was before, fragment shader is one texture and color multiplications, the vertex shaders have calculations but, 36 vertex * 80 ? i barely can check the clock diference or ms using Snapdragon profiler, is looks like win win for my project.
Also, it must run on OpenGL2.0, our target device are around 2015, the game is already released and we have to make space for new functions.