materials_normalmap.html 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. <!DOCTYPE HTML>
  2. <html lang="en">
  3. <head>
  4. <title>three.js - webgl 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. font-family:monospace;
  28. font-size:13px;
  29. text-align:center;
  30. background:rgb(200,100,0);
  31. color:#fff;
  32. padding:1em;
  33. width:475px;
  34. margin:5em auto 0;
  35. border:solid 2px #fff;
  36. border-radius:10px;
  37. display:none;
  38. }
  39. #vt { display:none }
  40. #vt, #vt a { color:orange; }
  41. .code { }
  42. #log { position:absolute; top:50px; text-align:left; display:block; z-index:100 }
  43. </style>
  44. </head>
  45. <body>
  46. <pre id="log"></pre>
  47. <div id="info">
  48. <a href="http://github.com/mrdoob/three.js" target="_blank">three.js</a> - webgl (<span id="description">normal + ao + displacement</span>) map demo.
  49. ninja head from <a href="http://developer.amd.com/archive/gpu/MeshMapper/pages/default.aspx" target="_blank">AMD GPU MeshMapper</a>
  50. <div id="vt">displacement mapping needs vertex textures (GPU with Shader Model 3.0)<br/>
  51. on Windows use <span class="code">Chrome --use-gl=desktop</span> or Firefox 4<br/>
  52. please star this <a href="http://code.google.com/p/chromium/issues/detail?id=52497">Chrome issue</a> to get ANGLE support
  53. </div>
  54. </div>
  55. <center>
  56. <div id="oldie">
  57. Sorry, your browser doesn't support <a href="http://khronos.org/webgl/wiki/Getting_a_WebGL_Implementation">WebGL</a>
  58. and <a href="http://www.whatwg.org/specs/web-workers/current-work/">Web Workers</a>.<br/>
  59. <br/>
  60. Please try in
  61. <a href="http://www.chromium.org/getting-involved/dev-channel">Chrome 9+</a> /
  62. <a href="http://www.mozilla.com/en-US/firefox/all-beta.html">Firefox 4+</a> /
  63. <a href="http://nightly.webkit.org/">Safari OSX 10.6+</a>
  64. </div>
  65. </center>
  66. <script type="text/javascript" src="../build/ThreeExtras.js"></script>
  67. <script type="text/javascript" src="js/Stats.js"></script>
  68. <script type="text/javascript">
  69. if ( !is_browser_compatible() ) {
  70. document.getElementById( "oldie" ).style.display = "block";
  71. }
  72. var statsEnabled = true;
  73. var container, stats, loader;
  74. var camera, scene, webglRenderer;
  75. var mesh, zmesh, lightMesh, geometry;
  76. var mesh1, mesh2;
  77. var directionalLight, pointLight, ambientLight;
  78. var mouseX = 0;
  79. var mouseY = 0;
  80. var windowHalfX = window.innerWidth / 2;
  81. var windowHalfY = window.innerHeight / 2;
  82. var r = 0.0;
  83. document.addEventListener( 'mousemove', onDocumentMouseMove, false );
  84. init();
  85. setInterval( loop, 1000 / 60 );
  86. function init() {
  87. container = document.createElement('div');
  88. document.body.appendChild(container);
  89. camera = new THREE.Camera( 60, window.innerWidth / window.innerHeight, 1, 100000 );
  90. camera.projectionMatrix = THREE.Matrix4.makeOrtho( window.innerWidth / - 2, window.innerWidth / 2, window.innerHeight / 2, window.innerHeight / - 2, -10000, 10000 );
  91. camera.position.z = 6200;
  92. scene = new THREE.Scene();
  93. // LIGHTS
  94. ambientLight = new THREE.AmbientLight( 0x111111 );
  95. scene.addLight( ambientLight );
  96. pointLight = new THREE.PointLight( 0xffff55 );
  97. pointLight.position.z = 10000;
  98. scene.addLight( pointLight );
  99. directionalLight = new THREE.DirectionalLight( 0xaaaa88 );
  100. directionalLight.position.x = 1;
  101. directionalLight.position.y = 1;
  102. directionalLight.position.z = 0.5;
  103. directionalLight.position.normalize();
  104. scene.addLight( directionalLight );
  105. // light representation
  106. var sphere = new Sphere( 100, 16, 8 );
  107. lightMesh = new THREE.Mesh( sphere, new THREE.MeshBasicMaterial( { color:0xffaa00 } ) );
  108. lightMesh.position = pointLight.position;
  109. lightMesh.scale.x = lightMesh.scale.y = lightMesh.scale.z = 0.05;
  110. scene.addObject(lightMesh);
  111. // common material parameters
  112. var ambient = 0x050505, diffuse = 0x555555, specular = 0xaa6600, shininess = 10, scale = 23;
  113. // normal map shader
  114. var shader = ShaderUtils.lib[ "normal" ];
  115. var uniforms = Uniforms.clone( shader.uniforms );
  116. uniforms[ "enableAO" ].value = true;
  117. uniforms[ "enableDiffuse" ].value = false;
  118. uniforms[ "tNormal" ].texture = ImageUtils.loadTexture( "textures/normal/ninja/normal.jpg" );
  119. uniforms[ "tAO" ].texture = ImageUtils.loadTexture( "textures/normal/ninja/ao.jpg" );
  120. uniforms[ "tDisplacement" ].texture = ImageUtils.loadTexture( "textures/normal/ninja/displacement.jpg" );
  121. uniforms[ "uDisplacementBias" ].value = -0.428408 * scale;
  122. uniforms[ "uDisplacementScale" ].value = 2.436143 * scale;
  123. uniforms[ "uPointLightPos" ].value = pointLight.position;
  124. uniforms[ "uPointLightColor" ].value = pointLight.color;
  125. uniforms[ "uDirLightPos" ].value = directionalLight.position;
  126. uniforms[ "uDirLightColor" ].value = directionalLight.color;
  127. uniforms[ "uAmbientLightColor" ].value = ambientLight.color;
  128. uniforms[ "uDiffuseColor" ].value.setHex( diffuse );
  129. uniforms[ "uSpecularColor" ].value.setHex( specular );
  130. uniforms[ "uAmbientColor" ].value.setHex( ambient );
  131. uniforms[ "uShininess" ].value = shininess;
  132. var parameters = { fragment_shader: shader.fragment_shader, vertex_shader: shader.vertex_shader, uniforms: uniforms };
  133. var material1 = new THREE.MeshShaderMaterial( parameters );
  134. var material2 = new THREE.MeshPhongMaterial( { color: diffuse, specular: specular, ambient: ambient, shininess: shininess } );
  135. loader = new THREE.Loader( true );
  136. document.body.appendChild( loader.statusDomElement );
  137. loader.loadBinary( "obj/ninja/NinjaLo_bin.js", function( geometry ) { createScene( geometry, scale, material1, material2 ) }, "obj/ninja" );
  138. webglRenderer = new THREE.WebGLRenderer();
  139. webglRenderer.setSize( window.innerWidth, window.innerHeight );
  140. container.appendChild( webglRenderer.domElement );
  141. var description = "normal + ao" + ( webglRenderer.supportsVertexTextures() ? " + displacement" : " + <strike>displacement</strike>" );
  142. document.getElementById( "description" ).innerHTML = description;
  143. document.getElementById( "vt" ).style.display = webglRenderer.supportsVertexTextures() ? "none" : "block";
  144. if ( statsEnabled ) {
  145. stats = new Stats();
  146. stats.domElement.style.position = 'absolute';
  147. stats.domElement.style.top = '0px';
  148. stats.domElement.style.zIndex = 100;
  149. container.appendChild( stats.domElement );
  150. }
  151. }
  152. function createScene( geometry, scale, material1, material2 ) {
  153. geometry.computeTangents();
  154. mesh1 = SceneUtils.addMesh( scene, geometry, scale, -scale * 12, 0, 0, 0,0,0, material1 );
  155. mesh2 = SceneUtils.addMesh( scene, geometry, scale, scale * 12, 0, 0, 0,0,0, material2 );
  156. loader.statusDomElement.style.display = "none";
  157. }
  158. function onDocumentMouseMove(event) {
  159. mouseX = ( event.clientX - windowHalfX ) * 10;
  160. mouseY = ( event.clientY - windowHalfY ) * 10;
  161. }
  162. function loop() {
  163. var ry = mouseX * 0.0003, rx = mouseY * 0.0003;
  164. if( mesh1 ) {
  165. mesh1.rotation.y = ry;
  166. mesh1.rotation.x = rx;
  167. }
  168. if( mesh2 ) {
  169. mesh2.rotation.y = ry;
  170. mesh2.rotation.x = rx;
  171. }
  172. lightMesh.position.x = 2500 * Math.cos( r );
  173. lightMesh.position.z = 2500 * Math.sin( r );
  174. r += 0.01;
  175. webglRenderer.render( scene, camera );
  176. if ( statsEnabled ) stats.update();
  177. }
  178. function log( text ) {
  179. var e = document.getElementById("log");
  180. e.innerHTML = text + "<br/>" + e.innerHTML;
  181. }
  182. function is_browser_compatible() {
  183. // WebGL support
  184. try { var test = new Float32Array(1); } catch(e) { return false; }
  185. // Web workers
  186. return !!window.Worker;
  187. }
  188. </script>
  189. </body>
  190. </html>