webgl_materials_normalmap2.html 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. <!DOCTYPE HTML>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - materials - normal map [Lee Perry-Smith]</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 normalmap demo.
  40. <a href="http://www.ir-ltd.net/infinite-3d-head-scan-released/" target="_blank">Lee Perry-Smith</a> head.
  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> or Firefox 4<br/>
  43. please star this <a href="http://code.google.com/p/chromium/issues/detail?id=52497">Chrome issue</a> to get ANGLE support
  44. </div>
  45. </div>
  46. <script type="text/javascript" src="../build/Three.js"></script>
  47. <script type="text/javascript" src="js/Detector.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 ( ! Detector.webgl ) 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;
  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. init();
  63. animate();
  64. function init() {
  65. container = document.createElement('div');
  66. document.body.appendChild(container);
  67. camera = new THREE.Camera( 50, window.innerWidth / window.innerHeight, 1, 10000 );
  68. camera.position.z = 900;
  69. scene = new THREE.Scene();
  70. // LIGHTS
  71. ambientLight = new THREE.AmbientLight( 0x444444 );
  72. scene.addLight( ambientLight );
  73. pointLight = new THREE.PointLight( 0xffffff );
  74. pointLight.position.z = 600;
  75. scene.addLight( pointLight );
  76. directionalLight = new THREE.DirectionalLight( 0xffffff );
  77. directionalLight.position.x = 1;
  78. directionalLight.position.y = 1;
  79. directionalLight.position.z = - 1;
  80. directionalLight.position.normalize();
  81. scene.addLight( directionalLight );
  82. // material parameters
  83. var ambient = 0x444444, diffuse = 0x888888, specular = 0x080810, shininess = 2;
  84. var shader = THREE.ShaderUtils.lib[ "normal" ];
  85. var uniforms = THREE.UniformsUtils.clone( shader.uniforms );
  86. uniforms[ "tNormal" ].texture = THREE.ImageUtils.loadTexture( "obj/leeperrysmith/Infinite-Level_02_Tangent_SmoothUV.jpg" );
  87. uniforms[ "uNormalScale" ].value = - 0.75;
  88. uniforms[ "tDiffuse" ].texture = THREE.ImageUtils.loadTexture( "obj/leeperrysmith/Map-COL.jpg" );
  89. uniforms[ "enableAO" ].value = false;
  90. uniforms[ "enableDiffuse" ].value = true;
  91. uniforms[ "enableSpecular" ].value = false;
  92. uniforms[ "uPointLightPos" ].value = pointLight.position;
  93. uniforms[ "uPointLightColor" ].value = pointLight.color;
  94. uniforms[ "uDirLightPos" ].value = directionalLight.position;
  95. uniforms[ "uDirLightColor" ].value = directionalLight.color;
  96. uniforms[ "uAmbientLightColor" ].value = ambientLight.color;
  97. uniforms[ "uDiffuseColor" ].value.setHex( diffuse );
  98. uniforms[ "uSpecularColor" ].value.setHex( specular );
  99. uniforms[ "uAmbientColor" ].value.setHex( ambient );
  100. uniforms[ "uShininess" ].value = shininess;
  101. var parameters = { fragmentShader: shader.fragmentShader, vertexShader: shader.vertexShader, uniforms: uniforms };
  102. var material = new THREE.MeshShaderMaterial( parameters );
  103. loader = new THREE.JSONLoader( true );
  104. document.body.appendChild( loader.statusDomElement );
  105. loader.load( { model: "obj/leeperrysmith/LeePerrySmith.js", callback: function( geometry ) { createScene( geometry, 100, material ) } } );
  106. webglRenderer = new THREE.WebGLRenderer();
  107. webglRenderer.setSize( window.innerWidth, window.innerHeight );
  108. container.appendChild( webglRenderer.domElement );
  109. if ( statsEnabled ) {
  110. stats = new Stats();
  111. stats.domElement.style.position = 'absolute';
  112. stats.domElement.style.top = '0px';
  113. stats.domElement.style.zIndex = 100;
  114. container.appendChild( stats.domElement );
  115. }
  116. document.addEventListener( 'mousemove', onDocumentMouseMove, false );
  117. }
  118. function createScene( geometry, scale, material ) {
  119. geometry.computeTangents();
  120. mesh1 = THREE.SceneUtils.addMesh( scene, geometry, scale, 0, - 50, 0, 0, 0, 0, material );
  121. loader.statusDomElement.style.display = "none";
  122. }
  123. function onDocumentMouseMove(event) {
  124. mouseX = ( event.clientX - windowHalfX ) * 10;
  125. mouseY = ( event.clientY - windowHalfY ) * 10;
  126. }
  127. //
  128. function animate() {
  129. requestAnimationFrame( animate );
  130. render();
  131. if ( statsEnabled ) stats.update();
  132. }
  133. function render() {
  134. var ry = mouseX * 0.0003, rx = mouseY * 0.0003;
  135. if( mesh1 ) {
  136. mesh1.rotation.y = ry;
  137. mesh1.rotation.x = rx;
  138. }
  139. webglRenderer.render( scene, camera );
  140. }
  141. function log( text ) {
  142. var e = document.getElementById("log");
  143. e.innerHTML = text + "<br/>" + e.innerHTML;
  144. }
  145. </script>
  146. </body>
  147. </html>