webgl_materials_normalmap.html 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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://threejs.org" 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.min.js"></script>
  42. <script src="js/loaders/BinaryLoader.js"></script>
  43. <script src="js/Detector.js"></script>
  44. <script src="js/libs/stats.min.js"></script>
  45. <script>
  46. if ( ! Detector.webgl ) Detector.addGetWebGLMessage();
  47. var statsEnabled = true;
  48. var container, stats, loader;
  49. var camera, scene, renderer;
  50. var mesh, zmesh, lightMesh, geometry;
  51. var mesh1, mesh2;
  52. var spotLight, pointLight, ambientLight;
  53. var mouseX = 0;
  54. var mouseY = 0;
  55. var windowHalfX = window.innerWidth / 2;
  56. var windowHalfY = window.innerHeight / 2;
  57. var r = 0.0;
  58. document.addEventListener( 'mousemove', onDocumentMouseMove, false );
  59. init();
  60. animate();
  61. function init() {
  62. container = document.createElement( 'div' );
  63. document.body.appendChild( container );
  64. camera = new THREE.OrthographicCamera( window.innerWidth / - 2, window.innerWidth / 2, window.innerHeight / 2, window.innerHeight / - 2, -10000, 10000 );
  65. camera.position.z = 1500;
  66. scene = new THREE.Scene();
  67. // LIGHTS
  68. ambientLight = new THREE.AmbientLight( 0x111111 );
  69. scene.add( ambientLight );
  70. pointLight = new THREE.PointLight( 0xff0000 );
  71. pointLight.position.z = 10000;
  72. pointLight.distance = 4000;
  73. scene.add( pointLight );
  74. pointLight2 = new THREE.PointLight( 0xff5500 );
  75. pointLight2.position.z = 1000;
  76. pointLight2.distance = 2000;
  77. scene.add( pointLight2 );
  78. pointLight3 = new THREE.PointLight( 0x0000ff );
  79. pointLight3.position.x = -1000;
  80. pointLight3.position.z = 1000;
  81. pointLight3.distance = 2000;
  82. scene.add( pointLight3 );
  83. spotLight = new THREE.SpotLight( 0xaaaaaa );
  84. spotLight.position.set( 1000, 500, 1000 );
  85. spotLight.castShadow = true;
  86. spotLight.shadowCameraNear = 500;
  87. spotLight.shadowCameraFov = 70;
  88. spotLight.shadowBias = 0.001;
  89. spotLight.shadowMapWidth = 1024;
  90. spotLight.shadowMapHeight = 1024;
  91. scene.add( spotLight );
  92. directionalLight2 = new THREE.DirectionalLight( 0xaaff33, 0 );
  93. directionalLight2.position.set( -1, 1, 0.5 ).normalize();
  94. //scene.add( directionalLight2 );
  95. directionalLight3 = new THREE.DirectionalLight( 0xaaff33 );
  96. directionalLight3.position.set( -1, 1, 0.5 ).normalize();
  97. //scene.add( directionalLight3 );
  98. // light representation
  99. var sphere = new THREE.SphereGeometry( 100, 16, 8 );
  100. lightMesh = new THREE.Mesh( sphere, new THREE.MeshBasicMaterial( { color: 0xffaa00 } ) );
  101. lightMesh.position = pointLight.position;
  102. lightMesh.scale.x = lightMesh.scale.y = lightMesh.scale.z = 0.05;
  103. //scene.add( lightMesh );
  104. var path = "textures/cube/SwedishRoyalCastle/";
  105. var format = '.jpg';
  106. var urls = [
  107. path + 'px' + format, path + 'nx' + format,
  108. path + 'py' + format, path + 'ny' + format,
  109. path + 'pz' + format, path + 'nz' + format
  110. ];
  111. var reflectionCube = THREE.ImageUtils.loadTextureCube( urls );
  112. // common material parameters
  113. var ambient = 0x050505, diffuse = 0x331100, specular = 0xffffff, shininess = 10, scale = 23;
  114. // normal map shader
  115. var shader = THREE.ShaderLib[ "normalmap" ];
  116. var uniforms = THREE.UniformsUtils.clone( shader.uniforms );
  117. uniforms[ "enableAO" ].value = true;
  118. uniforms[ "enableDiffuse" ].value = false;
  119. uniforms[ "enableSpecular" ].value = false;
  120. uniforms[ "enableReflection" ].value = true;
  121. uniforms[ "enableDisplacement" ].value = true;
  122. uniforms[ "tNormal" ].value = THREE.ImageUtils.loadTexture( "textures/normal/ninja/normal.jpg" );
  123. uniforms[ "tAO" ].value = THREE.ImageUtils.loadTexture( "textures/normal/ninja/ao.jpg" );
  124. uniforms[ "tDisplacement" ].value = THREE.ImageUtils.loadTexture( "textures/normal/ninja/displacement.jpg" );
  125. uniforms[ "uDisplacementBias" ].value = - 0.428408;
  126. uniforms[ "uDisplacementScale" ].value = 2.436143;
  127. uniforms[ "uNormalScale" ].value.y = -1;
  128. uniforms[ "diffuse" ].value.setHex( diffuse );
  129. uniforms[ "specular" ].value.setHex( specular );
  130. uniforms[ "ambient" ].value.setHex( ambient );
  131. uniforms[ "shininess" ].value = shininess;
  132. uniforms[ "tCube" ].value = reflectionCube;
  133. uniforms[ "reflectivity" ].value = 0.1;
  134. uniforms[ "diffuse" ].value.convertGammaToLinear();
  135. uniforms[ "specular" ].value.convertGammaToLinear();
  136. uniforms[ "ambient" ].value.convertGammaToLinear();
  137. var parameters = { fragmentShader: shader.fragmentShader, vertexShader: shader.vertexShader, uniforms: uniforms, lights: true, fog: false };
  138. var material1 = new THREE.ShaderMaterial( parameters );
  139. var material2 = new THREE.MeshPhongMaterial( {
  140. color: diffuse,
  141. specular: specular,
  142. ambient: ambient,
  143. shininess: shininess,
  144. normalMap: uniforms[ "tNormal" ].value,
  145. normalScale: uniforms[ "uNormalScale" ].value,
  146. envMap: reflectionCube,
  147. combine: THREE.MixOperation,
  148. reflectivity: 0.1
  149. } );
  150. //
  151. loader = new THREE.BinaryLoader( true );
  152. document.body.appendChild( loader.statusDomElement );
  153. loader.load( "obj/ninja/NinjaLo_bin.js", function( geometry ) { createScene( geometry, scale, material1, material2 ) } );
  154. //
  155. renderer = new THREE.WebGLRenderer();
  156. renderer.setSize( window.innerWidth, window.innerHeight );
  157. container.appendChild( renderer.domElement );
  158. //
  159. renderer.gammaInput = true;
  160. renderer.gammaOutput = true;
  161. //
  162. renderer.shadowMapEnabled = true;
  163. renderer.shadowMapType = THREE.PCFShadowMap;
  164. //
  165. var description = "normal + ao" + ( renderer.supportsVertexTextures() ? " + displacement + environment + shadow" : " + <strike>displacement</strike>" );
  166. document.getElementById( "description" ).innerHTML = description;
  167. document.getElementById( "vt" ).style.display = renderer.supportsVertexTextures() ? "none" : "block";
  168. if ( statsEnabled ) {
  169. stats = new Stats();
  170. stats.domElement.style.position = 'absolute';
  171. stats.domElement.style.top = '0px';
  172. stats.domElement.style.zIndex = 100;
  173. container.appendChild( stats.domElement );
  174. }
  175. //
  176. window.addEventListener( 'resize', onWindowResize, false );
  177. }
  178. function onWindowResize() {
  179. windowHalfX = window.innerWidth / 2;
  180. windowHalfY = window.innerHeight / 2;
  181. camera.left = window.innerWidth / - 2;
  182. camera.right = window.innerWidth / 2;
  183. camera.top = window.innerHeight / 2;
  184. camera.bottom = window.innerHeight / - 2;
  185. camera.updateProjectionMatrix();
  186. renderer.setSize( window.innerWidth, window.innerHeight );
  187. }
  188. function createScene( geometry, scale, material1, material2 ) {
  189. geometry.computeTangents();
  190. mesh1 = new THREE.Mesh( geometry, material1 );
  191. mesh1.position.x = - scale * 12;
  192. mesh1.scale.set( scale, scale, scale );
  193. mesh1.castShadow = true;
  194. mesh1.receiveShadow = true;
  195. scene.add( mesh1 );
  196. mesh2 = new THREE.Mesh( geometry, material2 );
  197. mesh2.position.x = scale * 12;
  198. mesh2.scale.set( scale, scale, scale );
  199. mesh2.castShadow = true;
  200. mesh2.receiveShadow = true;
  201. scene.add( mesh2 );
  202. loader.statusDomElement.style.display = "none";
  203. }
  204. function onDocumentMouseMove(event) {
  205. mouseX = ( event.clientX - windowHalfX ) * 10;
  206. mouseY = ( event.clientY - windowHalfY ) * 10;
  207. }
  208. //
  209. function animate() {
  210. requestAnimationFrame( animate );
  211. render();
  212. if ( statsEnabled ) stats.update();
  213. }
  214. function render() {
  215. var ry = mouseX * 0.0003, rx = mouseY * 0.0003;
  216. if( mesh1 ) {
  217. mesh1.rotation.y = ry;
  218. mesh1.rotation.x = rx;
  219. }
  220. if( mesh2 ) {
  221. mesh2.rotation.y = ry;
  222. mesh2.rotation.x = rx;
  223. }
  224. lightMesh.position.x = 2500 * Math.cos( r );
  225. lightMesh.position.z = 2500 * Math.sin( r );
  226. r += 0.01;
  227. renderer.render( scene, camera );
  228. }
  229. </script>
  230. </body>
  231. </html>