forked from mrdoob/three.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebgl_materials_displacementmap.html
295 lines (194 loc) · 6.99 KB
/
webgl_materials_displacementmap.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
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
<!DOCTYPE html>
<html lang="en">
<head>
<title>three.js webgl - materials - displacement map</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
<style>
body {
background:#000;
color:#fff;
padding:0;
margin:0;
font-weight: bold;
overflow:hidden;
}
a { color: #ffffff; }
#info {
position: absolute;
top: 0px; width: 100%;
color: #ffffff;
padding: 5px;
font-family:Monospace;
font-size:13px;
text-align:center;
}
#vt { display:none }
#vt, #vt a { color:orange; }
</style>
</head>
<body>
<div id="info">
<a href="http://threejs.org" target="_blank" rel="noopener">three.js</a> - (<span id="description">normal + ao + displacement + environment</span>) map demo.<br />
ninja head from <a href="http://developer.amd.com/tools-and-sdks/archive/legacy-cpu-gpu-tools/amd-gpu-meshmapper/" target="_blank" rel="noopener">AMD GPU MeshMapper</a>
<div id="vt">displacement mapping requires vertex textures</div>
</div>
<script src="../build/three.js"></script>
<script src="js/controls/OrbitControls.js"></script>
<script src="js/loaders/OBJLoader.js"></script>
<script src="js/WebGL.js"></script>
<script src="js/libs/stats.min.js"></script>
<script src='js/libs/dat.gui.min.js'></script>
<script>
if ( WEBGL.isWebGLAvailable() === false ) {
document.body.appendChild( WEBGL.getWebGLErrorMessage() );
}
var stats;
var camera, scene, renderer, controls;
var settings = {
metalness: 1.0,
roughness: 0.4,
ambientIntensity: 0.2,
aoMapIntensity: 1.0,
envMapIntensity: 1.0,
displacementScale: 2.436143, // from original model
normalScale: 1.0
};
var mesh, material;
var pointLight, ambientLight;
var height = 500; // of camera frustum
var r = 0.0;
init();
animate();
initGui();
// Init gui
function initGui() {
var gui = new dat.GUI();
//var gui = gui.addFolder( "Material" );
gui.add( settings, "metalness" ).min( 0 ).max( 1 ).onChange( function ( value ) {
material.metalness = value;
} );
gui.add( settings, "roughness" ).min( 0 ).max( 1 ).onChange( function ( value ) {
material.roughness = value;
} );
gui.add( settings, "aoMapIntensity" ).min( 0 ).max( 1 ).onChange( function ( value ) {
material.aoMapIntensity = value;
} );
gui.add( settings, "ambientIntensity" ).min( 0 ).max( 1 ).onChange( function ( value ) {
ambientLight.intensity = value;
} );
gui.add( settings, "envMapIntensity" ).min( 0 ).max( 3 ).onChange( function ( value ) {
material.envMapIntensity = value;
} );
gui.add( settings, "displacementScale" ).min( 0 ).max( 3.0 ).onChange( function ( value ) {
material.displacementScale = value;
} );
gui.add( settings, "normalScale" ).min( - 1 ).max( 1 ).onChange( function ( value ) {
material.normalScale.set( 1, - 1 ).multiplyScalar( value );
} );
}
function init() {
var container = document.createElement( 'div' );
document.body.appendChild( container );
renderer = new THREE.WebGLRenderer();
renderer.setPixelRatio( window.devicePixelRatio );
renderer.setSize( window.innerWidth, window.innerHeight );
container.appendChild( renderer.domElement );
renderer.gammaInput = true;
renderer.gammaOutput = true;
//
scene = new THREE.Scene();
var aspect = window.innerWidth / window.innerHeight;
camera = new THREE.OrthographicCamera( - height * aspect, height * aspect, height, - height, 1, 10000 );
camera.position.z = 1500;
scene.add( camera );
controls = new THREE.OrbitControls( camera, renderer.domElement );
controls.enableZoom = false;
controls.enableDamping = true;
// lights
ambientLight = new THREE.AmbientLight( 0xffffff, settings.ambientIntensity );
scene.add( ambientLight );
pointLight = new THREE.PointLight( 0xff0000, 0.5 );
pointLight.position.z = 2500;
scene.add( pointLight );
var pointLight2 = new THREE.PointLight( 0xff6666, 1 );
camera.add( pointLight2 );
var pointLight3 = new THREE.PointLight( 0x0000ff, 0.5 );
pointLight3.position.x = - 1000;
pointLight3.position.z = 1000;
scene.add( pointLight3 );
// env map
var path = "textures/cube/SwedishRoyalCastle/";
var format = '.jpg';
var urls = [
path + 'px' + format, path + 'nx' + format,
path + 'py' + format, path + 'ny' + format,
path + 'pz' + format, path + 'nz' + format
];
var reflectionCube = new THREE.CubeTextureLoader().load( urls );
// textures
var textureLoader = new THREE.TextureLoader();
var normalMap = textureLoader.load( "models/obj/ninja/normal.jpg" );
var aoMap = textureLoader.load( "models/obj/ninja/ao.jpg" );
var displacementMap = textureLoader.load( "models/obj/ninja/displacement.jpg" );
// material
material = new THREE.MeshStandardMaterial( {
color: 0x888888,
roughness: settings.roughness,
metalness: settings.metalness,
normalMap: normalMap,
normalScale: new THREE.Vector2( 1, - 1 ), // why does the normal map require negation in this case?
aoMap: aoMap,
aoMapIntensity: 1,
displacementMap: displacementMap,
displacementScale: settings.displacementScale,
displacementBias: - 0.428408, // from original model
envMap: reflectionCube,
envMapIntensity: settings.envMapIntensity,
side: THREE.DoubleSide
} );
//
var loader = new THREE.OBJLoader();
loader.load( "models/obj/ninja/ninjaHead_Low.obj", function ( group ) {
var geometry = group.children[ 0 ].geometry;
geometry.attributes.uv2 = geometry.attributes.uv;
geometry.center();
mesh = new THREE.Mesh( geometry, material );
mesh.scale.multiplyScalar( 25 );
scene.add( mesh );
} );
//
var description = "normal + ao" + ( renderer.capabilities.vertexTextures ? " + displacement + environment" : " + <strike>displacement</strike>" );
document.getElementById( "description" ).innerHTML = description;
document.getElementById( "vt" ).style.display = renderer.capabilities.vertexTextures ? "none" : "block";
//
stats = new Stats();
container.appendChild( stats.dom );
//
window.addEventListener( 'resize', onWindowResize, false );
}
function onWindowResize() {
var aspect = window.innerWidth / window.innerHeight;
camera.left = - height * aspect;
camera.right = height * aspect;
camera.top = height;
camera.bottom = - height;
camera.updateProjectionMatrix();
renderer.setSize( window.innerWidth, window.innerHeight );
}
//
function animate() {
requestAnimationFrame( animate );
stats.begin();
render();
stats.end();
}
function render() {
pointLight.position.x = 2500 * Math.cos( r );
pointLight.position.z = 2500 * Math.sin( r );
r += 0.01;
renderer.render( scene, camera );
}
</script>
</body>
</html>