Like the name implies, post-processing is the last step in the render pipeline. It runs after (post) all scene elements are rendered to the scene.
Some common examples of post-processing effects are bloom, depth-of-field, recoloring, etc. These effects are applied on the entire canvas, not just on specific elements in the scene. This means they should be applied to the final rendered 2D image.
The process is very similar to what we covered in the Image Compositing class, but instead of using an image or video frame as our input texture, we will use the scene we are rendering through the camera. We use a shader to render our effect on this input texture, and output the result to an offscreen buffer.
If we want to apply many post-processing effects, we can use two offscreen buffers and render from one to the other, alternating which is the source and which is the destination for every step, or pass. The final step, which consists of all the effects stacked on top of each other, gets rendered to the screen. This process is called ping pong buffering.

Effect Composer
In three.js, we can achieve all this using the EffectComposer. Once it is set up and linked to our renderer, we can add a RenderPass to render our scene using the EffectComposer.
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
|
/* eslint-disable no-undef, no-unused-vars, import/first */
// Page styles are defined in index.html.
import * as THREE from "three";
import { OrbitControls } from "three/examples/jsm/controls/OrbitControls.js";
import { EffectComposer } from "three/examples/jsm/postprocessing/EffectComposer.js";
import { RenderPass } from "three/examples/jsm/postprocessing/RenderPass.js";
import GUI from "lil-gui";
// Create debug GUI.
const gui = new GUI();
// Create renderer.
const canvas = document.querySelector("#canvas");
const renderer = new THREE.WebGLRenderer({ canvas, antialias: true });
renderer.setSize(window.innerWidth, window.innerHeight);
// Create scene.
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x292f33);
// Create camera.
const camera = new THREE.PerspectiveCamera(
75,
window.innerWidth / window.innerHeight
);
camera.position.z = -5;
scene.add(camera);
// Add mouse controls for camera.
const controls = new OrbitControls(camera, canvas);
controls.enableDamping = true;
// Load textures.
const textureLoader = new THREE.TextureLoader();
const earthTex = textureLoader.load(sketchFile0);
// Create groups and objects.
const group = new THREE.Group();
scene.add(group);
const ringGeo = new THREE.TorusGeometry(1.0, 0.05, 16, 64);
const ringMat = new THREE.MeshNormalMaterial();
const ringMesh = new THREE.Mesh(ringGeo, ringMat);
group.add(ringMesh);
import earthVertShader from "./shaders/earth.vert";
import earthFragShader from "./shaders/earth.frag";
const sphereGeo = new THREE.SphereGeometry(0.5, 32, 32);
const sphereMat = new THREE.RawShaderMaterial({
vertexShader: earthVertShader,
fragmentShader: earthFragShader,
uniforms: {
uTerrainMap: { value: earthTex },
uTime: { value: 0.0 },
uNoiseScale: { value: 2.0 },
uOffsetScale: { value: 0.4 },
},
});
const sphereMesh = new THREE.Mesh(sphereGeo, sphereMat);
group.add(sphereMesh);
gui
.add(sphereMat.uniforms.uNoiseScale, "value")
.min(0)
.max(5)
.step(0.01)
.name("noise scale");
gui
.add(sphereMat.uniforms.uOffsetScale, "value")
.min(0)
.max(1)
.step(0.01)
.name("offset scale");
// Create effect composer.
const effectComposer = new EffectComposer(renderer);
effectComposer.setSize(window.innerWidth, window.innerHeight);
// Add render passes.
const renderPass = new RenderPass(scene, camera);
effectComposer.addPass(renderPass);
// Animation loop.
const clock = new THREE.Clock();
const tick = () => {
const elapsedTime = clock.getElapsedTime();
sphereMat.uniforms.uTime.value = elapsedTime;
sphereMesh.position.z = Math.sin(elapsedTime * 0.5) * 3.0;
sphereMesh.rotation.x += 0.03;
group.rotation.x += 0.01;
group.rotation.y += 0.02;
controls.update();
effectComposer.render();
requestAnimationFrame(tick);
};
tick();
// Window resize listener.
window.addEventListener("resize", () => {
const w = window.innerWidth;
const h = window.innerHeight;
camera.aspect = w / h;
camera.updateProjectionMatrix();
renderer.setSize(w, h);
effectComposer.setSize(w, h);
});
import sketchFile0 from "../textures/terrain.jpg";
|
The default RenderPass just copies the buffer data, so our example looks identical as if we were rendering to the screen directly. Let’s add a built-in effect pass that renders the image using dot patterns.
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
|
/* eslint-disable no-undef, no-unused-vars, import/first */
// Page styles are defined in index.html.
import * as THREE from "three";
import { OrbitControls } from "three/examples/jsm/controls/OrbitControls.js";
import { EffectComposer } from "three/examples/jsm/postprocessing/EffectComposer.js";
import { RenderPass } from "three/examples/jsm/postprocessing/RenderPass.js";
import { DotScreenPass } from "three/examples/jsm/postprocessing/DotScreenPass.js";
import GUI from "lil-gui";
// Create debug GUI.
const gui = new GUI();
// Create renderer.
const canvas = document.querySelector("#canvas");
const renderer = new THREE.WebGLRenderer({ canvas, antialias: true });
renderer.setSize(window.innerWidth, window.innerHeight);
// Create scene.
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x292f33);
// Create camera.
const camera = new THREE.PerspectiveCamera(
75,
window.innerWidth / window.innerHeight
);
camera.position.z = -5;
scene.add(camera);
// Add mouse controls for camera.
const controls = new OrbitControls(camera, canvas);
controls.enableDamping = true;
// Load textures.
const textureLoader = new THREE.TextureLoader();
const earthTex = textureLoader.load(sketchFile0);
// Create groups and objects.
const group = new THREE.Group();
scene.add(group);
const ringGeo = new THREE.TorusGeometry(1.0, 0.05, 16, 64);
const ringMat = new THREE.MeshNormalMaterial();
const ringMesh = new THREE.Mesh(ringGeo, ringMat);
group.add(ringMesh);
import earthVertShader from "./shaders/earth.vert";
import earthFragShader from "./shaders/earth.frag";
const sphereGeo = new THREE.SphereGeometry(0.5, 32, 32);
const sphereMat = new THREE.RawShaderMaterial({
vertexShader: earthVertShader,
fragmentShader: earthFragShader,
uniforms: {
uTerrainMap: { value: earthTex },
uTime: { value: 0.0 },
uNoiseScale: { value: 2.0 },
uOffsetScale: { value: 0.4 },
},
});
const sphereMesh = new THREE.Mesh(sphereGeo, sphereMat);
group.add(sphereMesh);
gui
.add(sphereMat.uniforms.uNoiseScale, "value")
.min(0)
.max(5)
.step(0.01)
.name("noise scale");
gui
.add(sphereMat.uniforms.uOffsetScale, "value")
.min(0)
.max(1)
.step(0.01)
.name("offset scale");
// Create effect composer.
const effectComposer = new EffectComposer(renderer);
effectComposer.setSize(window.innerWidth, window.innerHeight);
// Add render passes.
const renderPass = new RenderPass(scene, camera);
effectComposer.addPass(renderPass);
const dotScreenPass = new DotScreenPass();
effectComposer.addPass(dotScreenPass);
// Animation loop.
const clock = new THREE.Clock();
const tick = () => {
const elapsedTime = clock.getElapsedTime();
sphereMat.uniforms.uTime.value = elapsedTime;
sphereMesh.position.z = Math.sin(elapsedTime * 0.5) * 3.0;
sphereMesh.rotation.x += 0.03;
group.rotation.x += 0.01;
group.rotation.y += 0.02;
controls.update();
effectComposer.render();
requestAnimationFrame(tick);
};
tick();
// Window resize listener.
window.addEventListener("resize", () => {
const w = window.innerWidth;
const h = window.innerHeight;
camera.aspect = w / h;
camera.updateProjectionMatrix();
renderer.setSize(w, h);
effectComposer.setSize(w, h);
});
import sketchFile0 from "../textures/terrain.jpg";
|
three.js ships with many built-in effects, which can be found in the examples/jsm/postprocessing folder. These effect passes can be stacked in the EffectComposer and will be applied in the order they are added in.
Documentation is unfortunately sparse on these effects, so we will need to look through the code to see which options can be configured.
Exercise
Try stacking a second effect pass in our app. Notice any differences if it’s added before or after the DotScreenPass.
Shader Pass
Next let’s add a kaleidoscope effect pass. three.js has a KaleidoShader object we can try. However, this is a shader and not a render pass, so we will need to wrap it in a ShaderPass to add it to the composer.
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
|
/* eslint-disable no-undef, no-unused-vars, import/first */
// Page styles are defined in index.html.
import * as THREE from "three";
import { OrbitControls } from "three/examples/jsm/controls/OrbitControls.js";
import { EffectComposer } from "three/examples/jsm/postprocessing/EffectComposer.js";
import { RenderPass } from "three/examples/jsm/postprocessing/RenderPass.js";
import { DotScreenPass } from "three/examples/jsm/postprocessing/DotScreenPass.js";
import { ShaderPass } from "three/examples/jsm/postprocessing/ShaderPass.js";
import { KaleidoShader } from "three/examples/jsm/shaders/KaleidoShader.js";
import GUI from "lil-gui";
// Create debug GUI.
const gui = new GUI();
// Create renderer.
const canvas = document.querySelector("#canvas");
const renderer = new THREE.WebGLRenderer({ canvas, antialias: true });
renderer.setSize(window.innerWidth, window.innerHeight);
// Create scene.
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x292f33);
// Create camera.
const camera = new THREE.PerspectiveCamera(
75,
window.innerWidth / window.innerHeight
);
camera.position.z = -5;
scene.add(camera);
// Add mouse controls for camera.
const controls = new OrbitControls(camera, canvas);
controls.enableDamping = true;
// Load textures.
const textureLoader = new THREE.TextureLoader();
const earthTex = textureLoader.load(sketchFile0);
// Create groups and objects.
const group = new THREE.Group();
scene.add(group);
const ringGeo = new THREE.TorusGeometry(1.0, 0.05, 16, 64);
const ringMat = new THREE.MeshNormalMaterial();
const ringMesh = new THREE.Mesh(ringGeo, ringMat);
group.add(ringMesh);
import earthVertShader from "./shaders/earth.vert";
import earthFragShader from "./shaders/earth.frag";
const sphereGeo = new THREE.SphereGeometry(0.5, 32, 32);
const sphereMat = new THREE.RawShaderMaterial({
vertexShader: earthVertShader,
fragmentShader: earthFragShader,
uniforms: {
uTerrainMap: { value: earthTex },
uTime: { value: 0.0 },
uNoiseScale: { value: 2.0 },
uOffsetScale: { value: 0.4 },
},
});
const sphereMesh = new THREE.Mesh(sphereGeo, sphereMat);
group.add(sphereMesh);
gui
.add(sphereMat.uniforms.uNoiseScale, "value")
.min(0)
.max(5)
.step(0.01)
.name("noise scale");
gui
.add(sphereMat.uniforms.uOffsetScale, "value")
.min(0)
.max(1)
.step(0.01)
.name("offset scale");
// Create effect composer.
const effectComposer = new EffectComposer(renderer);
effectComposer.setSize(window.innerWidth, window.innerHeight);
// Add render passes.
const renderPass = new RenderPass(scene, camera);
effectComposer.addPass(renderPass);
const dotScreenPass = new DotScreenPass();
effectComposer.addPass(dotScreenPass);
const kaleidoPass = new ShaderPass(KaleidoShader);
effectComposer.addPass(kaleidoPass);
// Animation loop.
const clock = new THREE.Clock();
const tick = () => {
const elapsedTime = clock.getElapsedTime();
sphereMat.uniforms.uTime.value = elapsedTime;
sphereMesh.position.z = Math.sin(elapsedTime * 0.5) * 3.0;
sphereMesh.rotation.x += 0.03;
group.rotation.x += 0.01;
group.rotation.y += 0.02;
controls.update();
effectComposer.render();
requestAnimationFrame(tick);
};
tick();
// Window resize listener.
window.addEventListener("resize", () => {
const w = window.innerWidth;
const h = window.innerHeight;
camera.aspect = w / h;
camera.updateProjectionMatrix();
renderer.setSize(w, h);
effectComposer.setSize(w, h);
});
import sketchFile0 from "../textures/terrain.jpg";
|
three.js ships with many built-in shaders, which can be found in the examples/jsm/shaders folder. If we look at the KaleidoShader.js source, we notice that there are two uniforms we can use to customize the effect. We can edit these by accessing the instance of the shader that is attached to our ShaderPass using the ShaderPass.material property. We can also use the ShaderPass.enabled property to toggle the effect on and off.
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
|
/* eslint-disable no-undef, no-unused-vars, import/first */
// Page styles are defined in index.html.
import * as THREE from "three";
import { OrbitControls } from "three/examples/jsm/controls/OrbitControls.js";
import { EffectComposer } from "three/examples/jsm/postprocessing/EffectComposer.js";
import { RenderPass } from "three/examples/jsm/postprocessing/RenderPass.js";
import { DotScreenPass } from "three/examples/jsm/postprocessing/DotScreenPass.js";
import { ShaderPass } from "three/examples/jsm/postprocessing/ShaderPass.js";
import { KaleidoShader } from "three/examples/jsm/shaders/KaleidoShader.js";
import GUI from "lil-gui";
// Create debug GUI.
const gui = new GUI();
// Create renderer.
const canvas = document.querySelector("#canvas");
const renderer = new THREE.WebGLRenderer({ canvas, antialias: true });
renderer.setSize(window.innerWidth, window.innerHeight);
// Create scene.
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x292f33);
// Create camera.
const camera = new THREE.PerspectiveCamera(
75,
window.innerWidth / window.innerHeight
);
camera.position.z = -5;
scene.add(camera);
// Add mouse controls for camera.
const controls = new OrbitControls(camera, canvas);
controls.enableDamping = true;
// Load textures.
const textureLoader = new THREE.TextureLoader();
const earthTex = textureLoader.load(sketchFile0);
// Create groups and objects.
const group = new THREE.Group();
scene.add(group);
const ringGeo = new THREE.TorusGeometry(1.0, 0.05, 16, 64);
const ringMat = new THREE.MeshNormalMaterial();
const ringMesh = new THREE.Mesh(ringGeo, ringMat);
group.add(ringMesh);
import earthVertShader from "./shaders/earth.vert";
import earthFragShader from "./shaders/earth.frag";
const sphereGeo = new THREE.SphereGeometry(0.5, 32, 32);
const sphereMat = new THREE.RawShaderMaterial({
vertexShader: earthVertShader,
fragmentShader: earthFragShader,
uniforms: {
uTerrainMap: { value: earthTex },
uTime: { value: 0.0 },
uNoiseScale: { value: 2.0 },
uOffsetScale: { value: 0.4 },
},
});
const sphereMesh = new THREE.Mesh(sphereGeo, sphereMat);
group.add(sphereMesh);
gui
.add(sphereMat.uniforms.uNoiseScale, "value")
.min(0)
.max(5)
.step(0.01)
.name("noise scale");
gui
.add(sphereMat.uniforms.uOffsetScale, "value")
.min(0)
.max(1)
.step(0.01)
.name("offset scale");
// Create effect composer.
const effectComposer = new EffectComposer(renderer);
effectComposer.setSize(window.innerWidth, window.innerHeight);
// Add render passes.
const renderPass = new RenderPass(scene, camera);
effectComposer.addPass(renderPass);
const dotScreenPass = new DotScreenPass();
effectComposer.addPass(dotScreenPass);
gui.add(dotScreenPass, "enabled").name("dot screen");
const kaleidoPass = new ShaderPass(KaleidoShader);
effectComposer.addPass(kaleidoPass);
gui.add(kaleidoPass, "enabled").name("kaleidoscope");
gui
.add(kaleidoPass.material.uniforms.angle, "value")
.min(0)
.max(Math.PI * 2)
.step(0.1)
.name("angle");
gui
.add(kaleidoPass.material.uniforms.sides, "value")
.min(0)
.max(12)
.step(1)
.name("sides");
// Animation loop.
const clock = new THREE.Clock();
const tick = () => {
const elapsedTime = clock.getElapsedTime();
sphereMat.uniforms.uTime.value = elapsedTime;
sphereMesh.position.z = Math.sin(elapsedTime * 0.5) * 3.0;
sphereMesh.rotation.x += 0.03;
group.rotation.x += 0.01;
group.rotation.y += 0.02;
controls.update();
effectComposer.render();
requestAnimationFrame(tick);
};
tick();
// Window resize listener.
window.addEventListener("resize", () => {
const w = window.innerWidth;
const h = window.innerHeight;
camera.aspect = w / h;
camera.updateProjectionMatrix();
renderer.setSize(w, h);
effectComposer.setSize(w, h);
});
import sketchFile0 from "../textures/terrain.jpg";
|
Custom Pass
We can also create our own effect by passing a custom shader to the ShaderPass!
The shader needs specific formatting to work correctly:
- It must have a uniform sampler named
tDiffuse. This uniform is how the EffectComposer passes the previous pass’ buffer to our shader.
- The vertex shader should not include the built-in uniform (
projectionMatrix, viewMatrix, …) or built-in attribute (position, uv, …) declarations at the top of the file. These can still be used, but they will be injected automatically by three.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
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
|
/* eslint-disable no-undef, no-unused-vars, import/first */
// Page styles are defined in index.html.
import * as THREE from "three";
import { OrbitControls } from "three/examples/jsm/controls/OrbitControls.js";
import { EffectComposer } from "three/examples/jsm/postprocessing/EffectComposer.js";
import { RenderPass } from "three/examples/jsm/postprocessing/RenderPass.js";
import { DotScreenPass } from "three/examples/jsm/postprocessing/DotScreenPass.js";
import { ShaderPass } from "three/examples/jsm/postprocessing/ShaderPass.js";
import { KaleidoShader } from "three/examples/jsm/shaders/KaleidoShader.js";
import GUI from "lil-gui";
// Create debug GUI.
const gui = new GUI();
// Create renderer.
const canvas = document.querySelector("#canvas");
const renderer = new THREE.WebGLRenderer({ canvas, antialias: true });
renderer.setSize(window.innerWidth, window.innerHeight);
// Create scene.
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x292f33);
// Create camera.
const camera = new THREE.PerspectiveCamera(
75,
window.innerWidth / window.innerHeight
);
camera.position.z = -5;
scene.add(camera);
// Add mouse controls for camera.
const controls = new OrbitControls(camera, canvas);
controls.enableDamping = true;
// Load textures.
const textureLoader = new THREE.TextureLoader();
const earthTex = textureLoader.load(sketchFile0);
// Create groups and objects.
const group = new THREE.Group();
scene.add(group);
const ringGeo = new THREE.TorusGeometry(1.0, 0.05, 16, 64);
const ringMat = new THREE.MeshNormalMaterial();
const ringMesh = new THREE.Mesh(ringGeo, ringMat);
group.add(ringMesh);
import earthVertShader from "./shaders/earth.vert";
import earthFragShader from "./shaders/earth.frag";
const sphereGeo = new THREE.SphereGeometry(0.5, 32, 32);
const sphereMat = new THREE.RawShaderMaterial({
vertexShader: earthVertShader,
fragmentShader: earthFragShader,
uniforms: {
uTerrainMap: { value: earthTex },
uTime: { value: 0.0 },
uNoiseScale: { value: 2.0 },
uOffsetScale: { value: 0.4 },
},
});
const sphereMesh = new THREE.Mesh(sphereGeo, sphereMat);
group.add(sphereMesh);
gui
.add(sphereMat.uniforms.uNoiseScale, "value")
.min(0)
.max(5)
.step(0.01)
.name("noise scale");
gui
.add(sphereMat.uniforms.uOffsetScale, "value")
.min(0)
.max(1)
.step(0.01)
.name("offset scale");
// Create effect composer.
const effectComposer = new EffectComposer(renderer);
effectComposer.setSize(window.innerWidth, window.innerHeight);
// Add render passes.
const renderPass = new RenderPass(scene, camera);
effectComposer.addPass(renderPass);
const dotScreenPass = new DotScreenPass();
effectComposer.addPass(dotScreenPass);
gui.add(dotScreenPass, "enabled").name("dot screen");
const kaleidoPass = new ShaderPass(KaleidoShader);
effectComposer.addPass(kaleidoPass);
gui.add(kaleidoPass, "enabled").name("kaleidoscope");
gui
.add(kaleidoPass.material.uniforms.angle, "value")
.min(0)
.max(Math.PI * 2)
.step(0.1)
.name("angle");
gui
.add(kaleidoPass.material.uniforms.sides, "value")
.min(0)
.max(12)
.step(1)
.name("sides");
import warpVertShader from "./shaders/warp.vert";
import warpFragShader from "./shaders/warp.frag";
const warpShader = {
vertexShader: warpVertShader,
fragmentShader: warpFragShader,
uniforms: {
tDiffuse: { value: null },
},
};
const warpPass = new ShaderPass(warpShader);
effectComposer.addPass(warpPass);
gui.add(warpPass, "enabled").name("warp");
// Animation loop.
const clock = new THREE.Clock();
const tick = () => {
const elapsedTime = clock.getElapsedTime();
sphereMat.uniforms.uTime.value = elapsedTime;
sphereMesh.position.z = Math.sin(elapsedTime * 0.5) * 3.0;
sphereMesh.rotation.x += 0.03;
group.rotation.x += 0.01;
group.rotation.y += 0.02;
controls.update();
effectComposer.render();
requestAnimationFrame(tick);
};
tick();
// Window resize listener.
window.addEventListener("resize", () => {
const w = window.innerWidth;
const h = window.innerHeight;
camera.aspect = w / h;
camera.updateProjectionMatrix();
renderer.setSize(w, h);
effectComposer.setSize(w, h);
});
import sketchFile0 from "../textures/terrain.jpg";
|
Our custom shader pass can now be used like any 2D image shader. Let’s recreate the fun house noise effect we developed previously. Note that any custom uniforms used should be declared when creating the shader object.
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
|
/* eslint-disable no-undef, no-unused-vars, import/first */
// Page styles are defined in index.html.
import * as THREE from "three";
import { OrbitControls } from "three/examples/jsm/controls/OrbitControls.js";
import { EffectComposer } from "three/examples/jsm/postprocessing/EffectComposer.js";
import { RenderPass } from "three/examples/jsm/postprocessing/RenderPass.js";
import { DotScreenPass } from "three/examples/jsm/postprocessing/DotScreenPass.js";
import { ShaderPass } from "three/examples/jsm/postprocessing/ShaderPass.js";
import { KaleidoShader } from "three/examples/jsm/shaders/KaleidoShader.js";
import GUI from "lil-gui";
// Create debug GUI.
const gui = new GUI();
// Create renderer.
const canvas = document.querySelector("#canvas");
const renderer = new THREE.WebGLRenderer({ canvas, antialias: true });
renderer.setSize(window.innerWidth, window.innerHeight);
// Create scene.
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x292f33);
// Create camera.
const camera = new THREE.PerspectiveCamera(
75,
window.innerWidth / window.innerHeight
);
camera.position.z = -5;
scene.add(camera);
// Add mouse controls for camera.
const controls = new OrbitControls(camera, canvas);
controls.enableDamping = true;
// Load textures.
const textureLoader = new THREE.TextureLoader();
const earthTex = textureLoader.load(sketchFile0);
// Create groups and objects.
const group = new THREE.Group();
scene.add(group);
const ringGeo = new THREE.TorusGeometry(1.0, 0.05, 16, 64);
const ringMat = new THREE.MeshNormalMaterial();
const ringMesh = new THREE.Mesh(ringGeo, ringMat);
group.add(ringMesh);
import earthVertShader from "./shaders/earth.vert";
import earthFragShader from "./shaders/earth.frag";
const sphereGeo = new THREE.SphereGeometry(0.5, 32, 32);
const sphereMat = new THREE.RawShaderMaterial({
vertexShader: earthVertShader,
fragmentShader: earthFragShader,
uniforms: {
uTerrainMap: { value: earthTex },
uTime: { value: 0.0 },
uNoiseScale: { value: 2.0 },
uOffsetScale: { value: 0.4 },
},
});
const sphereMesh = new THREE.Mesh(sphereGeo, sphereMat);
group.add(sphereMesh);
gui
.add(sphereMat.uniforms.uNoiseScale, "value")
.min(0)
.max(5)
.step(0.01)
.name("noise scale");
gui
.add(sphereMat.uniforms.uOffsetScale, "value")
.min(0)
.max(1)
.step(0.01)
.name("offset scale");
// Create effect composer.
const effectComposer = new EffectComposer(renderer);
effectComposer.setSize(window.innerWidth, window.innerHeight);
// Add render passes.
const renderPass = new RenderPass(scene, camera);
effectComposer.addPass(renderPass);
const dotScreenPass = new DotScreenPass();
effectComposer.addPass(dotScreenPass);
gui.add(dotScreenPass, "enabled").name("dot screen");
const kaleidoPass = new ShaderPass(KaleidoShader);
effectComposer.addPass(kaleidoPass);
gui.add(kaleidoPass, "enabled").name("kaleidoscope");
gui
.add(kaleidoPass.material.uniforms.angle, "value")
.min(0)
.max(Math.PI * 2)
.step(0.1)
.name("angle");
gui
.add(kaleidoPass.material.uniforms.sides, "value")
.min(0)
.max(12)
.step(1)
.name("sides");
import warpVertShader from "./shaders/warp.vert";
import warpFragShader from "./shaders/warp.frag";
const warpShader = {
vertexShader: warpVertShader,
fragmentShader: warpFragShader,
uniforms: {
tDiffuse: { value: null },
uTime: { value: 0.0 },
uNoiseScale: { value: 0.1 },
uOffsetScale: { value: 0.3 },
},
};
const warpPass = new ShaderPass(warpShader);
effectComposer.addPass(warpPass);
gui.add(warpPass, "enabled").name("warp");
gui
.add(warpPass.material.uniforms.uNoiseScale, "value")
.min(0)
.max(5)
.step(0.01)
.name("post noise");
gui
.add(warpPass.material.uniforms.uOffsetScale, "value")
.min(0)
.max(1)
.step(0.01)
.name("post offset");
// Animation loop.
const clock = new THREE.Clock();
const tick = () => {
const elapsedTime = clock.getElapsedTime();
sphereMat.uniforms.uTime.value = elapsedTime;
sphereMesh.position.z = Math.sin(elapsedTime * 0.5) * 3.0;
sphereMesh.rotation.x += 0.03;
group.rotation.x += 0.01;
group.rotation.y += 0.02;
controls.update();
warpPass.material.uniforms.uTime.value = elapsedTime;
effectComposer.render();
requestAnimationFrame(tick);
};
tick();
// Window resize listener.
window.addEventListener("resize", () => {
const w = window.innerWidth;
const h = window.innerHeight;
camera.aspect = w / h;
camera.updateProjectionMatrix();
renderer.setSize(w, h);
effectComposer.setSize(w, h);
});
import sketchFile0 from "../textures/terrain.jpg";
|