Search Unity

  1. Calling all beginners! Join the FPS Beginners Mods Challenge until December 13.
    Dismiss Notice
  2. It's Cyber Week at the Asset Store!
    Dismiss Notice

Use a projector to cull pixels with alpha transparency.

Discussion in 'Shaders' started by geostegma, May 28, 2018.

  1. geostegma

    geostegma

    Joined:
    Jun 12, 2017
    Posts:
    5
    Here is a screenshot taken from a GDC on Left 4 Dead 2's wound simulation and its many experimental iterations:


    https://postimg.cc/image/feah5kgnv/

    How would one replicate this effect within Unity? I'm assuming that you could use a projector to map a texture onto a mesh and use that texture's transparency in a custom shader to cull pixels on both materials. If someone could point me in the right direction that would be much appreciated, I am quite inexperienced when it comes to writing shaders.

    Thanks!
     
  2. Seyed_Morteza_Kamaly

    Seyed_Morteza_Kamaly

    Joined:
    Nov 18, 2015
    Posts:
    69
    In unity there is no option to killing pixel by projector component.Instead of using projector you can painting with Raycast and store painting as texture by using RenderTexture.

    then you can use it to cutting mesh parts by multiplying alpha channel





    also you can use use vertex shader to cutting body parts
    https://en.wikibooks.org/wiki/Cg_Programming/Unity/Cutaways

    another approach to making mask is using stencil buffer
    I made tutorial that will show you how you can use it





    suppose we have skeleton model that we want to place In above mask



    masking again!






    Stencil mask is done!



    Stencil that I used to making this effect:

    Human:
    Code (CSharp):
    1.         Stencil {
    2.             Ref 1
    3.             Comp NotEqual
    4.         }
    Mask:
    Code (CSharp):
    1.         Stencil
    2.         {
    3.             Ref 1
    4.             Comp Always
    5.             Pass Replace
    6.         }
    Skeleton:
    Code (CSharp):
    1.         Stencil {
    2.             Ref 1
    3.             Comp Equal
    4.         }

    some examples in different games:

    Mortal Kombat 9 Komplete Edition:



    Sniper Elite 4:

     
    Last edited: Jun 2, 2018
  3. geostegma

    geostegma

    Joined:
    Jun 12, 2017
    Posts:
    5
    Thank you!!
     
    Seyed_Morteza_Kamaly likes this.
  4. Pandur1982

    Pandur1982

    Joined:
    Jun 16, 2015
    Posts:
    241
    @Seyed_Morteza_Kamaly ,


    Hello i have use your Tutorial from here : https://gamedev.stackexchange.com/questions/152824/how-can-i-create-a-see-behind-walls-effect, for the behind the wall effect. That works great but i have a question on the wall Shader from that tutorial i can only give color and texture, is it possibel to bring in AO,Normal,Roughnes,Metalic,Height in that WallShader? Iam not good in Shaderprog, can you help me on that, i looking for the wall shader with the standard shader or is that not possibel?

    Thx for your help :D
     
  5. Seyed_Morteza_Kamaly

    Seyed_Morteza_Kamaly

    Joined:
    Nov 18, 2015
    Posts:
    69
    Hello Pandur1982 yes It's possible
    this is a standard shader with all properties:
    just add stencil above of CGPROGRAM

    Code (CSharp):
    1. Shader "Custom/Standard" {
    2.     Properties {
    3.         _Color ("Color", Color) = (1,1,1,1)
    4.         _EmissionColor("EmissionColor",Color) = (1,0,0,1)
    5.         _Emission("Emission",Range(0,1)) = 0.0
    6.         _MainTex ("Albedo (RGB)", 2D) = "white" {}
    7.         _GlossMap("GlossMap",2D) = "white"{}
    8.         _Glossiness ("Smoothness", Float) = 0.5
    9.         _Metallic ("Metallic", Float) = 0.0
    10.         _AO("AO",2D) = "white"{}
    11.         _NormalMap("NormalMap",2D) = "bump"{}
    12.         _NormalMapIntensity("NormalMapIntensity",Range(1,5)) = 1.0
    13.     }
    14.     SubShader {
    15.         Tags { "RenderType"="Opaque" }
    16.         LOD 200
    17.    
    18.         CGPROGRAM
    19.         #pragma surface surf Standard fullforwardshadows
    20.  
    21.         #pragma target 3.0
    22.  
    23.         sampler2D _MainTex;
    24.  
    25.         struct Input {
    26.             float2 uv_MainTex;
    27.             float2 uv_Gloss;
    28.         };
    29.  
    30.         half _Glossiness;
    31.         half _Metallic;
    32.         fixed4 _Color;
    33.         float4 _EmissionColor;
    34.         float _NormalMapIntensity;
    35.         float _Emission;
    36.         sampler2D _GlossMap,_AO,_NormalMap;
    37.  
    38.         UNITY_INSTANCING_BUFFER_START(Props)
    39.         UNITY_INSTANCING_BUFFER_END(Props)
    40.  
    41.         void surf (Input IN, inout SurfaceOutputStandard o) {
    42.             fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
    43.             o.Albedo = c.rgb;
    44.             o.Metallic = _Metallic;
    45.             o.Smoothness = _Glossiness*tex2D(_GlossMap,IN.uv_MainTex);
    46.             o.Alpha = c.a;
    47.             o.Occlusion = tex2D(_AO,IN.uv_MainTex);
    48.             o.Emission = _Emission * _EmissionColor;
    49.             fixed3 n = UnpackNormal(tex2D(_NormalMap, IN.uv_MainTex)).rgb;
    50.             n.x *= _NormalMapIntensity;
    51.             n.y *= _NormalMapIntensity;
    52.             o.Normal = normalize(n);
    53.         }
    54.         ENDCG
    55.     }
    56.     FallBack "Diffuse"
    57. }
    Wall:
    Code (CSharp):
    1. // Upgrade NOTE: upgraded instancing buffer 'Props' to new syntax.
    2.  
    3. Shader "Custom/Wall" {
    4.     Properties {
    5.         _Color ("Color", Color) = (1,1,1,1)
    6.         _EmissionColor("EmissionColor",Color) = (1,0,0,1)
    7.         _Emission("Emission",Range(0,1)) = 0.0
    8.         _MainTex ("Albedo (RGB)", 2D) = "white" {}
    9.         _GlossMap("GlossMap",2D) = "white"{}
    10.         _Glossiness ("Smoothness", Float) = 0.5
    11.         _Metallic ("Metallic", Float) = 0.0
    12.         _AO("AO",2D) = "white"{}
    13.         _NormalMap("NormalMap",2D) = "bump"{}
    14.         _NormalMapIntensity("NormalMapIntensity",Range(1,5)) = 1.0
    15.     }
    16.     SubShader {
    17.         Tags { "RenderType"="Opaque" }
    18.         LOD 200
    19. Stencil
    20. {
    21.     Ref 1 // ReferenceValue = 1
    22.     Comp NotEqual // Only render pixels whose reference value differs from the value in the buffer.
    23. }
    24.         CGPROGRAM
    25.         #pragma surface surf Standard fullforwardshadows
    26.  
    27.         #pragma target 3.0
    28.  
    29.         sampler2D _MainTex;
    30.  
    31.         struct Input {
    32.             float2 uv_MainTex;
    33.             float2 uv_Gloss;
    34.         };
    35.  
    36.         half _Glossiness;
    37.         half _Metallic;
    38.         fixed4 _Color;
    39.         float4 _EmissionColor;
    40.         float _NormalMapIntensity;
    41.         float _Emission;
    42.         sampler2D _GlossMap,_AO,_NormalMap;
    43.  
    44.         UNITY_INSTANCING_BUFFER_START(Props)
    45.         UNITY_INSTANCING_BUFFER_END(Props)
    46.  
    47.         void surf (Input IN, inout SurfaceOutputStandard o) {
    48.             fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
    49.             o.Albedo = c.rgb;
    50.             o.Metallic = _Metallic;
    51.             o.Smoothness = _Glossiness*tex2D(_GlossMap,IN.uv_MainTex);
    52.             o.Alpha = c.a;
    53.             o.Occlusion = tex2D(_AO,IN.uv_MainTex);
    54.             o.Emission = _Emission * _EmissionColor;
    55.             fixed3 n = UnpackNormal(tex2D(_NormalMap, IN.uv_MainTex)).rgb;
    56.             n.x *= _NormalMapIntensity;
    57.             n.y *= _NormalMapIntensity;
    58.             o.Normal = normalize(n);
    59.         }
    60.         ENDCG
    61.     }
    62.     FallBack "Diffuse"
    63. }
    64.  
    Mask:
    Code (CSharp):
    1.  
    2. Shader "Custom/SimpleMask"
    3. {
    4.    Properties
    5.    {
    6.        _MainTex ("Texture", 2D) = "white" {}
    7.        _CutOff("CutOff", Range(0,1)) = 0
    8.    }
    9.    SubShader
    10.    {
    11.        LOD 100
    12.        Blend One OneMinusSrcAlpha
    13.        Tags { "Queue" = "Geometry-1" }  // Write to the stencil buffer before drawing any geometry to the screen
    14.        ColorMask 0 // Don't write to any colour channels
    15.        ZWrite Off // Don't write to the Depth buffer
    16.        // Write the value 1 to the stencil buffer
    17.        Stencil
    18.        {
    19.            Ref 1
    20.            Comp Always
    21.            Pass Replace
    22.        }
    23.  
    24.        Pass
    25.        {
    26.            CGPROGRAM
    27.            #pragma vertex vert
    28.            #pragma fragment frag
    29.  
    30.            #include "UnityCG.cginc"
    31.  
    32.            struct appdata
    33.            {
    34.                float4 vertex : POSITION;
    35.                float2 uv : TEXCOORD0;
    36.            };
    37.  
    38.            struct v2f
    39.            {
    40.                float2 uv : TEXCOORD0;
    41.                float4 vertex : SV_POSITION;
    42.            };
    43.  
    44.            sampler2D _MainTex;
    45.            float4 _MainTex_ST;
    46.            float _CutOff;
    47.  
    48.            v2f vert (appdata v)
    49.            {
    50.                v2f o;
    51.                o.vertex = UnityObjectToClipPos(v.vertex);
    52.  
    53.                o.uv = TRANSFORM_TEX(v.uv, _MainTex);
    54.  
    55.                return o;
    56.            }
    57.  
    58.            fixed4 frag (v2f i) : SV_Target
    59.            {
    60.                fixed4 col = tex2D(_MainTex, i.uv);
    61.                float dissolve = step(col, _CutOff);
    62.                clip(_CutOff-dissolve);
    63.                return float4(1,1,1,1)*dissolve;
    64.            }
    65.            ENDCG
    66.        }
    67.    }
    68. }
    69.  
     
    Last edited: Mar 23, 2019
  6. Pandur1982

    Pandur1982

    Joined:
    Jun 16, 2015
    Posts:
    241
    Big Thx for your answer, that help me a lot.That is a TopNotch Asset, i think you must bring it to the asset store, that can help many peopel for aTopDown Game.

    I have change a littel on the Wall Shader for emissionsTexture :

    Code (CSharp):
    1. // Upgrade NOTE: upgraded instancing buffer 'Props' to new syntax.
    2. Shader "Custom/Wall" {
    3.     Properties {
    4.         _Color ("Color", Color) = (1,1,1,1)
    5.         _EmissionColor("EmissionColor",Color) = (1,0,0,1)
    6.         _Emission("Emission",Range(0,100)) = 0.0
    7.         _EmissionMap("Emissive", 2D) = "black" {}
    8.         _MainTex ("Albedo (RGB)", 2D) = "white" {}
    9.         _GlossMap("GlossMap",2D) = "white"{}
    10.         _Glossiness ("Smoothness", Float) = 0.5
    11.         _Metallic ("Metallic", Float) = 0.0
    12.         _AO("AO",2D) = "white"{}
    13.         _NormalMap("NormalMap",2D) = "bump"{}
    14.         _NormalMapIntensity("NormalMapIntensity",Range(1,5)) = 1.0
    15.     }
    16.     SubShader {
    17.         Tags { "RenderType"="Opaque" }
    18.         LOD 200
    19. Stencil
    20. {
    21.     Ref 1 // ReferenceValue = 1
    22.     Comp NotEqual // Only render pixels whose reference value differs from the value in the buffer.
    23. }
    24.         CGPROGRAM
    25.         #pragma surface surf Standard fullforwardshadows
    26.         #pragma target 3.0
    27.         sampler2D _MainTex;
    28.         struct Input {
    29.             float2 uv_MainTex;
    30.             float2 uv_Gloss;
    31.         };
    32.         half _Glossiness;
    33.         half _Metallic;
    34.         fixed4 _Color;
    35.         float4 _EmissionColor;
    36.         float _NormalMapIntensity;
    37.         float _Emission;
    38.         sampler2D _GlossMap,_AO,_NormalMap,_EmissionMap;
    39.         UNITY_INSTANCING_BUFFER_START(Props)
    40.         UNITY_INSTANCING_BUFFER_END(Props)
    41.         void surf (Input IN, inout SurfaceOutputStandard o) {
    42.             fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
    43.             o.Albedo = c.rgb;
    44.             o.Metallic = _Metallic;
    45.             o.Smoothness = _Glossiness*tex2D(_GlossMap,IN.uv_MainTex);
    46.             o.Alpha = c.a;
    47.             o.Occlusion = tex2D(_AO,IN.uv_MainTex);
    48.             o.Emission = _Emission * _EmissionColor*tex2D(_EmissionMap,IN.uv_MainTex);
    49.             fixed3 n = UnpackNormal(tex2D(_NormalMap, IN.uv_MainTex)).rgb;
    50.             n.x *= _NormalMapIntensity;
    51.             n.y *= _NormalMapIntensity;
    52.             o.Normal = normalize(n);
    53.         }
    54.         ENDCG
    55.     }
    56.     FallBack "Diffuse"
    57. }
    I think its good do bring more in that shader like Mask.Roughness and so one :D
     
    Seyed_Morteza_Kamaly likes this.
  7. Seyed_Morteza_Kamaly

    Seyed_Morteza_Kamaly

    Joined:
    Nov 18, 2015
    Posts:
    69
    why not? I like to publish it in assetstore but I need your help to make it possible. :)

    Unity's Standard shader has the smoothness stored in the alpha channel of the metal texture (or optionally the albedo).
    I explained Roughness here:
    https://gamedev.stackexchange.com/q...-shallow-puddle-shader-in-unity/153620#153620

    but anyway if you like to have o.Roughness you can use this:
    https://gist.github.com/smkplus/17fea90de9215b1f8e1085912d9e11dc
     
    Last edited: Mar 24, 2019
    Pandur1982 likes this.
  8. Pandur1982

    Pandur1982

    Joined:
    Jun 16, 2015
    Posts:
    241
    Thx for it, very usefull links, yes how can i help you to bring that on the asset store?
     
  9. Seyed_Morteza_Kamaly

    Seyed_Morteza_Kamaly

    Joined:
    Nov 18, 2015
    Posts:
    69
    I just need a list of features to Implement because I want to make the complete asset not a simple asset
    can you send image from your Top Down game?
     
  10. Pandur1982

    Pandur1982

    Joined:
    Jun 16, 2015
    Posts:
    241
    Hello Seyed_Morteza_Kamaly , sry for my late answer at the moment i have many work at time. On the 17.4. iam back at home and i can create a list for you and some images from my project.
    Wish you a nice day , see you later.
     
    Seyed_Morteza_Kamaly likes this.
  11. Pandur1982

    Pandur1982

    Joined:
    Jun 16, 2015
    Posts:
    241
    So iam Back at home, so here 2 screens from a testlevel with the mask action.




    So and here a littel list for it :

    * Noise Mask
    * Basic Mask

    * Standard Shader
    * Untilt Texture Shader
    * Nature Shader (speed Tree...)

    At the moment i work with Raycast and Layers, on the Camera is a Littel Quad Object with the Basic Mask when the Raycast hit a Object with Layer "BlockObjects" the Quad Object will set on when the Raycast no more hit a Object with Layer "BlockObjects" the Quad Bject will set off.

    So i hope that help you.
     
  12. Seyed_Morteza_Kamaly

    Seyed_Morteza_Kamaly

    Joined:
    Nov 18, 2015
    Posts:
    69

    Hi thanks for adding wish list I will check it out
    it seems your games is fun :)
     
    Pandur1982 likes this.
  13. Pandur1982

    Pandur1982

    Joined:
    Jun 16, 2015
    Posts:
    241
    Thx, but at the moment i have not so mutch time for my project, from time to time i work on the player Controller