-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathViewport2.shader
More file actions
307 lines (238 loc) · 11.4 KB
/
Viewport2.shader
File metadata and controls
307 lines (238 loc) · 11.4 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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
Shader "OwenTheProgrammer/Viewport2" {
Properties {
_CameraPosition("Camera Position", Vector) = (0, 0, -1, 0)
_CameraRotation("Camera Rotation", Vector) = (0, 0, 0, 0)
_CameraFOV("Camera FOV", Range(1, 179)) = 60
}
SubShader {
Pass {
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
#define cot(x) ( cos(x) / sin(x) )
struct inputData {
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};
struct v2f {
float4 vertex : SV_POSITION;
float2 uv : TEXCOORD0;
};
v2f vert(inputData i) {
v2f o;
o.vertex = UnityObjectToClipPos(i.vertex);
o.uv = i.uv;
return o;
}
float3 _CameraPosition;
float3 _CameraRotation;
float _CameraFOV;
struct Camera {
float3 worldPos;
float fovAngle;
float fovSlope;
float2 screenPos;
float2 clipPos;
float3x3 viewMatrix;
float3 localRayDir;
float3 worldRayDir;
};
struct DirectionalLight {
float3 direction;
float3 color;
};
struct Plane {
float3 worldPos;
float2 scale;
float3 normal;
float rayDistance;
float3 worldRayPos;
float3 localRayPos;
float geometryMask;
float3 albedo;
};
struct Box {
float3 worldPos;
float3 scale;
float rayDistance;
float3 worldRayPos;
float3 localRayPos;
float geometryMask;
float3 normal;
float3 albedo;
};
struct Sphere {
float3 worldPos;
float radius;
float rayDistance;
float3 worldRayPos;
float3 localRayPos;
float geometryMask;
float3 normal;
float3 albedo;
};
float4x4 RotationMatrix(float3 angles) {
float3 angle_rad = radians(angles);
float3 c = cos(angle_rad);
float3 s = sin(angle_rad);
float4x4 RX = {
1.0, 0.0, 0.0, 0.0,
0.0, c.x, -s.x, 0.0,
0.0, s.x, c.x, 0.0,
0.0, 0.0, 0.0, 1.0
};
float4x4 RY = {
c.y, 0.0, s.y, 0.0,
0.0, 1.0, 0.0, 0.0,
-s.y, 0.0, c.y, 0.0,
0.0, 0.0, 0.0, 1.0
};
float4x4 RZ = {
c.z, -s.z, 0.0, 0.0,
s.z, c.z, 0.0, 0.0,
0.0, 0.0, 1.0, 0.0,
0.0, 0.0, 0.0, 1.0
};
return mul(RY, mul(RX, RZ));
}
//Projects a direction vector from the camera position through the unit distance clipping plane
float3 GetCameraRayDirection(float2 clipPos, float fovSlope) {
//Scale [-1 | +1] range to [-viewAngle | +viewAngle]
float3 viewPlane = float3(clipPos * fovSlope, 1);
return normalize(viewPlane);
}
float3x3 GetCameraLookAtMatrix(float3 camPos, float3 targetPos) {
float3 fwd = normalize(camPos - targetPos);
float3 right = cross(fwd, float3(0, 1, 0));
float3 up = cross(right, fwd);
return transpose(float3x3(normalize(right), normalize(up), -fwd));
}
float3 Skybox(float3 rayDir, DirectionalLight light) {
//return max(0.0, dot(rayDir, light.direction)) * 0.7 + 0.04;
return max(0, rayDir.y) * 0.7 + 0.04;
}
float3 PlaneMaterial(float2 uv) {
float2 terms = step(frac(uv), 0.5);
float checkers = abs(terms.x - abs(terms.y)); //xor
return lerp(0.2, 0.4, checkers);
}
float3 SphereMaterial(Sphere sphere, DirectionalLight light) {
float NdotL = max(0.0, dot(sphere.normal, light.direction));
return max(0.1, NdotL * light.color);
}
Camera CreateCamera(float2 uv) {
Camera camera = (Camera)0;
camera.worldPos = _CameraPosition;
//comment out for no more rotation
camera.worldPos.xz = 3 * float2(cos(_Time.y * 0.5), sin(_Time.y * 0.5));
camera.fovAngle = radians(_CameraFOV * 0.5);
camera.fovSlope = tan(camera.fovAngle);
camera.screenPos = uv;
camera.clipPos = camera.screenPos * 2 - 1;
camera.viewMatrix = GetCameraLookAtMatrix(camera.worldPos, float3(0,0,0));
//camera.viewMatrix = (float3x3)RotationMatrix(_CameraRotation);
camera.localRayDir = GetCameraRayDirection(camera.clipPos, camera.fovSlope);
camera.worldRayDir = mul(camera.viewMatrix, camera.localRayDir);
return camera;
}
void CalculatePlaneInfo(Camera cam, inout Plane p) {
//Ray to plane formula
float t_n = dot(p.worldPos - cam.worldPos, p.normal);
float t_d = dot(cam.worldRayDir, p.normal);
float t = t_n / t_d;
//Calculate ray position in different spaces
p.rayDistance = t;
p.worldRayPos = cam.worldPos + cam.worldRayDir * t;
p.localRayPos = p.worldRayPos - p.worldPos;
//Geometry masking
float isFrontface = t_d < 1e-6;
float isInBounds = all(abs(p.localRayPos.xz) < (p.scale * 0.5));
p.geometryMask = isFrontface && isInBounds;
p.albedo = PlaneMaterial(p.localRayPos.xz);
}
void CalculateSphereInfo(Camera cam, DirectionalLight light, inout Sphere sphere) {
float3 localRay = sphere.worldPos - cam.worldPos;
float tn = dot(localRay, cam.worldRayDir);
//if(tn < 0) no hit
float sdf = dot(localRay, localRay) - tn * tn;
//if(sdf > tn*tn) no hit
float hitDist = sqrt(sphere.radius * sphere.radius - sdf);
float t = tn - hitDist;
//Calculate ray position in different spaces
sphere.rayDistance = t;
sphere.worldRayPos = cam.worldPos + cam.worldRayDir * t;
sphere.localRayPos = sphere.worldRayPos - sphere.worldPos;
//Geometry masking
sphere.geometryMask = (tn > 0) && (sdf < sphere.radius * sphere.radius);
sphere.normal = sphere.localRayPos / sphere.radius;
//sphere.normal = sdf; //sphere.localRayPos / sphere.radius;
sphere.albedo = SphereMaterial(sphere, light);
}
void CalculateBoxInfo(Camera cam, inout Box box) {
float3 invRayDir = rcp(cam.worldRayDir);
float3 camTangent = invRayDir * (cam.worldPos - box.worldPos);
float3 quadrant = abs(invRayDir) * (box.scale * 0.5);
float3 t1 = -camTangent - quadrant;
float3 t2 = -camTangent + quadrant;
float tNear = max(t1.x, max(t1.y, t1.z));
float tFar = min(t2.x, min(t2.y, t2.z));
//Calculate ray positionin different spaces
box.rayDistance = tNear;
box.worldRayPos = cam.worldPos + cam.worldRayDir * tNear;
box.localRayPos = box.worldRayPos - box.worldPos;
float isInBounds = (tNear <= tFar && tFar > 0.0);
box.geometryMask = isInBounds;
//box.normal = -sign(cam.worldRayDir) * step(t1.yzx, t1.xyz) * step(t1.zxy, t1.xyz);
box.normal = step(t1.yzx, t1.xyz) * step(t1.zxy, t1.xyz);
box.albedo = box.normal;
}
float Shadow(Plane plane, DirectionalLight light, Sphere sphere) {
float3 rayHit = plane.worldRayPos;
float3 ballPivot = sphere.worldPos;
float3 lightDir = light.direction;
float shadowPlaneDist = dot(ballPivot - rayHit, lightDir) / dot(lightDir, lightDir);
float3 shadowPlaneHit = rayHit + shadowPlaneDist * lightDir;
float3 localPlanePos = shadowPlaneHit - ballPivot;
return saturate(distance(rayHit, ballPivot) - sphere.radius);
}
float3 frag (v2f i) : SV_Target {
DirectionalLight light = (DirectionalLight)0;
light.direction = normalize(_WorldSpaceLightPos0);
light.color = float3(1,1,1);
Camera camera = CreateCamera(i.uv);
Plane groundPlane = (Plane)0;
groundPlane.worldPos = float3(0,-0.5,0);
groundPlane.scale = float2(4, 4);
groundPlane.normal = float3(0,1,0);
Sphere ball = (Sphere)0;
ball.worldPos = float3(0, 0, 0);
ball.worldPos = float3(0, sin(_Time.y) * 0.5 + 0.5, 0);
ball.radius = 0.5;
Box cube = (Box)0;
cube.worldPos = float3(0, 1, 0);
cube.scale = float3(1, 1, 1);
CalculatePlaneInfo(camera, groundPlane);
float3 groundColor = groundPlane.albedo * groundPlane.geometryMask;
CalculateBoxInfo(camera, cube);
float3 boxColor = cube.albedo * cube.geometryMask;
CalculateSphereInfo(camera, light, ball);
float3 sphereColor = ball.normal * ball.geometryMask;
float ballShadow = Shadow(groundPlane, light, ball);
float3 frameBuffer = Skybox(camera.worldRayDir, light);
frameBuffer *= (1 - groundPlane.geometryMask);
//frameBuffer += groundColor;
frameBuffer += groundColor * ballShadow;
frameBuffer *= (1 - ball.geometryMask);
frameBuffer += saturate(sphereColor);
//frameBuffer *= (1 - cube.geometryMask);
//frameBuffer += saturate(boxColor);
//frameBuffer = lerp(frameBuffer, groundColor, groundPlane.geometryMask);
//frameBuffer = (1 - ball.geometryMask) * frameBuffer;
//return sphereColor + frameBuffer;
return frameBuffer;
}
ENDCG
}
}
}