webgpu_tsl_halftone.html 7.6 KB

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