webgl_materials_normaldisplacementmap.html 8.3 KB

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