forked from gam0022/UnityBible2-RaymarchingTutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path07_RaymarchingMaterial.shader
More file actions
257 lines (213 loc) · 9.32 KB
/
07_RaymarchingMaterial.shader
File metadata and controls
257 lines (213 loc) · 9.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
Shader "Unlit/07_RaymarchingMaterial"
{
Properties
{
_BallAlbedo ("Ball Albedo", Color) = (1, 0, 0, 1)
_FloorAlbedoA ("Floor Albedo A", Color) = (0, 0, 0, 1)
_FloorAlbedoB ("Floor Albedo B", Color) = (1, 1, 1, 1)
_SkyTopColor ("Sky Top Color", Color) = (1, 1, 1, 1)
_SkyBottomColor ("Sky Bottom Color", Color) = (1, 1, 1, 1)
}
SubShader
{
Tags { "RenderType" = "Opaque" }
Pass
{
Cull Off
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
float3 _BallAlbedo;
float3 _FloorAlbedoA;
float3 _FloorAlbedoB;
float3 _SkyTopColor;
float3 _SkyBottomColor;
struct appdata
{
float4 vertex: POSITION;
float2 uv: TEXCOORD0;
};
struct v2f
{
float2 uv: TEXCOORD0;
float4 vertex: SV_POSITION;
};
v2f vert(appdata v)
{
v2f o;
o.vertex = float4(v.vertex.xy * 2.0, 0.5, 1.0);
o.uv = v.uv;
// Direct3DのようにUVの上下が反転したプラットフォームを考慮します
#if UNITY_UV_STARTS_AT_TOP
o.uv.y = 1 - o.uv.y;
#endif
return o;
}
float mod(float x, float y)
{
return x - y * floor(x / y);
}
float sdSphere(float3 p, float r)
{
return length(p) - r;
}
float sdPlane(float3 p, float3 n, float h)
{
// nは正規化された法線
// hは原点からの距離
return dot(p, n) + h;
}
float dBall(float3 p)
{
return sdSphere(p - float3(0, 1, 0), 1);
}
float dFloor(float3 p)
{
return sdPlane(p, float3(0, 1, 0), 0);
}
float map(float3 p)
{
float d = dBall(p);
d = min(d, dFloor(p));
return d;
}
// 偏微分から法線を計算します
float3 calcNormal(float3 p)
{
float eps = 0.001;
return normalize(float3(
map(p + float3(eps, 0.0, 0.0)) - map(p + float3(-eps, 0.0, 0.0)),
map(p + float3(0.0, eps, 0.0)) - map(p + float3(0.0, -eps, 0.0)),
map(p + float3(0.0, 0.0, eps)) - map(p + float3(0.0, 0.0, -eps))
));
}
float calcAO(float3 pos, float3 nor)
{
float occ = 0.0;
float sca = 1.0;
for (int i = 0; i < 5; i++)
{
float h = 0.01 + 0.12 * float(i) / 4.0;
float d = map(pos + h * nor).x;
occ += (h - d) * sca;
sca *= 0.95;
if (occ > 0.35) break;
}
return clamp(1.0 - 3.0 * occ, 0.0, 1.0) * (0.5 + 0.5 * nor.y);
}
// http://iquilezles.org/www/articles/rmshadows/rmshadows.htm
float calcSoftshadow(float3 ro, float3 rd, float mint, float tmax)
{
// bounding volume
float tp = (0.8 - ro.y) / rd.y;
if (tp > 0.0) tmax = min(tmax, tp);
float res = 1.0;
float t = mint;
for (int i = 0; i < 24; i++)
{
float h = map(ro + rd * t).x;
float s = clamp(8.0 * h / t, 0.0, 1.0);
res = min(res, s * s * (3.0 - 2.0 * s));
t += clamp(h, 0.02, 0.2);
if (res < 0.004 || t > tmax) break;
}
return clamp(res, 0.0, 1.0);
}
float3 acesFilm(float3 x)
{
const float a = 2.51;
const float b = 0.03;
const float c = 2.43;
const float d = 0.59;
const float e = 0.14;
return saturate((x * (a * x + b)) / (x * (c * x + d) + e));
}
float4 frag(v2f input): SV_Target
{
float3 col = float3(0.0, 0.0, 0.0);
// UVを -1~1 の範囲に変換します
float2 uv = 2.0 * input.uv - 1.0;
// アスペクト比を考慮します
uv.x *= _ScreenParams.x / _ScreenParams.y;
// カメラの情報
float3 cameraOrigin = float3(0, 1, -3);// カメラの位置
float3 cameraTarget = float3(0, 1, 0);// カメラのターゲット
float3 cameraUp = float3(0, 1, 0);// カメラのUPベクトル
float cameraFov = 60;// カメラのFOV
// UVに対応するレイを計算
float3 forward = normalize(cameraTarget - cameraOrigin);
float3 right = normalize(cross(cameraUp, forward));
float3 up = normalize(cross(forward, right));
float PI = 3.14159265359;
float3 ray = normalize(
right * uv.x +
up * uv.y +
forward / tan(cameraFov / 360 * PI)
);
// レイマーチング
float t = 0.0;// レイの進んだ距離
float3 p = cameraOrigin;// レイの先端の座標
int i = 0;// レイマーチングのループカウンター
bool hit = false;// オブジェクトに衝突したかどうか
for (i = 0; i < 500; i++)
{
float d = map(p);// 最短距離を計算します
// 最短距離を0に近似できるなら、オブジェクトに衝突したとみなして、ループを抜けます
if (d < 0.0001)
{
hit = true;
break;
}
t += d;// 最短距離だけレイを進めます
p = cameraOrigin + ray * t;// レイの先端の座標を更新します
}
if (hit)
{
// ライティングのパラメーター
float3 normal = calcNormal(p);// 法線
float3 light = normalize(float3(1, 1, -1));// 平行光源の方向ベクトル
// マテリアルのパラメーター
float3 albedo = float3(1, 1, 1);// アルベド
float metalness = 0.5;// メタルネス(金属の度合い)
// ボールのマテリアルを設定
if (dBall(p) < 0.0001)
{
albedo = _BallAlbedo;
metalness = 0.8;
}
// 床のマテリアルを設定
if (dFloor(p) < 0.0001)
{
float checker = mod(floor(p.x) + floor(p.z), 2.0);
albedo = lerp(_FloorAlbedoA, _FloorAlbedoB, checker);
metalness = 0.1;
}
// ライティング計算
float diffuse = saturate(dot(normal, light));// 拡散反射
float specular = pow(saturate(dot(reflect(light, normal), ray)), 10.0);// 鏡面反射
float ao = calcAO(p, normal);// AO
float shadow = calcSoftshadow(p, light, 0.25, 5);// シャドウ
// ライティング結果の合成
col += albedo * diffuse * shadow * (1 - metalness);// 直接光の拡散反射
col += albedo * specular * shadow * metalness;// 直接光の鏡面反射
col += albedo * ao * lerp(_SkyBottomColor, _SkyTopColor, 0.3);// 環境光
// 遠景のフォグ
float invFog = exp(-0.02 * t);
col = lerp(_SkyBottomColor, col, invFog);
}
else
{
// 空
col = lerp(_SkyBottomColor, _SkyTopColor, ray.y);
}
// トーンマッピング
col = acesFilm(col * 0.8);
// ガンマ補正
col = pow(col, 1 / 2.2);
return float4(col, 1);
}
ENDCG
}
}
}