質問編集履歴
1
shaderやmaterialの追加
title
CHANGED
File without changes
|
body
CHANGED
@@ -12,4 +12,164 @@
|
|
12
12
|

|
13
13
|
(クロマキー適用後)
|
14
14
|
|
15
|
-
なぜかすごくちらつきがあるのと、元のカラフルな色合いが真っ黒になってしまうのでそちらを修正したいです
|
15
|
+
なぜかすごくちらつきがあるのと、元のカラフルな色合いが真っ黒になってしまうのでそちらを修正したいです
|
16
|
+
|
17
|
+
### 追記 materialとshaderの設定のスクショ
|
18
|
+
|
19
|
+
material
|
20
|
+

|
21
|
+
|
22
|
+
ChromaKey_Standard_Transparent(テクスチャにつけているshader)
|
23
|
+
```shader
|
24
|
+
Shader "ChromaKey/Standard/Transparent"
|
25
|
+
{
|
26
|
+
|
27
|
+
Properties
|
28
|
+
{
|
29
|
+
[Header(Material)]
|
30
|
+
_Color ("Color", Color) = (1, 1, 1, 1)
|
31
|
+
_MainTex ("Albedo (RGB)", 2D) = "white" {}
|
32
|
+
_Glossiness ("Smoothness", Range(0, 1)) = 0.5
|
33
|
+
_Metallic ("Metallic", Range(0, 1)) = 0.0
|
34
|
+
[Enum(UnityEngine.Rendering.CullMode)] _Cull("Culling", Int) = 2
|
35
|
+
|
36
|
+
[Header(Chroma Key)]
|
37
|
+
_ChromaKeyColor("Color", Color) = (0.0, 0.0, 1.0, 0.0)
|
38
|
+
_ChromaKeyHueRange("Hue Range", Range(0, 1)) = 0.1
|
39
|
+
_ChromaKeySaturationRange("Saturation Range", Range(0, 1)) = 0.5
|
40
|
+
_ChromaKeyBrightnessRange("Brightness Range", Range(0, 1)) = 0.5
|
41
|
+
}
|
42
|
+
|
43
|
+
SubShader
|
44
|
+
{
|
45
|
+
Tags
|
46
|
+
{
|
47
|
+
"Queue" = "Transparent"
|
48
|
+
"RenderType" = "Transparent"
|
49
|
+
"IgnoreProjector" = "True"
|
50
|
+
"PreviewType" = "Plane"
|
51
|
+
}
|
52
|
+
|
53
|
+
Cull [_Cull]
|
54
|
+
|
55
|
+
CGPROGRAM
|
56
|
+
#pragma surface surf Standard alpha:blend addshadow fullforwardshadows
|
57
|
+
#pragma target 3.0
|
58
|
+
#define CHROMA_KEY_ALPHA
|
59
|
+
#include "./ChromaKey_Standard.cginc"
|
60
|
+
ENDCG
|
61
|
+
|
62
|
+
Pass
|
63
|
+
{
|
64
|
+
Tags { "LightMode" = "ShadowCaster" }
|
65
|
+
ZWrite On
|
66
|
+
ZTest LEqual
|
67
|
+
Cull Off
|
68
|
+
|
69
|
+
CGPROGRAM
|
70
|
+
#include "./Chromakey_Shadow.cginc"
|
71
|
+
#pragma vertex vert
|
72
|
+
#pragma fragment frag
|
73
|
+
#pragma multi_compilecaster
|
74
|
+
ENDCG
|
75
|
+
}
|
76
|
+
}
|
77
|
+
|
78
|
+
FallBack "Diffuse"
|
79
|
+
|
80
|
+
}
|
81
|
+
```
|
82
|
+
|
83
|
+
ChromaKey_Standard(ChromaKey_Standard_TransParentで使われているcgincファイル)
|
84
|
+
```cginc
|
85
|
+
#ifndef CHROMA_KEY_STANDARD_CGINC
|
86
|
+
#define CHROMA_KEY_STANDARD_CGINC
|
87
|
+
|
88
|
+
#include "UnityCG.cginc"
|
89
|
+
#include "./ChromaKey.cginc"
|
90
|
+
|
91
|
+
sampler2D _MainTex;
|
92
|
+
|
93
|
+
struct Input
|
94
|
+
{
|
95
|
+
float2 uv_MainTex;
|
96
|
+
};
|
97
|
+
|
98
|
+
half _Glossiness;
|
99
|
+
half _Metallic;
|
100
|
+
fixed4 _Color;
|
101
|
+
|
102
|
+
void surf(Input IN, inout SurfaceOutputStandard o)
|
103
|
+
{
|
104
|
+
fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
|
105
|
+
#ifdef CHROMA_KEY_ALPHA
|
106
|
+
ChromaKeyApplyAlpha(c);
|
107
|
+
#else
|
108
|
+
ChromaKeyApplyCutout(c);
|
109
|
+
#endif
|
110
|
+
o.Albedo = c.rgb;
|
111
|
+
o.Metallic = _Metallic;
|
112
|
+
o.Smoothness = _Glossiness;
|
113
|
+
o.Alpha = c.a;
|
114
|
+
}
|
115
|
+
|
116
|
+
#endif
|
117
|
+
```
|
118
|
+
|
119
|
+
ChromaKey.cginc(ChromaKey_Standardで使われているcgincファイル)
|
120
|
+
```cginc
|
121
|
+
#ifndef CHROMA_KEY_COMMON_CGINC
|
122
|
+
#define CHROMA_KEY_COMMON_CGINC
|
123
|
+
|
124
|
+
// Refer to the following website regarding conversions between RGB and HSV:
|
125
|
+
// https://www.laurivan.com/rgb-to-hsv-to-rgb-for-shaders/
|
126
|
+
|
127
|
+
float4 _ChromaKeyColor;
|
128
|
+
float _ChromaKeyHueRange;
|
129
|
+
float _ChromaKeySaturationRange;
|
130
|
+
float _ChromaKeyBrightnessRange;
|
131
|
+
|
132
|
+
inline float3 ChromaKeyRGB2HSV(float3 rgb)
|
133
|
+
{
|
134
|
+
float4 k = float4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0);
|
135
|
+
float4 p = lerp(float4(rgb.bg, k.wz), float4(rgb.gb, k.xy), step(rgb.b, rgb.g));
|
136
|
+
float4 q = lerp(float4(p.xyw, rgb.r), float4(rgb.r, p.yzx), step(p.x, rgb.r));
|
137
|
+
float d = q.x - min(q.w, q.y);
|
138
|
+
float e = 1e-10;
|
139
|
+
return float3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x);
|
140
|
+
}
|
141
|
+
|
142
|
+
inline float3 ChromaKeyHSV2RGB(float3 hsv)
|
143
|
+
{
|
144
|
+
float4 k = float4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0);
|
145
|
+
float3 p = abs(frac(hsv.xxx + k.xyz) * 6.0 - k.www);
|
146
|
+
return hsv.z * lerp(k.xxx, clamp(p - k.xxx, 0.0, 1.0), hsv.y);
|
147
|
+
}
|
148
|
+
|
149
|
+
inline float3 ChromaKeyCalcDiffrence(float4 col)
|
150
|
+
{
|
151
|
+
float3 hsv = ChromaKeyRGB2HSV(col);
|
152
|
+
float3 key = ChromaKeyRGB2HSV(_ChromaKeyColor);
|
153
|
+
return abs(hsv - key);
|
154
|
+
}
|
155
|
+
|
156
|
+
inline float3 ChromaKeyGetRange()
|
157
|
+
{
|
158
|
+
return float3(_ChromaKeyHueRange, _ChromaKeySaturationRange, _ChromaKeyBrightnessRange);
|
159
|
+
}
|
160
|
+
|
161
|
+
inline void ChromaKeyApplyCutout(float4 col)
|
162
|
+
{
|
163
|
+
float3 d = ChromaKeyCalcDiffrence(col);
|
164
|
+
if (all(step(0.0, ChromaKeyGetRange() - d))) discard;
|
165
|
+
}
|
166
|
+
|
167
|
+
inline void ChromaKeyApplyAlpha(inout float4 col)
|
168
|
+
{
|
169
|
+
float3 d = ChromaKeyCalcDiffrence(col);
|
170
|
+
if (all(step(0.0, ChromaKeyGetRange() - d))) discard;
|
171
|
+
col.a *= saturate(length(d / ChromaKeyGetRange()) - 1.0);
|
172
|
+
}
|
173
|
+
|
174
|
+
#endif
|
175
|
+
```
|