webgl_materials_normalmap.html 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - materials - normal map</title>
  5. <meta charset="utf-8">
  6. <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
  7. <style>
  8. body {
  9. background:#000;
  10. color:#fff;
  11. padding:0;
  12. margin:0;
  13. font-weight: bold;
  14. overflow:hidden;
  15. }
  16. a { color: #ffffff; }
  17. #info {
  18. position: absolute;
  19. top: 0px; width: 100%;
  20. color: #ffffff;
  21. padding: 5px;
  22. font-family:Monospace;
  23. font-size:13px;
  24. text-align:center;
  25. z-index:1000;
  26. }
  27. #oldie {
  28. background:rgb(200,100,0) !important;
  29. color:#fff;
  30. }
  31. #vt { display:none }
  32. #vt, #vt a { color:orange; }
  33. </style>
  34. </head>
  35. <body>
  36. <div id="info">
  37. <a href="http://github.com/mrdoob/three.js" target="_blank">three.js</a> - webgl (<span id="description">normal + ao + displacement + environment + shadow</span>) map demo.
  38. ninja head from <a href="http://developer.amd.com/archive/gpu/MeshMapper/pages/default.aspx" target="_blank">AMD GPU MeshMapper</a>
  39. <div id="vt">displacement mapping needs vertex textures (GPU with Shader Model 3.0)</div>
  40. </div>
  41. <script src="../build/Three.js"></script>
  42. <script src="js/Detector.js"></script>
  43. <script src="js/Stats.js"></script>
  44. <script>
  45. if ( ! Detector.webgl ) Detector.addGetWebGLMessage();
  46. var statsEnabled = true;
  47. var container, stats, loader;
  48. var camera, scene, renderer;
  49. var mesh, zmesh, lightMesh, geometry;
  50. var mesh1, mesh2;
  51. var directionalLight, pointLight, ambientLight;
  52. var mouseX = 0;
  53. var mouseY = 0;
  54. var windowHalfX = window.innerWidth / 2;
  55. var windowHalfY = window.innerHeight / 2;
  56. var r = 0.0;
  57. document.addEventListener( 'mousemove', onDocumentMouseMove, false );
  58. init();
  59. animate();
  60. function init() {
  61. container = document.createElement( 'div' );
  62. document.body.appendChild( container );
  63. scene = new THREE.Scene();
  64. // CAMERA
  65. camera = new THREE.OrthographicCamera( window.innerWidth / - 2, window.innerWidth / 2, window.innerHeight / 2, window.innerHeight / - 2, -10000, 10000 );
  66. camera.position.z = 1500;
  67. scene.add( camera );
  68. // LIGHTS
  69. ambientLight = new THREE.AmbientLight( 0x111111 );
  70. scene.add( ambientLight );
  71. pointLight = new THREE.PointLight( 0xff0000 );
  72. pointLight.position.z = 10000;
  73. pointLight.distance = 4000;
  74. scene.add( pointLight );
  75. pointLight2 = new THREE.PointLight( 0xff5500 );
  76. pointLight2.position.z = 1000;
  77. pointLight2.distance = 2000;
  78. scene.add( pointLight2 );
  79. pointLight3 = new THREE.PointLight( 0x0000ff );
  80. pointLight3.position.x = -1000;
  81. pointLight3.position.z = 1000;
  82. pointLight3.distance = 2000;
  83. scene.add( pointLight3 );
  84. directionalLight = new THREE.SpotLight( 0xaaaaaa );
  85. directionalLight.position.set( 1000, 500, 1000 );
  86. directionalLight.castShadow = true;
  87. scene.add( directionalLight );
  88. directionalLight2 = new THREE.DirectionalLight( 0xaaff33, 0 );
  89. directionalLight2.position.set( -1, 1, 0.5 ).normalize();
  90. //scene.add( directionalLight2 );
  91. directionalLight3 = new THREE.DirectionalLight( 0xaaff33 );
  92. directionalLight3.position.set( -1, 1, 0.5 ).normalize();
  93. //scene.add( directionalLight3 );
  94. // light representation
  95. var sphere = new THREE.SphereGeometry( 100, 16, 8 );
  96. lightMesh = new THREE.Mesh( sphere, new THREE.MeshBasicMaterial( { color: 0xffaa00 } ) );
  97. lightMesh.position = pointLight.position;
  98. lightMesh.scale.x = lightMesh.scale.y = lightMesh.scale.z = 0.05;
  99. //scene.add( lightMesh );
  100. var path = "textures/cube/SwedishRoyalCastle/";
  101. var format = '.jpg';
  102. var urls = [
  103. path + 'px' + format, path + 'nx' + format,
  104. path + 'py' + format, path + 'ny' + format,
  105. path + 'pz' + format, path + 'nz' + format
  106. ];
  107. var reflectionCube = THREE.ImageUtils.loadTextureCube( urls );
  108. // common material parameters
  109. var ambient = 0x050505, diffuse = 0x331100, specular = 0xffffff, shininess = 10, scale = 23;
  110. // normal map shader
  111. var shader = THREE.ShaderUtils.lib[ "normal" ];
  112. var uniforms = THREE.UniformsUtils.clone( shader.uniforms );
  113. uniforms[ "enableAO" ].value = true;
  114. uniforms[ "enableDiffuse" ].value = false;
  115. uniforms[ "enableSpecular" ].value = false;
  116. uniforms[ "enableReflection" ].value = true;
  117. uniforms[ "tNormal" ].texture = THREE.ImageUtils.loadTexture( "textures/normal/ninja/normal.jpg" );
  118. uniforms[ "tAO" ].texture = THREE.ImageUtils.loadTexture( "textures/normal/ninja/ao.jpg" );
  119. uniforms[ "tDisplacement" ].texture = THREE.ImageUtils.loadTexture( "textures/normal/ninja/displacement.jpg" );
  120. uniforms[ "uDisplacementBias" ].value = - 0.428408 * scale;
  121. uniforms[ "uDisplacementScale" ].value = 2.436143 * scale;
  122. uniforms[ "uDiffuseColor" ].value.setHex( diffuse );
  123. uniforms[ "uSpecularColor" ].value.setHex( specular );
  124. uniforms[ "uAmbientColor" ].value.setHex( ambient );
  125. uniforms[ "uShininess" ].value = shininess;
  126. uniforms[ "tCube" ].texture = reflectionCube;
  127. uniforms[ "uReflectivity" ].value = 0.1;
  128. var parameters = { fragmentShader: shader.fragmentShader, vertexShader: shader.vertexShader, uniforms: uniforms, lights: true, fog: false };
  129. var material1 = new THREE.ShaderMaterial( parameters );
  130. var material2 = new THREE.MeshPhongMaterial( { color: diffuse, specular: specular, ambient: ambient, shininess: shininess, envMap: reflectionCube, combine: THREE.MixOperation, reflectivity: 0.1 } );
  131. //
  132. loader = new THREE.BinaryLoader( true );
  133. document.body.appendChild( loader.statusDomElement );
  134. loader.load( "obj/ninja/NinjaLo_bin.js", function( geometry ) { createScene( geometry, scale, material1, material2 ) } );
  135. //
  136. renderer = new THREE.WebGLRenderer( { maxLights: 8 } );
  137. renderer.setSize( window.innerWidth, window.innerHeight );
  138. container.appendChild( renderer.domElement );
  139. //
  140. renderer.shadowCameraFov = 70;
  141. renderer.shadowMapBias = 0.0039;
  142. renderer.shadowMapEnabled = true;
  143. renderer.shadowMapSoft = true;
  144. //
  145. var description = "normal + ao" + ( renderer.supportsVertexTextures() ? " + displacement + environment + shadow" : " + <strike>displacement</strike>" );
  146. document.getElementById( "description" ).innerHTML = description;
  147. document.getElementById( "vt" ).style.display = renderer.supportsVertexTextures() ? "none" : "block";
  148. if ( statsEnabled ) {
  149. stats = new Stats();
  150. stats.domElement.style.position = 'absolute';
  151. stats.domElement.style.top = '0px';
  152. stats.domElement.style.zIndex = 100;
  153. container.appendChild( stats.domElement );
  154. }
  155. }
  156. function createScene( geometry, scale, material1, material2 ) {
  157. geometry.computeTangents();
  158. mesh1 = new THREE.Mesh( geometry, material1 );
  159. mesh1.position.x = - scale * 12;
  160. mesh1.scale.set( scale, scale, scale );
  161. mesh1.castShadow = true;
  162. mesh1.receiveShadow = true;
  163. scene.add( mesh1 );
  164. mesh2 = new THREE.Mesh( geometry, material2 );
  165. mesh2.position.x = scale * 12;
  166. mesh2.scale.set( scale, scale, scale );
  167. mesh2.castShadow = true;
  168. mesh2.receiveShadow = true;
  169. scene.add( mesh2 );
  170. loader.statusDomElement.style.display = "none";
  171. }
  172. function onDocumentMouseMove(event) {
  173. mouseX = ( event.clientX - windowHalfX ) * 10;
  174. mouseY = ( event.clientY - windowHalfY ) * 10;
  175. }
  176. //
  177. function animate() {
  178. requestAnimationFrame( animate );
  179. render();
  180. if ( statsEnabled ) stats.update();
  181. }
  182. function render() {
  183. var ry = mouseX * 0.0003, rx = mouseY * 0.0003;
  184. if( mesh1 ) {
  185. mesh1.rotation.y = ry;
  186. mesh1.rotation.x = rx;
  187. }
  188. if( mesh2 ) {
  189. mesh2.rotation.y = ry;
  190. mesh2.rotation.x = rx;
  191. }
  192. lightMesh.position.x = 2500 * Math.cos( r );
  193. lightMesh.position.z = 2500 * Math.sin( r );
  194. r += 0.01;
  195. renderer.render( scene, camera );
  196. }
  197. </script>
  198. </body>
  199. </html>