webgl_materials_channels.html 8.6 KB

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