webgpu_tsl_halftone.html 7.2 KB

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