-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
289 lines (287 loc) · 15.1 KB
/
main.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
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
import * as THREE from 'https://cdn.skypack.dev/pin/[email protected]/mode=imports/optimized/three.js';
import { EffectComposer } from 'https://unpkg.com/[email protected]/examples/jsm/postprocessing/EffectComposer.js';
import { RenderPass } from 'https://unpkg.com/[email protected]/examples/jsm/postprocessing/RenderPass.js';
import { FullScreenQuad } from 'https://unpkg.com/[email protected]/examples/jsm/postprocessing/Pass.js';
import { ShaderPass } from 'https://unpkg.com/[email protected]/examples/jsm/postprocessing/ShaderPass.js';
import { VerticalBlurShader } from 'https://unpkg.com/[email protected]/examples/jsm/shaders/VerticalBlurShader.js';
import { HorizontalBlurShader } from 'https://unpkg.com/[email protected]/examples/jsm/shaders/HorizontalBlurShader.js';
import { SMAAPass } from 'https://unpkg.com/[email protected]/examples/jsm/postprocessing/SMAAPass.js';
import { GammaCorrectionShader } from 'https://unpkg.com/[email protected]/examples/jsm/shaders/GammaCorrectionShader.js';
import { EffectShader } from "./EffectShader.js";
import { EffectCompositer } from "./EffectCompositer.js";
import { OrbitControls } from 'https://unpkg.com/[email protected]/examples/jsm/controls/OrbitControls.js';
import { TeapotGeometry } from 'https://unpkg.com/[email protected]/examples/jsm/geometries/TeapotGeometry.js';
import { AssetManager } from './AssetManager.js';
import { Stats } from "./stats.js";
import { GUI } from 'https://unpkg.com/[email protected]/examples/jsm/libs/lil-gui.module.min.js';
async function main() {
// Setup basic renderer, controls, and profiler
const clientWidth = window.innerWidth;
const clientHeight = window.innerHeight;
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(75, clientWidth / clientHeight, 0.1, 1000);
camera.position.set(50, 75, 50);
const renderer = new THREE.WebGLRenderer();
renderer.setSize(clientWidth, clientHeight);
document.body.appendChild(renderer.domElement);
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.shadowMap.autoUpdate = false;
const controls = new OrbitControls(camera, renderer.domElement);
controls.target.set(0, 25, 0);
const stats = new Stats();
stats.showPanel(0);
document.body.appendChild(stats.dom);
// Setup scene
// Skybox
const environment = new THREE.CubeTextureLoader().load([
"skybox/Box_Right.bmp",
"skybox/Box_Left.bmp",
"skybox/Box_Top.bmp",
"skybox/Box_Bottom.bmp",
"skybox/Box_Front.bmp",
"skybox/Box_Back.bmp"
]);
scene.background = environment;
// Lighting
const ambientLight = new THREE.AmbientLight(new THREE.Color(1.0, 1.0, 1.0), 0.25);
//scene.add(ambientLight);
const directionalLight = new THREE.DirectionalLight(0xffffff, 0.35);
directionalLight.position.set(150, 200, 50);
// Shadows
directionalLight.castShadow = true;
directionalLight.shadow.mapSize.width = 1024;
directionalLight.shadow.mapSize.height = 1024;
directionalLight.shadow.camera.left = -75;
directionalLight.shadow.camera.right = 75;
directionalLight.shadow.camera.top = 75;
directionalLight.shadow.camera.bottom = -75;
directionalLight.shadow.camera.near = 0.1;
directionalLight.shadow.camera.far = 500;
directionalLight.shadow.bias = -0.001;
directionalLight.shadow.blurSamples = 8;
directionalLight.shadow.radius = 4;
//scene.add(directionalLight);
const directionalLight2 = new THREE.DirectionalLight(0xffffff, 0.15);
directionalLight2.color.setRGB(1.0, 1.0, 1.0);
directionalLight2.position.set(-50, 200, -150);
//scene.add(directionalLight2);
// Objects
const ground = new THREE.Mesh(new THREE.PlaneGeometry(100, 100).applyMatrix4(new THREE.Matrix4().makeRotationX(-Math.PI / 2)), new THREE.MeshStandardMaterial({ side: THREE.DoubleSide }));
ground.castShadow = true;
ground.receiveShadow = true;
// scene.add(ground);
const box = new THREE.Mesh(new THREE.BoxGeometry(10, 10, 10), new THREE.MeshStandardMaterial({ side: THREE.DoubleSide, color: new THREE.Color(1.0, 0.0, 0.0) }));
box.castShadow = true;
box.receiveShadow = true;
box.position.y = 5.01;
// scene.add(box);
const sphere = new THREE.Mesh(new THREE.SphereGeometry(6.25, 32, 32), new THREE.MeshStandardMaterial({ side: THREE.DoubleSide, envMap: environment, metalness: 1.0, roughness: 0.25 }));
sphere.position.y = 7.5;
sphere.position.x = 25;
sphere.position.z = 25;
sphere.castShadow = true;
sphere.receiveShadow = true;
// scene.add(sphere);
const torusKnot = new THREE.Mesh(new THREE.TorusKnotGeometry(5, 1.5, 200, 32), new THREE.MeshStandardMaterial({ side: THREE.DoubleSide, envMap: environment, metalness: 0.5, roughness: 0.5, color: new THREE.Color(0.0, 1.0, 0.0) }));
torusKnot.position.y = 10;
torusKnot.position.x = -25;
torusKnot.position.z = -25;
torusKnot.castShadow = true;
torusKnot.receiveShadow = true;
// scene.add(torusKnot);
const dragonGeo = (await AssetManager.loadGLTFAsync("dragon.glb")).scene.children[0].children[0].geometry.applyMatrix4(new THREE.Matrix4().makeRotationX(Math.PI / 2)).applyMatrix4(new THREE.Matrix4().makeScale(1.75, 1.75, 1.75));
const dragon2 = new THREE.Mesh(dragonGeo,
new THREE.MeshStandardMaterial({ side: THREE.DoubleSide, envMap: environment, envMapIntensity: 0.05, metalness: 0.5, roughness: 0.2, color: new THREE.Color(1.0, 0.0, 0.0), dithering: true }));
dragon2.position.x = 80;
dragon2.rotation.y = Math.PI / 2;
dragon2.castShadow = true;
dragon2.receiveShadow = true;
scene.add(dragon2);
const bunnyGeo = (await AssetManager.loadGLTFAsync("bunny.glb")).scene.children[0].children[0].geometry;
const bunny = new THREE.Mesh(bunnyGeo.applyMatrix4(new THREE.Matrix4().makeScale(0.075 / 2, 0.075 / 2, 0.075 / 2)).applyMatrix4(new THREE.Matrix4().makeTranslation(0, 9, 0)), new THREE.MeshStandardMaterial({ side: THREE.DoubleSide, envMap: environment, envMapIntensity: 0.05, metalness: 0.5, roughness: 0.2, color: new THREE.Color(0.0, 0.0, 1.0), dithering: true }));
bunny.position.x = -85;
bunny.castShadow = true;
bunny.receiveShadow = true;
scene.add(bunny);
const teapotGeo = new TeapotGeometry().applyMatrix4(new THREE.Matrix4().makeScale(0.25, 0.25, 0.25)).applyMatrix4(new THREE.Matrix4().makeTranslation(0, 10, 0));
const teapot1 = new THREE.Mesh(teapotGeo, new THREE.MeshStandardMaterial({ side: THREE.DoubleSide }));
teapot1.position.x = 0;
//teapot1.scale.set(0.5, 0.5, 0.5);
teapot1.castShadow = true;
teapot1.receiveShadow = true;
//scene.add(teapot1);
const teapot2 = new THREE.Mesh(teapotGeo, new THREE.MeshStandardMaterial({ side: THREE.DoubleSide }));
teapot2.position.x = -40;
teapot2.scale.set(0.5, 0.5, 0.5);
//scene.add(teapot2);
// Build postprocessing stack
// Render Targets
const sponza = (await AssetManager.loadGLTFAsync("sponza.glb")).scene;
sponza.traverse(object => {
if (object.material) {
object.material.envMap = environment;
object.material.envMapIntensity = 0.05;
object.material.dithering = true;
object.castShadow = true;
object.receiveShadow = true;
}
});
sponza.scale.set(10, 10, 10)
scene.add(sponza);
const defaultTexture = new THREE.WebGLRenderTarget(clientWidth, clientHeight, {
minFilter: THREE.LinearFilter,
magFilter: THREE.NearestFilter
});
defaultTexture.depthTexture = new THREE.DepthTexture(clientWidth, clientHeight, THREE.FloatType);
const godraysTexture = new THREE.WebGLRenderTarget(clientWidth / 2, clientHeight / 2, {
minFilter: THREE.LinearFilter,
magFilter: THREE.LinearFilter,
type: THREE.FloatType
});
godraysTexture.depthTexture = new THREE.DepthTexture(clientWidth / 2, clientHeight / 2, THREE.FloatType);
const godraysBlurTexture = godraysTexture.clone();
// Post Effects
const composer = new EffectComposer(renderer);
const smaaPass = new SMAAPass(clientWidth, clientHeight);
const effectCompositer = new ShaderPass(EffectCompositer);
composer.addPass(effectCompositer);
composer.addPass(new ShaderPass(GammaCorrectionShader));
composer.addPass(smaaPass);
const light = new THREE.Mesh(new THREE.SphereGeometry(5, 32, 32), new THREE.MeshBasicMaterial());
light.position.set(0, 50, 0);
scene.add(light);
const cubeRenderTargetDepth = new THREE.WebGLCubeRenderTarget(512, {
type: THREE.FloatType,
minFilter: THREE.NearestFilter,
maxFilter: THREE.NearestFilter
});
const cubeCameraDepth = new THREE.CubeCamera(0.1, 1000, cubeRenderTargetDepth);
scene.add(cubeCameraDepth);
const customMeshDepth = new THREE.ShaderMaterial({
uniforms: {
lightPos: { value: light.position }
},
vertexShader: /*glsl*/ `
varying vec3 vPosition;
void main() {
vPosition = (modelMatrix * vec4(position, 1.0)).xyz;
gl_Position = projectionMatrix * viewMatrix * modelMatrix * vec4(position, 1.0);
}
`,
fragmentShader: /*glsl*/ `
varying vec3 vPosition;
uniform vec3 lightPos;
void main() {
float lightDistance = length(vPosition - lightPos);
gl_FragColor = vec4(lightDistance / 1000.0, 0.0, 0.0, 1.0);
}
`
});
const lightDepthFallback = new THREE.Mesh(new THREE.SphereGeometry(450, 32, 32), new THREE.MeshBasicMaterial({ side: THREE.DoubleSide }));
// scene.add(lightDepthFallback);
const pointLight = new THREE.PointLight(new THREE.Color(1.0, 1.0, 1.0), 5, 200);
pointLight.position.copy(light.position);
pointLight.castShadow = true;
pointLight.shadow.bias = -0.005;
pointLight.shadow.mapSize.width = 1024;
pointLight.shadow.mapSize.height = 1024;
scene.add(pointLight);
let frame = 0;
const effectQuad = new FullScreenQuad(new THREE.ShaderMaterial(EffectShader));
const verticalBlur = new FullScreenQuad(new THREE.ShaderMaterial(VerticalBlurShader));
verticalBlur.material.uniforms.v.value = 0.25 / (clientHeight / 2.0);
const horizontalBlur = new FullScreenQuad(new THREE.ShaderMaterial(HorizontalBlurShader));
horizontalBlur.material.uniforms.h.value = 0.25 / (clientWidth / 2.0);
const effectPass = effectQuad.material;
const gui = new GUI();
const effectController = {
edgeStrength: 2,
edgeRadius: 2,
distanceAttenuation: 0.005,
density: 1.0 / 128.0,
color: [1, 1, 1],
maxDensity: 0.5
}
gui.add(effectController, "density", 0, 1.0 / 16.0, 0.0001).name("Density");
gui.add(effectController, "maxDensity", 0, 1.0, 0.001).name("Max Density");
gui.add(effectController, "distanceAttenuation", 0, 0.1, 0.0001).name("Distance Attenuation");
gui.addColor(effectController, "color").name("Color");
gui.add(effectController, "edgeStrength", 0, 8, 0.001).name("Edge Strength");
gui.add(effectController, "edgeRadius", 0, 4, 1.0).name("Edge Radius");
const noiseTex = await new THREE.TextureLoader().loadAsync("bluenoise.png");
noiseTex.wrapS = THREE.RepeatWrapping;
noiseTex.wrapT = THREE.RepeatWrapping;
noiseTex.magFilter = THREE.NearestFilter;
noiseTex.minFilter = THREE.NearestFilter;
function animate() {
if (frame === 0) {
/* lightDepthFallback.position.copy(light.position);
scene.overrideMaterial = customMeshDepth;
cubeCameraDepth.position.copy(light.position);
light.visible = false;
lightDepthFallback.visible = true;
//console.log(renderer.getClearColor(new THREE.Vector3()));
renderer.setClearColor(new THREE.Color(1.0, 0.0, 0.0), 1.0);
cubeCameraDepth.update(renderer, scene);
renderer.setClearColor(new THREE.Color(0.0, 0.0, 0.0), 1.0);
//scene.background = cubeRenderTargetDepth.texture;
light.visible = true;
lightDepthFallback.visible = false;
scene.overrideMaterial = null;*/
}
light.position.y = 50.0 + 10.0 * Math.sin(performance.now() / 2500);
pointLight.position.copy(light.position);
pointLight.color.setRGB(...effectController.color);
light.material.color.copy(pointLight.color);
if (frame === 0) {
renderer.shadowMap.needsUpdate = true;
} else {
renderer.shadowMap.needsUpdate = true;
}
renderer.setRenderTarget(defaultTexture);
renderer.clear();
renderer.render(scene, camera);
effectPass.uniforms["sceneDiffuse"].value = defaultTexture.texture;
effectPass.uniforms["sceneDepth"].value = defaultTexture.depthTexture;
effectPass.uniforms["depthCube"].value = pointLight.shadow.map.texture;
camera.updateMatrixWorld();
effectPass.uniforms["projectionMatrixInv"].value = camera.projectionMatrixInverse;
effectPass.uniforms["viewMatrixInv"].value = camera.matrixWorld;
effectPass.uniforms["lightPos"].value = light.position;
effectPass.uniforms['resolution'].value = new THREE.Vector2(godraysTexture.width, godraysTexture.height);
effectPass.uniforms['noiseResolution'].value = new THREE.Vector2(noiseTex.image.width, noiseTex.image.height);
effectPass.uniforms['time'].value = performance.now() / 1000;
effectPass.uniforms["cameraPos"].value = camera.position;
effectPass.uniforms['mapSize'].value = pointLight.shadow.mapSize.height;
effectPass.uniforms["near"].value = pointLight.shadow.camera.near;
effectPass.uniforms["far"].value = pointLight.shadow.camera.far;
effectPass.uniforms["blueNoise"].value = noiseTex;
effectPass.uniforms["density"].value = effectController.density;
effectPass.uniforms["maxDensity"].value = effectController.maxDensity;
effectPass.uniforms["distanceAttenuation"].value = effectController.distanceAttenuation;
renderer.setRenderTarget(godraysTexture);
effectQuad.render(renderer);
renderer.setRenderTarget(godraysBlurTexture);
/* horizontalBlur.material.uniforms.tDiffuse.value = godraysTexture.texture;
horizontalBlur.render(renderer);
renderer.setRenderTarget(godraysTexture);
verticalBlur.material.uniforms.tDiffuse.value = godraysBlurTexture.texture;
verticalBlur.render(renderer);*/
effectCompositer.uniforms["sceneDiffuse"].value = defaultTexture.texture;
effectCompositer.uniforms["sceneDepth"].value = defaultTexture.depthTexture;
effectCompositer.uniforms["resolution"].value = new THREE.Vector2(clientWidth, clientHeight);
effectCompositer.uniforms["edgeRadius"].value = effectController.edgeRadius;
effectCompositer.uniforms["edgeStrength"].value = effectController.edgeStrength;
effectCompositer.uniforms["color"].value = new THREE.Vector3(...effectController.color);
effectCompositer.uniforms["godrays"].value = godraysTexture.texture;
renderer.setRenderTarget(null);
composer.render();
controls.update();
stats.update();
requestAnimationFrame(animate);
frame++;
}
requestAnimationFrame(animate);
}
main();