webgl_materials_channels.html 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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. <link type="text/css" rel="stylesheet" href="main.css">
  8. </head>
  9. <body>
  10. <div id="info">
  11. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - <span id="description">Normal, Depth, DepthRGBA, DepthRGBAUnpacked, Materials</span><br/>
  12. by <a href="https://Clara.io">Ben Houston</a>. ninja head from <a href="http://developer.amd.com/tools-and-sdks/archive/legacy-cpu-gpu-tools/amd-gpu-meshmapper/" target="_blank" rel="noopener">AMD GPU MeshMapper</a>
  13. </div>
  14. <script type="module">
  15. import * as THREE from '../build/three.module.js';
  16. import Stats from './jsm/libs/stats.module.js';
  17. import { GUI } from './jsm/libs/dat.gui.module.js';
  18. import { OBJLoader } from './jsm/loaders/OBJLoader.js';
  19. import { OrbitControls } from './jsm/controls/OrbitControls.js';
  20. var stats;
  21. var camera, scene, renderer;
  22. var params = {
  23. material: 'normal',
  24. camera: 'perspective',
  25. side: 'double'
  26. };
  27. var sides = {
  28. 'front': THREE.FrontSide,
  29. 'back': THREE.BackSide,
  30. 'double': THREE.DoubleSide
  31. };
  32. var cameraOrtho, cameraPerspective;
  33. var controlsOrtho, controlsPerspective;
  34. var mesh, materialStandard, materialDepthBasic, materialDepthRGBA, materialNormal;
  35. var height = 500; // of camera frustum
  36. var SCALE = 2.436143; // from original model
  37. var BIAS = - 0.428408; // from original model
  38. init();
  39. animate();
  40. initGui();
  41. // Init gui
  42. function initGui() {
  43. var gui = new GUI();
  44. gui.add( params, 'material', [ 'standard', 'normal', 'depthBasic', 'depthRGBA' ] );
  45. gui.add( params, 'camera', [ 'perspective', 'ortho' ] );
  46. gui.add( params, 'side', [ 'front', 'back', 'double' ] );
  47. }
  48. function init() {
  49. var container = document.createElement( 'div' );
  50. document.body.appendChild( container );
  51. renderer = new THREE.WebGLRenderer();
  52. renderer.setPixelRatio( window.devicePixelRatio );
  53. renderer.setSize( window.innerWidth, window.innerHeight );
  54. container.appendChild( renderer.domElement );
  55. //
  56. scene = new THREE.Scene();
  57. var aspect = window.innerWidth / window.innerHeight;
  58. cameraPerspective = new THREE.PerspectiveCamera( 45, aspect, 500, 3000 );
  59. cameraPerspective.position.z = 1500;
  60. scene.add( cameraPerspective );
  61. cameraOrtho = new THREE.OrthographicCamera( - height * aspect, height * aspect, height, - height, 1000, 2500 );
  62. cameraOrtho.position.z = 1500;
  63. scene.add( cameraOrtho );
  64. camera = cameraPerspective;
  65. controlsPerspective = new OrbitControls( cameraPerspective, renderer.domElement );
  66. controlsPerspective.minDistance = 1000;
  67. controlsPerspective.maxDistance = 2400;
  68. controlsPerspective.enablePan = false;
  69. controlsPerspective.enableDamping = true;
  70. controlsOrtho = new OrbitControls( cameraOrtho, renderer.domElement );
  71. controlsOrtho.minZoom = 0.5;
  72. controlsOrtho.maxZoom = 1.5;
  73. controlsOrtho.enablePan = false;
  74. controlsOrtho.enableDamping = true;
  75. // lights
  76. var ambientLight = new THREE.AmbientLight( 0xffffff, 0.1 );
  77. scene.add( ambientLight );
  78. var pointLight = new THREE.PointLight( 0xff0000, 0.5 );
  79. pointLight.position.z = 2500;
  80. scene.add( pointLight );
  81. var pointLight2 = new THREE.PointLight( 0xff6666, 1 );
  82. camera.add( pointLight2 );
  83. var pointLight3 = new THREE.PointLight( 0x0000ff, 0.5 );
  84. pointLight3.position.x = - 1000;
  85. pointLight3.position.z = 1000;
  86. scene.add( pointLight3 );
  87. // textures
  88. var textureLoader = new THREE.TextureLoader();
  89. var normalMap = textureLoader.load( "models/obj/ninja/normal.jpg" );
  90. var aoMap = textureLoader.load( "models/obj/ninja/ao.jpg" );
  91. var displacementMap = textureLoader.load( "models/obj/ninja/displacement.jpg" );
  92. // material
  93. materialStandard = new THREE.MeshStandardMaterial( {
  94. color: 0xffffff,
  95. metalness: 0.5,
  96. roughness: 0.6,
  97. displacementMap: displacementMap,
  98. displacementScale: SCALE,
  99. displacementBias: BIAS,
  100. aoMap: aoMap,
  101. normalMap: normalMap,
  102. normalScale: new THREE.Vector2( 1, - 1 ),
  103. //flatShading: true,
  104. side: THREE.DoubleSide
  105. } );
  106. materialDepthBasic = new THREE.MeshDepthMaterial( {
  107. depthPacking: THREE.BasicDepthPacking,
  108. displacementMap: displacementMap,
  109. displacementScale: SCALE,
  110. displacementBias: BIAS,
  111. side: THREE.DoubleSide
  112. } );
  113. materialDepthRGBA = new THREE.MeshDepthMaterial( {
  114. depthPacking: THREE.RGBADepthPacking,
  115. displacementMap: displacementMap,
  116. displacementScale: SCALE,
  117. displacementBias: BIAS,
  118. side: THREE.DoubleSide
  119. } );
  120. materialNormal = new THREE.MeshNormalMaterial( {
  121. displacementMap: displacementMap,
  122. displacementScale: SCALE,
  123. displacementBias: BIAS,
  124. normalMap: normalMap,
  125. normalScale: new THREE.Vector2( 1, - 1 ),
  126. //flatShading: true,
  127. side: THREE.DoubleSide
  128. } );
  129. //
  130. var loader = new OBJLoader();
  131. loader.load( "models/obj/ninja/ninjaHead_Low.obj", function ( group ) {
  132. var geometry = group.children[ 0 ].geometry;
  133. geometry.attributes.uv2 = geometry.attributes.uv;
  134. geometry.center();
  135. mesh = new THREE.Mesh( geometry, materialNormal );
  136. mesh.scale.multiplyScalar( 25 );
  137. scene.add( mesh );
  138. } );
  139. //
  140. stats = new Stats();
  141. container.appendChild( stats.dom );
  142. //
  143. window.addEventListener( 'resize', onWindowResize, false );
  144. }
  145. function onWindowResize() {
  146. var width = window.innerWidth;
  147. var height = window.innerHeight;
  148. var aspect = window.innerWidth / window.innerHeight;
  149. camera.aspect = aspect;
  150. camera.left = - height * aspect;
  151. camera.right = height * aspect;
  152. camera.top = height;
  153. camera.bottom = - height;
  154. camera.updateProjectionMatrix();
  155. renderer.setSize( width, height );
  156. }
  157. //
  158. function animate() {
  159. requestAnimationFrame( animate );
  160. stats.begin();
  161. render();
  162. stats.end();
  163. }
  164. function render() {
  165. if ( mesh ) {
  166. var material = mesh.material;
  167. switch ( params.material ) {
  168. case 'standard': material = materialStandard; break;
  169. case 'depthBasic': material = materialDepthBasic; break;
  170. case 'depthRGBA': material = materialDepthRGBA; break;
  171. case 'normal': material = materialNormal; break;
  172. }
  173. if ( sides[ params.side ] !== material.side ) {
  174. switch ( params.side ) {
  175. case 'front': material.side = THREE.FrontSide; break;
  176. case 'back': material.side = THREE.BackSide; break;
  177. case 'double': material.side = THREE.DoubleSide; break;
  178. }
  179. material.needsUpdate = true;
  180. }
  181. mesh.material = material;
  182. }
  183. switch ( params.camera ) {
  184. case 'perspective':
  185. camera = cameraPerspective;
  186. break;
  187. case 'ortho':
  188. camera = cameraOrtho;
  189. break;
  190. }
  191. controlsPerspective.update();
  192. controlsOrtho.update(); // must update both controls for damping to complete
  193. renderer.render( scene, camera );
  194. }
  195. </script>
  196. </body>
  197. </html>