Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

SurfaceShader vert function

Discussion in 'Shaders' started by UnityLighting, Aug 23, 2016.

  1. UnityLighting

    UnityLighting

    Joined:
    Mar 31, 2015
    Posts:
    3,866
    hello

    I want to change texture UV in Vert function of Surface shader
    here is Vertex\Fragment code:



    I want same thing on Surface shader
    how can i use this code on Surface shader code?

    My purpose is change UV tiling on vertex shader of surface shader
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,329
    Use a custom vertex function.

    See the example for "Custom data computed per-vertex":
    https://docs.unity3d.com/Manual/SL-SurfaceShaderExamples.html

    Replace float3 customColor; with float2 customUV; and o.customColor = ... with o.customUV = v.texcoord.xy * _MainTex_ST.xy * 7 + _MainTex_ST.zw;

    Alternatively you could just use tex2D(_DetailTex, IN.uv_MainTex * 7) in your surface shader and get basically the same result; exactly the same if you don't use any texture offset.
     
    UnityLighting likes this.
  3. UnityLighting

    UnityLighting

    Joined:
    Mar 31, 2015
    Posts:
    3,866
    Last edited: Aug 24, 2016
  4. UnityLighting

    UnityLighting

    Joined:
    Mar 31, 2015
    Posts:
    3,866



    you help me a lot. I spend 7 hours yesterday without result and your help solve my hole problem.
    thanks again.


    my code:



    Code (CSharp):
    1. Shader "Custom/DetailSurface" {
    2.     Properties {
    3.         _MainTex ("Base (RGB)", 2D) = "white" {}
    4.         _DetailTex ("Base (RGB)", 2D) = "white" {}
    5.     }
    6.     SubShader {
    7.         Tags { "RenderType"="Opaque" }
    8.         LOD 200
    9.        
    10.         CGPROGRAM
    11.         #pragma surface surf Lambert vertex:vert
    12.         #pragma target 3.0
    13.         sampler2D _MainTex;
    14.         sampler2D _DetailTex;  
    15.  
    16.         struct Input {
    17.             float2 uv_MainTex;
    18.             float2 uv_DetailTex;
    19. float2 customUV;      
    20.            
    21.         };
    22.         void vert (inout appdata_full v, out Input o) {
    23.           UNITY_INITIALIZE_OUTPUT(Input,o);
    24.           o.customUV = v.texcoord.xy * 7;
    25.       }
    26.         void surf (Input IN, inout SurfaceOutput o) {
    27.             half4 c = tex2D (_MainTex, IN.uv_MainTex)*tex2D (_DetailTex, IN.customUV);
    28.             o.Albedo = c.rgb*7;
    29.  
    30.             o.Alpha = c.a;
    31.         }
    32.         ENDCG
    33.     }
    34.     FallBack "Diffuse"
    35. }
    36.  
    result on Mali400: