webgpu_tsl_angular_slicing.html 6.0 KB

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