• 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 Steven-1 · May 30, 2012 at 11:01 AM · shaderscenegametransparent

weird shader problem (correct in editor, weird ingame)

I wrote a simple shader that's basically just bumped-diffuse, but fades out when close to the camera.

It works in the scene (also when playing), but in the game it looks as if the areas that should be transparent is instead the previous frame beeing added additively (relative to the supposed transparency).

I have no idea what's wrong, any ideas ?

here's the shader: Shader "Custom/Planet" { Properties { _Color ("Main Color", Color) = (1,1,1,1) _MainTex ("Base (RGB)", 2D) = "white" {} _BumpMap ("Normalmap", 2D) = "bump" {} _Dist ("FadeOut Distance", float) = 20 //_PlanetFogColor ("fog Color", Color) = (1,1,1,1) }

 SubShader 
 {
     ZWrite On
     Fog { Mode off }
     Tags { "RenderType"="Transparent" "Queue" = "Geometry+1" } //Queue is nodig (anders fout)
     Blend SrcAlpha OneMinusSrcAlpha 
     //Tags { "RenderType" = "Opaque" }
     //LOD 300
 
     CGPROGRAM
     #pragma surface surf Lambert vertex:vert
     
     fixed4 _Color;
     sampler2D _MainTex;
     sampler2D _BumpMap;    
     float _Dist;
     
     fixed4 _PlanetFogColor; //global
     
     float _UsePlanetFog; //global (0 of 1, bool bestaat niet)
     
     struct Input 
     {
         float2 uv_MainTex;
         float2 uv_BumpMap;
         float z;
     };

     void vert(inout appdata_full v, out Input o)
     {
         o.z = (mul(UNITY_MATRIX_MVP, v.vertex)).w;
     }
     
     void surf (Input IN, inout SurfaceOutput o) 
     {
         fixed4 c = tex2D(_MainTex, IN.uv_MainTex) * _Color;

         o.Albedo = c.rgb;
         o.Alpha = 1;//c.a;
         o.Normal = UnpackNormal(tex2D(_BumpMap, IN.uv_BumpMap));
         
         if (_UsePlanetFog>0.5)
         {
             float fade = 1;
             if (IN.z<_Dist)
                 fade = IN.z/_Dist;
             fade=sqrt(fade);
             fade = fade*1.05-0.05;
             if (fade<0)    fade=0;

             o.Alpha = fade;
         
             ////o.Albedo = c.rgb;
             //o.Albedo = (c*fade).rgb;//(c*fade + _PlanetFogColor*(1-fade)).rgb;
             ////o.Normal = UnpackNormal(tex2D(_BumpMap, IN.uv_BumpMap));//normalize(UnpackNormal(tex2D(_BumpMap, IN.uv_BumpMap))*fade + float3(0,0,1)*(1-fade)); //normal ook uitfaden //niet meer nodig
             //o.Emission = ((1-fade)*_PlanetFogColor).rgb;//(1-fade)*o.Albedo; //niet ideaal, zou eigenlijk moeten faden naar unlit color, dit is maar een benadering
         }
         //else
         //{
         //    o.Albedo = c.rgb;
         //}
         
         //o.Alpha = 0;
     }
         
     ENDCG  
 }
 
 FallBack "Bumped Diffuse"

}

also, the should fix/change the formatting for code blocks on UnityAnswers, as they never seem to work (Edit: the button dousn't work, but writing it yourself apparently does)

Comment
Add comment · Show 2
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 Steven-1 · May 30, 2012 at 11:07 AM 0
Share

ok, what's up with the code formatting, I fixed it, but it still looks the same afterwards

avatar image whydoidoit · May 30, 2012 at 11:11 AM 0
Share

I've started to use $$anonymous$$onoDevelop to indent once, then copy the code over... Sorry can't help with the Shader though :(

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Steven-1 · May 30, 2012 at 11:16 AM

writing this question I realised I had based this on the builtin bumped-diffuse shader instead of the bumped-transparent, looking at it I noticed I shouldn't have put in "Blend SrcAlpha OneMinusSrcAlpha" as it's a surface shader, and I also had to add "alpha" after "#pragma surface surf Lambert"

woops, not used to writing surface shaders

Comment
Add comment · Show 2 · 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 whydoidoit · May 30, 2012 at 11:28 AM 0
Share

Well I learned something ;)

avatar image Steven-1 · May 30, 2012 at 11:43 AM 0
Share

ha, thanks

avatar image
0

Answer by CaliCoastReplay · Feb 11, 2018 at 04:12 AM

This can also be caused by failing to have a Render Queue setting above 3000 (Transparent) in the shader's material in the Editor - I experienced it myself today.

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

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

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

6 People are following this question.

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

Related Questions

Shader looks different in game than in scene view 0 Answers

Transparent Objects within Transparent Objects 1 Answer

User Facing Semi Transparent View 0 Answers

Game view auto-scaling resolution. 1 Answer

Why is half of my "game" tab completely grayed out? 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges