webgpu_lensflares.html 4.8 KB

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