webgl_materials_channels.html 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - materials - displacement 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. }
  26. #vt { display:none }
  27. #vt, #vt a { color:orange; }
  28. #stats { position: absolute; top:0; left: 0 }
  29. #stats #fps { background: transparent !important }
  30. #stats #fps #fpsText { color: #aaa !important }
  31. #stats #fps #fpsGraph { display: none }
  32. </style>
  33. </head>
  34. <body>
  35. <div id="info">
  36. <a href="http://threejs.org" target="_blank">three.js</a> - <span id="description">Normal, Depth, DepthRGBA, DepthRGBAUnpacked, Materials</span> by <a href="https://Clara.io">Ben Houston</a>.<br />
  37. ninja head from <a href="http://developer.amd.com/tools-and-sdks/archive/legacy-cpu-gpu-tools/amd-gpu-meshmapper/" target="_blank">AMD GPU MeshMapper</a>
  38. <div id="vt">displacement mapping requires vertex textures</div>
  39. </div>
  40. <script src="../build/three.min.js"></script>
  41. <script src="js/controls/OrbitControls.js"></script>
  42. <script src="js/loaders/BinaryLoader.js"></script>
  43. <script src="js/shaders/DepthRGBAUnpackedShader.js"></script>
  44. <script src="js/Detector.js"></script>
  45. <script src="js/libs/stats.min.js"></script>
  46. <script src='js/libs/dat.gui.min.js'></script>
  47. <script>
  48. if ( ! Detector.webgl ) Detector.addGetWebGLMessage();
  49. var stats, loader;
  50. var camera, scene, renderer, controls;
  51. var params = {
  52. material: 'normal',
  53. camera: 'perspective'
  54. };
  55. var cameraOrtho, cameraPerspective;
  56. var controlsOrtho, controlsPerspective;
  57. var mesh, materialStandard, materialDepth, materialDepthRGBA, materialDepthRGBAUnpacked, materialNormal;
  58. var pointLight, ambientLight;
  59. var mouseX = 0;
  60. var mouseY = 0;
  61. var windowHalfX = window.innerWidth / 2;
  62. var windowHalfY = window.innerHeight / 2;
  63. var height = 500; // of camera frustum
  64. var r = 0.0;
  65. init();
  66. animate();
  67. initGui();
  68. // Init gui
  69. function initGui() {
  70. var gui = new dat.GUI();
  71. gui.add( params, 'material', [ 'standard', 'normal', 'depth', 'depthRGBA', 'depthRGBAUnpacked' ] );
  72. gui.add( params, 'camera', [ 'perspective', 'ortho' ] );
  73. }
  74. function init() {
  75. var container = document.createElement( 'div' );
  76. document.body.appendChild( container );
  77. renderer = new THREE.WebGLRenderer();
  78. renderer.setPixelRatio( window.devicePixelRatio );
  79. renderer.setSize( window.innerWidth, window.innerHeight );
  80. container.appendChild( renderer.domElement );
  81. //
  82. scene = new THREE.Scene();
  83. var aspect = window.innerWidth / window.innerHeight;
  84. cameraPerspective = new THREE.PerspectiveCamera( 45, aspect, 1000, 2500 );
  85. cameraPerspective.position.z = 1500;
  86. scene.add( cameraPerspective );
  87. cameraOrtho = new THREE.OrthographicCamera( - height * aspect, height * aspect, height, - height, 1000, 2500 );
  88. cameraOrtho.position.z = 1500;
  89. scene.add( cameraOrtho );
  90. camera = cameraPerspective;
  91. controlsPerspective = new THREE.OrbitControls( cameraPerspective, renderer.domElement );
  92. controlsPerspective.enableZoom = true;
  93. controlsPerspective.enableDamping = true;
  94. controlsOrtho = new THREE.OrbitControls( cameraOrtho, renderer.domElement );
  95. controlsOrtho.enableZoom = true;
  96. controlsOrtho.enableDamping = true;
  97. // lights
  98. var ambientLight = new THREE.AmbientLight( 0xffffff, 0.1 );
  99. scene.add( ambientLight );
  100. var pointLight = new THREE.PointLight( 0xff0000, 0.5 );
  101. pointLight.position.z = 2500;
  102. scene.add( pointLight );
  103. var pointLight2 = new THREE.PointLight( 0xff6666, 1 );
  104. camera.add( pointLight2 );
  105. var pointLight3 = new THREE.PointLight( 0x0000ff, 0.5 );
  106. pointLight3.position.x = - 1000;
  107. pointLight3.position.z = 1000;
  108. scene.add( pointLight3 );
  109. // textures
  110. var textureLoader = new THREE.TextureLoader();
  111. var normalMap = textureLoader.load( "textures/normal/ninja/normal.jpg" );
  112. var aoMap = textureLoader.load( "textures/normal/ninja/ao.jpg" );
  113. var displacementMap = textureLoader.load( "textures/normal/ninja/displacement.jpg" );
  114. // material
  115. materialStandard = new THREE.MeshStandardMaterial( { color: 0xffffff });
  116. materialStandard.metalness = 0;
  117. materialStandard.roughness = 0.6;
  118. materialStandard.displacementMap = displacementMap;
  119. materialStandard.displacementScale = 1.5;
  120. materialStandard.aoMap = aoMap;
  121. materialStandard.normalMap = normalMap;
  122. materialStandard.normalScale.set( 1, - 1 );
  123. materialDepth = new THREE.MeshDepthMaterial();
  124. materialDepthRGBA = new THREE.ShaderMaterial( THREE.ShaderLib.depthRGBA );
  125. materialDepthRGBAUnpacked = new THREE.ShaderMaterial( THREE.DepthRGBAUnpackedShader );
  126. materialNormal = new THREE.MeshNormalMaterial();
  127. //
  128. loader = new THREE.BinaryLoader();
  129. loader.load( "obj/ninja/NinjaLo_bin.js", function( geometry ) {
  130. geometry.faceVertexUvs[ 1 ] = geometry.faceVertexUvs[ 0 ]; // 2nd set of UVs required for aoMap
  131. mesh = new THREE.Mesh( geometry, materialNormal );
  132. mesh.scale.multiplyScalar( 25 );
  133. scene.add( mesh );
  134. } );
  135. //
  136. stats = new Stats();
  137. container.appendChild( stats.domElement );
  138. //
  139. window.addEventListener( 'resize', onWindowResize, false );
  140. }
  141. function onWindowResize() {
  142. var aspect = window.innerWidth / window.innerHeight;
  143. camera.left = - height * aspect;
  144. camera.right = height * aspect;
  145. camera.top = height;
  146. camera.bottom = - height;
  147. camera.updateProjectionMatrix();
  148. renderer.setSize( window.innerWidth, window.innerHeight );
  149. }
  150. //
  151. function animate() {
  152. requestAnimationFrame( animate );
  153. controlsOrtho.update();
  154. controlsPerspective.update();
  155. stats.begin();
  156. render();
  157. stats.end();
  158. }
  159. function render() {
  160. if( mesh ) {
  161. var material = mesh.material;
  162. switch( params.material ) {
  163. case 'standard': material = materialStandard; break;
  164. case 'depth': material = materialDepth; break;
  165. case 'depthRGBA': material = materialDepthRGBA; break;
  166. case 'depthRGBAUnpacked': material = materialDepthRGBAUnpacked; break;
  167. case 'normal': material = materialNormal; break;
  168. }
  169. mesh.material = material;
  170. }
  171. switch( params.camera ) {
  172. case 'perspective': camera = cameraPerspective; break;
  173. case 'ortho': camera = cameraOrtho; break;
  174. }
  175. r += 0.01;
  176. renderer.render( scene, camera );
  177. }
  178. </script>
  179. </body>
  180. </html>