forked from mrdoob/three.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebgl_materials_normalmap.html
254 lines (168 loc) · 6.13 KB
/
webgl_materials_normalmap.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
<!DOCTYPE html>
<html lang="en">
<head>
<title>three.js webgl - materials - normal map [Lee Perry-Smith]</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;
z-index:1000;
}
#webglmessage {
background:rgb(200,100,0) !important;
color:#fff;
}
#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> - webgl normalmap demo.
<a href="http://graphics.cs.williams.edu/data/meshes.xml#14" target="_blank" rel="noopener">Lee Perry-Smith</a> head.
<div id="vt">displacement mapping needs vertex textures (GPU with Shader Model 3.0)<br/>
on Windows use <span class="code">Chrome --use-gl=desktop</span> or Firefox 4<br/>
please star this <a href="http://code.google.com/p/chromium/issues/detail?id=52497">Chrome issue</a> to get ANGLE support
</div>
</div>
<script src="../build/three.js"></script>
<script src="js/loaders/GLTFLoader.js"></script>
<script src="js/WebGL.js"></script>
<script src="js/libs/stats.min.js"></script>
<script src="js/shaders/BleachBypassShader.js"></script>
<script src="js/shaders/ColorCorrectionShader.js"></script>
<script src="js/shaders/CopyShader.js"></script>
<script src="js/shaders/FXAAShader.js"></script>
<script src="js/postprocessing/EffectComposer.js"></script>
<script src="js/postprocessing/RenderPass.js"></script>
<script src="js/postprocessing/ShaderPass.js"></script>
<script src="js/postprocessing/MaskPass.js"></script>
<script>
if ( WEBGL.isWebGLAvailable() === false ) {
document.body.appendChild( WEBGL.getWebGLErrorMessage() );
}
var container, stats, loader;
var camera, scene, renderer;
var mesh;
var directionalLight, pointLight, ambientLight;
var mouseX = 0;
var mouseY = 0;
var targetX = 0;
var targetY = 0;
var windowHalfX = window.innerWidth / 2;
var windowHalfY = window.innerHeight / 2;
var composer, effectFXAA;
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 = 1200;
scene = new THREE.Scene();
scene.background = new THREE.Color( 0x111111 );
// LIGHTS
ambientLight = new THREE.AmbientLight( 0x444444 );
scene.add( ambientLight );
pointLight = new THREE.PointLight( 0xffffff, 1.25, 1000 );
pointLight.position.set( 0, 0, 600 );
scene.add( pointLight );
directionalLight = new THREE.DirectionalLight( 0xffffff );
directionalLight.position.set( 1, - 0.5, - 1 );
scene.add( directionalLight );
var textureLoader = new THREE.TextureLoader();
var material = new THREE.MeshPhongMaterial( {
color: 0xdddddd,
specular: 0x222222,
shininess: 35,
map: textureLoader.load( "models/gltf/LeePerrySmith/Map-COL.jpg" ),
specularMap: textureLoader.load( "models/gltf/LeePerrySmith/Map-SPEC.jpg" ),
normalMap: textureLoader.load( "models/gltf/LeePerrySmith/Infinite-Level_02_Tangent_SmoothUV.jpg" ),
normalScale: new THREE.Vector2( 0.8, 0.8 )
} );
loader = new THREE.GLTFLoader();
loader.load( "models/gltf/LeePerrySmith/LeePerrySmith.glb", function ( gltf ) {
createScene( gltf.scene.children[ 0 ].geometry, 100, material );
} );
renderer = new THREE.WebGLRenderer();
renderer.setSize( window.innerWidth, window.innerHeight );
container.appendChild( renderer.domElement );
//
renderer.gammaInput = true;
renderer.gammaOutput = true;
//
stats = new Stats();
container.appendChild( stats.dom );
// COMPOSER
renderer.autoClear = false;
var renderModel = new THREE.RenderPass( scene, camera );
var effectBleach = new THREE.ShaderPass( THREE.BleachBypassShader );
var effectColor = new THREE.ShaderPass( THREE.ColorCorrectionShader );
effectFXAA = new THREE.ShaderPass( THREE.FXAAShader );
effectFXAA.uniforms[ 'resolution' ].value.set( 1 / window.innerWidth, 1 / window.innerHeight );
effectBleach.uniforms[ 'opacity' ].value = 0.4;
effectColor.uniforms[ 'powRGB' ].value.set( 1.4, 1.45, 1.45 );
effectColor.uniforms[ 'mulRGB' ].value.set( 1.1, 1.1, 1.1 );
composer = new THREE.EffectComposer( renderer );
composer.addPass( renderModel );
composer.addPass( effectFXAA );
composer.addPass( effectBleach );
composer.addPass( effectColor );
// EVENTS
document.addEventListener( 'mousemove', onDocumentMouseMove, false );
window.addEventListener( 'resize', onWindowResize, false );
}
function createScene( geometry, scale, material ) {
mesh = new THREE.Mesh( geometry, material );
mesh.position.y = - 50;
mesh.scale.x = mesh.scale.y = mesh.scale.z = scale;
scene.add( mesh );
}
//
function onWindowResize() {
var width = window.innerWidth;
var height = window.innerHeight;
camera.aspect = width / height;
camera.updateProjectionMatrix();
renderer.setSize( width, height );
composer.setSize( width, height );
effectFXAA.uniforms[ 'resolution' ].value.set( 1 / width, 1 / height );
}
function onDocumentMouseMove( event ) {
mouseX = ( event.clientX - windowHalfX );
mouseY = ( event.clientY - windowHalfY );
}
//
function animate() {
requestAnimationFrame( animate );
render();
stats.update();
}
function render() {
targetX = mouseX * .001;
targetY = mouseY * .001;
if ( mesh ) {
mesh.rotation.y += 0.05 * ( targetX - mesh.rotation.y );
mesh.rotation.x += 0.05 * ( targetY - mesh.rotation.x );
}
composer.render();
}
</script>
</body>
</html>