webgl_materials_channels.html 7.5 KB

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