forked from mrdoob/three.js
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwebgl_furnace_test.html
163 lines (109 loc) · 3.73 KB
/
webgl_furnace_test.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
<!DOCTYPE html>
<html lang="en">
<head>
<title>three.js white furnace test</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="container">
<div id="info">
<a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> -
<a href="https://google.github.io/filament/Filament.md.html#toc4.7.2" target="_blank" rel="noopener">White Furnace</a> energy conservation test by <a href="https://jsantell.com/" target="_blank" rel="noopener">Jordan Santell</a>
<br>Roughness increases left to right
<br>Metalness increases top to bottom
</div>
</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/"
}
}
</script>
<script type="module">
import * as THREE from 'three';
let scene, camera, renderer, radianceMap;
const COLOR = 0xcccccc;
function init() {
const width = window.innerWidth;
const height = window.innerHeight;
const aspect = width / height;
// renderer
renderer = new THREE.WebGLRenderer( { antialias: true } );
renderer.setSize( width, height );
renderer.setPixelRatio( window.devicePixelRatio );
document.body.appendChild( renderer.domElement );
//renderer.outputEncoding = THREE.sRGBEncoding; // optional
window.addEventListener( 'resize', onWindowResize );
document.body.addEventListener( 'mouseover', function () {
scene.traverse( function ( child ) {
if ( child.isMesh ) child.material.color.setHex( 0xffffff );
} );
render();
} );
document.body.addEventListener( 'mouseout', function () {
scene.traverse( function ( child ) {
if ( child.isMesh ) child.material.color.setHex( 0xccccff ); // tinted for visibility
} );
render();
} );
// scene
scene = new THREE.Scene();
// camera
camera = new THREE.PerspectiveCamera( 40, aspect, 1, 30 );
camera.position.set( 0, 0, 18 );
}
function createObjects() {
const geometry = new THREE.SphereGeometry( 0.4, 32, 16 );
for ( let x = 0; x <= 10; x ++ ) {
for ( let y = 0; y <= 10; y ++ ) {
const material = new THREE.MeshPhysicalMaterial( {
roughness: x / 10,
metalness: y / 10,
color: 0xffffff,
envMap: radianceMap,
envMapIntensity: 1,
transmission: 0,
ior: 1.5
} );
const mesh = new THREE.Mesh( geometry, material );
mesh.position.x = x - 5;
mesh.position.y = 5 - y;
scene.add( mesh );
}
}
}
function createEnvironment() {
const envScene = new THREE.Scene();
envScene.background = new THREE.Color( COLOR );
if ( renderer.outputEncoding === THREE.sRGBEncoding ) envScene.background.convertSRGBToLinear();
const pmremGenerator = new THREE.PMREMGenerator( renderer );
radianceMap = pmremGenerator.fromScene( envScene ).texture;
pmremGenerator.dispose();
scene.background = envScene.background;
}
function onWindowResize() {
const width = window.innerWidth;
const height = window.innerHeight;
camera.aspect = width / height;
camera.updateProjectionMatrix();
renderer.setSize( width, height );
render();
}
function render() {
renderer.render( scene, camera );
}
Promise.resolve()
.then( init )
.then( createEnvironment )
.then( createObjects )
.then( render );
</script>
</body>
</html>