webgpu_lensflares.html 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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 timer = new THREE.Timer();
  39. timer.connect( document );
  40. init();
  41. function init() {
  42. container = document.createElement( 'div' );
  43. document.body.appendChild( container );
  44. // camera
  45. camera = new THREE.PerspectiveCamera( 40, window.innerWidth / window.innerHeight, 1, 15000 );
  46. camera.position.z = 250;
  47. // scene
  48. scene = new THREE.Scene();
  49. scene.background = new THREE.Color().setHSL( 0.51, 0.4, 0.01, THREE.SRGBColorSpace );
  50. scene.fog = new THREE.Fog( scene.background, 3500, 15000 );
  51. // world
  52. const s = 250;
  53. const geometry = new THREE.BoxGeometry( s, s, s );
  54. const material = new THREE.MeshPhongNodeMaterial( { color: 0xffffff, specular: 0xffffff, shininess: 50 } );
  55. for ( let i = 0; i < 3000; i ++ ) {
  56. const mesh = new THREE.Mesh( geometry, material );
  57. mesh.position.x = 8000 * ( 2.0 * Math.random() - 1.0 );
  58. mesh.position.y = 8000 * ( 2.0 * Math.random() - 1.0 );
  59. mesh.position.z = 8000 * ( 2.0 * Math.random() - 1.0 );
  60. mesh.rotation.x = Math.random() * Math.PI;
  61. mesh.rotation.y = Math.random() * Math.PI;
  62. mesh.rotation.z = Math.random() * Math.PI;
  63. mesh.matrixAutoUpdate = false;
  64. mesh.updateMatrix();
  65. scene.add( mesh );
  66. }
  67. // lights
  68. const dirLight = new THREE.DirectionalLight( 0xffffff, 0.15 );
  69. dirLight.position.set( 0, - 1, 0 ).normalize();
  70. dirLight.color.setHSL( 0.1, 0.7, 0.5 );
  71. scene.add( dirLight );
  72. // lensflares
  73. const textureLoader = new THREE.TextureLoader();
  74. const textureFlare0 = textureLoader.load( 'textures/lensflare/lensflare0.png' );
  75. const textureFlare3 = textureLoader.load( 'textures/lensflare/lensflare3.png' );
  76. textureFlare0.colorSpace = THREE.SRGBColorSpace;
  77. textureFlare3.colorSpace = THREE.SRGBColorSpace;
  78. addLight( 0.55, 0.95, 0.6, 5000, 0, - 1000 );
  79. addLight( 0.1, 0.85, 0.65, 0, 0, - 1000 );
  80. addLight( 0.995, 0.5, 0.95, 5000, 5000, - 1000 );
  81. function addLight( h, s, l, x, y, z ) {
  82. const light = new THREE.PointLight( 0xffffff, 1.5, 2000, 0 );
  83. light.color.setHSL( h, s, l );
  84. light.position.set( x, y, z );
  85. scene.add( light );
  86. const lensflare = new LensflareMesh();
  87. lensflare.addElement( new LensflareElement( textureFlare0, 700, 0, light.color ) );
  88. lensflare.addElement( new LensflareElement( textureFlare3, 60, 0.6 ) );
  89. lensflare.addElement( new LensflareElement( textureFlare3, 70, 0.7 ) );
  90. lensflare.addElement( new LensflareElement( textureFlare3, 120, 0.9 ) );
  91. lensflare.addElement( new LensflareElement( textureFlare3, 70, 1 ) );
  92. light.add( lensflare );
  93. }
  94. // renderer
  95. renderer = new THREE.WebGPURenderer();
  96. renderer.setPixelRatio( window.devicePixelRatio );
  97. renderer.setSize( window.innerWidth, window.innerHeight );
  98. renderer.setAnimationLoop( animate );
  99. renderer.inspector = new Inspector();
  100. container.appendChild( renderer.domElement );
  101. //
  102. controls = new FlyControls( camera, renderer.domElement );
  103. controls.movementSpeed = 2500;
  104. controls.domElement = container;
  105. controls.rollSpeed = Math.PI / 6;
  106. controls.autoForward = false;
  107. controls.dragToLook = false;
  108. // events
  109. window.addEventListener( 'resize', onWindowResize );
  110. }
  111. //
  112. function onWindowResize() {
  113. renderer.setSize( window.innerWidth, window.innerHeight );
  114. camera.aspect = window.innerWidth / window.innerHeight;
  115. camera.updateProjectionMatrix();
  116. }
  117. //
  118. function animate() {
  119. timer.update();
  120. render();
  121. }
  122. function render() {
  123. const delta = timer.getDelta();
  124. controls.update( delta );
  125. renderer.render( scene, camera );
  126. }
  127. </script>
  128. </body>
  129. </html>
粤ICP备19079148号