Hi, I'm attempting to use the logic in this video to create a dynamic nebula shader: The video basically says I need to use the absolute of the dot product of the vertex normal and the camera view direction. Here is my shader code: Code (csharp): Shader "Custom/Dynamic Nebula" { Properties { _MainTex ("MainTex (RGB)", 2D) = "white" {} _Color ("Color", Color) = (0,0,0,1) } Category { Tags { "Queue"="Transparent" } SubShader { Blend SrcAlpha One Cull Off Lighting Off ZWrite Off Fog { Color (0,0,0,0) } Pass { Name "BASE" Tags { "LightMode" = "Always" } CGPROGRAM #pragma vertex vert #pragma fragment frag #pragma fragmentoption ARB_precision_hint_fastest #include "UnityCG.cginc" struct appdata_t { float4 vertex : POSITION; float4 color : COLOR; float2 texcoord: TEXCOORD0; float3 normal : NORMAL0; }; struct v2f { float4 vertex : POSITION; float4 color : COLOR; float2 texcoord: TEXCOORD0; float3 normal : TEXCOORD1; float3 view : TEXCOORD2; }; float4 _MainTex_ST; float4 _Color; v2f vert (appdata_t v) { v2f o; o.vertex = mul(UNITY_MATRIX_MVP, v.vertex); o.color = _Color; o.texcoord = v.texcoord; o.view = normalize(ObjSpaceViewDir(v.vertex)); o.normal = v.normal; return o; } sampler2D _MainTex; half4 frag(v2f i) :COLOR0 { float4 result = i.color * tex2D(_MainTex, i.texcoord); result.a *= abs(dot(i.view, i.normal)); return result; } ENDCG } // end pass } // end subshader } // end category } // end shader Here is the screen shot of several planes with the shader that are rotated at different angles, the plane in the middle oriented straight up and down is an example fo the problem: What I need is for the apha to drop as the polygon normal lines up closer to the normal of the camera, that way when the polygon is rotated flat the image is invisible and when the polygon normal faces the camera the image is fully visible. What I have is close but not the same as the video shows, so I figured I'd see if some of you shader experts can help! Thanks in advance!
If you listen to that guy in video he says the trick is to hide the polygon when its almost facing its edge to the camera. One way is to apply a function to the texture alpha that is not linear like yours, instead you need a power function like in the picture bellow So, instead of Code (csharp): result.a *= abs(dot(i.view, i.normal)); use Code (csharp): result.a *= pow(abs(dot(i.view, i.normal)), 4); PS The more you increase the power of the function, the more it will be flattened at the bottom, meaning the you will get near zero values for higher values of abs(dot(i.view, i.normal))
There's no reason this shouldn't work anymore, it's basic shader math. Unless you mean that this shader file doesn't work with LWRP/HDRP, then sure, in which case you can recreate it in shadergraph or copy the default shader of the pipeline you're using and slot the relevant math from this shader into its appropriate sections.
Thanks for the reply! So from what I understand (very little about shaders) this shader is supposed to reduce the alpha to 0 as the camera angle approaches the edges. However if I copy pasta and save the shader (inside Visual studio... going ot give shadergraph a shot), it appears to work as a normal additive particle shader would, but it does not adjust the alpha depending on camera angle. I know little to nothing about shaders so any direction is helpful, going to experiment with shadergraph today! Edit: Tried again, and for some reason its working now. Just one of those things I guess where a Unity restart fixed the issue... OR perhaps I thought the effect would be more dramatic and just didn't notice (it seems to only reach alpha 0 when camera is directly on edge, it should reach 0before). I'm going to mess with the algo and see if I can get it to work a little better. Edit: So I thought I had saved the shader with scarpeliuses algo, but I had not, after making the changes it works great, just user error! Thanks for the help