webgl_materials_channels.html 8.9 KB

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