-
-
Notifications
You must be signed in to change notification settings - Fork 36.4k
Expand file tree
/
Copy pathwebgpu_materials_envmaps_bpcem.html
More file actions
240 lines (169 loc) · 7.02 KB
/
webgpu_materials_envmaps_bpcem.html
File metadata and controls
240 lines (169 loc) · 7.02 KB
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
<!DOCTYPE html>
<html lang="en">
<head>
<title>three.js webgpu - bpcem</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
<link type="text/css" rel="stylesheet" href="example.css">
</head>
<body>
<div id="info">
<a href="https://threejs.org/" target="_blank" rel="noopener" class="logo-link"></a>
<div class="title-wrapper">
<a href="https://threejs.org/" target="_blank" rel="noopener">three.js</a><span>BPCEM</span>
</div>
<small>
Box projected cube environment mapping (BPCEM).
</small>
</div>
<script type="importmap">
{
"imports": {
"three": "../build/three.webgpu.js",
"three/webgpu": "../build/three.webgpu.js",
"three/tsl": "../build/three.tsl.js",
"three/addons/": "./jsm/"
}
}
</script>
<script type="module">
import * as THREE from 'three/webgpu';
import { bumpMap, float, getParallaxCorrectNormal, pmremTexture, reflectVector, texture, uniform, vec3 } from 'three/tsl';
import { Inspector } from 'three/addons/inspector/Inspector.js';
import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
import { RectAreaLightHelper } from 'three/addons/helpers/RectAreaLightHelper.js';
import { RectAreaLightTexturesLib } from 'three/addons/lights/RectAreaLightTexturesLib.js';
let camera, scene, renderer;
let controls, cubeCamera;
let groundPlane, wallMat;
init();
async function init() {
THREE.RectAreaLightNode.setLTC( RectAreaLightTexturesLib.init() );
// scene
scene = new THREE.Scene();
// camera
camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 0.1, 1000 );
camera.position.set( 0, 200, - 200 );
// cube camera for environment map
const renderTarget = new THREE.CubeRenderTarget( 512 );
renderTarget.texture.type = THREE.HalfFloatType;
renderTarget.texture.minFilter = THREE.LinearMipmapLinearFilter;
renderTarget.texture.magFilter = THREE.LinearFilter;
renderTarget.texture.generateMipmaps = true;
renderTarget.texture.mapping = THREE.CubeReflectionMapping;
cubeCamera = new THREE.CubeCamera( 1, 1000, renderTarget );
cubeCamera.position.set( 0, - 100, 0 );
// ground floor ( with box projected environment mapping )
const loader = new THREE.TextureLoader();
const rMap = loader.load( 'textures/lava/lavatile.jpg' );
rMap.wrapS = THREE.RepeatWrapping;
rMap.wrapT = THREE.RepeatWrapping;
rMap.repeat.set( 2, 1 );
const roughnessUniform = uniform( 0.25 );
const defaultMat = new THREE.MeshStandardNodeMaterial();
defaultMat.envNode = pmremTexture( renderTarget.texture );
defaultMat.roughnessNode = texture( rMap ).mul( roughnessUniform );
defaultMat.metalnessNode = float( 1 );
const boxProjectedMat = new THREE.MeshStandardNodeMaterial();
boxProjectedMat.envNode = pmremTexture( renderTarget.texture, getParallaxCorrectNormal( reflectVector, vec3( 200, 100, 100 ), vec3( 0, - 50, 0 ) ) );
boxProjectedMat.roughnessNode = texture( rMap ).mul( roughnessUniform );
boxProjectedMat.metalnessNode = float( 1 );
groundPlane = new THREE.Mesh( new THREE.PlaneGeometry( 200, 100, 100 ), boxProjectedMat );
groundPlane.rotateX( - Math.PI / 2 );
groundPlane.position.set( 0, - 49, 0 );
scene.add( groundPlane );
// walls
const [ diffuseTex, bumpTex ] = await Promise.all( [
loader.loadAsync( 'textures/brick_diffuse.jpg' ),
loader.loadAsync( 'textures/brick_bump.jpg' )
] );
diffuseTex.colorSpace = THREE.SRGBColorSpace;
wallMat = new THREE.MeshStandardNodeMaterial();
wallMat.colorNode = texture( diffuseTex );
wallMat.normalNode = bumpMap( texture( bumpTex ), float( 5 ) );
const planeGeo = new THREE.PlaneGeometry( 100, 100 );
const planeBack1 = new THREE.Mesh( planeGeo, wallMat );
planeBack1.position.z = - 50;
planeBack1.position.x = - 50;
scene.add( planeBack1 );
const planeBack2 = new THREE.Mesh( planeGeo, wallMat );
planeBack2.position.z = - 50;
planeBack2.position.x = 50;
scene.add( planeBack2 );
const planeFront1 = new THREE.Mesh( planeGeo, wallMat );
planeFront1.position.z = 50;
planeFront1.position.x = - 50;
planeFront1.rotateY( Math.PI );
scene.add( planeFront1 );
const planeFront2 = new THREE.Mesh( planeGeo, wallMat );
planeFront2.position.z = 50;
planeFront2.position.x = 50;
planeFront2.rotateY( Math.PI );
scene.add( planeFront2 );
const planeRight = new THREE.Mesh( planeGeo, wallMat );
planeRight.position.x = 100;
planeRight.rotateY( - Math.PI / 2 );
scene.add( planeRight );
const planeLeft = new THREE.Mesh( planeGeo, wallMat );
planeLeft.position.x = - 100;
planeLeft.rotateY( Math.PI / 2 );
scene.add( planeLeft );
// area lights
const width = 50;
const height = 50;
const intensity = 5;
const blueRectLight = new THREE.RectAreaLight( 0x9aaeff, intensity, width, height );
blueRectLight.position.set( - 99, 5, 0 );
blueRectLight.lookAt( 0, 5, 0 );
scene.add( blueRectLight );
const blueRectLightHelper = new RectAreaLightHelper( blueRectLight, 0xffffff );
blueRectLight.add( blueRectLightHelper );
const redRectLight = new THREE.RectAreaLight( 0xf3aaaa, intensity, width, height );
redRectLight.position.set( 99, 5, 0 );
redRectLight.lookAt( 0, 5, 0 );
scene.add( redRectLight );
const redRectLightHelper = new RectAreaLightHelper( redRectLight, 0xffffff );
redRectLight.add( redRectLightHelper );
// renderer
renderer = new THREE.WebGPURenderer( { antialias: true } );
renderer.setPixelRatio( window.devicePixelRatio );
renderer.setSize( window.innerWidth, window.innerHeight );
renderer.setAnimationLoop( animate );
renderer.inspector = new Inspector();
document.body.appendChild( renderer.domElement );
await renderer.init();
updateCubeMap();
window.addEventListener( 'resize', onWindowResize );
// controls
controls = new OrbitControls( camera, renderer.domElement );
controls.target.set( 0, - 10, 0 );
controls.maxDistance = 400;
controls.minDistance = 10;
controls.update();
// gui
const gui = renderer.inspector.createParameters( 'Parameters' );
const params = {
'box projected': true
};
gui.add( params, 'box projected' ).onChange( ( value ) => {
groundPlane.material = ( value ) ? boxProjectedMat : defaultMat;
} );
gui.add( roughnessUniform, 'value', 0, 1 ).name( 'roughness' );
}
function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize( window.innerWidth, window.innerHeight );
}
function updateCubeMap() {
groundPlane.visible = false;
cubeCamera.position.copy( groundPlane.position );
cubeCamera.update( renderer, scene );
groundPlane.visible = true;
}
function animate() {
renderer.render( scene, camera );
}
</script>
</body>
</html>