webgpu_postprocessing_ca.html 9.4 KB

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