webgl_materials_normalmap2.html 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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.add( ambientLight );
  73. pointLight = new THREE.PointLight( 0xffffff );
  74. pointLight.position.z = 600;
  75. scene.add( 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.add( 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[ "uDiffuseColor" ].value.setHex( diffuse );
  93. uniforms[ "uSpecularColor" ].value.setHex( specular );
  94. uniforms[ "uAmbientColor" ].value.setHex( ambient );
  95. uniforms[ "uShininess" ].value = shininess;
  96. var parameters = { fragmentShader: shader.fragmentShader, vertexShader: shader.vertexShader, uniforms: uniforms, lights: true };
  97. var material = new THREE.MeshShaderMaterial( parameters );
  98. loader = new THREE.JSONLoader( true );
  99. document.body.appendChild( loader.statusDomElement );
  100. loader.load( { model: "obj/leeperrysmith/LeePerrySmith.js", callback: function( geometry ) { createScene( geometry, 100, material ) } } );
  101. webglRenderer = new THREE.WebGLRenderer();
  102. webglRenderer.setSize( window.innerWidth, window.innerHeight );
  103. container.appendChild( webglRenderer.domElement );
  104. if ( statsEnabled ) {
  105. stats = new Stats();
  106. stats.domElement.style.position = 'absolute';
  107. stats.domElement.style.top = '0px';
  108. stats.domElement.style.zIndex = 100;
  109. container.appendChild( stats.domElement );
  110. }
  111. document.addEventListener( 'mousemove', onDocumentMouseMove, false );
  112. }
  113. function createScene( geometry, scale, material ) {
  114. geometry.computeTangents();
  115. mesh1 = new THREE.Mesh( geometry, material );
  116. mesh1.position.y = - 50;
  117. mesh1.scale.x = mesh1.scale.y = mesh1.scale.z = scale;
  118. scene.add( mesh1 );
  119. loader.statusDomElement.style.display = "none";
  120. }
  121. function onDocumentMouseMove(event) {
  122. mouseX = ( event.clientX - windowHalfX ) * 10;
  123. mouseY = ( event.clientY - windowHalfY ) * 10;
  124. }
  125. //
  126. function animate() {
  127. requestAnimationFrame( animate );
  128. render();
  129. if ( statsEnabled ) stats.update();
  130. }
  131. function render() {
  132. var ry = mouseX * 0.0003, rx = mouseY * 0.0003;
  133. if( mesh1 ) {
  134. mesh1.rotation.y = ry;
  135. mesh1.rotation.x = rx;
  136. }
  137. webglRenderer.render( scene, camera );
  138. }
  139. function log( text ) {
  140. var e = document.getElementById("log");
  141. e.innerHTML = text + "<br/>" + e.innerHTML;
  142. }
  143. </script>
  144. </body>
  145. </html>