Hi all
I got a piece of shader code that make object rotation around it’s origin.
Here it is:
Shader “rotate” {
** Properties {**
** _MainTex (“Base (RGB)”, 2D) = “white” {}**
** }**
** SubShader {**
** Tags { “RenderType”=“Opaque” }**
** LOD 200**
CGPROGRAM
** #pragma surface surf Lambert vertex:vertex_program**
#include “UnityCG.cginc”
** sampler2D MainTex;**
** float4x4 rotate(float3 r, float4 d) // r=rotations axes**
** {**
** float cx, cy, cz, sx, sy, sz;**
** sincos(r.x, sx, cx);**
** sincos(r.y, sy, cy);**
** sincos(r.z, sz, cz); **
__ return float4x4( cy*cz, -sz, sy, d.x,_
__ sz, cxcz, -sx, d.y,__
__ -sy, sx, cxcy, d.z,__
** 0, 0, 0, d.w ); **
** }**
void vertex_program(inout appdata_full v)
{
** float4x4 rot_mat;**
** rot_mat = rotate(float3(0,_Time.y/3,_Time.x),float4(0,0,0,1));**
** float4 a = v.vertex;**
** float4 b = mul(rot_mat, a);**
** v.vertex = b;**
}
** struct Input {**
** float2 uv_MainTex;**
** };**
** void surf (Input IN, inout SurfaceOutput o)**
** {**
** half4 c = tex2D (_MainTex, IN.uv_MainTex);**
** o.Albedo = c.rgb;**
** o.Alpha = c.a;**
** }**
ENDCG
** }**
}
It works pretty fine when i only rotate on 1 axis ( X or Y or Z ). The problem comes when i want to rotate
the vertex on 2 or 3 axes. It completely messes up the model projection. I suspect, as i’m a matrix math gimp
that the projection parameters in the 4x4 matrix have somthing to do with this.
Maybe someone knowing matrix maths could tell me more please ?
I found nothing about matrices in shaders nor vertex rotations here or on the web…
Maybe if this one works it would help a bit the community ?
What’s going wrong with those rotations ?
Thanks for your answers !