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): Shader "Portal/Camera And Mask" { Properties { _Color ("Main Color", Color) = (1,1,1,1) _MainTex ("Base (RGB) Trans (A)", 2D) = "white" {} _CutTex ("Cutout (A)", 2D) = "white" {} _Cutoff ("Alpha cutoff", Range(0,1)) = 0.5 } SubShader { Tags{ "Queue" = "Transparent" "IgnoreProjector" = "True" "RenderType" = "Transparent" } Lighting Off Cull Back ZWrite On ZTest Less Fog{ Mode Off } LOD 100 Pass { CGPROGRAM #pragma vertex vert #pragma fragment frag #include "UnityCG.cginc" struct appdata { float4 vertex : POSITION; float2 uv : TEXCOORD0; }; struct v2f { //float2 uv : TEXCOORD0; float4 vertex : SV_POSITION; float4 screenPos : TEXCOORD1; }; v2f vert (appdata v) { v2f o; o.vertex = UnityObjectToClipPos(v.vertex); o.screenPos = ComputeScreenPos(o.vertex); return o; } sampler2D _MainTex; fixed4 frag (v2f i) : SV_Target { i.screenPos /= i.screenPos.w; fixed4 col = tex2D(_MainTex, float2(i.screenPos.x, i.screenPos.y)); return col; } ENDCG } CGPROGRAM #pragma surface surf Lambert alpha sampler2D _MainTex; sampler2D _CutTex; fixed4 _Color; float _Cutoff; struct Input { float2 col; }; void surf (Input IN, inout SurfaceOutput o) { fixed4 c = tex2D(_MainTex, IN.col) * _Color; float ca = tex2D(_CutTex, IN.col).a; o.Albedo = c.rgb; if (ca > _Cutoff) o.Alpha = c.a; else o.Alpha = 0; } ENDCG } } 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.
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?
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.
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.
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)