Search Unity

Shader doesnt behave as intended?(Newbie to shaders)

Discussion in 'Scripting' started by TheOtherUserName, Feb 3, 2021.

  1. TheOtherUserName

    TheOtherUserName

    Joined:
    May 30, 2020
    Posts:
    136
    So I didn't work a lot with shaders and I downloaded a low poly water asset from the asset store but it is dramaticly resource expensive bc it calculates the vertecies on the cpu. I made an shader to handle this and used this tutorial as reference. I did not copy the code from the tutorial but used it to optimize the code from the asset. I dont get any errors but when running the game nothing happens.

    This is the shader code (it says 'C#' bc they dont provide the 'Shader' option):
    Code (CSharp):
    1. Shader "Custom/Waves"
    2. {
    3.     Properties
    4.     {
    5.         waveOriginPosition ("Wave Origin Position", Vector) = (0, 0, 0, 0)
    6.         waveHeight ("Wave Height", Float) = 0.5
    7.         waveFrequency ("Wave Frequency", Float) = 0.5
    8.         waveLength("Wave Length", Float) = 0.75
    9.  
    10.         _Color("Color", Color) = (1,1,1,1)
    11.         _MainTex("Albedo (RGB)", 2D) = "white" {}
    12.         _Glossiness("Smoothness", Range(0,1)) = 0.5
    13.         _Metallic("Metallic", Range(0,1)) = 0.0
    14.     }
    15.     SubShader
    16.     {
    17.         Tags { "RenderType"="Opaque" }
    18.         LOD 200
    19.  
    20.         CGPROGRAM
    21.         // Physically based Standard lighting model, and enable shadows on all light types
    22.         #pragma surface surf Standard fullforwardshadows
    23.  
    24.         // Use shader model 3.0 target, to get nicer looking lighting
    25.         #pragma target 3.0
    26.  
    27.         sampler2D _MainTex;
    28.  
    29.         struct Input
    30.         {
    31.             float2 uv_MainTex;
    32.         };
    33.  
    34.         UNITY_INSTANCING_BUFFER_START(Props)
    35.         UNITY_INSTANCING_BUFFER_END(Props)
    36.  
    37.         half _Glossiness;
    38.         half _Metallic;
    39.         fixed4 _Color;
    40.  
    41.         const float PI = 3.141592653589793238462;
    42.         float4 waveOriginPosition;
    43.         float waveHeight;
    44.         float waveFrequency;
    45.         float waveLength;
    46.  
    47.         void vert(inout appdata_full vertexData) {
    48.             float3 v = vertexData.vertex.xyz;
    49.  
    50.             //Initially set the wave height to 0
    51.             v.y = 0;
    52.  
    53.             float4 someVector = ((v.x - waveOriginPosition.x) * (v.x - waveOriginPosition.x), (v.y - waveOriginPosition.y) * (v.y - waveOriginPosition.y), (v.z - waveOriginPosition.z) * (v.z - waveOriginPosition.z), 0);
    54.  
    55.             //Get the distance between wave origin position and the current vertex
    56.             float distance = sqrt(someVector.x + someVector.y + someVector.z);
    57.             distance = (distance % waveLength) / waveLength;
    58.  
    59.             //Oscilate the wave height via sine to create a wave effect
    60.             v.y = waveHeight * sin(_Time.y * UNITY_PI * 50 * waveFrequency + (UNITY_PI * 50 * distance));
    61.  
    62.             //Update the vertex
    63.             vertexData.vertex.xyz = v;
    64.         }
    65.  
    66.         void surf (Input IN, inout SurfaceOutputStandard o)
    67.         {
    68.             fixed4 c = tex2D(_MainTex, IN.uv_MainTex) * _Color;
    69.             o.Albedo = c.rgb;
    70.             o.Metallic = _Metallic;
    71.             o.Smoothness = _Glossiness;
    72.             o.Alpha = c.a;
    73.         }
    74.         ENDCG
    75.     }
    76.     FallBack "Diffuse"
    77. }
    I tried optimizing this:
    Code (CSharp):
    1. void GenerateWaves()
    2.         {
    3.             for (int i = 0; i < vertices.Length; i++)
    4.             {
    5.                 Vector3 v = vertices[i];
    6.  
    7.                 //Initially set the wave height to 0
    8.                 v.y = 0.0f;
    9.  
    10.                 //Get the distance between wave origin position and the current vertex
    11.                 float distance = Vector3.Distance(v, waveOriginPosition);
    12.                 distance = (distance % waveLength) / waveLength;
    13.  
    14.                 //Oscilate the wave height via sine to create a wave effect
    15.                 v.y = waveHeight * Mathf.Sin(Time.time * Mathf.PI * 2.0f * waveFrequency + (Mathf.PI * 2.0f * distance));
    16.                
    17.                 //Update the vertex
    18.                 vertices[i] = v;
    19.             }
    20.  
    21.             //Update the mesh properties
    22.             mesh.vertices = vertices;
    23.             mesh.RecalculateNormals();
    24.             mesh.MarkDynamic();
    25.             meshFilter.mesh = mesh;
    26.         }
     
  2. csofranz

    csofranz

    Joined:
    Apr 29, 2017
    Posts:
    1,556
    It looks as if your vertex subshader isn't executed, you are probably missing the relevant #pragma directive
     
  3. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,909
    csofranz likes this.
  4. csofranz

    csofranz

    Joined:
    Apr 29, 2017
    Posts:
    1,556
    Oh, yes, sorry for being cryptic. In this case, though, the correct pragmas can be found here (it's apparently a surface shader).
     
    Last edited: Feb 3, 2021
    PraetorBlue likes this.