• 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 bpdavis · Oct 24, 2012 at 07:31 AM · shaderrendererflicker

Basic replacement shader causes objects to flicker

Hi,

I'm in the process of learning all I can about Unity's shader support. I found a simple shader example here (http://docs.unity3d.com/Documentation/Manual/ShaderTut2.html), but when I replace my main camera's renderer with it, I get strange flickering with the roads in my scene.

This first image seems ok...

alt text

However, if I move the camera just slightly, the roads flicker in/out. The image below shows that the left half of the scene's roads are gone, but it's more random than that.

Any thoughts as to what might be going on? If I comment out the call to SetReplacementShader, everything renders properly.

alt text

Here is the C# code that replaces the shader.

 using UnityEngine;
 using System.Collections;
 
 public class ShaderReplacer : MonoBehaviour 
 {
 
     // Use this for initialization
     void Start () 
     {
         GameObject go = GameObject.Find("Main Camera");
         Camera c = go.GetComponent<Camera>();
 
         Shader s = Shader.Find("Custom/MyFirstShader");
 
         Debug.Log("Camera name:" + c.name);
         
         if (s == null)
         {
             Debug.Log("Shader could not be found");
         }
         else
         {
             c.SetReplacementShader(s, "");
             Debug.Log("Shader name:" + s.name);
         }
     }
     
     // Update is called once per frame
     void Update ()
     {
     
     }
 }
 

... and the shader code itself

 Shader "Custom/MyFirstShader" 
 {
 Properties {
     _Color ("Main Color", Color) = (1,1,1,0.5)
     _MainTex ("Texture", 2D) = "white" { }
 }
 SubShader {
     Pass {
 
 CGPROGRAM
 #pragma vertex vert
 #pragma fragment frag
 
 #include "UnityCG.cginc"
 
 float4 _Color;
 sampler2D _MainTex;
 
 struct v2f {
     float4  pos : SV_POSITION;
     float2  uv : TEXCOORD0;
 };
 
 float4 _MainTex_ST;
 
 v2f vert (appdata_base v)
 {
     v2f o;
     o.pos = mul (UNITY_MATRIX_MVP, v.vertex);
     o.uv = TRANSFORM_TEX (v.texcoord, _MainTex);
     return o;
 }
 
 float4 frag (v2f i) : COLOR
 {
     float4 texcol;
     texcol.rgb = tex2D (_MainTex, i.uv).r;
     texcol.a = 1;
     return texcol * _Color;
 }
 ENDCG
 
     }
 }
 Fallback "VertexLit"
 } 



shaderimage1.jpg (31.5 kB)
shaderimage2.jpg (30.9 kB)
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

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by bpdavis · Oct 24, 2012 at 07:49 PM

I added a toggle keypress so that I could show identically oriented images of using the replacement shader vs using the original shader provided by Unity. I set the camera's near plane to 1 and far plane to 300 and positioned the camera just above street level. As shown in the attached images, the problem still exists.

Couple of things to note:

My shader has been modified to output in gray scale.

The skybox (not shown in attached images) does not render when using a replacement shader.

Original Unity Shader

Original Unity shader

Replacement shader output

alt text


streetleveloriginal.jpg (20.5 kB)
streetlevelreplace.jpg (20.8 kB)
Comment
Add comment · Show 1 · Share
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
avatar image bpdavis · Oct 25, 2012 at 03:51 PM 0
Share

Ok, the terrain and the roads are the problem because I can lower the terrain just slightly and the z fighting goes away. What I don't understand is why this is the case solely for shaders that are not surface shaders? Is there some special processing that goes on with surface shaders that should be done for vertex/fragment shaders? I tried replacing the shader with some Unity-supplied vertex/fragment shaders and got the same z-fighting issue.

avatar image
0

Answer by 61# · Oct 24, 2012 at 11:41 AM

Agreed upstairs! In the scene so as not to appear two surface overlap, or too near.

Comment
Add comment · Share
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
avatar image
0

Answer by whydoidoit · Oct 24, 2012 at 07:32 AM

It looks to me like you have a Z fighting issue - this is caused by the resolution of the Z buffer - you could make your Camera's far clip plane smaller (or near clip plane bigger) to reduce the range, or probably the best idea is to raise the roads very slightly so that they are further above the ground.

Comment
Add comment · Show 1 · Share
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
avatar image bpdavis · Oct 24, 2012 at 03:42 PM 0
Share

Any idea as to why z-fighting would suddenly become an issue just because the shader was replaced? The scene renders fine using the default shaders. While the images depict the camera being far away, I get the same results down at street level.

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

11 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

Related Questions

Why is only the last Material in the Materials array on a Mesh Renderer used? 2 Answers

Is it possible to render a solid color where the Camera clips to the Near Clipping Plane? 1 Answer

Unity 5.4 - SetVectorArray - how to read values back from MaterialPropertyBlock? 0 Answers

Select material of Canvas Renderer 0 Answers

How to make a localToWorld matrix for shader parameters? 1 Answer


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