webgpu_tsl_halftone.html 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgpu - halftone</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> WebGPU - Halftone
  12. </div>
  13. <script type="importmap">
  14. {
  15. "imports": {
  16. "three": "../build/three.webgpu.js",
  17. "three/webgpu": "../build/three.webgpu.js",
  18. "three/tsl": "../build/three.tsl.js",
  19. "three/addons/": "./jsm/"
  20. }
  21. }
  22. </script>
  23. <script type="module">
  24. import * as THREE from 'three/webgpu';
  25. import { color, mix, normalWorld, output, Fn, uniform, vec4, rotate, screenCoordinate, screenSize } from 'three/tsl';
  26. import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
  27. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  28. import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
  29. let camera, scene, renderer, controls, clock, halftoneSettings;
  30. init();
  31. function init() {
  32. camera = new THREE.PerspectiveCamera( 25, window.innerWidth / window.innerHeight, 0.1, 100 );
  33. camera.position.set( 6, 3, 10 );
  34. scene = new THREE.Scene();
  35. clock = new THREE.Clock();
  36. const gui = new GUI();
  37. // lights
  38. const ambientLight = new THREE.AmbientLight( '#ffffff', 3 );
  39. scene.add( ambientLight );
  40. const directionalLight = new THREE.DirectionalLight( '#ffffff', 8 );
  41. directionalLight.position.set( 4, 3, 1 );
  42. scene.add( directionalLight );
  43. const lightsFolder = gui.addFolder( '💡 lights' );
  44. lightsFolder.add( ambientLight, 'intensity', 0, 10, 0.001 ).name( 'ambientIntensity' );
  45. lightsFolder.add( directionalLight, 'intensity', 0, 20, 0.001 ).name( 'directionalIntensity' );
  46. // halftone settings
  47. halftoneSettings = [
  48. // purple shade
  49. {
  50. count: 140,
  51. color: '#fb00ff',
  52. direction: new THREE.Vector3( - 0.4, - 1, 0.5 ),
  53. start: 1,
  54. end: 0,
  55. mixLow: 0,
  56. mixHigh: 0.5,
  57. radius: 0.8
  58. },
  59. // cyan highlight
  60. {
  61. count: 180,
  62. color: '#94ffd1',
  63. direction: new THREE.Vector3( 0.5, 0.5, - 0.2 ),
  64. start: 0.55,
  65. end: 0.2,
  66. mixLow: 0.5,
  67. mixHigh: 1,
  68. radius: 0.8
  69. }
  70. ];
  71. for ( const index in halftoneSettings ) {
  72. const settings = halftoneSettings[ index ];
  73. // uniforms
  74. const uniforms = {};
  75. uniforms.count = uniform( settings.count );
  76. uniforms.color = uniform( color( settings.color ) );
  77. uniforms.direction = uniform( settings.direction );
  78. uniforms.start = uniform( settings.start );
  79. uniforms.end = uniform( settings.end );
  80. uniforms.mixLow = uniform( settings.mixLow );
  81. uniforms.mixHigh = uniform( settings.mixHigh );
  82. uniforms.radius = uniform( settings.radius );
  83. settings.uniforms = uniforms;
  84. // debug
  85. const folder = gui.addFolder( `⚪️ halftone ${index}` );
  86. folder.addColor( { color: uniforms.color.value.getHexString( THREE.SRGBColorSpace ) }, 'color' )
  87. .onChange( value => uniforms.color.value.set( value ) );
  88. folder.add( uniforms.count, 'value', 1, 200, 1 ).name( 'count' );
  89. folder.add( uniforms.direction.value, 'x', - 1, 1, 0.01 ).listen();
  90. folder.add( uniforms.direction.value, 'y', - 1, 1, 0.01 ).listen();
  91. folder.add( uniforms.direction.value, 'z', - 1, 1, 0.01 ).listen();
  92. folder.add( uniforms.start, 'value', - 1, 1, 0.01 ).name( 'start' );
  93. folder.add( uniforms.end, 'value', - 1, 1, 0.01 ).name( 'end' );
  94. folder.add( uniforms.mixLow, 'value', 0, 1, 0.01 ).name( 'mixLow' );
  95. folder.add( uniforms.mixHigh, 'value', 0, 1, 0.01 ).name( 'mixHigh' );
  96. folder.add( uniforms.radius, 'value', 0, 1, 0.01 ).name( 'radius' );
  97. }
  98. // halftone functions
  99. const halftone = Fn( ( [ count, color, direction, start, end, radius, mixLow, mixHigh ] ) => {
  100. // grid pattern
  101. let gridUv = screenCoordinate.xy.div( screenSize.yy ).mul( count );
  102. gridUv = rotate( gridUv, Math.PI * 0.25 ).mod( 1 );
  103. // orientation strength
  104. const orientationStrength = normalWorld
  105. .dot( direction.normalize() )
  106. .remapClamp( end, start, 0, 1 );
  107. // mask
  108. const mask = orientationStrength.mul( radius ).mul( 0.5 )
  109. .step( gridUv.sub( 0.5 ).length() )
  110. .mul( mix( mixLow, mixHigh, orientationStrength ) );
  111. return vec4( color, mask );
  112. } );
  113. const halftones = Fn( ( [ input ] ) => {
  114. const halftonesOutput = input;
  115. for ( const settings of halftoneSettings ) {
  116. const halfToneOutput = halftone( settings.uniforms.count, settings.uniforms.color, settings.uniforms.direction, settings.uniforms.start, settings.uniforms.end, settings.uniforms.radius, settings.uniforms.mixLow, settings.uniforms.mixHigh );
  117. halftonesOutput.rgb.assign( mix( halftonesOutput.rgb, halfToneOutput.rgb, halfToneOutput.a ) );
  118. }
  119. return halftonesOutput;
  120. } );
  121. // default material
  122. const defaultMaterial = new THREE.MeshStandardNodeMaterial( { color: '#ff622e' } );
  123. defaultMaterial.outputNode = halftones( output );
  124. const folder = gui.addFolder( '🎨 default material' );
  125. folder.addColor( { color: defaultMaterial.color.getHexString( THREE.SRGBColorSpace ) }, 'color' )
  126. .onChange( value => defaultMaterial.color.set( value ) );
  127. // objects
  128. const torusKnot = new THREE.Mesh(
  129. new THREE.TorusKnotGeometry( 0.6, 0.25, 128, 32 ),
  130. defaultMaterial
  131. );
  132. torusKnot.position.x = 3;
  133. scene.add( torusKnot );
  134. const sphere = new THREE.Mesh(
  135. new THREE.SphereGeometry( 1, 64, 64 ),
  136. defaultMaterial
  137. );
  138. sphere.position.x = - 3;
  139. scene.add( sphere );
  140. const gltfLoader = new GLTFLoader();
  141. gltfLoader.load(
  142. './models/gltf/Michelle.glb',
  143. ( gltf ) => {
  144. const model = gltf.scene;
  145. model.position.y = - 2;
  146. model.scale.setScalar( 2.5 );
  147. model.traverse( ( child ) => {
  148. if ( child.isMesh )
  149. child.material.outputNode = halftones( output );
  150. } );
  151. scene.add( model );
  152. }
  153. );
  154. // renderer
  155. renderer = new THREE.WebGPURenderer( { antialias: true } );
  156. renderer.setPixelRatio( window.devicePixelRatio );
  157. renderer.setSize( window.innerWidth, window.innerHeight );
  158. renderer.setAnimationLoop( animate );
  159. renderer.setClearColor( '#000000' );
  160. document.body.appendChild( renderer.domElement );
  161. controls = new OrbitControls( camera, renderer.domElement );
  162. controls.enableDamping = true;
  163. controls.minDistance = 0.1;
  164. controls.maxDistance = 50;
  165. window.addEventListener( 'resize', onWindowResize );
  166. }
  167. function onWindowResize() {
  168. camera.aspect = window.innerWidth / window.innerHeight;
  169. camera.updateProjectionMatrix();
  170. renderer.setSize( window.innerWidth, window.innerHeight );
  171. }
  172. async function animate() {
  173. controls.update();
  174. const time = clock.getElapsedTime();
  175. halftoneSettings[ 1 ].uniforms.direction.value.x = Math.cos( time );
  176. halftoneSettings[ 1 ].uniforms.direction.value.y = Math.sin( time );
  177. renderer.render( scene, camera );
  178. }
  179. </script>
  180. </body>
  181. </html>
粤ICP备19079148号