Skip to content

Commit 8e86545

Browse files
committed
Improve light rendering
1 parent cebbd11 commit 8e86545

File tree

4 files changed

+82
-41
lines changed

4 files changed

+82
-41
lines changed

.idea/vcs.xml

+6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

index.html

+2-2
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
let direction
4040
let angle
4141

42-
// Duration of the loop
42+
// Duration of the loop (in seconds)
4343
app.duration = 10
4444

4545
// Code to run at the beginning of the loop
@@ -95,7 +95,7 @@
9595
<div id="progressBar"></div>
9696
<label id="fpsLabel">
9797
Frames per second:
98-
<input type="number" value="25" min="1" max="60">
98+
<input type="number" value="60" min="1" max="60">
9999
</label>
100100
</div>
101101
<canvas id="mainCanvas"></canvas>

src/MainApp.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import {
2929
ShaderMaterial,
3030
TextureLoader,
3131
WebGLRenderer,
32+
AxesHelper,
3233
} from 'three'
3334
import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls'
3435
import rawCoords from './coords.gift'
@@ -78,7 +79,7 @@ export class MainApp {
7879
private fpsControl: HTMLInputElement
7980

8081
public recording = false
81-
public fps = 25
82+
public fps = 60
8283

8384
private prerecorded = false
8485

@@ -112,6 +113,8 @@ export class MainApp {
112113

113114
this.createLights()
114115

116+
this.scene.add(new AxesHelper(1))
117+
115118
this.state = {
116119
time: 0,
117120
duration: 60,

src/shaders.ts

+70-38
Original file line numberDiff line numberDiff line change
@@ -17,47 +17,79 @@ export const fragmentShader = `
1717
uniform sampler2D pointTexture;
1818
varying vec3 vColor;
1919
20-
vec3 hsl2rgb(in vec3 c) {
21-
vec3 rgb = clamp( abs(mod(c.x*6.0+vec3(0.0,4.0,2.0),6.0)-3.0)-1.0, 0.0, 1.0 );
22-
return c.z + c.y * (rgb-0.5)*(1.0-abs(2.0*c.z-1.0));
23-
}
24-
25-
vec3 rgb2hsl( in vec3 c ){
26-
float h = 0.0;
27-
float s = 0.0;
28-
float l = 0.0;
29-
float r = c.r;
30-
float g = c.g;
31-
float b = c.b;
32-
float cMin = min( r, min( g, b ) );
33-
float cMax = max( r, max( g, b ) );
34-
35-
l = ( cMax + cMin ) / 2.0;
36-
if ( cMax > cMin ) {
37-
float cDelta = cMax - cMin;
38-
s = l < .0 ? cDelta / ( cMax + cMin ) : cDelta / ( 2.0 - ( cMax + cMin ) );
39-
40-
if ( r == cMax ) {
41-
h = ( g - b ) / cDelta;
42-
} else if ( g == cMax ) {
43-
h = 2.0 + ( b - r ) / cDelta;
44-
} else {
45-
h = 4.0 + ( r - g ) / cDelta;
46-
}
47-
48-
if ( h < 0.0) {
49-
h += 6.0;
50-
}
51-
h = h / 6.0;
52-
}
53-
return vec3( h, s, l );
20+
vec3 rgb2xyz(vec3 c) {
21+
vec3 tmp = vec3(
22+
(c.r > .04045) ? pow((c.r + .055) / 1.055, 2.4) : c.r / 12.92,
23+
(c.g > .04045) ? pow((c.g + .055) / 1.055, 2.4) : c.g / 12.92,
24+
(c.b > .04045) ? pow((c.b + .055) / 1.055, 2.4) : c.b / 12.92
25+
);
26+
mat3 mat = mat3(
27+
.4124, .3576, .1805,
28+
.2126, .7152, .0722,
29+
.0193, .1192, .9505
30+
);
31+
return 100. * tmp * mat;
32+
}
33+
34+
vec3 xyz2lab(vec3 c) {
35+
vec3 n = c / vec3(95.047, 100., 108.883);
36+
vec3 v = vec3(
37+
(n.x > .008856) ? pow(n.x, 1./3.) : (7.787 * n.x) + (16. / 116.),
38+
(n.y > .008856) ? pow(n.y, 1./3.) : (7.787 * n.y) + (16. / 116.),
39+
(n.z > .008856) ? pow(n.z, 1./3.) : (7.787 * n.z) + (16. / 116.)
40+
);
41+
return vec3((116. * v.y) - 16., 500. * (v.x - v.y), 200. * (v.y - v.z));
42+
}
43+
44+
vec3 rgb2lab(vec3 c) {
45+
vec3 lab = xyz2lab(rgb2xyz(c));
46+
return vec3(lab.x / 100., .5 + .5 * (lab.y / 127.), .5 + .5 * (lab.z / 127.));
47+
}
48+
49+
vec3 lab2xyz(vec3 c) {
50+
float fy = (c.x + 16.) / 116.;
51+
float fx = c.y / 500. + fy;
52+
float fz = fy - c.z / 200.;
53+
54+
return vec3(
55+
95.047 * ((fx > .206897) ? fx * fx * fx : (fx - 16. / 116.) / 7.787),
56+
100.000 * ((fy > .206897) ? fy * fy * fy : (fy - 16. / 116.) / 7.787),
57+
108.883 * ((fz > .206897) ? fz * fz * fz : (fz - 16. / 116.) / 7.787)
58+
);
59+
}
60+
61+
vec3 xyz2rgb(vec3 c) {
62+
mat3 mat=mat3(
63+
3.2406, -1.5372, -0.4986,
64+
-0.9689, 1.8758, 0.0415,
65+
0.0557, -.2040, 1.0570
66+
);
67+
vec3 v = 0.01 * c * mat;
68+
vec3 r = vec3(
69+
(v.r > .0031308) ? ((1.055 * pow(v.r, (1. / 2.4))) - .055) : 12.92 * v.r,
70+
(v.g > .0031308) ? ((1.055 * pow(v.g, (1. / 2.4))) - .055) : 12.92 * v.g,
71+
(v.b > .0031308) ? ((1.055 * pow(v.b, (1. / 2.4))) - .055) : 12.92 * v.b
72+
);
73+
return r;
74+
}
75+
vec3 lab2rgb(vec3 c){
76+
return xyz2rgb(
77+
lab2xyz(
78+
vec3(
79+
100. * c.x,
80+
2. * 127. * (c.y - .5),
81+
2. * 127. * (c.z - .5)
82+
)
83+
)
84+
);
5485
}
5586
5687
void main() {
5788
float lightness = texture2D(pointTexture, gl_PointCoord).r;
58-
vec3 color = rgb2hsl(vColor);
59-
color.z *= 2.0 * lightness * lightness * lightness;
60-
color = hsl2rgb(color);
61-
gl_FragColor = vec4(color, 1.0);
89+
vec3 color = clamp(vColor, vec3(0.0), vec3(1.0));
90+
color = rgb2lab(color);
91+
color.x *= 2.0 * lightness * lightness;
92+
color = lab2rgb(color);
93+
gl_FragColor = vec4(color, lightness);
6294
}
6395
`

0 commit comments

Comments
 (0)