forked from mattdesl/canvas-sketch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanimated-three-sphere-shader.js
117 lines (99 loc) · 2.8 KB
/
animated-three-sphere-shader.js
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
/**
* An example of a rotating rainbow sphere, using Nadieh Bremer's
* loop as a reference, but implemented with a custom shader.
*
* See here:
* https://twitter.com/NadiehBremer/status/1058016472759496711
*
* @author Matt DesLauriers (@mattdesl), inspired by Nadieh Bremer's loop
*/
const canvasSketch = require('canvas-sketch');
const THREE = require('three');
const glslify = require('glslify');
const settings = {
dimensions: [ 512, 512 ],
// Make the loop animated
animate: true,
// Get a WebGL canvas rather than 2D
context: 'webgl',
// Loop itme in seconds
duration: 5,
// Loop framerate
fps: 24,
// Visualize the above FPS in-browser
playbackRate: 'throttle',
// Turn on MSAA
attributes: { antialias: true }
};
const sketch = ({ context }) => {
// Create a renderer
const renderer = new THREE.WebGLRenderer({
context
});
// WebGL background color
renderer.setClearColor('#fff', 1);
// Setup a camera
const camera = new THREE.PerspectiveCamera(45, 1, 0.01, 100);
camera.position.set(0, 0, -4);
camera.lookAt(new THREE.Vector3());
// Setup your scene
const scene = new THREE.Scene();
const fragmentShader = glslify(/* glsl */`
#pragma glslify: hsl2rgb = require('glsl-hsl2rgb');
varying vec2 vUv;
uniform float playhead;
void main () {
// number of horizontal bands
float bands = 12.0;
// offset texture by loop time
float offset = playhead;
// get a 0..1 value from this
float y = mod(offset + vUv.y, 1.0);
// get N discrete steps of hue
float hue = floor(y * bands) / bands;
// now get a color
float sat = 0.55;
float light = 0.6;
vec3 color = hsl2rgb(hue, sat, light);
gl_FragColor = vec4(color, 1.0);
}
`);
const vertexShader = glslify(/* glsl */`
varying vec2 vUv;
void main () {
vUv = uv;
gl_Position = projectionMatrix * modelViewMatrix * vec4(position.xyz, 1.0);
}
`);
const mesh = new THREE.Mesh(
new THREE.SphereGeometry(1, 64, 64),
new THREE.ShaderMaterial({
fragmentShader,
vertexShader,
uniforms: {
playhead: { value: 0 }
}
})
);
scene.add(mesh);
// draw each frame
return {
// Handle resize events here
resize ({ pixelRatio, viewportWidth, viewportHeight }) {
renderer.setPixelRatio(pixelRatio);
renderer.setSize(viewportWidth, viewportHeight);
camera.aspect = viewportWidth / viewportHeight;
camera.updateProjectionMatrix();
},
// Update & render your scene here
render ({ playhead }) {
mesh.material.uniforms.playhead.value = playhead;
renderer.render(scene, camera);
},
// Dispose of events & renderer for cleaner hot-reloading
unload () {
renderer.dispose();
}
};
};
canvasSketch(sketch, settings);