Search Unity

How to do "Single Pass Instanced" stereo rendering with custom geometry shader

Discussion in 'Shaders' started by briank, Jul 30, 2018.

  1. briank

    briank

    Joined:
    Mar 25, 2017
    Posts:
    74
    I have a geometry shader that generates new triangles from an input triangle. I'm trying to make this work on HoloLens using the "Single Pass Instanced" stereo rendering method.

    The issue I'm having is that the right eye is not rendering correctly. The position of what is being rendered is noticeably wrong (if you look through the left eye, things line up. If you look through the right eye, the mesh looks correct, but in the wrong location).

    I'm not sure if I'm missing something; Most examples I've seen don't use a geometry shader. Any ideas are appreciated.


    Roughly, shader code looks like this:

    struct appdata
    {
    float4 vertex : POSITION;
    UNITY_VERTEX_INPUT_INSTANCE_ID
    };

    struct v2g
    {
    float4 vertex : SV_POSITION;
    UNITY_VERTEX_OUTPUT_STEREO
    };

    struct g2f
    {
    float4 vertex : SV_POSITION;
    UNITY_VERTEX_OUTPUT_STEREO
    };

    v2g vert(appdata v)
    {
    v2g o;
    UNITY_SETUP_INSTANCE_ID(v);
    UNITY_INITIALIZE_OUTPUT(v2g, o);
    UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);

    o.vertex = UnityObjectToClipPos(v.vertex);​
    }

    [maxvertexcount(4)]
    void geom(triangle v2g input[3], inout TriangleStream<g2f> OutputStream)
    {
    // Geometry shader code generates a quad like shape from the input triangle.

    g2f v0 = (g2f)0;
    UNITY_TRANSFER_VERTEX_OUTPUT_STEREO(input[0], v0);
    v0.vertex = UnityWorldToClipPos(pos0);

    g2f v1 = (g2f)0;
    UNITY_TRANSFER_VERTEX_OUTPUT_STEREO(input[0], v1);
    v1.vertex = UnityWorldToClipPos(pos1);

    g2f v2 = (g2f)0;
    UNITY_TRANSFER_VERTEX_OUTPUT_STEREO(input[0], v2);
    v2.vertex = UnityWorldToClipPos(pos2);

    g2f v3 = (g2f)0;
    UNITY_TRANSFER_VERTEX_OUTPUT_STEREO(input[0], v3);
    v3.vertex = UnityWorldToClipPos(pos3);

    OutputStream.Append(v0);
    OutputStream.Append(v1);
    OutputStream.Append(v2);
    OutputStream.Append(v3);
    OutputStream.RestartStrip();​
    }
     
  2. mptp

    mptp

    Joined:
    May 30, 2014
    Posts:
    29
    I'm fairly sure it's because UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO is supposed to be called in the final shader phase before fragment (i.e. in the geo phase).

    I had the same problem as you and am working on it right now - I moved it to the geometry shader phase (one call per output vertex), but now I'm just not getting anything rendering.

    I'm sure together we can work this out! I'll post again if I can get it working :D
     
    briank likes this.
  3. mptp

    mptp

    Joined:
    May 30, 2014
    Posts:
    29
    Aha I've got it!

    1. define UNITY_VERTEX_INPUT_INSTANCE_ID in the input of both your vertex shader *and* your geometry shader
    2. define UNITY_VERTEX_OUTPUT_STEREO in the input of your fragment shader
    3. call UNITY_SETUP_INSTANCE_ID and UNITY_TRANSFER_INSTANCE_ID at the *beginning* of your vertex shader
    4. call UNITY_SETUP_INSTANCE_ID at the *beginning* of your geometry shader (this is the one that fixed the wrong positioning for me)
    5. call UNITY_INITIALIZE_OUTPUT_STEREO for each vertex you're emitting *before* you append it to whatever output stream you're using
     
    Last edited: Aug 2, 2018
    briank likes this.
  4. briank

    briank

    Joined:
    Mar 25, 2017
    Posts:
    74
    Thanks for taking a look! I trolled through Unity's cginc's and finally figured out what I need to do. The key part is getting the stereo eye index setup correctly at the beginning of the geom shader.

    I ended up doing #1, #2 and #3.

    For #4, instead of calling UNITY_SETUP_INSTANCE_ID at the beginning of the geometry shader, I used DEFAULT_UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input[0]).

    For #5, I continued using UNITY_TRANSFER_VERTEX_OUTPUT_STEREO for each emitted vertex.
     
  5. pan-master

    pan-master

    Joined:
    Nov 11, 2013
    Posts:
    127
    Could sombody fix that shader for VR?

    Code (CSharp):
    1.  
    2. Shader "Flex/FlexDrawFluid2" {
    3.     Properties {
    4.         _Color ("Color", Color) = (1,1,1,1)
    5.         //_MainTex ("Albedo (RGB)", 2D) = "white" {}
    6.         _Glossiness ("Smoothness", Range(0,1)) = 1.0
    7.         _Specular ("Specular", Color) = (0,0,0,1)
    8.         _Fresnel ("Fresnel", Range(0,1)) = 1.0
    9.     }
    10.     SubShader {
    11.         Tags { "Queue" = "Geometry" }//Transparent//Geometry
    12.         LOD 200
    13.        
    14.        
    15.     // ------------------------------------------------------------
    16.     // Surface shader code generated out of a CGPROGRAM block:
    17.    
    18.  
    19.     // ---- forward rendering base pass:
    20.     Pass {
    21.         Name "FORWARD"
    22.         Tags { "LightMode" = "ForwardBase" }
    23.         Cull off
    24.  
    25. CGPROGRAM
    26. // compile directives
    27. #pragma vertex vert_flex
    28. #pragma geometry geom_flex
    29. #pragma fragment frag_flex
    30. #pragma target 5.0
    31. #pragma multi_compile_fog
    32. #pragma multi_compile_fwdbase
    33. #include "HLSLSupport.cginc"
    34. #include "UnityShaderVariables.cginc"
    35. // Surface shader code generated based on:
    36. // writes to per-pixel normal: no
    37. // writes to emission: no
    38. // writes to occlusion: no
    39. // needs world space reflection vector: no
    40. // needs world space normal vector: no
    41. // needs screen space position: no
    42. // needs world space position: no
    43. // needs view direction: no
    44. // needs world space view direction: no
    45. // needs world space position for lighting: YES
    46. // needs world space view direction for lighting: YES
    47. // needs world space view direction for lightmaps: no
    48. // needs vertex color: no
    49. // needs VFACE: no
    50. // passes tangent-to-world matrix to pixel shader: no
    51. // reads from normal: no
    52. // 1 texcoords actually used
    53. //   float2 _MainTex
    54. #define UNITY_PASS_FORWARDBASE
    55. #include "UnityCG.cginc"
    56. #include "Lighting.cginc"
    57. #include "UnityPBSLighting.cginc"
    58. #include "AutoLight.cginc"
    59.  
    60. #define INTERNAL_DATA
    61. #define WorldReflectionVector(data,normal) data.worldRefl
    62. #define WorldNormalVector(data,normal) normal
    63.  
    64. // Original surface shader snippet:
    65. #line 10 ""
    66. #ifdef DUMMY_PREPROCESSOR_TO_WORK_AROUND_HLSL_COMPILER_LINE_HANDLING
    67. #endif
    68. /* UNITY: Original start of shader */
    69.         // Physically based Standard lighting model, and enable shadows on all light types
    70.         //#pragma surface surf StandardSpecular fullforwardshadows addshadow
    71.  
    72.         // Use shader model 3.0 target, to get nicer looking lighting
    73.         //#pragma target 3.0
    74.  
    75.         sampler2D _MainTex;
    76.  
    77.         struct Input {
    78.             float2 uv_MainTex;
    79.         };
    80.  
    81.         half _Fresnel;
    82.         half _Glossiness;
    83.         fixed4 _Color;
    84.         fixed4 _Specular;
    85.  
    86.         void surf (Input IN, inout SurfaceOutputStandardSpecular o) {
    87.             // Albedo comes from a texture tinted by color
    88.             fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
    89.             float3 vdir = normalize(mul(unity_CameraInvProjection, float4(IN.uv_MainTex * 2.0 - 1.0, 1, 1)));
    90.             float fren = saturate(1 - dot(vdir, mul(unity_WorldToCamera, o.Normal)));
    91.             //fren = 0.2 + 0.8 * (fren * fren * fren);//;_Fresnel
    92.             fren = _Fresnel + (1 - _Fresnel) * (fren * fren * fren);//;_Fresnel
    93.             fren = 0.0 + 0.6 * fren;
    94.             o.Albedo = 0;//c.rgb * (fren);
    95.             o.Emission = c.rgb * (1 - fren);
    96.             o.Specular = _Specular * (fren);
    97.             o.Smoothness = _Glossiness;
    98.             o.Alpha = 0;//c.a;
    99.         }
    100.        
    101.  
    102. // vertex-to-fragment interpolation data
    103. // no lightmaps:
    104. #ifndef LIGHTMAP_ON
    105. struct v2f_surf {
    106.   float4 pos : SV_POSITION;
    107.   float2 pack0 : TEXCOORD0; // _MainTex
    108.   half3 worldNormal : TEXCOORD1;
    109.   float3 worldPos : TEXCOORD2;
    110.   #if UNITY_SHOULD_SAMPLE_SH
    111.   half3 sh : TEXCOORD3; // SH
    112.   #endif
    113.   SHADOW_COORDS(4)
    114.   UNITY_FOG_COORDS(5)
    115.   #if SHADER_TARGET >= 30
    116.   float4 lmap : TEXCOORD6;
    117.   #endif
    118.   UNITY_VERTEX_INPUT_INSTANCE_ID
    119.   UNITY_VERTEX_OUTPUT_STEREO
    120. };
    121. #endif
    122. // with lightmaps:
    123. #ifdef LIGHTMAP_ON
    124. struct v2f_surf {
    125.   float4 pos : SV_POSITION;
    126.   float2 pack0 : TEXCOORD0; // _MainTex
    127.   half3 worldNormal : TEXCOORD1;
    128.   float3 worldPos : TEXCOORD2;
    129.   float4 lmap : TEXCOORD3;
    130.   SHADOW_COORDS(4)
    131.   UNITY_FOG_COORDS(5)
    132.   #ifdef DIRLIGHTMAP_COMBINED
    133.   fixed3 tSpace0 : TEXCOORD6;
    134.   fixed3 tSpace1 : TEXCOORD7;
    135.   fixed3 tSpace2 : TEXCOORD8;
    136.   #endif
    137.   UNITY_VERTEX_INPUT_INSTANCE_ID
    138.   UNITY_VERTEX_OUTPUT_STEREO
    139. };
    140. #endif
    141. float4 _MainTex_ST;
    142.  
    143. // vertex shader
    144. v2f_surf vert_surf (appdata_full v) {
    145.   UNITY_SETUP_INSTANCE_ID(v);
    146.   v2f_surf o;
    147.   UNITY_INITIALIZE_OUTPUT(v2f_surf,o);
    148.   UNITY_TRANSFER_INSTANCE_ID(v,o);
    149.   UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);
    150.   o.pos = UnityObjectToClipPos(v.vertex);
    151.   o.pack0.xy = TRANSFORM_TEX(v.texcoord, _MainTex);
    152.   float3 worldPos = mul(unity_ObjectToWorld, v.vertex).xyz;
    153.   fixed3 worldNormal = UnityObjectToWorldNormal(v.normal);
    154.   #if defined(LIGHTMAP_ON) && defined(DIRLIGHTMAP_COMBINED)
    155.   fixed3 worldTangent = UnityObjectToWorldDir(v.tangent.xyz);
    156.   fixed tangentSign = v.tangent.w * unity_WorldTransformParams.w;
    157.   fixed3 worldBinormal = cross(worldNormal, worldTangent) * tangentSign;
    158.   #endif
    159.   #if defined(LIGHTMAP_ON) && defined(DIRLIGHTMAP_COMBINED)
    160.   o.tSpace0 = float4(worldTangent.x, worldBinormal.x, worldNormal.x, worldPos.x);
    161.   o.tSpace1 = float4(worldTangent.y, worldBinormal.y, worldNormal.y, worldPos.y);
    162.   o.tSpace2 = float4(worldTangent.z, worldBinormal.z, worldNormal.z, worldPos.z);
    163.   #endif
    164.   o.worldPos = worldPos;
    165.   o.worldNormal = worldNormal;
    166.   #ifdef DYNAMICLIGHTMAP_ON
    167.   o.lmap.zw = v.texcoord2.xy * unity_DynamicLightmapST.xy + unity_DynamicLightmapST.zw;
    168.   #endif
    169.   #ifdef LIGHTMAP_ON
    170.   o.lmap.xy = v.texcoord1.xy * unity_LightmapST.xy + unity_LightmapST.zw;
    171.   #endif
    172.  
    173.   // SH/ambient and vertex lights
    174.   #ifndef LIGHTMAP_ON
    175.     #if UNITY_SHOULD_SAMPLE_SH
    176.       o.sh = 0;
    177.       // Approximated illumination from non-important point lights
    178.       #ifdef VERTEXLIGHT_ON
    179.         o.sh += Shade4PointLights (
    180.           unity_4LightPosX0, unity_4LightPosY0, unity_4LightPosZ0,
    181.           unity_LightColor[0].rgb, unity_LightColor[1].rgb, unity_LightColor[2].rgb, unity_LightColor[3].rgb,
    182.           unity_4LightAtten0, worldPos, worldNormal);
    183.       #endif
    184.       o.sh = ShadeSHPerVertex (worldNormal, o.sh);
    185.     #endif
    186.   #endif // !LIGHTMAP_ON
    187.  
    188.   TRANSFER_SHADOW(o); // pass shadow coordinates to pixel shader
    189.   UNITY_TRANSFER_FOG(o,o.pos); // pass fog coordinates to pixel shader
    190.   return o;
    191. }
    192.  
    193. // fragment shader
    194. fixed4 frag_surf (v2f_surf IN) : SV_Target {
    195.   UNITY_SETUP_INSTANCE_ID(IN);
    196.   // prepare and unpack data
    197.   Input surfIN;
    198.   UNITY_INITIALIZE_OUTPUT(Input,surfIN);
    199.   surfIN.uv_MainTex.x = 1.0;
    200.   surfIN.uv_MainTex = IN.pack0.xy;
    201.   float3 worldPos = IN.worldPos;
    202.   #ifndef USING_DIRECTIONAL_LIGHT
    203.     fixed3 lightDir = normalize(UnityWorldSpaceLightDir(worldPos));
    204.   #else
    205.     fixed3 lightDir = _WorldSpaceLightPos0.xyz;
    206.   #endif
    207.   fixed3 worldViewDir = normalize(UnityWorldSpaceViewDir(worldPos));
    208.   #ifdef UNITY_COMPILER_HLSL
    209.   SurfaceOutputStandardSpecular o = (SurfaceOutputStandardSpecular)0;
    210.   #else
    211.   SurfaceOutputStandardSpecular o;
    212.   #endif
    213.   o.Albedo = 0.0;
    214.   o.Emission = 0.0;
    215.   o.Specular = 0.0;
    216.   o.Alpha = 0.0;
    217.   o.Occlusion = 1.0;
    218.   fixed3 normalWorldVertex = fixed3(0,0,1);
    219.   o.Normal = IN.worldNormal;
    220.   normalWorldVertex = IN.worldNormal;
    221.  
    222.   // call surface function
    223.   surf (surfIN, o);
    224.  
    225.   // compute lighting & shadowing factor
    226.   UNITY_LIGHT_ATTENUATION(atten, IN, worldPos)
    227.   fixed4 c = 0;
    228.  
    229.   // Setup lighting environment
    230.   UnityGI gi;
    231.   UNITY_INITIALIZE_OUTPUT(UnityGI, gi);
    232.   gi.indirect.diffuse = 0;
    233.   gi.indirect.specular = 0;
    234.   #if !defined(LIGHTMAP_ON)
    235.       gi.light.color = _LightColor0.rgb;
    236.       gi.light.dir = lightDir;
    237.   #endif
    238.   // Call GI (lightmaps/SH/reflections) lighting function
    239.   UnityGIInput giInput;
    240.   UNITY_INITIALIZE_OUTPUT(UnityGIInput, giInput);
    241.   giInput.light = gi.light;
    242.   giInput.worldPos = worldPos;
    243.   giInput.worldViewDir = worldViewDir;
    244.   giInput.atten = atten;
    245.   #if defined(LIGHTMAP_ON) || defined(DYNAMICLIGHTMAP_ON)
    246.     giInput.lightmapUV = IN.lmap;
    247.   #else
    248.     giInput.lightmapUV = 0.0;
    249.   #endif
    250.   #if UNITY_SHOULD_SAMPLE_SH
    251.     giInput.ambient = IN.sh;
    252.   #else
    253.     giInput.ambient.rgb = 0.0;
    254.   #endif
    255.   giInput.probeHDR[0] = unity_SpecCube0_HDR;
    256.   giInput.probeHDR[1] = unity_SpecCube1_HDR;
    257.   #if UNITY_SPECCUBE_BLENDING || UNITY_SPECCUBE_BOX_PROJECTION
    258.     giInput.boxMin[0] = unity_SpecCube0_BoxMin; // .w holds lerp value for blending
    259.   #endif
    260.   #if UNITY_SPECCUBE_BOX_PROJECTION
    261.     giInput.boxMax[0] = unity_SpecCube0_BoxMax;
    262.     giInput.probePosition[0] = unity_SpecCube0_ProbePosition;
    263.     giInput.boxMax[1] = unity_SpecCube1_BoxMax;
    264.     giInput.boxMin[1] = unity_SpecCube1_BoxMin;
    265.     giInput.probePosition[1] = unity_SpecCube1_ProbePosition;
    266.   #endif
    267.   LightingStandardSpecular_GI(o, giInput, gi);
    268.  
    269.   // realtime lighting: call lighting function
    270.   c += LightingStandardSpecular (o, worldViewDir, gi);
    271.   c.rgb += o.Emission;
    272.   UNITY_APPLY_FOG(IN.fogCoord, c); // apply fog
    273.   UNITY_OPAQUE_ALPHA(c.a);
    274.   return c;
    275. }
    276.  
    277. #include "FlexDrawFluid.cginc"
    278.  
    279. ENDCG
    280.  
    281. }
    282.  
    283.     // ---- forward rendering additive lights pass:
    284.     Pass {
    285.         Name "FORWARD"
    286.         Tags { "LightMode" = "ForwardAdd" }
    287.         ZWrite Off Blend One One
    288.  
    289. CGPROGRAM
    290. // compile directives
    291. #pragma vertex vert_flex
    292. #pragma geometry geom_flex
    293. #pragma fragment frag_flex
    294. #pragma target 5.0
    295. #pragma multi_compile_fog
    296. #pragma multi_compile_fwdadd_fullshadows
    297. #pragma skip_variants INSTANCING_ON
    298. #include "HLSLSupport.cginc"
    299. #include "UnityShaderVariables.cginc"
    300. // Surface shader code generated based on:
    301. // writes to per-pixel normal: no
    302. // writes to emission: no
    303. // writes to occlusion: no
    304. // needs world space reflection vector: no
    305. // needs world space normal vector: no
    306. // needs screen space position: no
    307. // needs world space position: no
    308. // needs view direction: no
    309. // needs world space view direction: no
    310. // needs world space position for lighting: YES
    311. // needs world space view direction for lighting: YES
    312. // needs world space view direction for lightmaps: no
    313. // needs vertex color: no
    314. // needs VFACE: no
    315. // passes tangent-to-world matrix to pixel shader: no
    316. // reads from normal: no
    317. // 1 texcoords actually used
    318. //   float2 _MainTex
    319. #define UNITY_PASS_FORWARDADD
    320. #include "UnityCG.cginc"
    321. #include "Lighting.cginc"
    322. #include "UnityPBSLighting.cginc"
    323. #include "AutoLight.cginc"
    324.  
    325. #define INTERNAL_DATA
    326. #define WorldReflectionVector(data,normal) data.worldRefl
    327. #define WorldNormalVector(data,normal) normal
    328.  
    329. // Original surface shader snippet:
    330. #line 10 ""
    331. #ifdef DUMMY_PREPROCESSOR_TO_WORK_AROUND_HLSL_COMPILER_LINE_HANDLING
    332. #endif
    333. /* UNITY: Original start of shader */
    334.         // Physically based Standard lighting model, and enable shadows on all light types
    335.         //#pragma surface surf StandardSpecular fullforwardshadows addshadow
    336.  
    337.         // Use shader model 3.0 target, to get nicer looking lighting
    338.         //#pragma target 3.0
    339.  
    340.         sampler2D _MainTex;
    341.  
    342.         struct Input {
    343.             float2 uv_MainTex;
    344.         };
    345.  
    346.         half _Glossiness;
    347.         fixed4 _Color;
    348.         fixed4 _Specular;
    349.  
    350.         void surf (Input IN, inout SurfaceOutputStandardSpecular o) {
    351.             // Albedo comes from a texture tinted by color
    352.             fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
    353.             o.Albedo = c.rgb;
    354.             // Specular and smoothness come from slider variables
    355.             o.Specular = _Specular;
    356.             o.Smoothness = _Glossiness;
    357.             o.Alpha = c.a;
    358.         }
    359.        
    360.  
    361. // vertex-to-fragment interpolation data
    362. struct v2f_surf {
    363.   float4 pos : SV_POSITION;
    364.   float2 pack0 : TEXCOORD0; // _MainTex
    365.   half3 worldNormal : TEXCOORD1;
    366.   float3 worldPos : TEXCOORD2;
    367.   SHADOW_COORDS(3)
    368.   UNITY_FOG_COORDS(4)
    369. };
    370. float4 _MainTex_ST;
    371.  
    372. // vertex shader
    373. v2f_surf vert_surf (appdata_full v) {
    374.   v2f_surf o;
    375.   UNITY_INITIALIZE_OUTPUT(v2f_surf,o);
    376.   o.pos = UnityObjectToClipPos(v.vertex);
    377.   o.pack0.xy = TRANSFORM_TEX(v.texcoord, _MainTex);
    378.   float3 worldPos = mul(unity_ObjectToWorld, v.vertex).xyz;
    379.   fixed3 worldNormal = UnityObjectToWorldNormal(v.normal);
    380.   o.worldPos = worldPos;
    381.   o.worldNormal = worldNormal;
    382.  
    383.   TRANSFER_SHADOW(o); // pass shadow coordinates to pixel shader
    384.   UNITY_TRANSFER_FOG(o,o.pos); // pass fog coordinates to pixel shader
    385.   return o;
    386. }
    387.  
    388. // fragment shader
    389. fixed4 frag_surf (v2f_surf IN) : SV_Target {
    390.   // prepare and unpack data
    391.   Input surfIN;
    392.   UNITY_INITIALIZE_OUTPUT(Input,surfIN);
    393.   surfIN.uv_MainTex.x = 1.0;
    394.   surfIN.uv_MainTex = IN.pack0.xy;
    395.   float3 worldPos = IN.worldPos;
    396.   #ifndef USING_DIRECTIONAL_LIGHT
    397.     fixed3 lightDir = normalize(UnityWorldSpaceLightDir(worldPos));
    398.   #else
    399.     fixed3 lightDir = _WorldSpaceLightPos0.xyz;
    400.   #endif
    401.   fixed3 worldViewDir = normalize(UnityWorldSpaceViewDir(worldPos));
    402.   #ifdef UNITY_COMPILER_HLSL
    403.   SurfaceOutputStandardSpecular o = (SurfaceOutputStandardSpecular)0;
    404.   #else
    405.   SurfaceOutputStandardSpecular o;
    406.   #endif
    407.   o.Albedo = 0.0;
    408.   o.Emission = 0.0;
    409.   o.Specular = 0.0;
    410.   o.Alpha = 0.0;
    411.   o.Occlusion = 1.0;
    412.   fixed3 normalWorldVertex = fixed3(0,0,1);
    413.   o.Normal = IN.worldNormal;
    414.   normalWorldVertex = IN.worldNormal;
    415.  
    416.   // call surface function
    417.   surf (surfIN, o);
    418.   UNITY_LIGHT_ATTENUATION(atten, IN, worldPos)
    419.   fixed4 c = 0;
    420.  
    421.   // Setup lighting environment
    422.   UnityGI gi;
    423.   UNITY_INITIALIZE_OUTPUT(UnityGI, gi);
    424.   gi.indirect.diffuse = 0;
    425.   gi.indirect.specular = 0;
    426.   #if !defined(LIGHTMAP_ON)
    427.       gi.light.color = _LightColor0.rgb;
    428.       gi.light.dir = lightDir;
    429.   #endif
    430.   gi.light.color *= atten;
    431.   c += LightingStandardSpecular (o, worldViewDir, gi);
    432.   c.a = 0.0;
    433.   UNITY_APPLY_FOG(IN.fogCoord, c); // apply fog
    434.   UNITY_OPAQUE_ALPHA(c.a);
    435.   return c;
    436. }
    437.  
    438. #include "FlexDrawFluid.cginc"
    439.  
    440. ENDCG
    441.  
    442. }
    443.  
    444.     // ---- deferred shading pass:
    445.     Pass {
    446.         Name "DEFERRED"
    447.         Tags { "LightMode" = "Deferred" }
    448.  
    449. CGPROGRAM
    450. // compile directives
    451. #pragma vertex vert_flex
    452. #pragma geometry geom_flex
    453. #pragma fragment frag_flex
    454. #pragma target 5.0
    455. #pragma exclude_renderers nomrt
    456. #pragma multi_compile_prepassfinal
    457. #pragma skip_variants FOG_LINEAR FOG_EXP FOG_EXP2
    458. #include "HLSLSupport.cginc"
    459. #include "UnityShaderVariables.cginc"
    460. // Surface shader code generated based on:
    461. // writes to per-pixel normal: no
    462. // writes to emission: no
    463. // writes to occlusion: no
    464. // needs world space reflection vector: no
    465. // needs world space normal vector: no
    466. // needs screen space position: no
    467. // needs world space position: no
    468. // needs view direction: no
    469. // needs world space view direction: no
    470. // needs world space position for lighting: YES
    471. // needs world space view direction for lighting: YES
    472. // needs world space view direction for lightmaps: no
    473. // needs vertex color: no
    474. // needs VFACE: no
    475. // passes tangent-to-world matrix to pixel shader: no
    476. // reads from normal: YES
    477. // 1 texcoords actually used
    478. //   float2 _MainTex
    479. #define UNITY_PASS_DEFERRED
    480. #include "UnityCG.cginc"
    481. #include "Lighting.cginc"
    482. #include "UnityPBSLighting.cginc"
    483.  
    484. #define INTERNAL_DATA
    485. #define WorldReflectionVector(data,normal) data.worldRefl
    486. #define WorldNormalVector(data,normal) normal
    487.  
    488. // Original surface shader snippet:
    489. #line 10 ""
    490. #ifdef DUMMY_PREPROCESSOR_TO_WORK_AROUND_HLSL_COMPILER_LINE_HANDLING
    491. #endif
    492. /* UNITY: Original start of shader */
    493.         // Physically based Standard lighting model, and enable shadows on all light types
    494.         //#pragma surface surf StandardSpecular fullforwardshadows addshadow
    495.  
    496.         // Use shader model 3.0 target, to get nicer looking lighting
    497.         //#pragma target 3.0
    498.  
    499.         sampler2D _MainTex;
    500.  
    501.         struct Input {
    502.             float2 uv_MainTex;
    503.         };
    504.  
    505.         half _Glossiness;
    506.         fixed4 _Color;
    507.         fixed4 _Specular;
    508.  
    509.         void surf (Input IN, inout SurfaceOutputStandardSpecular o) {
    510.             // Albedo comes from a texture tinted by color
    511.             fixed4 c = tex2D (_MainTex, IN.uv_MainTex);// * _Color;
    512.             o.Albedo = c.rgb; //o.Emission = 1;//c.rgb;
    513.             // Specular and smoothness come from slider variables
    514.             o.Specular = _Specular;
    515.             o.Smoothness = _Glossiness;
    516.             o.Alpha = c.a;
    517.         }
    518.        
    519.  
    520. // vertex-to-fragment interpolation data
    521. struct v2f_surf {
    522.   float4 pos : SV_POSITION;
    523.   float2 pack0 : TEXCOORD0; // _MainTex
    524.   half3 worldNormal : TEXCOORD1;
    525.   float3 worldPos : TEXCOORD2;
    526. #ifndef DIRLIGHTMAP_OFF
    527.   half3 viewDir : TEXCOORD3;
    528. #endif
    529.   float4 lmap : TEXCOORD4;
    530. #ifndef LIGHTMAP_ON
    531.   #if UNITY_SHOULD_SAMPLE_SH
    532.     half3 sh : TEXCOORD5; // SH
    533.   #endif
    534. #else
    535.   #ifdef DIRLIGHTMAP_OFF
    536.     float4 lmapFadePos : TEXCOORD5;
    537.   #endif
    538. #endif
    539.   UNITY_VERTEX_INPUT_INSTANCE_ID
    540.   UNITY_VERTEX_OUTPUT_STEREO
    541. };
    542. float4 _MainTex_ST;
    543.  
    544. // vertex shader
    545. v2f_surf vert_surf (appdata_full v) {
    546.   UNITY_SETUP_INSTANCE_ID(v);
    547.   v2f_surf o;
    548.   UNITY_INITIALIZE_OUTPUT(v2f_surf,o);
    549.   UNITY_TRANSFER_INSTANCE_ID(v,o);
    550.   UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);
    551.   o.pos = UnityObjectToClipPos(v.vertex);
    552.   o.pack0.xy = TRANSFORM_TEX(v.texcoord, _MainTex);
    553.   float3 worldPos = mul(unity_ObjectToWorld, v.vertex).xyz;
    554.   fixed3 worldNormal = UnityObjectToWorldNormal(v.normal);
    555.   o.worldPos = worldPos;
    556.   o.worldNormal = worldNormal;
    557.   float3 viewDirForLight = UnityWorldSpaceViewDir(worldPos);
    558.   #ifndef DIRLIGHTMAP_OFF
    559.   o.viewDir = viewDirForLight;
    560.   #endif
    561. #ifdef DYNAMICLIGHTMAP_ON
    562.   o.lmap.zw = v.texcoord2.xy * unity_DynamicLightmapST.xy + unity_DynamicLightmapST.zw;
    563. #else
    564.   o.lmap.zw = 0;
    565. #endif
    566. #ifdef LIGHTMAP_ON
    567.   o.lmap.xy = v.texcoord1.xy * unity_LightmapST.xy + unity_LightmapST.zw;
    568.   #ifdef DIRLIGHTMAP_OFF
    569.     o.lmapFadePos.xyz = (mul(unity_ObjectToWorld, v.vertex).xyz - unity_ShadowFadeCenterAndType.xyz) * unity_ShadowFadeCenterAndType.w;
    570.     o.lmapFadePos.w = (-UnityObjectToViewPos(v.vertex).z) * (1.0 - unity_ShadowFadeCenterAndType.w);
    571.   #endif
    572. #else
    573.   o.lmap.xy = 0;
    574.     #if UNITY_SHOULD_SAMPLE_SH
    575.       o.sh = 0;
    576.       o.sh = ShadeSHPerVertex (worldNormal, o.sh);
    577.     #endif
    578. #endif
    579.   return o;
    580. }
    581. #ifdef LIGHTMAP_ON
    582. float4 unity_LightmapFade;
    583. #endif
    584. fixed4 unity_Ambient;
    585.  
    586. // fragment shader
    587. void frag_surf(v2f_surf IN,
    588.                out half4 outGBuffer0 : SV_Target0,
    589.                out half4 outGBuffer1 : SV_Target1,
    590.                out half4 outGBuffer2 : SV_Target2,
    591.                out half4 outEmission : SV_Target3)
    592. {
    593.     UNITY_SETUP_INSTANCE_ID(IN);
    594.     // prepare and unpack data
    595.     Input surfIN;
    596.     UNITY_INITIALIZE_OUTPUT(Input, surfIN);
    597.     surfIN.uv_MainTex.x = 1.0;
    598.     surfIN.uv_MainTex = IN.pack0.xy;
    599.     float3 worldPos = IN.worldPos;
    600. #ifndef USING_DIRECTIONAL_LIGHT
    601.     fixed3 lightDir = normalize(UnityWorldSpaceLightDir(worldPos));
    602. #else
    603.     fixed3 lightDir = _WorldSpaceLightPos0.xyz;
    604. #endif
    605.     fixed3 worldViewDir = normalize(UnityWorldSpaceViewDir(worldPos));
    606. #ifdef UNITY_COMPILER_HLSL
    607.     SurfaceOutputStandardSpecular o = (SurfaceOutputStandardSpecular)0;
    608. #else
    609.     SurfaceOutputStandardSpecular o;
    610. #endif
    611.     o.Albedo = 0.0;
    612.     o.Emission = 0.0;
    613.     o.Specular = 0.0;
    614.     o.Alpha = 0.0;
    615.     o.Occlusion = 1.0;
    616.     fixed3 normalWorldVertex = fixed3(0, 0, 1);
    617.     o.Normal = IN.worldNormal;
    618.     normalWorldVertex = IN.worldNormal;
    619.  
    620.     // call surface function
    621.     surf(surfIN, o);
    622.     fixed3 originalNormal = o.Normal;
    623.     half atten = 1;
    624.  
    625.     // Setup lighting environment
    626.     UnityGI gi;
    627.     UNITY_INITIALIZE_OUTPUT(UnityGI, gi);
    628.     gi.indirect.diffuse = 0;
    629.     gi.indirect.specular = 0;
    630.     gi.light.color = 0;
    631.     gi.light.dir = half3(0, 1, 0);
    632.     // Call GI (lightmaps/SH/reflections) lighting function
    633.     UnityGIInput giInput;
    634.     UNITY_INITIALIZE_OUTPUT(UnityGIInput, giInput);
    635.     giInput.light = gi.light;
    636.     giInput.worldPos = worldPos;
    637.     giInput.worldViewDir = worldViewDir;
    638.     giInput.atten = atten;
    639. #if defined(LIGHTMAP_ON) || defined(DYNAMICLIGHTMAP_ON)
    640.     giInput.lightmapUV = IN.lmap;
    641. #else
    642.     giInput.lightmapUV = 0.0;
    643. #endif
    644. #if UNITY_SHOULD_SAMPLE_SH
    645.     giInput.ambient = IN.sh;
    646. #else
    647.     giInput.ambient.rgb = 0.0;
    648. #endif
    649.     giInput.probeHDR[0] = unity_SpecCube0_HDR;
    650.     giInput.probeHDR[1] = unity_SpecCube1_HDR;
    651. #if UNITY_SPECCUBE_BLENDING || UNITY_SPECCUBE_BOX_PROJECTION
    652.     giInput.boxMin[0] = unity_SpecCube0_BoxMin; // .w holds lerp value for blending
    653. #endif
    654. #if UNITY_SPECCUBE_BOX_PROJECTION
    655.     giInput.boxMax[0] = unity_SpecCube0_BoxMax;
    656.     giInput.probePosition[0] = unity_SpecCube0_ProbePosition;
    657.     giInput.boxMax[1] = unity_SpecCube1_BoxMax;
    658.     giInput.boxMin[1] = unity_SpecCube1_BoxMin;
    659.     giInput.probePosition[1] = unity_SpecCube1_ProbePosition;
    660. #endif
    661.     LightingStandardSpecular_GI(o, giInput, gi);
    662.  
    663.     // call lighting function to output g-buffer
    664.     outEmission = LightingStandardSpecular_Deferred(o, worldViewDir, gi, outGBuffer0, outGBuffer1, outGBuffer2);
    665. #ifndef UNITY_HDR_ON
    666.     outEmission.rgb = exp2(-outEmission.rgb);
    667. #endif
    668. }
    669.  
    670. #include "FlexDrawFluid.cginc"
    671.  
    672. ENDCG
    673.  
    674. }
    675.  
    676.     // ---- shadow caster pass:
    677.     Pass {
    678.         Name "ShadowCaster"
    679.         Tags { "LightMode" = "ShadowCaster" }
    680.         ZWrite On ZTest LEqual
    681.         Cull Off
    682.  
    683. CGPROGRAM
    684. // compile directives
    685. #pragma vertex vert_flex
    686. #pragma geometry geom_flex
    687. #pragma fragment frag_flex
    688. #pragma target 5.0
    689. #pragma multi_compile_shadowcaster
    690. #pragma skip_variants FOG_LINEAR FOG_EXP FOG_EXP2
    691. #include "HLSLSupport.cginc"
    692. #include "UnityShaderVariables.cginc"
    693. // Surface shader code generated based on:
    694. // writes to per-pixel normal: no
    695. // writes to emission: no
    696. // writes to occlusion: no
    697. // needs world space reflection vector: no
    698. // needs world space normal vector: no
    699. // needs screen space position: no
    700. // needs world space position: no
    701. // needs view direction: no
    702. // needs world space view direction: no
    703. // needs world space position for lighting: YES
    704. // needs world space view direction for lighting: YES
    705. // needs world space view direction for lightmaps: no
    706. // needs vertex color: no
    707. // needs VFACE: no
    708. // passes tangent-to-world matrix to pixel shader: no
    709. // reads from normal: no
    710. // 0 texcoords actually used
    711. #define UNITY_PASS_SHADOWCASTER
    712. #include "UnityCG.cginc"
    713. #include "Lighting.cginc"
    714. #include "UnityPBSLighting.cginc"
    715.  
    716. #define INTERNAL_DATA
    717. #define WorldReflectionVector(data,normal) data.worldRefl
    718. #define WorldNormalVector(data,normal) normal
    719.  
    720. // Original surface shader snippet:
    721. #line 10 ""
    722. #ifdef DUMMY_PREPROCESSOR_TO_WORK_AROUND_HLSL_COMPILER_LINE_HANDLING
    723. #endif
    724. /* UNITY: Original start of shader */
    725.         // Physically based Standard lighting model, and enable shadows on all light types
    726.         //#pragma surface surf StandardSpecular fullforwardshadows addshadow
    727.  
    728.         // Use shader model 3.0 target, to get nicer looking lighting
    729.         //#pragma target 3.0
    730.  
    731.         sampler2D _MainTex;
    732.  
    733.         struct Input {
    734.             float2 uv_MainTex;
    735.         };
    736.  
    737.         half _Glossiness;
    738.         fixed4 _Color;
    739.         fixed4 _Specular;
    740.  
    741.         void surf (Input IN, inout SurfaceOutputStandardSpecular o) {
    742.             // Albedo comes from a texture tinted by color
    743.             fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
    744.             o.Albedo = c.rgb;
    745.             // Specular and smoothness come from slider variables
    746.             o.Specular = _Specular;
    747.             o.Smoothness = _Glossiness;
    748.             o.Alpha = 1;//c.a;
    749.         }
    750.        
    751.  
    752. // vertex-to-fragment interpolation data
    753. struct v2f_surf {
    754.   V2F_SHADOW_CASTER;
    755.   float3 worldPos : TEXCOORD1;
    756.   UNITY_VERTEX_INPUT_INSTANCE_ID
    757.   UNITY_VERTEX_OUTPUT_STEREO
    758. };
    759.  
    760. // vertex shader
    761. v2f_surf vert_surf (appdata_full v) {
    762.   UNITY_SETUP_INSTANCE_ID(v);
    763.   v2f_surf o;
    764.   UNITY_INITIALIZE_OUTPUT(v2f_surf,o);
    765.   UNITY_TRANSFER_INSTANCE_ID(v,o);
    766.   UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);
    767.   float3 worldPos = mul(unity_ObjectToWorld, v.vertex).xyz;
    768.   fixed3 worldNormal = UnityObjectToWorldNormal(v.normal);
    769.   o.worldPos = worldPos;
    770.   TRANSFER_SHADOW_CASTER_NORMALOFFSET(o)
    771.   return o;
    772. }
    773.  
    774. // fragment shader
    775. fixed4 frag_surf (v2f_surf IN) : SV_Target {
    776.   UNITY_SETUP_INSTANCE_ID(IN);
    777.   // prepare and unpack data
    778.   Input surfIN;
    779.   UNITY_INITIALIZE_OUTPUT(Input,surfIN);
    780.   surfIN.uv_MainTex.x = 1.0;
    781.   float3 worldPos = IN.worldPos;
    782.   #ifndef USING_DIRECTIONAL_LIGHT
    783.     fixed3 lightDir = normalize(UnityWorldSpaceLightDir(worldPos));
    784.   #else
    785.     fixed3 lightDir = _WorldSpaceLightPos0.xyz;
    786.   #endif
    787.   #ifdef UNITY_COMPILER_HLSL
    788.   SurfaceOutputStandardSpecular o = (SurfaceOutputStandardSpecular)0;
    789.   #else
    790.   SurfaceOutputStandardSpecular o;
    791.   #endif
    792.   o.Albedo = 0.0;
    793.   o.Emission = 0.0;
    794.   o.Specular = 0.0;
    795.   o.Alpha = 0.0;
    796.   o.Occlusion = 1.0;
    797.   fixed3 normalWorldVertex = fixed3(0,0,1);
    798.  
    799.   // call surface function
    800.   surf (surfIN, o);
    801.   SHADOW_CASTER_FRAGMENT(IN)
    802. }
    803.  
    804. #include "FlexDrawFluid.cginc"
    805.  
    806. ENDCG
    807.  
    808. }
    809.  
    810.     // ---- meta information extraction pass:
    811.     Pass {
    812.         Name "Meta"
    813.         Tags { "LightMode" = "Meta" }
    814.         Cull Off
    815.  
    816. CGPROGRAM
    817. // compile directives
    818. #pragma vertex vert_surf
    819. #pragma fragment frag_surf
    820. #pragma target 3.0
    821. #pragma skip_variants FOG_LINEAR FOG_EXP FOG_EXP2
    822. #pragma skip_variants INSTANCING_ON
    823. #include "HLSLSupport.cginc"
    824. #include "UnityShaderVariables.cginc"
    825. // Surface shader code generated based on:
    826. // writes to per-pixel normal: no
    827. // writes to emission: no
    828. // writes to occlusion: no
    829. // needs world space reflection vector: no
    830. // needs world space normal vector: no
    831. // needs screen space position: no
    832. // needs world space position: no
    833. // needs view direction: no
    834. // needs world space view direction: no
    835. // needs world space position for lighting: YES
    836. // needs world space view direction for lighting: YES
    837. // needs world space view direction for lightmaps: no
    838. // needs vertex color: no
    839. // needs VFACE: no
    840. // passes tangent-to-world matrix to pixel shader: no
    841. // reads from normal: no
    842. // 1 texcoords actually used
    843. //   float2 _MainTex
    844. #define UNITY_PASS_META
    845. #include "UnityCG.cginc"
    846. #include "Lighting.cginc"
    847. #include "UnityPBSLighting.cginc"
    848.  
    849. #define INTERNAL_DATA
    850. #define WorldReflectionVector(data,normal) data.worldRefl
    851. #define WorldNormalVector(data,normal) normal
    852.  
    853. // Original surface shader snippet:
    854. #line 10 ""
    855. #ifdef DUMMY_PREPROCESSOR_TO_WORK_AROUND_HLSL_COMPILER_LINE_HANDLING
    856. #endif
    857. /* UNITY: Original start of shader */
    858.         // Physically based Standard lighting model, and enable shadows on all light types
    859.         //#pragma surface surf StandardSpecular fullforwardshadows addshadow
    860.  
    861.         // Use shader model 3.0 target, to get nicer looking lighting
    862.         //#pragma target 3.0
    863.  
    864.         sampler2D _MainTex;
    865.  
    866.         struct Input {
    867.             float2 uv_MainTex;
    868.         };
    869.  
    870.         half _Glossiness;
    871.         fixed4 _Color;
    872.         fixed4 _Specular;
    873.  
    874.         void surf (Input IN, inout SurfaceOutputStandardSpecular o) {
    875.             // Albedo comes from a texture tinted by color
    876.             fixed4 c = tex2D (_MainTex, IN.uv_MainTex);// * _Color;
    877.             o.Albedo = c.rgb; o.Emission = c.rgb;
    878.             // Specular and smoothness come from slider variables
    879.             o.Specular = _Specular;
    880.             o.Smoothness = _Glossiness;
    881.             o.Alpha = c.a;
    882.         }
    883.        
    884. #include "UnityMetaPass.cginc"
    885.  
    886. // vertex-to-fragment interpolation data
    887. struct v2f_surf {
    888.   float4 pos : SV_POSITION;
    889.   float2 pack0 : TEXCOORD0; // _MainTex
    890.   float3 worldPos : TEXCOORD1;
    891. };
    892. float4 _MainTex_ST;
    893.  
    894. // vertex shader
    895. v2f_surf vert_surf (appdata_full v) {
    896.   v2f_surf o;
    897.   UNITY_INITIALIZE_OUTPUT(v2f_surf,o);
    898.   o.pos = UnityMetaVertexPosition(v.vertex, v.texcoord1.xy, v.texcoord2.xy, unity_LightmapST, unity_DynamicLightmapST);
    899.   o.pack0.xy = TRANSFORM_TEX(v.texcoord, _MainTex);
    900.   float3 worldPos = mul(unity_ObjectToWorld, v.vertex).xyz;
    901.   fixed3 worldNormal = UnityObjectToWorldNormal(v.normal);
    902.   o.worldPos = worldPos;
    903.   return o;
    904. }
    905.  
    906. // fragment shader
    907. fixed4 frag_surf (v2f_surf IN) : SV_Target {
    908.   // prepare and unpack data
    909.   Input surfIN;
    910.   UNITY_INITIALIZE_OUTPUT(Input,surfIN);
    911.   surfIN.uv_MainTex.x = 1.0;
    912.   surfIN.uv_MainTex = IN.pack0.xy;
    913.   float3 worldPos = IN.worldPos;
    914.   #ifndef USING_DIRECTIONAL_LIGHT
    915.     fixed3 lightDir = normalize(UnityWorldSpaceLightDir(worldPos));
    916.   #else
    917.     fixed3 lightDir = _WorldSpaceLightPos0.xyz;
    918.   #endif
    919.   #ifdef UNITY_COMPILER_HLSL
    920.   SurfaceOutputStandardSpecular o = (SurfaceOutputStandardSpecular)0;
    921.   #else
    922.   SurfaceOutputStandardSpecular o;
    923.   #endif
    924.   o.Albedo = 0.0;
    925.   o.Emission = 0.0;
    926.   o.Specular = 0.0;
    927.   o.Alpha = 0.0;
    928.   o.Occlusion = 1.0;
    929.   fixed3 normalWorldVertex = fixed3(0,0,1);
    930.  
    931.   // call surface function
    932.   surf (surfIN, o);
    933.   UnityMetaInput metaIN;
    934.   UNITY_INITIALIZE_OUTPUT(UnityMetaInput, metaIN);
    935.   metaIN.Albedo = o.Albedo;
    936.   metaIN.Emission = o.Emission;
    937.   return UnityMetaFragment(metaIN);
    938. }
    939.  
    940. ENDCG
    941.  
    942. }
    943.  
    944.     // ---- end of surface shader generated code
    945.  
    946. #LINE 38
    947.  
    948.     }
    949.     FallBack Off
    950. }
    951.  
     
  6. G33RT

    G33RT

    Joined:
    Dec 27, 2013
    Posts:
    52
    I'm trying to reconstruct what you did to get it working with my geometry shader, but without succes. Could we have a look at the final (working) result, the shader code?

    That would really make my day (or week :D )
     
    abegue likes this.
  7. mmvirsabi

    mmvirsabi

    Joined:
    Aug 29, 2019
    Posts:
    4
    I have the same issue with a MRTK shader. Can you please post your shader from other people to learn from. Thank you :)
     
  8. homoinanis

    homoinanis

    Joined:
    Sep 29, 2020
    Posts:
    7
    I have searched a lot on the internet. I can save you time. One day as of this post Unity offers absolutely nothing to get a single render texture with stereoscopic view using Single Instance.

    But of course, there is the issue that if you want to make a relatively complex game Single Instance is practically mandatory.

    The only solution I have found is also quite simple: use two cameras for each of the eyes of the HMD viewer. So, for each stereoscopic texture that we want, for example a portal, we will have to create two cameras that will track the position of the eyes in a relative way, recording the texture in two render textures that we can assign to each of the textures of a shader that unify in a single vision.
     
    smash-ter likes this.
  9. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,352
    ... your post is entirely unrelated to the topic of this thread.
     
    smash-ter likes this.