質問編集履歴

1

shaderやmaterialの追加

2020/02/16 01:18

投稿

zenobread
zenobread

スコア44

test CHANGED
File without changes
test CHANGED
@@ -27,3 +27,323 @@
27
27
 
28
28
 
29
29
  なぜかすごくちらつきがあるのと、元のカラフルな色合いが真っ黒になってしまうのでそちらを修正したいです
30
+
31
+
32
+
33
+ ### 追記 materialとshaderの設定のスクショ
34
+
35
+
36
+
37
+ material
38
+
39
+ ![イメージ説明](b45d9dd408e7293e140fc22ba28dd64f.png)
40
+
41
+
42
+
43
+ ChromaKey_Standard_Transparent(テクスチャにつけているshader)
44
+
45
+ ```shader
46
+
47
+ Shader "ChromaKey/Standard/Transparent"
48
+
49
+ {
50
+
51
+
52
+
53
+ Properties
54
+
55
+ {
56
+
57
+ [Header(Material)]
58
+
59
+ _Color ("Color", Color) = (1, 1, 1, 1)
60
+
61
+ _MainTex ("Albedo (RGB)", 2D) = "white" {}
62
+
63
+ _Glossiness ("Smoothness", Range(0, 1)) = 0.5
64
+
65
+ _Metallic ("Metallic", Range(0, 1)) = 0.0
66
+
67
+ [Enum(UnityEngine.Rendering.CullMode)] _Cull("Culling", Int) = 2
68
+
69
+
70
+
71
+ [Header(Chroma Key)]
72
+
73
+ _ChromaKeyColor("Color", Color) = (0.0, 0.0, 1.0, 0.0)
74
+
75
+ _ChromaKeyHueRange("Hue Range", Range(0, 1)) = 0.1
76
+
77
+ _ChromaKeySaturationRange("Saturation Range", Range(0, 1)) = 0.5
78
+
79
+ _ChromaKeyBrightnessRange("Brightness Range", Range(0, 1)) = 0.5
80
+
81
+ }
82
+
83
+
84
+
85
+ SubShader
86
+
87
+ {
88
+
89
+ Tags
90
+
91
+ {
92
+
93
+ "Queue" = "Transparent"
94
+
95
+ "RenderType" = "Transparent"
96
+
97
+ "IgnoreProjector" = "True"
98
+
99
+ "PreviewType" = "Plane"
100
+
101
+ }
102
+
103
+
104
+
105
+ Cull [_Cull]
106
+
107
+
108
+
109
+ CGPROGRAM
110
+
111
+ #pragma surface surf Standard alpha:blend addshadow fullforwardshadows
112
+
113
+ #pragma target 3.0
114
+
115
+ #define CHROMA_KEY_ALPHA
116
+
117
+ #include "./ChromaKey_Standard.cginc"
118
+
119
+ ENDCG
120
+
121
+
122
+
123
+ Pass
124
+
125
+ {
126
+
127
+ Tags { "LightMode" = "ShadowCaster" }
128
+
129
+ ZWrite On
130
+
131
+ ZTest LEqual
132
+
133
+ Cull Off
134
+
135
+
136
+
137
+ CGPROGRAM
138
+
139
+ #include "./Chromakey_Shadow.cginc"
140
+
141
+ #pragma vertex vert
142
+
143
+ #pragma fragment frag
144
+
145
+ #pragma multi_compilecaster
146
+
147
+ ENDCG
148
+
149
+ }
150
+
151
+ }
152
+
153
+
154
+
155
+ FallBack "Diffuse"
156
+
157
+
158
+
159
+ }
160
+
161
+ ```
162
+
163
+
164
+
165
+ ChromaKey_Standard(ChromaKey_Standard_TransParentで使われているcgincファイル)
166
+
167
+ ```cginc
168
+
169
+ #ifndef CHROMA_KEY_STANDARD_CGINC
170
+
171
+ #define CHROMA_KEY_STANDARD_CGINC
172
+
173
+
174
+
175
+ #include "UnityCG.cginc"
176
+
177
+ #include "./ChromaKey.cginc"
178
+
179
+
180
+
181
+ sampler2D _MainTex;
182
+
183
+
184
+
185
+ struct Input
186
+
187
+ {
188
+
189
+ float2 uv_MainTex;
190
+
191
+ };
192
+
193
+
194
+
195
+ half _Glossiness;
196
+
197
+ half _Metallic;
198
+
199
+ fixed4 _Color;
200
+
201
+
202
+
203
+ void surf(Input IN, inout SurfaceOutputStandard o)
204
+
205
+ {
206
+
207
+ fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
208
+
209
+ #ifdef CHROMA_KEY_ALPHA
210
+
211
+ ChromaKeyApplyAlpha(c);
212
+
213
+ #else
214
+
215
+ ChromaKeyApplyCutout(c);
216
+
217
+ #endif
218
+
219
+ o.Albedo = c.rgb;
220
+
221
+ o.Metallic = _Metallic;
222
+
223
+ o.Smoothness = _Glossiness;
224
+
225
+ o.Alpha = c.a;
226
+
227
+ }
228
+
229
+
230
+
231
+ #endif
232
+
233
+ ```
234
+
235
+
236
+
237
+ ChromaKey.cginc(ChromaKey_Standardで使われているcgincファイル)
238
+
239
+ ```cginc
240
+
241
+ #ifndef CHROMA_KEY_COMMON_CGINC
242
+
243
+ #define CHROMA_KEY_COMMON_CGINC
244
+
245
+
246
+
247
+ // Refer to the following website regarding conversions between RGB and HSV:
248
+
249
+ // https://www.laurivan.com/rgb-to-hsv-to-rgb-for-shaders/
250
+
251
+
252
+
253
+ float4 _ChromaKeyColor;
254
+
255
+ float _ChromaKeyHueRange;
256
+
257
+ float _ChromaKeySaturationRange;
258
+
259
+ float _ChromaKeyBrightnessRange;
260
+
261
+
262
+
263
+ inline float3 ChromaKeyRGB2HSV(float3 rgb)
264
+
265
+ {
266
+
267
+ float4 k = float4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0);
268
+
269
+ float4 p = lerp(float4(rgb.bg, k.wz), float4(rgb.gb, k.xy), step(rgb.b, rgb.g));
270
+
271
+ float4 q = lerp(float4(p.xyw, rgb.r), float4(rgb.r, p.yzx), step(p.x, rgb.r));
272
+
273
+ float d = q.x - min(q.w, q.y);
274
+
275
+ float e = 1e-10;
276
+
277
+ return float3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x);
278
+
279
+ }
280
+
281
+
282
+
283
+ inline float3 ChromaKeyHSV2RGB(float3 hsv)
284
+
285
+ {
286
+
287
+ float4 k = float4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0);
288
+
289
+ float3 p = abs(frac(hsv.xxx + k.xyz) * 6.0 - k.www);
290
+
291
+ return hsv.z * lerp(k.xxx, clamp(p - k.xxx, 0.0, 1.0), hsv.y);
292
+
293
+ }
294
+
295
+
296
+
297
+ inline float3 ChromaKeyCalcDiffrence(float4 col)
298
+
299
+ {
300
+
301
+ float3 hsv = ChromaKeyRGB2HSV(col);
302
+
303
+ float3 key = ChromaKeyRGB2HSV(_ChromaKeyColor);
304
+
305
+ return abs(hsv - key);
306
+
307
+ }
308
+
309
+
310
+
311
+ inline float3 ChromaKeyGetRange()
312
+
313
+ {
314
+
315
+ return float3(_ChromaKeyHueRange, _ChromaKeySaturationRange, _ChromaKeyBrightnessRange);
316
+
317
+ }
318
+
319
+
320
+
321
+ inline void ChromaKeyApplyCutout(float4 col)
322
+
323
+ {
324
+
325
+ float3 d = ChromaKeyCalcDiffrence(col);
326
+
327
+ if (all(step(0.0, ChromaKeyGetRange() - d))) discard;
328
+
329
+ }
330
+
331
+
332
+
333
+ inline void ChromaKeyApplyAlpha(inout float4 col)
334
+
335
+ {
336
+
337
+ float3 d = ChromaKeyCalcDiffrence(col);
338
+
339
+ if (all(step(0.0, ChromaKeyGetRange() - d))) discard;
340
+
341
+ col.a *= saturate(length(d / ChromaKeyGetRange()) - 1.0);
342
+
343
+ }
344
+
345
+
346
+
347
+ #endif
348
+
349
+ ```