Recreate shader in Unity 3D with Cg -
i practicing @ writing shaders running problems. want create shader:
struct c3e4_output { float4 position : position; float4 color : color; }; c3e4_output c3e4v_twist(float2 position : position, float4 color : color, uniform float twisting) { c3e4_output out; float angle = twisting * length(position); float coslength, sinlength; sincos(angle, sinlength, coslength); out.position[0] = coslength * position[0] + -sinlength * position[1]; out.position[1] = sinlength * position[0] + coslength * position[1]; out.position[2] = 0; out.position[3] = 1; out.color = color; return out; } its http://http.developer.nvidia.com/cgtutorial/cg_tutorial_chapter03.html. have somthing result wierd.

this have:
shader "custom/noobshader_02" { properties { twisting ("twist", range(-10,10)) = 1 } subshader { pass{ cgprogram #pragma vertex vert #pragma fragment frag float twisting; struct vertexoutput { float4 pos : sv_position; float3 nor : normal; }; struct vertexinput { float4 vertex : position; float3 normal : normal; }; struct fragmentoutput { float4 color : color; }; vertexoutput vert (vertexinput i) { vertexoutput vout; vout.pos = mul(unity_matrix_mvp, i.vertex); float angle = twisting * length(i.vertex); float coslength, sinlength; sincos(angle, sinlength, coslength); vout.pos[0] = coslength * i.vertex[0] + -sinlength * i.vertex[1]; vout.pos[1] = sinlength * i.vertex[0] + coslength * i.vertex[1]; vout.pos[2] = 0; vout.pos[3] = 1; vout.nor[0] = coslength * i.normal[0] + -sinlength * i.normal[1]; vout.nor[1] = sinlength * i.normal[0] + coslength * i.normal[1]; vout.nor[2] = 0; return vout; } fragmentoutput frag() { fragmentoutput fout; float4 tempcol = {abs(_sintime.z),0,0,1}; fout.color = tempcol; return fout; } endcg } } fallback "diffuse" } thanks in advance!
i found anwser needed apply matrix after vertex manipulation:
shader "custom/noobshader_02" { properties { twisting ("twist", range(-10,10)) = 1 } subshader { pass{ cgprogram #pragma vertex vert #pragma fragment frag float twisting; struct vertexoutput { float4 pos : sv_position; float3 nor : normal; }; struct vertexinput { float4 vertex : position; float3 normal : normal; }; struct fragmentoutput { float4 color : color; }; vertexoutput vert (vertexinput i) { vertexoutput vout; float angle = twisting * length(i.vertex); float coslength, sinlength; sincos(angle, sinlength, coslength); i.vertex[0] = coslength * i.vertex[0] + -sinlength * i.vertex[1]; i.vertex[1] = sinlength * i.vertex[0] + coslength * i.vertex[1]; i.vertex[2] = 0; i.vertex[3] = 1; i.normal[0] = coslength * i.normal[0] + -sinlength * i.normal[1]; i.normal[1] = sinlength * i.normal[0] + coslength * i.normal[1]; i.normal[2] = 0; vout.pos = mul(unity_matrix_mvp, i.vertex); vout.nor = mul(unity_matrix_mvp, i.normal); return vout; } fragmentoutput frag() { fragmentoutput fout; float4 tempcol = {abs(_sintime.z),0,0,1}; fout.color = tempcol; return fout; } endcg } } fallback "diffuse" }
Comments
Post a Comment