-
-
Notifications
You must be signed in to change notification settings - Fork 35.5k
/
Copy pathwebgpu_cubemap_adjustments.html
212 lines (143 loc) · 6.5 KB
/
webgpu_cubemap_adjustments.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<title>three.js webgpu - cubemap adjustments</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="main.css">
</head>
<body>
<div id="info">
<a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - Env. Adjustments<br />
Battle Damaged Sci-fi Helmet by
<a href="https://sketchfab.com/theblueturtle_" target="_blank" rel="noopener">theblueturtle_</a><br />
</div>
<!-- Import maps polyfill -->
<!-- Remove this when import maps will be widely supported -->
<script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
<script type="importmap">
{
"imports": {
"three": "../build/three.module.js",
"three/addons/": "./jsm/",
"three/nodes": "./jsm/nodes/Nodes.js"
}
}
</script>
<script type="module">
import * as THREE from 'three';
import { uniform, mix, cubeTexture, reference, positionLocal, positionWorld, normalWorld, positionWorldDirection, reflectVector, toneMapping } from 'three/nodes';
import WebGPU from 'three/addons/capabilities/WebGPU.js';
import WebGPURenderer from 'three/addons/renderers/webgpu/WebGPURenderer.js';
import { RGBMLoader } from 'three/addons/loaders/RGBMLoader.js';
import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
let camera, scene, renderer;
init();
function init() {
if ( WebGPU.isAvailable() === false ) {
document.body.appendChild( WebGPU.getErrorMessage() );
throw new Error( 'No WebGPU support' );
}
const container = document.createElement( 'div' );
document.body.appendChild( container );
const initialDistance = 2;
camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 0.25, 20 );
camera.position.set( - 1.8 * initialDistance, 0.6 * initialDistance, 2.7 * initialDistance );
scene = new THREE.Scene();
// cube textures
const rgbmUrls = [ 'px.png', 'nx.png', 'py.png', 'ny.png', 'pz.png', 'nz.png' ];
const cube1Texture = new RGBMLoader()
.setMaxRange( 16 )
.setPath( './textures/cube/pisaRGBM16/' )
.loadCubemap( rgbmUrls );
cube1Texture.generateMipmaps = true;
cube1Texture.minFilter = THREE.LinearMipmapLinearFilter;
const cube2Urls = [ 'posx.jpg', 'negx.jpg', 'posy.jpg', 'negy.jpg', 'posz.jpg', 'negz.jpg' ];
const cube2Texture = new THREE.CubeTextureLoader()
.setPath( './textures/cube/Park2/' )
.load( cube2Urls );
cube2Texture.generateMipmaps = true;
cube2Texture.minFilter = THREE.LinearMipmapLinearFilter;
// nodes and environment
const adjustments = {
mix: 0,
procedural: 0,
intensity: 1,
hue: 0,
saturation: 1
};
const mixNode = reference( 'mix', 'float', adjustments );
const proceduralNode = reference( 'procedural', 'float', adjustments );
const intensityNode = reference( 'intensity', 'float', adjustments );
const hueNode = reference( 'hue', 'float', adjustments );
const saturationNode = reference( 'saturation', 'float', adjustments );
const rotateY1Matrix = new THREE.Matrix4();
const rotateY2Matrix = new THREE.Matrix4();
const getEnvironmentNode = ( reflectNode, positionNode ) => {
const custom1UV = reflectNode.xyz.mul( uniform( rotateY1Matrix ) );
const custom2UV = reflectNode.xyz.mul( uniform( rotateY2Matrix ) );
const mixCubeMaps = mix( cubeTexture( cube1Texture, custom1UV ), cubeTexture( cube2Texture, custom2UV ), positionNode.y.add( mixNode ).clamp() );
const proceduralEnv = mix( mixCubeMaps, normalWorld, proceduralNode );
const intensityFilter = proceduralEnv.mul( intensityNode );
const hueFilter = intensityFilter.hue( hueNode );
return hueFilter.saturation( saturationNode );
};
const blurNode = uniform( 0 );
scene.environmentNode = getEnvironmentNode( reflectVector, positionWorld );
scene.backgroundNode = getEnvironmentNode( positionWorldDirection, positionLocal ).context( {
getSamplerLevelNode: () => blurNode
} );
// scene objects
const loader = new GLTFLoader().setPath( 'models/gltf/DamagedHelmet/glTF/' );
loader.load( 'DamagedHelmet.gltf', function ( gltf ) {
scene.add( gltf.scene );
} );
const sphereGeometry = new THREE.SphereGeometry( .5, 64, 32 );
const sphereRightView = new THREE.Mesh( sphereGeometry, new THREE.MeshStandardMaterial( { roughness: 0, metalness: 1 } ) );
sphereRightView.position.x += 2;
const sphereLeftView = new THREE.Mesh( sphereGeometry, new THREE.MeshStandardMaterial( { roughness: 1, metalness: 1 } ) );
sphereLeftView.position.x -= 2;
scene.add( sphereLeftView );
scene.add( sphereRightView );
// renderer and controls
renderer = new WebGPURenderer( { antialias: true } );
renderer.setPixelRatio( window.devicePixelRatio );
renderer.setSize( window.innerWidth, window.innerHeight );
renderer.toneMappingNode = toneMapping( THREE.LinearToneMapping, 1 );
renderer.setAnimationLoop( render );
container.appendChild( renderer.domElement );
const controls = new OrbitControls( camera, renderer.domElement );
controls.minDistance = 2;
controls.maxDistance = 10;
window.addEventListener( 'resize', onWindowResize );
// gui
const gui = new GUI();
gui.add( { blurBackground: blurNode.value }, 'blurBackground', 0, 1, 0.01 ).onChange( value => {
blurNode.value = value;
} );
gui.add( { offsetCube1: 0 }, 'offsetCube1', 0, Math.PI * 2, 0.01 ).onChange( value => {
rotateY1Matrix.makeRotationY( value );
} );
gui.add( { offsetCube2: 0 }, 'offsetCube2', 0, Math.PI * 2, 0.01 ).onChange( value => {
rotateY2Matrix.makeRotationY( value );
} );
gui.add( adjustments, 'mix', - 1, 2, 0.01 );
gui.add( adjustments, 'procedural', 0, 1, 0.01 );
gui.add( adjustments, 'intensity', 0, 5, 0.01 );
gui.add( adjustments, 'hue', 0, Math.PI * 2, 0.01 );
gui.add( adjustments, 'saturation', 0, 2, 0.01 );
}
function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize( window.innerWidth, window.innerHeight );
}
//
function render() {
renderer.render( scene, camera );
}
</script>
</body>
</html>