webgl_materials_channels.html 7.4 KB

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