webgpu_tsl_angular_slicing.html 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgpu - angular slicing</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 webgpu</a> - angular slicing
  12. <br>
  13. Based on <a href="https://threejs-journey.com/lessons/sliced-model-shader" target="_blank" rel="noopener">Three.js Journey</a> lesson
  14. </div>
  15. <script type="importmap">
  16. {
  17. "imports": {
  18. "three": "../build/three.webgpu.js",
  19. "three/tsl": "../build/three.webgpu.js",
  20. "three/addons/": "./jsm/"
  21. }
  22. }
  23. </script>
  24. <script type="module">
  25. import * as THREE from 'three';
  26. import { If, PI2, atan2, color, frontFacing, output, positionLocal, Fn, uniform, vec4 } from 'three/tsl';
  27. import { DRACOLoader } from 'three/addons/loaders/DRACOLoader.js';
  28. import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
  29. import { RGBELoader } from 'three/addons/loaders/RGBELoader.js';
  30. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  31. import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
  32. let camera, scene, renderer, controls;
  33. init();
  34. function init() {
  35. camera = new THREE.PerspectiveCamera( 35, window.innerWidth / window.innerHeight, 0.1, 100 );
  36. camera.position.set( - 5, 5, 12 );
  37. scene = new THREE.Scene();
  38. const gui = new GUI();
  39. // environment
  40. const rgbeLoader = new RGBELoader();
  41. rgbeLoader.load( './textures/equirectangular/royal_esplanade_1k.hdr', ( environmentMap ) => {
  42. environmentMap.mapping = THREE.EquirectangularReflectionMapping;
  43. scene.background = environmentMap;
  44. scene.environment = environmentMap;
  45. } );
  46. // lights
  47. const directionalLight = new THREE.DirectionalLight( '#ffffff', 4 );
  48. directionalLight.position.set( 6.25, 3, 4 );
  49. directionalLight.castShadow = true;
  50. directionalLight.shadow.mapSize.set( 1024, 1024 );
  51. directionalLight.shadow.camera.near = 0.1;
  52. directionalLight.shadow.camera.far = 30;
  53. directionalLight.shadow.camera.top = 8;
  54. directionalLight.shadow.camera.right = 8;
  55. directionalLight.shadow.camera.bottom = - 8;
  56. directionalLight.shadow.camera.left = - 8;
  57. directionalLight.shadow.normalBias = 0.05;
  58. scene.add( directionalLight );
  59. // TSL functions
  60. const inAngle = Fn( ( [ position, angleStart, angleArc ] ) => {
  61. const angle = atan2( position.y, position.x ).sub( angleStart ).mod( PI2 ).toVar();
  62. return angle.greaterThan( 0 ).and( angle.lessThan( angleArc ) );
  63. } );
  64. // materials
  65. const defaultMaterial = new THREE.MeshPhysicalNodeMaterial( {
  66. metalness: 0.5,
  67. roughness: 0.25,
  68. envMapIntensity: 0.5,
  69. color: '#858080'
  70. } );
  71. const slicedMaterial = new THREE.MeshPhysicalNodeMaterial( {
  72. metalness: 0.5,
  73. roughness: 0.25,
  74. envMapIntensity: 0.5,
  75. color: '#858080',
  76. side: THREE.DoubleSide
  77. } );
  78. // uniforms
  79. const sliceStart = uniform( 1.75 );
  80. const sliceArc = uniform( 1.25 );
  81. const sliceColor = uniform( color( '#b62f58' ) );
  82. // debug
  83. gui.add( sliceStart, 'value', - Math.PI, Math.PI, 0.001 ).name( 'sliceStart' );
  84. gui.add( sliceArc, 'value', 0, Math.PI * 2, 0.001 ).name( 'sliceArc' );
  85. gui.addColor( { color: sliceColor.value.getHexString( THREE.SRGBColorSpace ) }, 'color' ).onChange( value => sliceColor.value.set( value ) );
  86. // output
  87. slicedMaterial.outputNode = Fn( () => {
  88. // discard
  89. inAngle( positionLocal.xy, sliceStart, sliceArc ).discard();
  90. // backface color
  91. const finalOutput = output;
  92. If( frontFacing.not(), () => {
  93. finalOutput.assign( vec4( sliceColor, 1 ) );
  94. } );
  95. return finalOutput;
  96. } )();
  97. // shadow
  98. slicedMaterial.shadowNode = Fn( () => {
  99. // discard
  100. inAngle( positionLocal.xy, sliceStart, sliceArc ).discard();
  101. return vec4( 0, 0, 0, 1 );
  102. } )();
  103. // model
  104. const dracoLoader = new DRACOLoader();
  105. dracoLoader.setDecoderPath( 'jsm/libs/draco/' );
  106. const gltfLoader = new GLTFLoader();
  107. gltfLoader.setDRACOLoader( dracoLoader );
  108. gltfLoader.load( './models/gltf/gears.glb', ( gltf ) => {
  109. const model = gltf.scene;
  110. model.traverse( ( child ) => {
  111. if ( child.isMesh ) {
  112. if ( child.name === 'outerHull' )
  113. child.material = slicedMaterial;
  114. else
  115. child.material = defaultMaterial;
  116. child.castShadow = true;
  117. child.receiveShadow = true;
  118. }
  119. } );
  120. scene.add( model );
  121. } );
  122. // plane
  123. const plane = new THREE.Mesh(
  124. new THREE.PlaneGeometry( 10, 10, 10 ),
  125. new THREE.MeshStandardMaterial( { color: '#aaaaaa' } )
  126. );
  127. plane.receiveShadow = true;
  128. plane.position.set( - 4, - 3, - 4 );
  129. plane.lookAt( new THREE.Vector3( 0, 0, 0 ) );
  130. scene.add( plane );
  131. // renderer
  132. renderer = new THREE.WebGPURenderer( { antialias: true } );
  133. renderer.toneMapping = THREE.ACESFilmicToneMapping;
  134. renderer.toneMappingExposure = 1;
  135. renderer.setPixelRatio( window.devicePixelRatio );
  136. renderer.setSize( window.innerWidth, window.innerHeight );
  137. renderer.setAnimationLoop( animate );
  138. document.body.appendChild( renderer.domElement );
  139. // controls
  140. controls = new OrbitControls( camera, renderer.domElement );
  141. controls.enableDamping = true;
  142. controls.minDistance = 0.1;
  143. controls.maxDistance = 50;
  144. window.addEventListener( 'resize', onWindowResize );
  145. }
  146. function onWindowResize() {
  147. camera.aspect = window.innerWidth / window.innerHeight;
  148. camera.updateProjectionMatrix();
  149. renderer.setSize( window.innerWidth, window.innerHeight );
  150. }
  151. async function animate() {
  152. controls.update();
  153. renderer.render( scene, camera );
  154. }
  155. </script>
  156. </body>
  157. </html>