Search Unity

Confused about editting final vertex position

Discussion in 'Shaders' started by LawrieCo, Jul 31, 2014.

  1. LawrieCo

    LawrieCo

    Joined:
    Nov 9, 2012
    Posts:
    16
    Hey everyone, I'm trying to combine two shaders. The first is borrowed from another community member (forgot who) which bends the vertices based on the distance away from the camera

    Code (CSharp):
    1. Shader "Custom/Curved" {
    2.     Properties {
    3.         _MainTex ("Base (RGB)", 2D) = "white" {}
    4.         _QOffset ("Offset", Vector) = (0,0,0,0)
    5.         _Dist ("Distance", Float) = 100.0
    6.     }
    7.     SubShader {
    8.         Tags { "RenderType"="Opaque" }
    9.         Lighting On
    10.         Pass
    11.         {
    12.             CGPROGRAM
    13.             #pragma vertex vert
    14.             #pragma fragment frag
    15.             #include "UnityCG.cginc"
    16.  
    17.             sampler2D _MainTex;
    18.             float4 _QOffset;
    19.             float _Dist;
    20.            
    21.             struct v2f {
    22.                 float4 pos : SV_POSITION;
    23.                 float4 uv : TEXCOORD0;
    24.             };
    25.  
    26.             v2f vert (appdata_base v)
    27.             {
    28.                 v2f o;
    29.                 float4 vPos = mul (UNITY_MATRIX_MV, v.vertex);
    30.                 float zOff = vPos.z/_Dist;
    31.                 vPos += _QOffset*zOff*zOff;
    32.                 o.pos = mul (UNITY_MATRIX_P, vPos);
    33.                 o.uv = v.texcoord;
    34.                 return o;
    35.             }
    36.  
    37.             half4 frag (v2f i) : COLOR
    38.             {
    39.                 half4 col = tex2D(_MainTex, i.uv.xy);
    40.                 return col;
    41.             }
    42.             ENDCG
    43.         }
    44.     }
    45.     FallBack "Diffuse"
    46. }
    The 2nd is the lit outline toon shader that comes with unity. In the curve world shader we edit SV_POSITION to edit the final position of the vericies (I think). I won't post the toon shader or the whole of my combined as it's long, but from what I understand I had to change it from a vert and frag shader to a vert and surface shader. I no longer have the v2f to use and the SV_POSITION it had so the surface version of that is worldPos? However it doesn't seem to work. Can anyone give me a push in the right direction?

    Code (CSharp):
    1. struct Input {
    2.             float2 uv_MainTex;// : TEXCOORD0;
    3.             float3 worldPos;
    4.         };
    5.                
    6.         void vert (inout appdata_full v, out Input o)
    7.         {
    8.             UNITY_INITIALIZE_OUTPUT(Input,o);
    9.            
    10.             float4 vPos = mul (UNITY_MATRIX_MV, v.vertex);
    11.             float zOff = vPos.z/_Dist;
    12.             vPos += _QOffset*zOff*zOff;
    13.             o.worldPos = mul (UNITY_MATRIX_P, vPos);
    14.         }      
    15.        
    16.         void surf (Input IN, inout SurfaceOutput o)
    17.         {
    18.             half4 c = tex2D(_MainTex, IN.uv_MainTex) * _Color;
    19.             o.Albedo = c.rgb;
    20.             o.Alpha = c.a;
    21.         }