1
0

webgpu_tsl_angular_slicing.html 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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="example.css">
  8. </head>
  9. <body>
  10. <div id="info">
  11. <a href="https://threejs.org/" target="_blank" rel="noopener" class="logo-link"></a>
  12. <div class="title-wrapper">
  13. <a href="https://threejs.org/" target="_blank" rel="noopener">three.js</a><span>Angular Slicing</span>
  14. </div>
  15. <small>
  16. Based on <a href="https://threejs-journey.com/lessons/sliced-model-shader" target="_blank" rel="noopener">Three.js Journey</a> lesson.
  17. </small>
  18. </div>
  19. <script type="importmap">
  20. {
  21. "imports": {
  22. "three": "../build/three.webgpu.js",
  23. "three/webgpu": "../build/three.webgpu.js",
  24. "three/tsl": "../build/three.tsl.js",
  25. "three/addons/": "./jsm/"
  26. }
  27. }
  28. </script>
  29. <script type="module">
  30. import * as THREE from 'three/webgpu';
  31. import { If, TWO_PI, atan, color, frontFacing, output, positionLocal, Fn, uniform, vec4 } from 'three/tsl';
  32. import { Inspector } from 'three/addons/inspector/Inspector.js';
  33. import { DRACOLoader } from 'three/addons/loaders/DRACOLoader.js';
  34. import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
  35. import { HDRLoader } from 'three/addons/loaders/HDRLoader.js';
  36. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  37. let camera, scene, renderer, controls;
  38. init();
  39. function init() {
  40. camera = new THREE.PerspectiveCamera( 35, window.innerWidth / window.innerHeight, 0.1, 100 );
  41. camera.position.set( - 5, 5, 12 );
  42. scene = new THREE.Scene();
  43. // environment
  44. const hdrLoader = new HDRLoader();
  45. hdrLoader.load( './textures/equirectangular/royal_esplanade_1k.hdr', ( environmentMap ) => {
  46. environmentMap.mapping = THREE.EquirectangularReflectionMapping;
  47. scene.background = environmentMap;
  48. scene.environment = environmentMap;
  49. } );
  50. // lights
  51. const directionalLight = new THREE.DirectionalLight( '#ffffff', 4 );
  52. directionalLight.position.set( 6.25, 3, 4 );
  53. directionalLight.castShadow = true;
  54. directionalLight.shadow.mapSize.set( 2048, 2048 );
  55. directionalLight.shadow.camera.near = 0.1;
  56. directionalLight.shadow.camera.far = 30;
  57. directionalLight.shadow.camera.top = 8;
  58. directionalLight.shadow.camera.right = 8;
  59. directionalLight.shadow.camera.bottom = - 8;
  60. directionalLight.shadow.camera.left = - 8;
  61. directionalLight.shadow.normalBias = 0.05;
  62. scene.add( directionalLight );
  63. // TSL functions
  64. const inAngle = Fn( ( [ position, angleStart, angleArc ] ) => {
  65. const angle = atan( position.y, position.x ).sub( angleStart ).mod( TWO_PI ).toVar();
  66. return angle.greaterThan( 0 ).and( angle.lessThan( angleArc ) );
  67. } );
  68. // materials
  69. const defaultMaterial = new THREE.MeshPhysicalNodeMaterial( {
  70. metalness: 0.5,
  71. roughness: 0.25,
  72. envMapIntensity: 0.5,
  73. color: '#858080'
  74. } );
  75. const slicedMaterial = new THREE.MeshPhysicalNodeMaterial( {
  76. metalness: 0.5,
  77. roughness: 0.25,
  78. envMapIntensity: 0.5,
  79. color: '#858080',
  80. side: THREE.DoubleSide
  81. } );
  82. // uniforms
  83. const sliceStart = uniform( 1.75 );
  84. const sliceArc = uniform( 1.25 );
  85. const sliceColor = uniform( color( '#b62f58' ) );
  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.castShadowNode = 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.shadowMap.enabled = true;
  136. renderer.inspector = new Inspector();
  137. renderer.setPixelRatio( window.devicePixelRatio );
  138. renderer.setSize( window.innerWidth, window.innerHeight );
  139. renderer.setAnimationLoop( animate );
  140. document.body.appendChild( renderer.domElement );
  141. // controls
  142. controls = new OrbitControls( camera, renderer.domElement );
  143. controls.enableDamping = true;
  144. controls.minDistance = 0.1;
  145. controls.maxDistance = 50;
  146. // events
  147. window.addEventListener( 'resize', onWindowResize );
  148. // debug
  149. const gui = renderer.inspector.createParameters( 'Parameters' );
  150. gui.add( sliceStart, 'value', - Math.PI, Math.PI, 0.001 ).name( 'sliceStart' );
  151. gui.add( sliceArc, 'value', 0, Math.PI * 2, 0.001 ).name( 'sliceArc' );
  152. gui.addColor( { color: sliceColor.value.getHexString( THREE.SRGBColorSpace ) }, 'color' ).onChange( value => sliceColor.value.set( value ) );
  153. }
  154. function onWindowResize() {
  155. camera.aspect = window.innerWidth / window.innerHeight;
  156. camera.updateProjectionMatrix();
  157. renderer.setSize( window.innerWidth, window.innerHeight );
  158. }
  159. async function animate() {
  160. controls.update();
  161. renderer.render( scene, camera );
  162. }
  163. </script>
  164. </body>
  165. </html>
粤ICP备19079148号