summaryrefslogtreecommitdiff
path: root/workspaces/main/shaders/scene1.wgsl
blob: 8d5d5db40c4bd905d70820301a19fe1f77df1325 (plain)
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
// Scene1 effect shader - ShaderToy conversion (raymarching cube & sphere)
// Source: Saturday cubism experiment by skal

#include "common_uniforms"
#include "math/color"
#include "math/utils"
#include "math/sdf_shapes"
#include "render/raymarching"

@group(0) @binding(0) var<uniform> uniforms: CommonUniforms;

const PI: f32 = 3.141592654;
const TAU: f32 = 6.283185307;

// Colors (precomputed HSV conversions)
const skyCol = vec3<f32>(0.176, 0.235, 0.25); // HSV(0.57, 0.90, 0.25)
const skylineCol = vec3<f32>(0.5, 0.125, 0.025); // HSV(0.02, 0.95, 0.5)
const sunCol = vec3<f32>(0.5, 0.163, 0.025); // HSV(0.07, 0.95, 0.5)
const diffCol1 = vec3<f32>(0.4, 1.0, 1.0); // HSV(0.60, 0.90, 1.0)
const diffCol2 = vec3<f32>(0.325, 1.0, 0.975); // HSV(0.55, 0.90, 1.0)

// Lighting (normalized manually)
const sunDir1 = vec3<f32>(0.0, 0.04997, -0.99875); // normalize(0, 0.05, -1)
const lightPos1 = vec3<f32>(10.0, 10.0, 10.0);
const lightPos2 = vec3<f32>(-10.0, 10.0, -10.0);

fn rayPlane(ro: vec3<f32>, rd: vec3<f32>, plane: vec4<f32>) -> f32 {
  return -(dot(ro, plane.xyz) + plane.w) / dot(rd, plane.xyz);
}

var<private> g_rot0: mat2x2<f32>;

fn render0(ro: vec3<f32>, rd: vec3<f32>) -> vec3<f32> {
  var col = vec3<f32>(0.0);
  var sf = 1.0001 - max(dot(sunDir1, rd), 0.0);
  col += skyCol * pow((1.0 - abs(rd.y)), 8.0);
  col += clamp(vec3<f32>(mix(0.0025, 0.125, tanh_approx(0.005 / sf)) / abs(rd.y)) * skylineCol, vec3<f32>(0.0), vec3<f32>(10.0));
  sf *= sf;
  col += sunCol * 0.00005 / sf;

  let tp1 = rayPlane(ro, rd, vec4<f32>(0.0, -1.0, 0.0, 6.0));
  if (tp1 > 0.0) {
    let pos = ro + tp1 * rd;
    let pp = pos.xz;
    let db = sdBox2D(pp, vec2<f32>(5.0, 9.0)) - 3.0;
    col += vec3<f32>(4.0) * skyCol * rd.y * rd.y * smoothstep(0.25, 0.0, db);
    col += vec3<f32>(0.8) * skyCol * exp(-0.5 * max(db, 0.0));
  }

  return clamp(col, vec3<f32>(0.0), vec3<f32>(10.0));
}

const OBJ_BACKGROUND: f32 = 0.0;
const OBJ_CUBE: f32 = 1.0;
const OBJ_SPHERE: f32 = 2.0;
const OBJ_PLANE: f32 = 3.0;

fn df(p_in: vec3<f32>) -> f32 {
  var p = p_in;
  p.x = p_in.x * g_rot0[0][0] + p_in.z * g_rot0[0][1];
  p.z = p_in.x * g_rot0[1][0] + p_in.z * g_rot0[1][1];

  // Cube
  var pc = p - vec3<f32>(-1.9, 0.0, 0.0);
  let dCube = sdBox(pc, vec3<f32>(1.6));

  // Sphere
  var ps = p - vec3<f32>(1.3, 0.0, 0.0);
  let dSphere = sdSphere(ps, 1.2);

  // Ground plane
  let dPlane = p.y + 1.0;

  // Union
  var d = min(dCube, dSphere);
  d = min(d, dPlane);

  return d;
}

fn dfWithID(p_in: vec3<f32>) -> RayMarchResult {
  var p = p_in;
  p.x = p_in.x * g_rot0[0][0] + p_in.z * g_rot0[0][1];
  p.z = p_in.x * g_rot0[1][0] + p_in.z * g_rot0[1][1];

  // Cube
  var pc = p - vec3<f32>(-1.9, 0.0, 0.0);
  let dCube = sdBox(pc, vec3<f32>(1.6));

  // Sphere
  var ps = p - vec3<f32>(1.3, 0.0, 0.0);
  let dSphere = sdSphere(ps, 1.2);

  // Ground plane
  let dPlane = p.y + 1.0;

  // Find closest object
  var result: RayMarchResult;
  result.distance = dCube;
  result.object_id = OBJ_CUBE;

  if (dSphere < result.distance) {
    result.distance = dSphere;
    result.object_id = OBJ_SPHERE;
  }

  if (dPlane < result.distance) {
    result.distance = dPlane;
    result.object_id = OBJ_PLANE;
  }

  result.distance_max = result.distance;
  return result;
}

fn boxCol(col: vec3<f32>, nsp: vec3<f32>, rd: vec3<f32>, nnor: vec3<f32>, nrcol: vec3<f32>, nshd1: f32, nshd2: f32) -> vec3<f32> {
  var nfre = 1.0 + dot(rd, nnor);
  nfre *= nfre;

  let nld1 = normalize(lightPos1 - nsp);
  let nld2 = normalize(lightPos2 - nsp);

  var ndif1 = max(dot(nld1, nnor), 0.0);
  ndif1 *= ndif1;

  var ndif2 = max(dot(nld2, nnor), 0.0);
  ndif2 *= ndif2;

  var scol = vec3<f32>(0.0);
  let rf = smoothstep(1.0, 0.9, nfre);
  scol += diffCol1 * ndif1 * nshd1;
  scol += diffCol2 * ndif2 * nshd2;
  scol += 0.1 * (skyCol + skylineCol);
  scol += nrcol * 0.75 * mix(vec3<f32>(0.25), vec3<f32>(0.5, 0.5, 1.0), nfre);

  return mix(col, scol, rf * smoothstep(90.0, 20.0, dot(nsp, nsp)));
}

fn render1(ro: vec3<f32>, rd: vec3<f32>) -> vec3<f32> {
  let skyCol_local = render0(ro, rd);
  var col = skyCol_local;

  var init: RayMarchResult;
  init.distance = 0.0;
  init.distance_max = 0.0;
  init.object_id = OBJ_BACKGROUND;

  let result = rayMarchWithID(ro, rd, init);
  if (result.distance < MAX_RAY_LENGTH) {
    let nsp = reconstructPosition(ro, rd, result);
    let nnor = normalWithID(nsp);

    let nref = reflect(rd, nnor);
    var refl_init: RayMarchResult;
    refl_init.distance = 0.2;
    refl_init.distance_max = 0.2;
    refl_init.object_id = OBJ_BACKGROUND;
    let nrt_result = rayMarchWithID(nsp, nref, refl_init);
    var nrcol = render0(nsp, nref);

    if (nrt_result.distance < MAX_RAY_LENGTH) {
      let nrsp = reconstructPosition(nsp, nref, nrt_result);
      let nrnor = normalWithID(nrsp);
      let nrref = reflect(nref, nrnor);
      nrcol = boxCol(nrcol, nrsp, nref, nrnor, render0(nrsp, nrref), 1.0, 1.0);
    }

    let light_dist1 = distance(lightPos1, nsp);
    let light_dist2 = distance(lightPos2, nsp);
    let nshd1 = mix(0.0, 1.0, shadowWithStoredDistance(nsp, normalize(lightPos1 - nsp), light_dist1));
    let nshd2 = mix(0.0, 1.0, shadowWithStoredDistance(nsp, normalize(lightPos2 - nsp), light_dist2));

    col = boxCol(col, nsp, rd, nnor, nrcol, nshd1, nshd2);
  }

  return col;
}

fn effect(p: vec2<f32>) -> vec3<f32> {
  g_rot0 = rot(-0.2 * uniforms.time);

  let fov = tan(TAU / 6.0);
  let ro = vec3<f32>(0.0, 2.5, 5.0);
  let la = vec3<f32>(0.0, 0.0, 0.0);
  let up = vec3<f32>(0.1, 1.0, 0.0);

  let ww = normalize(la - ro);
  let uu = normalize(cross(up, ww));
  let vv = cross(ww, uu);
  let rd = normalize(-p.x * uu + p.y * vv + fov * ww);

  return render1(ro, rd);
}

#include "render/fullscreen_vs"

@fragment fn fs_main(@builtin(position) p: vec4<f32>) -> @location(0) vec4<f32> {
  let q = p.xy / uniforms.resolution;
  var coord = -1.0 + 2.0 * q;
  coord.x *= uniforms.resolution.x / uniforms.resolution.y;
  var col = effect(coord);
  col = aces_approx(col);
  col = sRGB(col);
  return vec4<f32>(col, 1.0);
}