SkyBoxにステンシルバッファを適応したい
該当のソースコード
ShaderLab
1Shader "Unlit/Stencil" 2{ 3 Properties 4 { 5 _MainTex ("Texture", 2D) = "white" {} 6 } 7 SubShader 8 { 9 Tags { "RenderType"="Transparent" "Queue"="Transparent" } 10 ZWrite Off 11 Blend SrcAlpha OneMinusSrcAlpha 12 13 Pass 14 { 15 Stencil 16 { 17 Ref 2 18 Comp Always 19 Pass Replace 20 } 21 22 CGPROGRAM 23 #pragma vertex vert 24 #pragma fragment frag 25 26 #include "UnityCG.cginc" 27 28 sampler2D _MainTex; 29 float4 _MainTex_ST; 30 31 struct appdata 32 { 33 float4 vertex : POSITION; 34 float2 texcoord : TEXCOORD0; 35 }; 36 37 struct v2f 38 { 39 float4 vertex : SV_POSITION; 40 float2 uv : TEXCOORD0; 41 }; 42 43 v2f vert (appdata v) 44 { 45 v2f o; 46 o.vertex = UnityObjectToClipPos(v.vertex); 47 o.uv = TRANSFORM_TEX(v.texcoord, _MainTex); 48 return o; 49 } 50 51 fixed4 frag (v2f i) : SV_Target 52 { 53 return tex2D(_MainTex, i.uv); 54 } 55 ENDCG 56 } 57 } 58} 59
ShaderLab
1// Upgrade NOTE: replaced 'glstate.matrix.mvp' with 'UNITY_MATRIX_MVP' 2// Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)' 3 4Shader "Unlit/StencilBase" 5{ 6 Properties { 7 _Tint ("Tint Color", Color) = (.5, .5, .5, .5) 8 [Gamma] _Exposure ("Exposure", Range(0, 8)) = 1.0 9 _Rotation ("Rotation", Range(0, 360)) = 0 10 [NoScaleOffset] _FrontTex ("Front [+Z] (HDR)", 2D) = "grey" {} 11 [NoScaleOffset] _BackTex ("Back [-Z] (HDR)", 2D) = "grey" {} 12 [NoScaleOffset] _LeftTex ("Left [+X] (HDR)", 2D) = "grey" {} 13 [NoScaleOffset] _RightTex ("Right [-X] (HDR)", 2D) = "grey" {} 14 [NoScaleOffset] _UpTex ("Up [+Y] (HDR)", 2D) = "grey" {} 15 [NoScaleOffset] _DownTex ("Down [-Y] (HDR)", 2D) = "grey" {} 16} 17 18 SubShader { 19 Tags { "Queue"="Background" "RenderType"="Background" "PreviewType"="Skybox" } 20 Cull Off ZWrite Off 21 22 Stencil 23 { 24 Ref 2 25 Comp Equal 26 } 27 28 CGINCLUDE 29 #include "UnityCG.cginc" 30 31 half4 _Tint; 32 half _Exposure; 33 float _Rotation; 34 35 float3 RotateAroundYInDegrees (float3 vertex, float degrees) 36 { 37 float alpha = degrees * UNITY_PI / 180.0; 38 float sina, cosa; 39 sincos(alpha, sina, cosa); 40 float2x2 m = float2x2(cosa, -sina, sina, cosa); 41 return float3(mul(m, vertex.xz), vertex.y).xzy; 42 } 43 44 struct appdata_t { 45 float4 vertex : POSITION; 46 float2 texcoord : TEXCOORD0; 47 UNITY_VERTEX_INPUT_INSTANCE_ID 48 }; 49 struct v2f { 50 float4 vertex : SV_POSITION; 51 float2 texcoord : TEXCOORD0; 52 UNITY_VERTEX_OUTPUT_STEREO 53 }; 54 v2f vert (appdata_t v) 55 { 56 v2f o; 57 UNITY_SETUP_INSTANCE_ID(v); 58 UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o); 59 float3 rotated = RotateAroundYInDegrees(v.vertex, _Rotation); 60 o.vertex = UnityObjectToClipPos(rotated); 61 o.texcoord = v.texcoord; 62 return o; 63 } 64 half4 skybox_frag (v2f i, sampler2D smp, half4 smpDecode) 65 { 66 half4 tex = tex2D (smp, i.texcoord); 67 half3 c = DecodeHDR (tex, smpDecode); 68 c = c * _Tint.rgb * unity_ColorSpaceDouble.rgb; 69 c *= _Exposure; 70 return half4(c, 1); 71 } 72 ENDCG 73 74 Pass { 75 CGPROGRAM 76 #pragma vertex vert 77 #pragma fragment frag 78 #pragma target 2.0 79 sampler2D _FrontTex; 80 half4 _FrontTex_HDR; 81 half4 frag (v2f i) : SV_Target { return skybox_frag(i,_FrontTex, _FrontTex_HDR); } 82 ENDCG 83 } 84 Pass{ 85 CGPROGRAM 86 #pragma vertex vert 87 #pragma fragment frag 88 #pragma target 2.0 89 sampler2D _BackTex; 90 half4 _BackTex_HDR; 91 half4 frag (v2f i) : SV_Target { return skybox_frag(i,_BackTex, _BackTex_HDR); } 92 ENDCG 93 } 94 Pass{ 95 CGPROGRAM 96 #pragma vertex vert 97 #pragma fragment frag 98 #pragma target 2.0 99 sampler2D _LeftTex; 100 half4 _LeftTex_HDR; 101 half4 frag (v2f i) : SV_Target { return skybox_frag(i,_LeftTex, _LeftTex_HDR); } 102 ENDCG 103 } 104 Pass{ 105 CGPROGRAM 106 #pragma vertex vert 107 #pragma fragment frag 108 #pragma target 2.0 109 sampler2D _RightTex; 110 half4 _RightTex_HDR; 111 half4 frag (v2f i) : SV_Target { return skybox_frag(i,_RightTex, _RightTex_HDR); } 112 ENDCG 113 } 114 Pass{ 115 CGPROGRAM 116 #pragma vertex vert 117 #pragma fragment frag 118 #pragma target 2.0 119 sampler2D _UpTex; 120 half4 _UpTex_HDR; 121 half4 frag (v2f i) : SV_Target { return skybox_frag(i,_UpTex, _UpTex_HDR); } 122 ENDCG 123 } 124 Pass{ 125 CGPROGRAM 126 #pragma vertex vert 127 #pragma fragment frag 128 #pragma target 2.0 129 sampler2D _DownTex; 130 half4 _DownTex_HDR; 131 half4 frag (v2f i) : SV_Target { return skybox_frag(i,_DownTex, _DownTex_HDR); } 132 ENDCG 133 } 134} 135} 136
試したこと
StencilBaseというSkyBoxを使い、Stencilというシェーダーを適応させたQuadをワールドに配置しましたが、Quadがある領域のみに描画されず、SkyBoxも描画されません。
どうすれば、SkyBoxにステンシルバッファを適応できるでしょうか?
回答お願いします。
↑Quadを配置した状態です
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2020/07/27 18:24 編集