webgl_materials_normaldisplacementmap.html 7.3 KB

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