前提・実現したいこと
unityシェーダーで「ライトを使用しないで奥に行くほど暗くなるシェーダ」を作成しています。
現状、Unlitで「Texture」は出来たのですが、Unlitで「Cutout」と「Transparent」の対応がしたいのですが
出来ておりません。
(シェーダー初心者なので、お力をお借りしたく)
Unlitで「Texture」の自作シェーダー → (自分では出来ていると思っている…)
Shader "Custom/UnlitDepthShadow_Normal" { Properties { _MainTex ("Main Texture", 2D) = "white" {} _StartDepth ("カメラからの開始深度(小数)", Float) = 50. _EndDepth ("カメラからの終了深度(小数)", Float) = 100. _DepthColor ("深度カラー", Color) = (1,1,1,1) } SubShader { Pass { Tags { "RenderType"="Opaque" "Queue"="Geometry"} Lighting Off CGPROGRAM #pragma vertex vert #pragma fragment frag #include "UnityCG.cginc" struct appdata { float4 vertex : POSITION; float2 uv : TEXCOORD0; }; struct v2f { float4 vertex : SV_POSITION; float2 uv : TEXCOORD0; float4 projPos : TEXCOORD3; }; v2f vert(appdata v) { v2f o; o.vertex = UnityObjectToClipPos(v.vertex); o.uv = v.uv; // World position float3 _worldPos = mul(unity_ObjectToWorld, v.vertex); // Clip position float4 _pos = mul(UNITY_MATRIX_VP, float4(_worldPos, 1.)); // Screen position o.projPos = ComputeScreenPos(_pos); return o; } sampler2D _MainTex; float _StartDepth; float _EndDepth; fixed4 _DepthColor; sampler2D _CameraDepthTexture; // Depth texture fixed4 frag(v2f i) : SV_Target { fixed4 c = tex2D(_MainTex, i.uv); // カメラからの距離 float sceneZ = LinearEyeDepth(SAMPLE_DEPTH_TEXTURE_PROJ(_CameraDepthTexture, UNITY_PROJ_COORD(i.projPos))); if( _StartDepth <= sceneZ ) { fixed4 c2 = tex2D(_MainTex, i.uv) * _DepthColor; ////float _z = clamp(sceneZ,_StartDepth,_EndDepth ); float _per = (sceneZ - _StartDepth) / (_EndDepth - _StartDepth); // 奥に行くほど1.0になる c.rgb = lerp(c.rgb, c2.rgb, _per); } return c; } ENDCG } } FallBack "Diffuse" }
> 結果(成功)
↓↓↓↓↓ 以下失敗の様子 ↓↓↓↓↓
Unlitで「Transparent」の自作シェーダー
上手くDepth値が取得出来ていないのか、または、その他設定が悪いのか、見当がつかない状態です。
自分なりに調べてコード書いてみたのですがうまく行かず。
Shader "Custom/UnlitDepthShadow_Transparent" { Properties { _MainTex ("Main Texture", 2D) = "white" {} _StartDepth ("カメラからの開始深度(小数)", Float) = 50. _EndDepth ("カメラからの終了深度(小数)", Float) = 100. _DepthColor ("深度カラー", Color) = (1,1,1,1) } SubShader { Tags {"Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent"} Pass { Lighting Off ZWrite Off ZTest LEqual Blend SrcAlpha OneMinusSrcAlpha CGPROGRAM #pragma vertex vert #pragma fragment frag #include "UnityCG.cginc" struct appdata { float4 vertex : POSITION; float2 uv : TEXCOORD0; }; struct v2f { float4 vertex : SV_POSITION; float2 uv : TEXCOORD0; float4 projPos : TEXCOORD3; }; v2f vert(appdata v) { v2f o; o.vertex = UnityObjectToClipPos(v.vertex); o.uv = v.uv; // World position float3 _worldPos = mul(unity_ObjectToWorld, v.vertex); // Clip position float4 _pos = mul(UNITY_MATRIX_VP, float4(_worldPos, 1.)); // Screen position o.projPos = ComputeScreenPos(_pos); return o; } sampler2D _MainTex; float _StartDepth; float _EndDepth; fixed4 _DepthColor; sampler2D _CameraDepthTexture; // Depth texture fixed4 frag(v2f i) : SV_Target { fixed4 c = tex2D(_MainTex, i.uv); // カメラからの距離 float sceneZ = LinearEyeDepth(SAMPLE_DEPTH_TEXTURE_PROJ(_CameraDepthTexture, UNITY_PROJ_COORD(i.projPos))); ////float partZ = i.projPos.z; if( _StartDepth <= sceneZ ) { fixed4 c2 = tex2D(_MainTex, i.uv) * _DepthColor; float _per = (sceneZ - _StartDepth) / (_EndDepth - _StartDepth); // 奥に行くほど1.0になる c.rgb = lerp(c.rgb, c2.rgb, _per); } return c; } ENDCG } } FallBack "Diffuse" }
> 結果
実現したい事
以下写真右の様に、「Transparent」または「Cutout」でも、奥に行くほど色(影)を付けたいのです。
ご教授お願い致します。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/02/26 19:16