webgpu_tsl_angular_slicing.html 6.5 KB

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