Search Unity

I'm having some Shader issues, someone able to give me some tips?

Discussion in 'Shaders' started by Shigbeard, Jan 25, 2020.

  1. Shigbeard

    Shigbeard

    Joined:
    Jan 24, 2020
    Posts:
    3
    Hey guys. I'm new to unity, and have been trying to make some rounded portals. I have just one problem though.... I can't seem to get the shader that fiddles with the camera material (seems to be a pixel or vertex shader) to play nice with the shader that masks out the edges of the plane.

    Code (CSharp):
    1. Shader "Portal/Camera And Mask"
    2. {
    3.     Properties
    4.     {
    5.         _Color ("Main Color", Color) = (1,1,1,1)
    6.         _MainTex ("Base (RGB) Trans (A)", 2D) = "white" {}
    7.         _CutTex ("Cutout (A)", 2D) = "white" {}
    8.         _Cutoff ("Alpha cutoff", Range(0,1)) = 0.5
    9.     }
    10.     SubShader
    11.     {
    12.         Tags{ "Queue" = "Transparent" "IgnoreProjector" = "True" "RenderType" = "Transparent" }
    13.         Lighting Off
    14.         Cull Back
    15.         ZWrite On
    16.         ZTest Less
    17.        
    18.         Fog{ Mode Off }
    19.         LOD 100
    20.         Pass
    21.         {
    22.             CGPROGRAM
    23.             #pragma vertex vert
    24.             #pragma fragment frag
    25.            
    26.             #include "UnityCG.cginc"
    27.  
    28.             struct appdata
    29.             {
    30.                 float4 vertex : POSITION;
    31.                 float2 uv : TEXCOORD0;
    32.             };
    33.  
    34.             struct v2f
    35.             {
    36.                 //float2 uv : TEXCOORD0;
    37.                 float4 vertex : SV_POSITION;
    38.                 float4 screenPos : TEXCOORD1;
    39.             };
    40.  
    41.             v2f vert (appdata v)
    42.             {
    43.                 v2f o;
    44.                 o.vertex = UnityObjectToClipPos(v.vertex);
    45.                 o.screenPos = ComputeScreenPos(o.vertex);
    46.                 return o;
    47.             }
    48.            
    49.             sampler2D _MainTex;
    50.  
    51.             fixed4 frag (v2f i) : SV_Target
    52.             {
    53.                 i.screenPos /= i.screenPos.w;
    54.                 fixed4 col = tex2D(_MainTex, float2(i.screenPos.x, i.screenPos.y));
    55.                
    56.                 return col;
    57.             }
    58.             ENDCG
    59.         }
    60.         CGPROGRAM
    61.         #pragma surface surf Lambert alpha
    62.  
    63.         sampler2D _MainTex;
    64.         sampler2D _CutTex;
    65.         fixed4 _Color;
    66.         float _Cutoff;
    67.  
    68.         struct Input {
    69.             float2 col;
    70.         };
    71.  
    72.         void surf (Input IN, inout SurfaceOutput o) {
    73.             fixed4 c = tex2D(_MainTex, IN.col) * _Color;
    74.             float ca = tex2D(_CutTex, IN.col).a;
    75.             o.Albedo = c.rgb;
    76.  
    77.             if (ca > _Cutoff)
    78.                 o.Alpha = c.a;
    79.             else
    80.                 o.Alpha = 0;
    81.         }
    82.         ENDCG
    83.     }
    84. }
    85.  
    This code is currently working with no errors, but does not produce the mask effect unless I remove everything in the Pass block. Here's what it currently looks like.



    Here's the plane when the mask is being applied correctly but note the shader isn't manipulating the camera like it should be.



    I'd appreciate any help.
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,348
    I’m ... I’m even not sure I understand how your shader is working. Or why you’re using a surface shader. I’m really just amazed that the surface shader compiles into anything useful. This line specifically:
    The fact you can use a variable named
    col
    and use that as UVs, and get anything useful is ... odd.

    I have one main question for you though. Why are you using a surface shader here along with a regular pass?
     
    Shigbeard likes this.
  3. Shigbeard

    Shigbeard

    Joined:
    Jan 24, 2020
    Posts:
    3
    I'm not gonna lie, I have no idea what I'm doing and am just trying to merge two shaders I found online together. I'm hoping to get a feel for how it all works as I go along... hell I don't even know what a UV is, all I know is that script A produces result A, script B produces result B, but I can't get result A and B at the same time.

    Like I said, my goal is to take a camera's image and manipulate it so that the plane i'm drawing it to shows the image as if the image were layered behind my scene and the plane was a transparent hole in the scene to that image... but also to apply an image mask to it so that it only draws the image in a circle (or in this case, an oval as the mask is stretched vertically). I can achieve one of those things, but not both.

    And I have no idea how I am achieving it.

    EDIT: I remember why im using a surface shader along with a regular pass. If I try to incorporate either element (the pass and the surface shader) into one (either a pass or not a pass), Unity complains about not being able to use
    #pragma surface
    with any other... programs i think? Anyway this basically suppresses that error. I thought it would allow the shader to work but... clearly I was wrong, like I am about so many other things.
     
    Last edited: Jan 28, 2020
  4. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,348
    Well, I can tell you ... you probably don't want the surface shader stuff at all (the
    CGPROGRAM
    with the
    #pragma surf
    line). Taking two shaders and merging them together (usually) requires more than copying the code from both wholesale into the same shader file. Usually it results in something that's no different than having both shaders as separate materials with the same settings and on duplicate mesh renderers in the same position. In fact that's probably kind of what's happening.

    My suggestion is to read through all 5 parts of this:
    https://www.alanzucconi.com/2015/06/10/a-gentle-introduction-to-shaders-in-unity3d/

    Then maybe watch some tutorial videos by Makin' Stuff Look Good, or otherwise search online for beginner shader tutorials.


    What you're trying to do isn't super hard, but just getting a little more knowledge about what's going on in the code you're looking at should help you take a step in the right direction. If you have problems merging them after you've done some research we'll be happy to help point you in the right direction.
     
    Shigbeard likes this.
  5. Shigbeard

    Shigbeard

    Joined:
    Jan 24, 2020
    Posts:
    3
    Looks like I got some learning to do. Thanks friend, I'll head on over to those links and get to work learning not to be a complete dunce.

    (incase I sounded disingenuous, i mean it. Thank you heaps)