webgpu_postprocessing_ca.html 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgpu - chromatic aberration</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 - chromatic aberration
  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';
  25. import { pass, renderOutput, uniform } from 'three/tsl';
  26. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  27. import { RoomEnvironment } from 'three/addons/environments/RoomEnvironment.js';
  28. import { chromaticAberration } from 'three/addons/tsl/display/ChromaticAberrationNode.js';
  29. import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
  30. const params = {
  31. enabled: true,
  32. animated: true,
  33. strength: 1.5,
  34. center: new THREE.Vector2( 0.5, 0.5 ),
  35. scale: 1.2,
  36. autoRotate: true,
  37. cameraDistance: 40
  38. };
  39. let camera, scene, renderer, clock, mainGroup;
  40. let controls, postProcessing;
  41. init();
  42. async function init() {
  43. renderer = new THREE.WebGPURenderer( { antialias: true } );
  44. renderer.setPixelRatio( window.devicePixelRatio );
  45. renderer.setSize( window.innerWidth, window.innerHeight );
  46. renderer.setAnimationLoop( animate );
  47. document.body.appendChild( renderer.domElement );
  48. await renderer.init();
  49. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 0.1, 200 );
  50. camera.position.set( 0, 15, params.cameraDistance );
  51. controls = new OrbitControls( camera, renderer.domElement );
  52. controls.enableDamping = true;
  53. controls.dampingFactor = 0.1;
  54. controls.autoRotate = true;
  55. controls.autoRotateSpeed = - 0.1;
  56. controls.target.set( 0, 0.5, 0 );
  57. controls.update();
  58. scene = new THREE.Scene();
  59. scene.background = new THREE.Color( 0x0a0a0a );
  60. const pmremGenerator = new THREE.PMREMGenerator( renderer );
  61. scene.environment = pmremGenerator.fromScene( new RoomEnvironment(), 0.04 ).texture;
  62. clock = new THREE.Clock();
  63. // Create main group
  64. mainGroup = new THREE.Group();
  65. scene.add( mainGroup );
  66. // Create shapes
  67. createShapes();
  68. // Add a grid for reference
  69. const gridHelper = new THREE.GridHelper( 40, 20, 0x444444, 0x222222 );
  70. gridHelper.position.y = - 10;
  71. scene.add( gridHelper );
  72. // post processing
  73. postProcessing = new THREE.PostProcessing( renderer );
  74. postProcessing.outputColorTransform = false;
  75. // scene pass
  76. const scenePass = pass( scene, camera );
  77. const outputPass = renderOutput( scenePass );
  78. // Create uniform nodes for the static version that can be updated
  79. const staticStrength = uniform( params.strength );
  80. const staticCenter = uniform( new THREE.Vector2( params.center.x, params.center.y ) );
  81. const staticScale = uniform( params.scale );
  82. // With static values (using uniform nodes)
  83. const caPass = chromaticAberration( outputPass, staticStrength, staticCenter, staticScale );
  84. // Set initial output based on params
  85. postProcessing.outputNode = params.enabled ? caPass : outputPass;
  86. window.addEventListener( 'resize', onWindowResize );
  87. // GUI
  88. const gui = new GUI();
  89. gui.title( 'Chromatic Aberration' );
  90. gui.add( params, 'enabled' ).onChange( ( value ) => {
  91. postProcessing.outputNode = value ? caPass : outputPass;
  92. postProcessing.needsUpdate = true;
  93. } );
  94. const staticFolder = gui.addFolder( 'Static Parameters' );
  95. staticFolder.add( staticStrength, 'value', 0, 3 ).name( 'Strength' );
  96. staticFolder.add( staticCenter.value, 'x', - 1, 1 ).name( 'Center X' );
  97. staticFolder.add( staticCenter.value, 'y', - 1, 1 ).name( 'Center Y' );
  98. staticFolder.add( staticScale, 'value', 0.5, 2 ).name( 'Scale' );
  99. const animationFolder = gui.addFolder( 'Animation' );
  100. animationFolder.add( params, 'animated' );
  101. animationFolder.add( params, 'autoRotate' ).onChange( ( value ) => {
  102. controls.autoRotate = value;
  103. } );
  104. }
  105. function createShapes() {
  106. const shapes = [];
  107. const materials = [];
  108. // Define colors for different materials
  109. const colors = [
  110. 0xff0000, // Red
  111. 0x00ff00, // Green
  112. 0x0000ff, // Blue
  113. 0xffff00, // Yellow
  114. 0xff00ff, // Magenta
  115. 0x00ffff, // Cyan
  116. 0xffffff, // White
  117. 0xff8800 // Orange
  118. ];
  119. // Create materials
  120. colors.forEach( color => {
  121. materials.push( new THREE.MeshStandardMaterial( {
  122. color: color,
  123. roughness: 0.2,
  124. metalness: 0.8
  125. } ) );
  126. } );
  127. // Create geometries
  128. const geometries = [
  129. new THREE.BoxGeometry( 3, 3, 3 ),
  130. new THREE.SphereGeometry( 2, 32, 16 ),
  131. new THREE.ConeGeometry( 2, 4, 8 ),
  132. new THREE.CylinderGeometry( 1.5, 1.5, 4, 8 ),
  133. new THREE.TorusGeometry( 2, 0.8, 8, 16 ),
  134. new THREE.OctahedronGeometry( 2.5 ),
  135. new THREE.IcosahedronGeometry( 2.5 ),
  136. new THREE.TorusKnotGeometry( 1.5, 0.5, 64, 8 )
  137. ];
  138. // Create central showcase
  139. const centralGroup = new THREE.Group();
  140. // Large central torus
  141. const centralTorus = new THREE.Mesh(
  142. new THREE.TorusGeometry( 5, 1.5, 16, 32 ),
  143. new THREE.MeshStandardMaterial( {
  144. color: 0xffffff,
  145. roughness: 0.1,
  146. metalness: 1,
  147. emissive: 0x222222
  148. } )
  149. );
  150. centralGroup.add( centralTorus );
  151. // Inner rotating shapes
  152. for ( let i = 0; i < 6; i ++ ) {
  153. const angle = ( i / 6 ) * Math.PI * 2;
  154. const radius = 3;
  155. const mesh = new THREE.Mesh(
  156. geometries[ i % geometries.length ],
  157. materials[ i % materials.length ]
  158. );
  159. mesh.position.set(
  160. Math.cos( angle ) * radius,
  161. 0,
  162. Math.sin( angle ) * radius
  163. );
  164. mesh.scale.setScalar( 0.5 );
  165. centralGroup.add( mesh );
  166. shapes.push( mesh );
  167. }
  168. mainGroup.add( centralGroup );
  169. shapes.push( centralGroup );
  170. // Create outer ring of shapes
  171. const numShapes = 12;
  172. const outerRadius = 15;
  173. for ( let i = 0; i < numShapes; i ++ ) {
  174. const angle = ( i / numShapes ) * Math.PI * 2;
  175. const shapesGroup = new THREE.Group();
  176. const geometry = geometries[ i % geometries.length ];
  177. const material = materials[ i % materials.length ];
  178. const mesh = new THREE.Mesh( geometry, material );
  179. mesh.castShadow = true;
  180. mesh.receiveShadow = true;
  181. shapesGroup.add( mesh );
  182. shapesGroup.position.set(
  183. Math.cos( angle ) * outerRadius,
  184. Math.sin( i * 0.5 ) * 2,
  185. Math.sin( angle ) * outerRadius
  186. );
  187. mainGroup.add( shapesGroup );
  188. shapes.push( shapesGroup );
  189. }
  190. // Add floating particles
  191. const particlesGeometry = new THREE.BufferGeometry();
  192. const particlesCount = 200;
  193. const positions = new Float32Array( particlesCount * 3 );
  194. for ( let i = 0; i < particlesCount * 3; i += 3 ) {
  195. const radius = 25 + Math.random() * 10;
  196. const theta = Math.random() * Math.PI * 2;
  197. const phi = Math.random() * Math.PI;
  198. positions[ i ] = radius * Math.sin( phi ) * Math.cos( theta );
  199. positions[ i + 1 ] = radius * Math.cos( phi );
  200. positions[ i + 2 ] = radius * Math.sin( phi ) * Math.sin( theta );
  201. }
  202. particlesGeometry.setAttribute( 'position', new THREE.BufferAttribute( positions, 3 ) );
  203. const particlesMaterial = new THREE.PointsMaterial( {
  204. color: 0xffffff,
  205. size: 0.5,
  206. sizeAttenuation: true
  207. } );
  208. const particles = new THREE.Points( particlesGeometry, particlesMaterial );
  209. mainGroup.add( particles );
  210. }
  211. function onWindowResize() {
  212. camera.aspect = window.innerWidth / window.innerHeight;
  213. camera.updateProjectionMatrix();
  214. renderer.setSize( window.innerWidth, window.innerHeight );
  215. }
  216. function animate() {
  217. const time = clock.getElapsedTime();
  218. controls.update();
  219. if ( params.animated ) {
  220. // Animate individual shapes
  221. mainGroup.children.forEach( ( child, index ) => {
  222. if ( child.children.length > 0 ) {
  223. // Central group
  224. child.rotation.y = time * 0.5;
  225. child.children.forEach( ( subChild, subIndex ) => {
  226. if ( subChild.geometry ) {
  227. subChild.rotation.x = time * ( 1 + subIndex * 0.1 );
  228. subChild.rotation.z = time * ( 1 - subIndex * 0.1 );
  229. }
  230. } );
  231. } else if ( child.type === 'Group' ) {
  232. // Outer shapes
  233. child.rotation.x = time * 0.5 + index;
  234. child.rotation.y = time * 0.3 + index;
  235. child.position.y = Math.sin( time + index ) * 2;
  236. }
  237. } );
  238. }
  239. postProcessing.render();
  240. }
  241. </script>
  242. </body>
  243. </html>
粤ICP备19079148号