x


Problem of color in custom vertex shader

Hi,

I need to change the position of a lot of vertex in real-time, so I wanted to do that with shader ( is my first time with shader, be kind with me please ^^ ), I copie a simple example of shader of unity ( toonBasic ) and modify it for change the height of my vertex. My problem now is the color is not change with the normal so is very difficult to see the vertices moves, I try to compute my new normal, but still nothing change, so I try to add a pass of the built-in Diffuse shader, when I do that the color are well compute with the normal but I vertices go back to the original position ! is like it use just the pass of Diffuse and not my shader after.

if somebody now how I can change my position of my vertices and after compute the good color for have a diffuse affect, that will be so helpfull ^^ thanks

My shader code :

Shader "Toon/TestShader" {
    Properties {
        _Color ("Main Color", Color) = (.5,.5,.5,1)
        _MainTex ("Base (RGB)", 2D) = "white" {}
        _ToonShade ("ToonShader Cubemap(RGB)", CUBE) = "" { Texgen CubeNormal }
        _Wave("Wave", float) = .0
        _Xhole("Xhole", float) = .0
        _Yhole("Yhole", float) = .0
    }


    SubShader {
        Tags { "RenderType"="Opaque" }


        UsePass "Diffuse"
        Pass {
            Name "BASE"
            Cull Off

            CGPROGRAM

            #pragma vertex vert
            #pragma fragment frag
            #pragma fragmentoption ARB_precision_hint_fastest 

            #include "UnityCG.cginc"

            sampler2D _MainTex;
            samplerCUBE _ToonShade;
            float4 _MainTex_ST;
            float4 _Color;
            float _Wave;
            float _Xhole;
            float _Yhole;

            struct appdata {
                float4 vertex : POSITION;
                float2 texcoord : TEXCOORD0;
                float3 normal : NORMAL;
            };

            struct v2f {
                float4 pos : POSITION;
                float2 texcoord : TEXCOORD0;
                float3 cubenormal : TEXCOORD1;
            };

            v2f vert (appdata v)
            {


                float3 worldPos = mul (_Object2World, v.vertex).xyz;


                float4 hole;
                hole[0] = _Xhole;
                hole[1] = .0;
                hole[2] = _Yhole;
                hole[3] = .0;

                float4 VectDist;
                VectDist[0] =  worldPos[0] - hole[0];
                VectDist[1] =  0.0f; //worldPos[1] - hole[1];
                VectDist[2] =  worldPos[2] - hole[2];
                VectDist[3] =  0.0;

                float dist;
                dist = sqrt(dot(VectDist,VectDist));

                if(dist > 3.0)
                {
                    dist = 3.0;
                }
                float coef;
                coef = (1.0f - (dist/ 3.0)) ;
                if(coef < 0.0)
                {
                    coef = 0.0;
                }
                v.vertex.y += -5.0*coef;

                if( coef > 0 )
                {
                    worldPos = mul (_Object2World, v.vertex).xyz;

                    VectDist[0] =  hole[0] - worldPos[0];
                    VectDist[1] =  hole[1] - worldPos[1];
                    VectDist[2] =  hole[2] - worldPos[2];
                    VectDist[3] =  0.0;

                    v.normal = normalize( VectDist );
                }

                v2f o;
                o.pos = mul (UNITY_MATRIX_MVP, v.vertex);
                o.texcoord = TRANSFORM_TEX(v.texcoord, _MainTex);
                o.cubenormal = mul (UNITY_MATRIX_MV, float4(v.normal,0));


                return o;
            }

            float4 frag (v2f i) : COLOR
            {
                float4 col = _Color * tex2D(_MainTex, i.texcoord);
                float4 cube = texCUBE(_ToonShade, i.cubenormal);
                return col; // float4(2.0f * cube.rgb * col.rgb, col.a);
            }

            ENDCG           
        }

        //UsePass "Diffuse/BASE"
    } 

    Fallback "Diffuse"
}
more ▼

asked May 01 '11 at 12:23 PM

Teq gravatar image

Teq
1 1 1 1

(comments are locked)
10|3000 characters needed characters left

0 answers: sort voted first
Be the first one to answer this question
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1664
x138
x126
x122
x54

asked: May 01 '11 at 12:23 PM

Seen: 1071 times

Last Updated: May 01 '11 at 12:23 PM