webgpu_lensflares.html 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgpu - lens flares</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>Lens Flares</span>
  14. </div>
  15. <small>
  16. Fly with WASD/RF/QE + mouse.<br/>
  17. Textures from <a href="http://www.ro.me" target="_blank" rel="noopener">ro.me</a>
  18. </small>
  19. </div>
  20. <script type="importmap">
  21. {
  22. "imports": {
  23. "three": "../build/three.webgpu.js",
  24. "three/webgpu": "../build/three.webgpu.js",
  25. "three/tsl": "../build/three.tsl.js",
  26. "three/addons/": "./jsm/"
  27. }
  28. }
  29. </script>
  30. <script type="module">
  31. import * as THREE from 'three/webgpu';
  32. import { FlyControls } from 'three/addons/controls/FlyControls.js';
  33. import { LensflareMesh, LensflareElement } from 'three/addons/objects/LensflareMesh.js';
  34. import { Inspector } from 'three/addons/inspector/Inspector.js';
  35. let container;
  36. let camera, scene, renderer;
  37. let controls;
  38. const clock = new THREE.Clock();
  39. init();
  40. function init() {
  41. container = document.createElement( 'div' );
  42. document.body.appendChild( container );
  43. // camera
  44. camera = new THREE.PerspectiveCamera( 40, window.innerWidth / window.innerHeight, 1, 15000 );
  45. camera.position.z = 250;
  46. // scene
  47. scene = new THREE.Scene();
  48. scene.background = new THREE.Color().setHSL( 0.51, 0.4, 0.01, THREE.SRGBColorSpace );
  49. scene.fog = new THREE.Fog( scene.background, 3500, 15000 );
  50. // world
  51. const s = 250;
  52. const geometry = new THREE.BoxGeometry( s, s, s );
  53. const material = new THREE.MeshPhongNodeMaterial( { color: 0xffffff, specular: 0xffffff, shininess: 50 } );
  54. for ( let i = 0; i < 3000; i ++ ) {
  55. const mesh = new THREE.Mesh( geometry, material );
  56. mesh.position.x = 8000 * ( 2.0 * Math.random() - 1.0 );
  57. mesh.position.y = 8000 * ( 2.0 * Math.random() - 1.0 );
  58. mesh.position.z = 8000 * ( 2.0 * Math.random() - 1.0 );
  59. mesh.rotation.x = Math.random() * Math.PI;
  60. mesh.rotation.y = Math.random() * Math.PI;
  61. mesh.rotation.z = Math.random() * Math.PI;
  62. mesh.matrixAutoUpdate = false;
  63. mesh.updateMatrix();
  64. scene.add( mesh );
  65. }
  66. // lights
  67. const dirLight = new THREE.DirectionalLight( 0xffffff, 0.15 );
  68. dirLight.position.set( 0, - 1, 0 ).normalize();
  69. dirLight.color.setHSL( 0.1, 0.7, 0.5 );
  70. scene.add( dirLight );
  71. // lensflares
  72. const textureLoader = new THREE.TextureLoader();
  73. const textureFlare0 = textureLoader.load( 'textures/lensflare/lensflare0.png' );
  74. const textureFlare3 = textureLoader.load( 'textures/lensflare/lensflare3.png' );
  75. textureFlare0.colorSpace = THREE.SRGBColorSpace;
  76. textureFlare3.colorSpace = THREE.SRGBColorSpace;
  77. addLight( 0.55, 0.95, 0.6, 5000, 0, - 1000 );
  78. addLight( 0.1, 0.85, 0.65, 0, 0, - 1000 );
  79. addLight( 0.995, 0.5, 0.95, 5000, 5000, - 1000 );
  80. function addLight( h, s, l, x, y, z ) {
  81. const light = new THREE.PointLight( 0xffffff, 1.5, 2000, 0 );
  82. light.color.setHSL( h, s, l );
  83. light.position.set( x, y, z );
  84. scene.add( light );
  85. const lensflare = new LensflareMesh();
  86. lensflare.addElement( new LensflareElement( textureFlare0, 700, 0, light.color ) );
  87. lensflare.addElement( new LensflareElement( textureFlare3, 60, 0.6 ) );
  88. lensflare.addElement( new LensflareElement( textureFlare3, 70, 0.7 ) );
  89. lensflare.addElement( new LensflareElement( textureFlare3, 120, 0.9 ) );
  90. lensflare.addElement( new LensflareElement( textureFlare3, 70, 1 ) );
  91. light.add( lensflare );
  92. }
  93. // renderer
  94. renderer = new THREE.WebGPURenderer();
  95. renderer.setPixelRatio( window.devicePixelRatio );
  96. renderer.setSize( window.innerWidth, window.innerHeight );
  97. renderer.setAnimationLoop( animate );
  98. renderer.inspector = new Inspector();
  99. container.appendChild( renderer.domElement );
  100. //
  101. controls = new FlyControls( camera, renderer.domElement );
  102. controls.movementSpeed = 2500;
  103. controls.domElement = container;
  104. controls.rollSpeed = Math.PI / 6;
  105. controls.autoForward = false;
  106. controls.dragToLook = false;
  107. // events
  108. window.addEventListener( 'resize', onWindowResize );
  109. }
  110. //
  111. function onWindowResize() {
  112. renderer.setSize( window.innerWidth, window.innerHeight );
  113. camera.aspect = window.innerWidth / window.innerHeight;
  114. camera.updateProjectionMatrix();
  115. }
  116. //
  117. function animate() {
  118. render();
  119. }
  120. function render() {
  121. const delta = clock.getDelta();
  122. controls.update( delta );
  123. renderer.render( scene, camera );
  124. }
  125. </script>
  126. </body>
  127. </html>
粤ICP备19079148号