Search Unity

[SOLVED] How to project a texture as non transparent?

Discussion in 'Shaders' started by omermecitoglu, Aug 4, 2017.

  1. omermecitoglu

    omermecitoglu

    Joined:
    Jul 24, 2017
    Posts:
    5
    I want to project this texture on the terrain



    I used "Projector/Multiply" shader for the material, and...



    how can I make it alpha? (or opaque? or solid? not sure about the word, forgive me)
     
    Last edited: Aug 4, 2017
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    7,000
    I find it a little odd that apart from a single asset on the store there's not much about projectors and projector shaders.

    Making a projector shader that does alpha blending is pretty easy to do. It's actually a slightly simpler shader than the built in multiply and light projector shaders. The funny thing is if you're not familiar with shaders and you try to follow any kind of "make a shader use alpha blending" tutorial it won't work on projectors just because the default shaders are doing stuff that will mess up alpha blending.

    So, instead of going over all of it, here's an alpha blend projector shader.

    Code (CSharp):
    1. Shader "Projector/AlphaBlend" {
    2.     Properties {
    3.         _ShadowTex ("Cookie", 2D) = "white" {}
    4.         _FalloffTex ("FallOff", 2D) = "white" {}
    5.     }
    6.     Subshader {
    7.         Tags {"Queue"="Transparent"}
    8.         Pass {
    9.             ZWrite Off
    10.             ColorMask RGB
    11.             Blend SrcAlpha OneMinusSrcAlpha
    12.             Offset -1, -1
    13.  
    14.             CGPROGRAM
    15.             #pragma vertex vert
    16.             #pragma fragment frag
    17.             #pragma multi_compile_fog
    18.             #include "UnityCG.cginc"
    19.            
    20.             struct v2f {
    21.                 float4 uvShadow : TEXCOORD0;
    22.                 float4 uvFalloff : TEXCOORD1;
    23.                 UNITY_FOG_COORDS(2)
    24.                 float4 pos : SV_POSITION;
    25.             };
    26.            
    27.             float4x4 unity_Projector;
    28.             float4x4 unity_ProjectorClip;
    29.            
    30.             v2f vert (float4 vertex : POSITION)
    31.             {
    32.                 v2f o;
    33.                 o.pos = UnityObjectToClipPos(vertex);
    34.                 o.uvShadow = mul (unity_Projector, vertex);
    35.                 o.uvFalloff = mul (unity_ProjectorClip, vertex);
    36.                 UNITY_TRANSFER_FOG(o,o.pos);
    37.                 return o;
    38.             }
    39.            
    40.             sampler2D _ShadowTex;
    41.             sampler2D _FalloffTex;
    42.            
    43.             fixed4 frag (v2f i) : SV_Target
    44.             {
    45.                 fixed4 texS = tex2Dproj (_ShadowTex, UNITY_PROJ_COORD(i.uvShadow));
    46.                 fixed4 texF = tex2Dproj (_FalloffTex, UNITY_PROJ_COORD(i.uvFalloff));
    47.  
    48.                 fixed4 res = texS;
    49.                 res.a *= texF.a;
    50.  
    51.                 UNITY_APPLY_FOG(i.fogCoord, res);
    52.                 return res;
    53.             }
    54.             ENDCG
    55.         }
    56.     }
    57. }
     
  3. omermecitoglu

    omermecitoglu

    Joined:
    Jul 24, 2017
    Posts:
    5
    @bgolus it worked like a charm! thank you :)
     
  4. thong341998

    thong341998

    Joined:
    Dec 1, 2018
    Posts:
    1
    Although this post was 2 years ago, you truly save my life, thank you vey much :)