1
0

webgpu_postprocessing_ca.html 9.0 KB

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