webgl_materials_channels.html 8.8 KB

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