webgpu_tsl_halftone.html 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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, tslFn, uniform, vec4, 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 = tslFn( ( [ count, color, direction, start, end, radius, mixLow, mixHigh ] ) => {
  99. // grid pattern
  100. const gridUv = viewportCoordinate.xy
  101. .div( viewportResolution.yy )
  102. .mul( count )
  103. .rotate( Math.PI * 0.25 )
  104. .mod( 1 );
  105. // orientation strength
  106. const orientationStrength = normalWorld
  107. .dot( direction.normalize() )
  108. .remapClamp( end, start, 0, 1 );
  109. // mask
  110. const mask = gridUv
  111. .sub( 0.5 )
  112. .length()
  113. .step( orientationStrength.mul( radius ).mul( 0.5 ) )
  114. .mul( mix( mixLow, mixHigh, orientationStrength ) );
  115. return vec4( color, mask );
  116. } );
  117. const halftones = tslFn( ( [ input ] ) => {
  118. const halftonesOutput = input;
  119. for ( const settings of halftoneSettings ) {
  120. 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 );
  121. halftonesOutput.rgb.assign( mix( halftonesOutput.rgb, halfToneOutput.rgb, halfToneOutput.a ) );
  122. }
  123. return halftonesOutput;
  124. } );
  125. // default material
  126. const defaultMaterial = new THREE.MeshStandardNodeMaterial( { color: '#ff622e' } );
  127. defaultMaterial.outputNode = halftones( output );
  128. const folder = gui.addFolder( '🎨 default material' );
  129. folder.addColor( { color: defaultMaterial.color.getHexString( THREE.SRGBColorSpace ) }, 'color' )
  130. .onChange( value => defaultMaterial.color.set( value ) );
  131. // objects
  132. const torusKnot = new THREE.Mesh(
  133. new THREE.TorusKnotGeometry( 0.6, 0.25, 128, 32 ),
  134. defaultMaterial
  135. );
  136. torusKnot.position.x = 3;
  137. scene.add( torusKnot );
  138. const sphere = new THREE.Mesh(
  139. new THREE.SphereGeometry( 1, 64, 64 ),
  140. defaultMaterial
  141. );
  142. sphere.position.x = - 3;
  143. scene.add( sphere );
  144. const gltfLoader = new GLTFLoader();
  145. gltfLoader.load(
  146. './models/gltf/Michelle.glb',
  147. ( gltf ) => {
  148. const model = gltf.scene;
  149. model.position.y = - 2;
  150. model.scale.setScalar( 2.5, 2.5, 2.5 );
  151. model.traverse( ( child ) => {
  152. if ( child.isMesh )
  153. child.material.outputNode = halftones( output );
  154. } );
  155. scene.add( model );
  156. }
  157. );
  158. // renderer
  159. renderer = new THREE.WebGPURenderer( { antialias: true } );
  160. renderer.setPixelRatio( window.devicePixelRatio );
  161. renderer.setSize( window.innerWidth, window.innerHeight );
  162. renderer.setAnimationLoop( animate );
  163. renderer.setClearColor( '#000000' );
  164. document.body.appendChild( renderer.domElement );
  165. controls = new OrbitControls( camera, renderer.domElement );
  166. controls.enableDamping = true;
  167. controls.minDistance = 0.1;
  168. controls.maxDistance = 50;
  169. window.addEventListener( 'resize', onWindowResize );
  170. }
  171. function onWindowResize() {
  172. camera.aspect = window.innerWidth / window.innerHeight;
  173. camera.updateProjectionMatrix();
  174. renderer.setSize( window.innerWidth, window.innerHeight );
  175. }
  176. async function animate() {
  177. controls.update();
  178. const time = clock.getElapsedTime();
  179. halftoneSettings[ 1 ].uniforms.direction.value.x = Math.cos( time );
  180. halftoneSettings[ 1 ].uniforms.direction.value.y = Math.sin( time );
  181. renderer.render( scene, camera );
  182. }
  183. </script>
  184. </body>
  185. </html>
粤ICP备19079148号