Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

URP + VR +Single pass Instanced Terrain (shader)

Discussion in 'Universal Render Pipeline' started by FlightOfOne, Mar 18, 2020.

  1. FlightOfOne

    FlightOfOne

    Joined:
    Aug 1, 2014
    Posts:
    659
    I am having this problem where when I use Single pass Instanced terrain only gets drawn on left eye. All the other objects work fine, just the terrain with (URP>Terrain>Lit shader). Also, this works fine in single pass non-instanced.

    Any idea what might be happening?

    Thanks!
     
    Last edited: Mar 18, 2020
  2. jbarbascubero

    jbarbascubero

    Joined:
    Jun 25, 2020
    Posts:
    3
    I'm having the same issue. I found this guide but it doesn't seem to work with the terrain shader.

    Did you find any solution?
     
  3. jbarbascubero

    jbarbascubero

    Joined:
    Jun 25, 2020
    Posts:
    3
    I finally solved it following the guide I spoke about before. The shader you have to change is: Library\PackageCache\com.unity.render-pipelines.universal@7.1.8\Shaders\Terrain\TerrainLitPasses.hlsl

    Change the struct Varyings (line 39) to this:

    Code (CSharp):
    1. struct Varyings
    2. {
    3.     float4 uvMainAndLM              : TEXCOORD0; // xy: control, zw: lightmap
    4. #ifndef TERRAIN_SPLAT_BASEPASS
    5.     float4 uvSplat01                : TEXCOORD1; // xy: splat0, zw: splat1
    6.     float4 uvSplat23                : TEXCOORD2; // xy: splat2, zw: splat3
    7. #endif
    8.  
    9. #if defined(_NORMALMAP) && !defined(ENABLE_TERRAIN_PERPIXEL_NORMAL)
    10.     float4 normal                   : TEXCOORD3;    // xyz: normal, w: viewDir.x
    11.     float4 tangent                  : TEXCOORD4;    // xyz: tangent, w: viewDir.y
    12.     float4 bitangent                : TEXCOORD5;    // xyz: bitangent, w: viewDir.z
    13. #else
    14.     float3 normal                   : TEXCOORD3;
    15.     float3 viewDir                  : TEXCOORD4;
    16.     half3 vertexSH                  : TEXCOORD5; // SH
    17. #endif
    18.  
    19.     half4 fogFactorAndVertexLight   : TEXCOORD6; // x: fogFactor, yzw: vertex light
    20.     float3 positionWS               : TEXCOORD7;
    21.     float4 shadowCoord              : TEXCOORD8;
    22.     float4 clipPos                  : SV_POSITION;
    23.     UNITY_VERTEX_OUTPUT_STEREO //Insert
    24. };
    and the SplatmapVert (line 239) to this:

    Code (CSharp):
    1. Varyings SplatmapVert(Attributes v)
    2. {
    3.     Varyings o = (Varyings)0;
    4.  
    5.     UNITY_SETUP_INSTANCE_ID(v);
    6.     #if defined(UNITY_COMPILER_HLSL)
    7.     #define UNITY_INITIALIZE_OUTPUT(type,name) name = (type)0;
    8.     #else
    9.     #define UNITY_INITIALIZE_OUTPUT(type,name)
    10.     #endif
    11.     UNITY_INITIALIZE_OUTPUT(Varyings, o); //Insert
    12.     UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o); //Insert
    13.     TerrainInstancing(v.positionOS, v.normalOS, v.texcoord);
    14.  
    15.     VertexPositionInputs Attributes = GetVertexPositionInputs(v.positionOS.xyz);
    16.  
    17.     o.uvMainAndLM.xy = v.texcoord;
    18.     o.uvMainAndLM.zw = v.texcoord * unity_LightmapST.xy + unity_LightmapST.zw;
    19. #ifndef TERRAIN_SPLAT_BASEPASS
    20.     o.uvSplat01.xy = TRANSFORM_TEX(v.texcoord, _Splat0);
    21.     o.uvSplat01.zw = TRANSFORM_TEX(v.texcoord, _Splat1);
    22.     o.uvSplat23.xy = TRANSFORM_TEX(v.texcoord, _Splat2);
    23.     o.uvSplat23.zw = TRANSFORM_TEX(v.texcoord, _Splat3);
    24. #endif
    25.  
    26.     half3 viewDirWS = GetCameraPositionWS() - Attributes.positionWS;
    27. #if !SHADER_HINT_NICE_QUALITY
    28.     viewDirWS = SafeNormalize(viewDirWS);
    29. #endif
    30.  
    31. #if defined(_NORMALMAP) && !defined(ENABLE_TERRAIN_PERPIXEL_NORMAL)
    32.     float4 vertexTangent = float4(cross(float3(0, 0, 1), v.normalOS), 1.0);
    33.     VertexNormalInputs normalInput = GetVertexNormalInputs(v.normalOS, vertexTangent);
    34.  
    35.     o.normal = half4(normalInput.normalWS, viewDirWS.x);
    36.     o.tangent = half4(normalInput.tangentWS, viewDirWS.y);
    37.     o.bitangent = half4(normalInput.bitangentWS, viewDirWS.z);
    38. #else
    39.     o.normal = TransformObjectToWorldNormal(v.normalOS);
    40.     o.viewDir = viewDirWS;
    41.     o.vertexSH = SampleSH(o.normal);
    42. #endif
    43.     o.fogFactorAndVertexLight.x = ComputeFogFactor(Attributes.positionCS.z);
    44.     o.fogFactorAndVertexLight.yzw = VertexLighting(Attributes.positionWS, o.normal.xyz);
    45.     o.positionWS = Attributes.positionWS;
    46.     o.clipPos = Attributes.positionCS;
    47.  
    48. #ifdef _MAIN_LIGHT_SHADOWS
    49.     o.shadowCoord = GetShadowCoord(Attributes);
    50. #endif
    51.  
    52.     return o;
    53. }
     
    Last edited: Sep 8, 2020
  4. FlightOfOne

    FlightOfOne

    Joined:
    Aug 1, 2014
    Posts:
    659

    I ended up using non instanced and haven't revisited this. Are you using the XR Management?

    Thanks for sharing the solution!
     
  5. jbarbascubero

    jbarbascubero

    Joined:
    Jun 25, 2020
    Posts:
    3
    Yes, I'm working with XR plugin management and Oculus Quest
     
  6. PeterMills

    PeterMills

    Joined:
    Jun 20, 2013
    Posts:
    6
    I'm working on a different custom shader for VR Single Pass and this just got me past a massive block so thanks jbarbascubero.

    Although I have no idea what

    #define UNITY_INITIALIZE_OUTPUT(type,name) name = (type)0;


    is actually doing?