webgpu_postprocessing_ca.html 9.0 KB

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