1
0

webgpu_postprocessing_ca.html 8.8 KB

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