• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
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
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

95 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

shaders : what is the use of UNITY_FOG_COORDS, UNITY_TRANSFER_FOG, UNITY_APPLY_FOG 1 Answer

Transparent object makes other objects act strange 0 Answers

creating portal effect using a render texture and Shadergraph with screen position node in VR 0 Answers

Adding alpha to shader to control opacity of VideoPlayer for 360 video? 1 Answer

Controlling Bloom Effect using Post Proccesing Behaviour (Include/Excluding Objects) 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges