I am trying to rotate a mesh with vertex shader.
I found this snippet and I am using it to rotate the mesh:
And in the vertex shader I do this, where v is the input struct and o is the output:
Problem is it is rotating around the world axis and not the mesh pivot. Why is it so if I am doing it before doing the UnityObjectToClipPos? I should be in object space. What am I missing?
Thanks! That was the problem, it was dynamically batched.
For completeness, in case somebody is looking to do the same, to disable dynamic batching only for some objects and not globally you can add a tag in the shader “DisableBatching” = “True” .
@00christian00 this is really interesting - any chance you could post your shader code up here, as I’m not sure how to put the rest of this together to rotate the mesh?
I’m not a shader expert to give advice, but anyway here is it. If you need to move automatically you multiply angle per _Time.y at row 49. In that case you will have angle per seconds as input.
Pump!
Hi @00christian00 , Did you figure how to do this? I’m having the same problem.
I think the first thing to do is figure out how to get the pivot point of each mesh in particles.
Thanks.
Particles are a different beast. By default particle systems are always batched meshes regardless of if you have disable batching in your shader. Particle systems simply had no other way of working.
Recently they added custom data streams, which includes the option to pass the particle’s position to each particle in the form of data stored in the mesh UVs. They also added support for instanced particles which give you this data even more readily. You’ll want to look at how the “Particles/Standard Unlit” shader works.
Not exactly, I’m looking for a way to rotate the particle mesh around its pivot like your first post mention
The problem can be visualized like this
I see there is a Center Vertex stream, I guess this is the Center of each mesh particle
If this is the center of the mesh, then this is the pivot I want to rotate around. But for now just assume this is the center of the mesh, how can I rotate around this center?
Here is my approach:
I generate the rotation matrix.
float3x3 YRotationMatrix(float degrees, float3 pivot)
{
float alpha = degrees * UNITY_PI / 180.0;
float s = sin(alpha);
float c = cos(alpha);
//But how can I insert the pivot???
return float3x3(
c, 0, -s,
0, 1, 0,
s, 0, c);
}
appdata_t vert (appdata_t v)
{
float4 pos = v.vertex;
//v.texcoord1.yzw is the pivot
pos.xyz = mul(YRotationMatrix(180,v.texcoord1.yzw), pos.xyz);
Except, as I stated above, this tag is ignored by particle systems. They are always batched unless you’re using a shader with particle system specific instancing code.