webgl_materials_normalmap.html 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. <!DOCTYPE HTML>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - materials - normal map</title>
  5. <meta charset="utf-8">
  6. <style type="text/css">
  7. body {
  8. background:#000;
  9. color:#fff;
  10. padding:0;
  11. margin:0;
  12. font-weight: bold;
  13. overflow:hidden;
  14. }
  15. a { color: #ffffff; }
  16. #info {
  17. position: absolute;
  18. top: 0px; width: 100%;
  19. color: #ffffff;
  20. padding: 5px;
  21. font-family:Monospace;
  22. font-size:13px;
  23. text-align:center;
  24. z-index:1000;
  25. }
  26. #oldie {
  27. background:rgb(200,100,0) !important;
  28. color:#fff;
  29. }
  30. #vt { display:none }
  31. #vt, #vt a { color:orange; }
  32. .code { }
  33. #log { position:absolute; top:50px; text-align:left; display:block; z-index:100 }
  34. </style>
  35. </head>
  36. <body>
  37. <pre id="log"></pre>
  38. <div id="info">
  39. <a href="http://github.com/mrdoob/three.js" target="_blank">three.js</a> - webgl (<span id="description">normal + ao + displacement</span>) map demo.
  40. ninja head from <a href="http://developer.amd.com/archive/gpu/MeshMapper/pages/default.aspx" target="_blank">AMD GPU MeshMapper</a>
  41. <div id="vt">displacement mapping needs vertex textures (GPU with Shader Model 3.0)<br/>
  42. on Windows use <span class="code">Chrome --use-gl=desktop</span> <br/>
  43. or Firefox 4 (about:config => webgl.mochitest_native_gl=true)<br/>
  44. please star this <a href="http://code.google.com/p/chromium/issues/detail?id=52497">Chrome issue</a> to get ANGLE support
  45. </div>
  46. </div>
  47. <script type="text/javascript" src="../build/ThreeExtras.js"></script>
  48. <script type="text/javascript" src="js/RequestAnimationFrame.js"></script>
  49. <script type="text/javascript" src="js/Stats.js"></script>
  50. <script type="text/javascript">
  51. if ( ! THREE.Detector.webgl ) THREE.Detector.addGetWebGLMessage();
  52. var statsEnabled = true;
  53. var container, stats, loader;
  54. var camera, scene, webglRenderer;
  55. var mesh, zmesh, lightMesh, geometry;
  56. var mesh1, mesh2;
  57. var directionalLight, pointLight, ambientLight;
  58. var mouseX = 0;
  59. var mouseY = 0;
  60. var windowHalfX = window.innerWidth / 2;
  61. var windowHalfY = window.innerHeight / 2;
  62. var r = 0.0;
  63. document.addEventListener( 'mousemove', onDocumentMouseMove, false );
  64. init();
  65. animate();
  66. function init() {
  67. container = document.createElement('div');
  68. document.body.appendChild(container);
  69. camera = new THREE.Camera( 60, window.innerWidth / window.innerHeight, 1, 100000 );
  70. camera.projectionMatrix = THREE.Matrix4.makeOrtho( window.innerWidth / - 2, window.innerWidth / 2, window.innerHeight / 2, window.innerHeight / - 2, -10000, 10000 );
  71. camera.position.z = 6200;
  72. scene = new THREE.Scene();
  73. // LIGHTS
  74. ambientLight = new THREE.AmbientLight( 0x111111 );
  75. scene.addLight( ambientLight );
  76. pointLight = new THREE.PointLight( 0xffff55 );
  77. pointLight.position.z = 10000;
  78. scene.addLight( pointLight );
  79. directionalLight = new THREE.DirectionalLight( 0xaaaa88 );
  80. directionalLight.position.x = 1;
  81. directionalLight.position.y = 1;
  82. directionalLight.position.z = 0.5;
  83. directionalLight.position.normalize();
  84. scene.addLight( directionalLight );
  85. // light representation
  86. var sphere = new Sphere( 100, 16, 8 );
  87. lightMesh = new THREE.Mesh( sphere, new THREE.MeshBasicMaterial( { color:0xffaa00 } ) );
  88. lightMesh.position = pointLight.position;
  89. lightMesh.scale.x = lightMesh.scale.y = lightMesh.scale.z = 0.05;
  90. scene.addObject(lightMesh);
  91. // common material parameters
  92. var ambient = 0x050505, diffuse = 0x555555, specular = 0xaa6600, shininess = 10, scale = 23;
  93. // normal map shader
  94. var shader = ShaderUtils.lib[ "normal" ];
  95. var uniforms = Uniforms.clone( shader.uniforms );
  96. uniforms[ "enableAO" ].value = true;
  97. uniforms[ "enableDiffuse" ].value = false;
  98. uniforms[ "tNormal" ].texture = ImageUtils.loadTexture( "textures/normal/ninja/normal.jpg" );
  99. uniforms[ "tAO" ].texture = ImageUtils.loadTexture( "textures/normal/ninja/ao.jpg" );
  100. uniforms[ "tDisplacement" ].texture = ImageUtils.loadTexture( "textures/normal/ninja/displacement.jpg" );
  101. uniforms[ "uDisplacementBias" ].value = -0.428408 * scale;
  102. uniforms[ "uDisplacementScale" ].value = 2.436143 * scale;
  103. uniforms[ "uPointLightPos" ].value = pointLight.position;
  104. uniforms[ "uPointLightColor" ].value = pointLight.color;
  105. uniforms[ "uDirLightPos" ].value = directionalLight.position;
  106. uniforms[ "uDirLightColor" ].value = directionalLight.color;
  107. uniforms[ "uAmbientLightColor" ].value = ambientLight.color;
  108. uniforms[ "uDiffuseColor" ].value.setHex( diffuse );
  109. uniforms[ "uSpecularColor" ].value.setHex( specular );
  110. uniforms[ "uAmbientColor" ].value.setHex( ambient );
  111. uniforms[ "uShininess" ].value = shininess;
  112. var parameters = { fragment_shader: shader.fragment_shader, vertex_shader: shader.vertex_shader, uniforms: uniforms };
  113. var material1 = new THREE.MeshShaderMaterial( parameters );
  114. var material2 = new THREE.MeshPhongMaterial( { color: diffuse, specular: specular, ambient: ambient, shininess: shininess } );
  115. loader = new THREE.Loader( true );
  116. document.body.appendChild( loader.statusDomElement );
  117. loader.loadBinary( { model: "obj/ninja/NinjaLo_bin.js", callback: function( geometry ) { createScene( geometry, scale, material1, material2 ) } } );
  118. webglRenderer = new THREE.WebGLRenderer();
  119. webglRenderer.setSize( window.innerWidth, window.innerHeight );
  120. container.appendChild( webglRenderer.domElement );
  121. var description = "normal + ao" + ( webglRenderer.supportsVertexTextures() ? " + displacement" : " + <strike>displacement</strike>" );
  122. document.getElementById( "description" ).innerHTML = description;
  123. document.getElementById( "vt" ).style.display = webglRenderer.supportsVertexTextures() ? "none" : "block";
  124. if ( statsEnabled ) {
  125. stats = new Stats();
  126. stats.domElement.style.position = 'absolute';
  127. stats.domElement.style.top = '0px';
  128. stats.domElement.style.zIndex = 100;
  129. container.appendChild( stats.domElement );
  130. }
  131. }
  132. function createScene( geometry, scale, material1, material2 ) {
  133. geometry.computeTangents();
  134. mesh1 = SceneUtils.addMesh( scene, geometry, scale, -scale * 12, 0, 0, 0,0,0, material1 );
  135. mesh2 = SceneUtils.addMesh( scene, geometry, scale, scale * 12, 0, 0, 0,0,0, material2 );
  136. loader.statusDomElement.style.display = "none";
  137. }
  138. function onDocumentMouseMove(event) {
  139. mouseX = ( event.clientX - windowHalfX ) * 10;
  140. mouseY = ( event.clientY - windowHalfY ) * 10;
  141. }
  142. //
  143. function animate() {
  144. requestAnimationFrame( animate );
  145. render();
  146. if ( statsEnabled ) stats.update();
  147. }
  148. function render() {
  149. var ry = mouseX * 0.0003, rx = mouseY * 0.0003;
  150. if( mesh1 ) {
  151. mesh1.rotation.y = ry;
  152. mesh1.rotation.x = rx;
  153. }
  154. if( mesh2 ) {
  155. mesh2.rotation.y = ry;
  156. mesh2.rotation.x = rx;
  157. }
  158. lightMesh.position.x = 2500 * Math.cos( r );
  159. lightMesh.position.z = 2500 * Math.sin( r );
  160. r += 0.01;
  161. webglRenderer.render( scene, camera );
  162. }
  163. function log( text ) {
  164. var e = document.getElementById("log");
  165. e.innerHTML = text + "<br/>" + e.innerHTML;
  166. }
  167. </script>
  168. </body>
  169. </html>