- Home /
 
               Question by 
               HatrabbitSvarling · Aug 24, 2017 at 09:28 AM · 
                vrshadersfogreflectionslightmaps  
              
 
              Single pass rendering + Custom fragment shader with lightmaps & reflection probes?
Hi folks! Stuck here with a shader problem.
Vr-Device: Gear VR Phone: Galaxy s7
With single pass rendering enabled (yes I know it's a preview feature), the game flickers when fog is enabled. By actually turning off the standard fog it seems to work like a charm.
Still I want fog in my world, so instead I implemented distance based fog in my vertex shader. Works well for everything except when you also combine lightmaps and reflection probes.
If i turn off lightmaps it works. Or if i don't use reflection probes. Combining them still flickers. Any ideas?
 Shader "Custom/VertexColorReflective"
 {
     Properties
     {
         _Color("Color", Color) = (1,1,1,1)
         _Blur("Blur", Range(0,5)) = 0
         _FogColor("Fog Color (RGB)", Color) = (0.5, 0.5, 0.5, 1.0)
     }
 
     SubShader
     {
         Pass
         {
             CGPROGRAM
             #pragma vertex vert
             #pragma fragment frag
             #include "UnityCG.cginc"
             #pragma multi_compile _ LIGHTMAP_ON
 
             float4 _Color;
             half _Blur;
             float4 _FogColor;
 
             struct appdata
             {
                 float4 vertex : POSITION;
                 float3 normal : NORMAL;
                 float4 color : COLOR;
                 float4 uv : TEXCOORD1;
             };
 
             struct v2f 
             {
                 float4 pos : SV_POSITION;
                 UNITY_VERTEX_OUTPUT_STEREO
                 half3 worldRefl : TEXCOORD0;
                 float4 color : COLOR;
                 float4 uv : TEXCOORD1;
                 float fog : TEXCOORD2;
             };
 
             v2f vert(appdata v)
             {
                 v2f o;
                 UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);
 
                 o.pos = UnityObjectToClipPos(v.vertex);
                 o.color = v.color;
                 float3 worldPos = mul(unity_ObjectToWorld, v.vertex).xyz;
 
                 //reflections
                 float3 worldViewDir = normalize(UnityWorldSpaceViewDir(worldPos));
                 float3 worldNormal = UnityObjectToWorldNormal(v.normal);
                 o.worldRefl = reflect(-worldViewDir, worldNormal);
 
                 //lightmaps
                 o.uv.xy = v.uv.xy * unity_LightmapST.xy + unity_LightmapST.zw;
 
                 //fog
                 float fogz = UnityObjectToViewPos(v.vertex).z;
                 o.fog = saturate((fogz + 10) / (10 - 65));
 
                 return o;
             }
 
             fixed4 frag(v2f i) : SV_Target
             {
                 UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(i);
 
                 //reflections
                 half4 skyData = UNITY_SAMPLE_TEXCUBE_LOD(unity_SpecCube0, i.worldRefl, _Blur);
                 fixed4 c = 0;
                 c.rgb = lerp(i.color.rgb * _Color.rgb, skyData, _Color.a) * 2;
 
                 //lightmaps
                 fixed4 lm = UNITY_SAMPLE_TEX2D(unity_Lightmap, i.uv.xy);
                 c.rgb *= lm.rgb * 3;
         
                 return lerp(c, _FogColor, i.fog);
             }
             ENDCG
         }
     }
 }
 
               Comment
              
 
               
              Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                