x


Transparent Solid Color Shader

Hi,

I know nothing of making shaders in Unity, and I'm trying to make a Solid Color shader which is transparent. I currently have the following, which works:

  Properties
  {
       _Color ("Color", Color) = (1,1,1,.5)
  }
  SubShader
  {
       Tags {"Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent"}
       LOD 100
  
       ZWrite On
       Blend SrcAlpha OneMinusSrcAlpha 
  
       Pass
       {
            Lighting Off
            Color [_Color]
       }
  }

The problem is though that you can see parts of the model itself that are behind it: alt text

Anyone knows how to fix this? I need/want a solid color, without those bright spots.

Thanks for the help!

more ▼

asked Jul 25, 2012 at 02:43 PM

The Oddler gravatar image

The Oddler
360 105 87 105

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

shader below is do what you want. but your platform should support GrabPass technology. this can be not supported by some mobile platforms.

i can propound 2 more ways without grabpass:

create a 2-pass shader that first draws only to alpha-channel and the second draws just color based on alpha value, this can be achieved by blending options create a object mask using render-texture and put it to main camera on top. this way is same as previous but uses render texture instead of shader calculations and blending

anyway issue is not so simple. with a just default blending two surfaces will be drawn twice and it will look not same as single surface.

shader

Shader "Unity Answers/Solid Transparency Color" { Properties { _Color ("Color", Color) = (0.5, 0.5, 0.5, 0.5) } SubShader { Tags {"Queue" = "Transparent"} ZWrite Off GrabPass { } Pass { Fog { Mode Off } Blend SrcAlpha OneMinusSrcAlpha

         CGPROGRAM

         #pragma vertex vert
         #pragma fragment frag
 
         fixed4 _Color;
         sampler2D _GrabTexture;

         struct appdata
         {
             float4 vertex : POSITION;
         };
         struct v2f
         {
             float4 pos : SV_POSITION;
             float4 uv : TEXCOORD0;
         };
         v2f vert (appdata v)
         {
             v2f o;
             o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
             o.uv = o.pos;
             return o;
         }
         half4 frag(v2f i) : COLOR
         {
             float2 coord = 0.5 + 0.5 * i.uv.xy / i.uv.w;
             fixed4 tex = tex2D(_GrabTexture, float2(coord.x, 1 - coord.y));
             return fixed4(lerp(tex.rgb, _Color.rgb, _Color.a), 1);
         }
         ENDCG
     }
 }

}

more ▼

answered Jul 27, 2012 at 09:30 PM

ScroodgeM gravatar image

ScroodgeM
8.5k 1 5 15

Thanks! I get an error "No subshader can run on this graphics card" though. Any idea? I'm using Unity Indie, can this be a problem?

Jul 27, 2012 at 11:27 PM The Oddler

AFAIK shaders are not depends on Unity license. so this is a hardware limits. which hardware do you use? graphic adapter model? and try menu "edit -> graphic emulation" - does it affect shader? which modes? i think GrabPass{} can be not supported y some cards

Jul 28, 2012 at 06:34 AM ScroodgeM

I currently have a Nvidia 460gtx. I already tried changing the graphic emulation, though that didn't change anything (currently it's at none.) And what do you mean by "which modes" ? Btw, thanks a lot for helping.

Jul 28, 2012 at 09:54 AM The Oddler

that's very strange. shader model 2.0 support must be enough to run this shader. your card supports up to shader model 5.0. did you edit this shader? try to use it as is. also, try to switch to fantastic mode in quality settings. if it still doesn't work, try to remove 'GrabPass {}' line. shader will not work properly then, but should draw an object (without transparency). it should help to detect what's wrong in shader. write response here.

Jul 28, 2012 at 10:46 AM ScroodgeM

Ok I tried all what you said. Changing the quality settings doesn't do anything. Removing the GrabPass removes the error, though as you said it's not transparant.

Jul 28, 2012 at 11:56 AM The Oddler
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x3142
x270
x2

asked: Jul 25, 2012 at 02:43 PM

Seen: 4392 times

Last Updated: Jul 28, 2012 at 03:33 PM