webgl_materials_channels.html 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - materials - channels</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/Detector.js"></script>
  44. <script src="js/libs/stats.min.js"></script>
  45. <script src='js/libs/dat.gui.min.js'></script>
  46. <script>
  47. if ( ! Detector.webgl ) Detector.addGetWebGLMessage();
  48. var stats, loader;
  49. var camera, scene, renderer, controls;
  50. var params = {
  51. material: 'normal',
  52. camera: 'perspective'
  53. };
  54. var cameraOrtho, cameraPerspective;
  55. var controlsOrtho, controlsPerspective;
  56. var mesh, materialStandard, materialDepthAuto, materialDepthAutoRGBA, materialDepthLinearClipZ, materialDepthLinearClipZRGBA, materialDepthInvClipZ, materialDepthInvClipZRGBA, materialNormal;
  57. var pointLight, ambientLight;
  58. var height = 500; // of camera frustum
  59. init();
  60. animate();
  61. initGui();
  62. // Init gui
  63. function initGui() {
  64. var gui = new dat.GUI();
  65. gui.add( params, 'material', [ 'standard', 'normal', 'depthAuto', 'depthAutoRGBA', 'depthLinearClipZ', 'depthLinearClipZRGBA', 'depthInvClipZ', 'depthInvClipZRGBA' ] );
  66. gui.add( params, 'camera', [ 'perspective', 'ortho' ] );
  67. }
  68. function init() {
  69. var container = document.createElement( 'div' );
  70. document.body.appendChild( container );
  71. renderer = new THREE.WebGLRenderer();
  72. renderer.setPixelRatio( window.devicePixelRatio );
  73. renderer.setSize( window.innerWidth, window.innerHeight );
  74. container.appendChild( renderer.domElement );
  75. //
  76. scene = new THREE.Scene();
  77. var aspect = window.innerWidth / window.innerHeight;
  78. cameraPerspective = new THREE.PerspectiveCamera( 45, aspect, 1000, 2500 );
  79. cameraPerspective.position.z = 1500;
  80. scene.add( cameraPerspective );
  81. cameraOrtho = new THREE.OrthographicCamera( - height * aspect, height * aspect, height, - height, 1000, 2500 );
  82. cameraOrtho.position.z = 1500;
  83. scene.add( cameraOrtho );
  84. camera = cameraPerspective;
  85. controlsPerspective = new THREE.OrbitControls( cameraPerspective, renderer.domElement );
  86. controlsPerspective.minDistance = 1000;
  87. controlsPerspective.maxDistance = 2500;
  88. controlsPerspective.enablePan = false;
  89. controlsPerspective.enableDamping = true;
  90. controlsOrtho = new THREE.OrbitControls( cameraOrtho, renderer.domElement );
  91. controlsOrtho.minZoom = 0.5;
  92. controlsOrtho.maxZoom = 2;
  93. controlsOrtho.enablePan = false;
  94. controlsOrtho.enableDamping = true;
  95. // lights
  96. var ambientLight = new THREE.AmbientLight( 0xffffff, 0.1 );
  97. scene.add( ambientLight );
  98. var pointLight = new THREE.PointLight( 0xff0000, 0.5 );
  99. pointLight.position.z = 2500;
  100. scene.add( pointLight );
  101. var pointLight2 = new THREE.PointLight( 0xff6666, 1 );
  102. camera.add( pointLight2 );
  103. var pointLight3 = new THREE.PointLight( 0x0000ff, 0.5 );
  104. pointLight3.position.x = - 1000;
  105. pointLight3.position.z = 1000;
  106. scene.add( pointLight3 );
  107. // textures
  108. var textureLoader = new THREE.TextureLoader();
  109. var normalMap = textureLoader.load( "textures/normal/ninja/normal.jpg" );
  110. var aoMap = textureLoader.load( "textures/normal/ninja/ao.jpg" );
  111. var displacementMap = textureLoader.load( "textures/normal/ninja/displacement.jpg" );
  112. // material
  113. materialStandard = new THREE.MeshStandardMaterial( { color: 0xffffff } );
  114. materialStandard.metalness = 0;
  115. materialStandard.roughness = 0.6;
  116. materialStandard.displacementMap = displacementMap;
  117. materialStandard.displacementScale = 1.5;
  118. materialStandard.aoMap = aoMap;
  119. materialStandard.normalMap = normalMap;
  120. materialStandard.normalScale.set( 1, - 1 );
  121. materialDepthAuto = new THREE.MeshDepthMaterial();
  122. materialDepthAuto.depthFormat = THREE.AutoDepthFormat;
  123. materialDepthAuto.depthPacking = THREE.LinearDepthPacking;
  124. materialDepthAutoRGBA = new THREE.MeshDepthMaterial();
  125. materialDepthAutoRGBA.depthFormat = THREE.AutoDepthFormat;
  126. materialDepthAutoRGBA.depthPacking = THREE.RGBADepthPacking;
  127. materialDepthLinearClipZ = new THREE.MeshDepthMaterial();
  128. materialDepthLinearClipZ.depthFormat = THREE.LinearClipZDepthFormat;
  129. materialDepthLinearClipZ.depthPacking = THREE.LinearDepthPacking;
  130. materialDepthLinearClipZRGBA = new THREE.MeshDepthMaterial();
  131. materialDepthLinearClipZRGBA.depthFormat = THREE.LinearClipZDepthFormat;
  132. materialDepthLinearClipZRGBA.depthPacking = THREE.RGBADepthPacking;
  133. materialDepthInvClipZ = new THREE.MeshDepthMaterial();
  134. materialDepthInvClipZ.depthFormat = THREE.InvClipZDepthFormat;
  135. materialDepthInvClipZ.depthPacking = THREE.LinearDepthPacking;
  136. materialDepthInvClipZRGBA = new THREE.MeshDepthMaterial();
  137. materialDepthInvClipZRGBA.depthFormat = THREE.InvClipZDepthFormat;
  138. materialDepthInvClipZRGBA.depthPacking = THREE.RGBADepthPacking;
  139. materialNormal = new THREE.MeshNormalMaterial();
  140. //
  141. loader = new THREE.BinaryLoader();
  142. loader.load( "obj/ninja/NinjaLo_bin.js", function( geometry ) {
  143. geometry.faceVertexUvs[ 1 ] = geometry.faceVertexUvs[ 0 ]; // 2nd set of UVs required for aoMap
  144. mesh = new THREE.Mesh( geometry, materialNormal );
  145. mesh.scale.multiplyScalar( 25 );
  146. scene.add( mesh );
  147. } );
  148. //
  149. stats = new Stats();
  150. container.appendChild( stats.domElement );
  151. //
  152. window.addEventListener( 'resize', onWindowResize, false );
  153. }
  154. function onWindowResize() {
  155. var aspect = window.innerWidth / window.innerHeight;
  156. camera.left = - height * aspect;
  157. camera.right = height * aspect;
  158. camera.top = height;
  159. camera.bottom = - height;
  160. camera.updateProjectionMatrix();
  161. renderer.setSize( window.innerWidth, window.innerHeight );
  162. }
  163. //
  164. function animate() {
  165. requestAnimationFrame( animate );
  166. controlsOrtho.update();
  167. controlsPerspective.update();
  168. stats.begin();
  169. render();
  170. stats.end();
  171. }
  172. function render() {
  173. if ( mesh ) {
  174. var material = mesh.material;
  175. switch ( params.material ) {
  176. case 'standard': material = materialStandard; break;
  177. case 'depthAuto': material = materialDepthAuto; break;
  178. case 'depthAutoRGBA': material = materialDepthAutoRGBA; break;
  179. case 'depthLinearClipZ': material = materialDepthLinearClipZ; break;
  180. case 'depthLinearClipZRGBA': material = materialDepthLinearClipZRGBA; break;
  181. case 'depthInvClipZ': material = materialDepthInvClipZ; break;
  182. case 'depthInvClipZRGBA': material = materialDepthInvClipZRGBA; break;
  183. case 'normal': material = materialNormal; break;
  184. }
  185. mesh.material = material;
  186. }
  187. switch ( params.camera ) {
  188. case 'perspective': camera = cameraPerspective; break;
  189. case 'ortho': camera = cameraOrtho; break;
  190. }
  191. renderer.render( scene, camera );
  192. }
  193. </script>
  194. </body>
  195. </html>