forked from mrdoob/three.js
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwebgl_materials_physical_clearcoat.html
253 lines (178 loc) · 6.38 KB
/
webgl_materials_physical_clearcoat.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
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
<!DOCTYPE html>
<html lang="en">
<head>
<title>three.js webgl - materials - clearcoat</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> webgl - materials - clearcoat
</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';
import Stats from 'three/addons/libs/stats.module.js';
import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
import { HDRCubeTextureLoader } from 'three/addons/loaders/HDRCubeTextureLoader.js';
import { FlakesTexture } from 'three/addons/textures/FlakesTexture.js';
let container, stats;
let camera, scene, renderer;
let particleLight;
let group;
init();
animate();
function init() {
container = document.createElement( 'div' );
document.body.appendChild( container );
camera = new THREE.PerspectiveCamera( 27, window.innerWidth / window.innerHeight, 1, 10000 );
camera.position.z = 1000;
scene = new THREE.Scene();
group = new THREE.Group();
scene.add( group );
new HDRCubeTextureLoader()
.setPath( 'textures/cube/pisaHDR/' )
.load( [ 'px.hdr', 'nx.hdr', 'py.hdr', 'ny.hdr', 'pz.hdr', 'nz.hdr' ],
function ( texture ) {
const geometry = new THREE.SphereGeometry( 80, 64, 32 );
const textureLoader = new THREE.TextureLoader();
const diffuse = textureLoader.load( 'textures/carbon/Carbon.png' );
diffuse.encoding = THREE.sRGBEncoding;
diffuse.wrapS = THREE.RepeatWrapping;
diffuse.wrapT = THREE.RepeatWrapping;
diffuse.repeat.x = 10;
diffuse.repeat.y = 10;
const normalMap = textureLoader.load( 'textures/carbon/Carbon_Normal.png' );
normalMap.wrapS = THREE.RepeatWrapping;
normalMap.wrapT = THREE.RepeatWrapping;
const normalMap2 = textureLoader.load( 'textures/water/Water_1_M_Normal.jpg' );
const normalMap3 = new THREE.CanvasTexture( new FlakesTexture() );
normalMap3.wrapS = THREE.RepeatWrapping;
normalMap3.wrapT = THREE.RepeatWrapping;
normalMap3.repeat.x = 10;
normalMap3.repeat.y = 6;
normalMap3.anisotropy = 16;
const normalMap4 = textureLoader.load( 'textures/golfball.jpg' );
const clearcoatNormalMap = textureLoader.load( 'textures/pbr/Scratched_gold/Scratched_gold_01_1K_Normal.png' );
// car paint
let material = new THREE.MeshPhysicalMaterial( {
clearcoat: 1.0,
clearcoatRoughness: 0.1,
metalness: 0.9,
roughness: 0.5,
color: 0x0000ff,
normalMap: normalMap3,
normalScale: new THREE.Vector2( 0.15, 0.15 )
} );
let mesh = new THREE.Mesh( geometry, material );
mesh.position.x = - 100;
mesh.position.y = 100;
group.add( mesh );
// fibers
material = new THREE.MeshPhysicalMaterial( {
roughness: 0.5,
clearcoat: 1.0,
clearcoatRoughness: 0.1,
map: diffuse,
normalMap: normalMap
} );
mesh = new THREE.Mesh( geometry, material );
mesh.position.x = 100;
mesh.position.y = 100;
group.add( mesh );
// golf
material = new THREE.MeshPhysicalMaterial( {
metalness: 0.0,
roughness: 0.1,
clearcoat: 1.0,
normalMap: normalMap4,
clearcoatNormalMap: clearcoatNormalMap,
// y scale is negated to compensate for normal map handedness.
clearcoatNormalScale: new THREE.Vector2( 2.0, - 2.0 )
} );
mesh = new THREE.Mesh( geometry, material );
mesh.position.x = - 100;
mesh.position.y = - 100;
group.add( mesh );
// clearcoat + normalmap
material = new THREE.MeshPhysicalMaterial( {
clearcoat: 1.0,
metalness: 1.0,
color: 0xff0000,
normalMap: normalMap2,
normalScale: new THREE.Vector2( 0.15, 0.15 ),
clearcoatNormalMap: clearcoatNormalMap,
// y scale is negated to compensate for normal map handedness.
clearcoatNormalScale: new THREE.Vector2( 2.0, - 2.0 )
} );
mesh = new THREE.Mesh( geometry, material );
mesh.position.x = 100;
mesh.position.y = - 100;
group.add( mesh );
//
scene.background = texture;
scene.environment = texture;
}
);
// LIGHTS
particleLight = new THREE.Mesh(
new THREE.SphereGeometry( 4, 8, 8 ),
new THREE.MeshBasicMaterial( { color: 0xffffff } )
);
scene.add( particleLight );
particleLight.add( new THREE.PointLight( 0xffffff, 1 ) );
renderer = new THREE.WebGLRenderer();
renderer.setPixelRatio( window.devicePixelRatio );
renderer.setSize( window.innerWidth, window.innerHeight );
container.appendChild( renderer.domElement );
//
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.25;
//
renderer.outputEncoding = THREE.sRGBEncoding;
//
stats = new Stats();
container.appendChild( stats.dom );
// EVENTS
new OrbitControls( camera, renderer.domElement );
window.addEventListener( 'resize', onWindowResize );
}
//
function onWindowResize() {
const width = window.innerWidth;
const height = window.innerHeight;
camera.aspect = width / height;
camera.updateProjectionMatrix();
renderer.setSize( width, height );
}
//
function animate() {
requestAnimationFrame( animate );
render();
stats.update();
}
function render() {
const timer = Date.now() * 0.00025;
particleLight.position.x = Math.sin( timer * 7 ) * 300;
particleLight.position.y = Math.cos( timer * 5 ) * 400;
particleLight.position.z = Math.cos( timer * 3 ) * 300;
for ( let i = 0; i < group.children.length; i ++ ) {
const child = group.children[ i ];
child.rotation.y += 0.005;
}
renderer.render( scene, camera );
}
</script>
</body>
</html>