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

I need a shader God, pleaseee help me understand these couple things

Discussion in 'General Graphics' started by N1warhead, Oct 18, 2020.

  1. N1warhead

    N1warhead

    Joined:
    Mar 12, 2014
    Posts:
    3,884
    I beg you, someone please I'm so lost on this.
    I'm trying to do some stuff with a custom terrain shader. (Standard Pipeline).

    But doing the Input and SurfaceOutput structs is all but impossible.
    Why is it impossible? Because I have no idea what stuff goes in them by default just to even work so I can get to the stuff I want to actually add into them to do what I want to do.

    The terrain system has changed so dramatically from when I used to make them.

    Now there's dependencies things are literally all over the place and I have no idea what to even do. Terrain shaders could legit in the past be changed in a single shader file. It's just gotten so overly complex.

    I just simply want to do the SurfaceOutput and Input structs, is it really this complicated now? I'm genuinely surprised there's virtually no information out there on this and the bit of information I do find (E.G. - Copy pasting a shader to see if it works), the terrain either flashes fragments of bright blooming lights and triangles repeatedly on the screen or it just goes to a gray terrain.

    Here's the base terrain (it works). It's just the standard Diffuse terrain shader.
    Code (CSharp):
    1. // Unity built-in shader source. Copyright (c) 2016 Unity Technologies. MIT license (see license.txt)
    2.  
    3. Shader "Nature/Terrain/Diffuse" {
    4.     Properties {
    5.         // used in fallback on old cards & base map
    6.         [HideInInspector] _MainTex ("BaseMap (RGB)", 2D) = "white" {}
    7.         [HideInInspector] _Color ("Main Color", Color) = (1,1,1,1)
    8.         [HideInInspector] _TerrainHolesTexture("Holes Map (RGB)", 2D) = "white" {}
    9.     }
    10.  
    11.     CGINCLUDE
    12.         #pragma surface surf Lambert vertex:SplatmapVert finalcolor:SplatmapFinalColor finalprepass:SplatmapFinalPrepass finalgbuffer:SplatmapFinalGBuffer addshadow fullforwardshadows
    13.         #pragma instancing_options assumeuniformscaling nomatrices nolightprobe nolightmap forwardadd
    14.         #pragma multi_compile_fog
    15.         #include "TerrainSplatmapCommon.cginc"
    16.  
    17.         void surf(Input IN, inout SurfaceOutput o)
    18.         {
    19.             half4 splat_control;
    20.             half weight;
    21.             fixed4 mixedDiffuse;
    22.             SplatmapMix(IN, splat_control, weight, mixedDiffuse, o.Normal);
    23.             o.Albedo = mixedDiffuse.rgb;
    24.             o.Alpha = weight;
    25.         }
    26.     ENDCG
    27.  
    28.     Category {
    29.         Tags {
    30.             "Queue" = "Geometry-99"
    31.             "RenderType" = "Opaque"
    32.         }
    33.         // TODO: Seems like "#pragma target 3.0 _NORMALMAP" can't fallback correctly on less capable devices?
    34.         // Use two sub-shaders to simulate different features for different targets and still fallback correctly.
    35.         SubShader { // for sm3.0+ targets
    36.             CGPROGRAM
    37.                 #pragma target 3.0
    38.                 #pragma multi_compile_local __ _ALPHATEST_ON
    39.                 #pragma multi_compile_local __ _NORMALMAP
    40.             ENDCG
    41.  
    42.             UsePass "Hidden/Nature/Terrain/Utilities/PICKING"
    43.             UsePass "Hidden/Nature/Terrain/Utilities/SELECTION"
    44.         }
    45.         SubShader { // for sm2.0 targets
    46.             CGPROGRAM
    47.             ENDCG
    48.         }
    49.     }
    50.  
    51.     Dependency "AddPassShader"    = "Hidden/TerrainEngine/Splatmap/Diffuse-AddPass"
    52.     Dependency "BaseMapShader"    = "Hidden/TerrainEngine/Splatmap/Diffuse-Base"
    53.     Dependency "BaseMapGenShader" = "Hidden/TerrainEngine/Splatmap/Diffuse-BaseGen"
    54.     Dependency "Details0"         = "Hidden/TerrainEngine/Details/Vertexlit"
    55.     Dependency "Details1"         = "Hidden/TerrainEngine/Details/WavingDoublePass"
    56.     Dependency "Details2"         = "Hidden/TerrainEngine/Details/BillboardWavingDoublePass"
    57.     Dependency "Tree0"            = "Hidden/TerrainEngine/BillboardTree"
    58.  
    59.     Fallback "Diffuse"
    60. }
    61.  
    Okay, now what I'm wanting to do is simply this -
    Code (CSharp):
    1.     struct SurfaceOutput {
    2. // Whatever default goes in here, than the fixed and float2.
    3.         fixed val;
    4.         float2 screenUV;
    5.     };
    6.  
    7.         struct Input
    8.         {
    9. // Whatever default goes in here, then the below float4.
    10.             float4 screenPos;
    11.         };

    EDIT: And the error I always get when trying to do my Input struct is this
    "Shader error in 'Nature/Terrain/Diffuse': Unexpected token usertype. Expected: identifier at" (first line in Input)

    So it makes me believe I'm missing stuff to make it work that the splats, etc need. I guess - maybe not as Splats aren't an option in the shader file, as it's now a _MainTex (for whatever reason), old Terrain shaders had splat files, etc so I could do uv_SplatMap0 or whatever. (I've tried uv_MainTex, etc no difference.

    Edit 2: It's not that I don't know how the Input and such works - I just don't know what I'm missing to just add into it for the way terrain works now days. Before Splatmaps and such were directly right there in the shader, now it's not. So I'm sure there's something I just need to add to it that's a bit different now.
     
    Last edited: Oct 18, 2020
  2. BrandyStarbrite

    BrandyStarbrite

    Joined:
    Aug 4, 2013
    Posts:
    2,074