-
-
Notifications
You must be signed in to change notification settings - Fork 36.4k
Expand file tree
/
Copy pathwebgpu_loader_materialx.html
More file actions
327 lines (226 loc) · 8.3 KB
/
webgpu_loader_materialx.html
File metadata and controls
327 lines (226 loc) · 8.3 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
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
<!DOCTYPE html>
<html lang="en">
<head>
<title>three.js webgpu - materialx loader</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">
<style>
.dg .property-name {
width: 20% !important;
}
</style>
</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>MaterialX Loader</span>
</div>
<small>
MaterialX Standard Surface loader.
</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 { Fn, abs, fract, fwidth, length, max, saturate, smoothstep, vec4, positionWorld, float } from 'three/tsl';
import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
import { HDRLoader } from 'three/addons/loaders/HDRLoader.js';
import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
import { MaterialXLoader } from 'three/addons/loaders/MaterialXLoader.js';
import { Inspector } from 'three/addons/inspector/Inspector.js';
const SAMPLE_PATH = 'https://raw.githubusercontent.com/materialx/MaterialX/main/resources/Materials/Examples/StandardSurface/';
const LOCAL_SAMPLE_PATH = 'materialx/';
const samples = [
'standard_surface_brass_tiled.mtlx',
'standard_surface_brick_procedural.mtlx',
'standard_surface_carpaint.mtlx',
//'standard_surface_chess_set.mtlx',
'standard_surface_chrome.mtlx',
'standard_surface_copper.mtlx',
//'standard_surface_default.mtlx',
//'standard_surface_glass.mtlx',
//'standard_surface_glass_tinted.mtlx',
'standard_surface_gold.mtlx',
//'standard_surface_greysphere.mtlx',
//'standard_surface_greysphere_calibration.mtlx',
'standard_surface_jade.mtlx',
//'standard_surface_look_brass_tiled.mtlx',
//'standard_surface_look_wood_tiled.mtlx',
'standard_surface_marble_solid.mtlx',
'standard_surface_metal_brushed.mtlx',
'standard_surface_plastic.mtlx',
//'standard_surface_thin_film.mtlx',
'standard_surface_velvet.mtlx',
'standard_surface_wood_tiled.mtlx'
];
const localSamples = [
'heightnormal.mtlx',
'conditional_if_float.mtlx',
'image_transform.mtlx',
'color3_vec3_cm_test.mtlx',
'rotate2d_test.mtlx',
'rotate3d_test.mtlx',
'heighttonormal_normal_input.mtlx',
'roughness_test.mtlx',
'opacity_test.mtlx',
'opacity_only_test.mtlx',
'specular_test.mtlx',
'ior_test.mtlx',
'combined_test.mtlx',
'texture_opacity_test.mtlx',
'transmission_test.mtlx',
'transmission_only_test.mtlx',
'transmission_rough.mtlx',
'thin_film_rainbow_test.mtlx',
'thin_film_ior_clamp_test.mtlx',
'sheen_test.mtlx',
];
let camera, scene, renderer;
let controls, prefab;
const models = [];
init();
function init() {
const container = document.createElement( 'div' );
document.body.appendChild( container );
renderer = new THREE.WebGPURenderer( { antialias: true } );
renderer.setPixelRatio( window.devicePixelRatio );
renderer.setSize( window.innerWidth, window.innerHeight );
renderer.toneMapping = THREE.LinearToneMapping;
renderer.toneMappingExposure = .5;
renderer.inspector = new Inspector();
renderer.setAnimationLoop( render );
container.appendChild( renderer.domElement );
camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 0.25, 200 );
camera.position.set( 10, 10, 20 );
scene = new THREE.Scene();
scene.background = new THREE.Color( 0xffffff );
// Ground plane
const material = new THREE.MeshBasicNodeMaterial();
const grid = Fn( ( [ coord, lineWidth = float( 0.01 ), dotSize = float( 0.03 ) ] ) => {
// https://bgolus.medium.com/the-best-darn-grid-shader-yet-727f9278b9d8
const g = fract( coord );
const fw = fwidth( coord );
const gx = abs( g.x.sub( 0.5 ) );
const gy = abs( g.y.sub( 0.5 ) );
const lineX = saturate( lineWidth.sub( gx ).div( fw.x ).add( 0.5 ) );
const lineY = saturate( lineWidth.sub( gy ).div( fw.y ).add( 0.5 ) );
const lines = max( lineX, lineY );
const squareDist = max( gx, gy );
const aa = max( fw.x, fw.y );
const dots = smoothstep( dotSize.add( aa ), dotSize.sub( aa ), squareDist );
return max( dots, lines );
} );
const fade = Fn( ( [ radius = float( 10.0 ), falloff = float( 1.0 ) ] ) => {
return smoothstep( radius, radius.sub( falloff ), length( positionWorld ) );
} );
const gridColor = vec4( 0.5, 0.5, 0.5, 1.0 );
const baseColor = vec4( 1.0, 1.0, 1.0, 0.0 );
material.colorNode = grid( positionWorld.xz, 0.007, 0.03 ).mix( baseColor, gridColor ).mul( fade( 30.0, 20.0 ) );
material.transparent = true;
const plane = new THREE.Mesh( new THREE.CircleGeometry( 40 ), material );
plane.rotation.x = - Math.PI / 2;
plane.renderOrder = - 1;
scene.add( plane );
//
controls = new OrbitControls( camera );
controls.connect( renderer.domElement );
controls.enableDamping = true;
controls.minDistance = 2;
controls.maxDistance = 40;
//
new HDRLoader()
.setPath( 'textures/equirectangular/' )
.load( 'san_giuseppe_bridge_2k.hdr', async ( texture ) => {
texture.mapping = THREE.EquirectangularReflectionMapping;
scene.environment = texture;
prefab = ( await new GLTFLoader().loadAsync( './models/gltf/ShaderBall.glb' ) ).scene;
for ( const sample of samples ) {
await addSample( sample, SAMPLE_PATH );
}
for ( const sample of localSamples ) {
await addSample( sample, LOCAL_SAMPLE_PATH );
}
addGUI();
} );
window.addEventListener( 'resize', onWindowResize );
}
function updateModelsAlign() {
const COLUMN_COUNT = 6;
const DIST_X = 3;
const DIST_Z = 3;
const lineCount = Math.floor( models.length / COLUMN_COUNT ) - 1.5;
const offsetX = ( DIST_X * ( COLUMN_COUNT - 1 ) ) * - .5;
const offsetZ = ( DIST_Z * lineCount ) * .5;
for ( let i = 0; i < models.length; i ++ ) {
const model = models[ i ];
model.position.x = ( ( i % COLUMN_COUNT ) * DIST_X ) + offsetX;
model.position.z = ( Math.floor( i / COLUMN_COUNT ) * - DIST_Z ) + offsetZ;
}
}
async function addSample( sample, path ) {
const model = prefab.clone();
models.push( model );
scene.add( model );
updateModelsAlign();
//
const material = await new MaterialXLoader()
.setPath( path )
.loadAsync( sample )
.then( ( { materials } ) => Object.values( materials ).pop() );
const calibrationMesh = model.getObjectByName( 'Calibration_Mesh' );
calibrationMesh.material = material;
const previewMesh = model.getObjectByName( 'Preview_Mesh' );
previewMesh.material = material;
if ( material.transparent ) {
calibrationMesh.renderOrder = 1;
previewMesh.renderOrder = 2;
}
}
function addGUI() {
const gui = renderer.inspector.createParameters( 'MaterialX Loader' );
const API = {
showCalibrationMesh: true,
showPreviewMesh: true
};
gui.add( API, 'showCalibrationMesh' )
.name( 'Calibration Mesh' )
.onChange( function ( value ) {
setVisibility( 'Calibration_Mesh', value );
} );
gui.add( API, 'showPreviewMesh' )
.name( 'Preview Mesh' )
.onChange( function ( value ) {
setVisibility( 'Preview_Mesh', value );
} );
}
function setVisibility( name, visible ) {
scene.traverse( function ( node ) {
if ( node.isMesh ) {
if ( node.name == name ) node.visible = visible;
}
} );
}
//
function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize( window.innerWidth, window.innerHeight );
}
function render() {
controls.update();
renderer.render( scene, camera );
}
</script>
</body>
</html>