webgpu_tsl_halftone.html 7.3 KB

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