123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173 |
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <title>three.js webgl - particles - billboards</title>
- <meta charset="utf-8">
- <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
- <link type="text/css" rel="stylesheet" href="main.css">
- </head>
- <body>
- <div id="info">
- <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - webgl particle billboards example
- </div>
- <script type="module">
- import * as THREE from '../build/three.module.js';
- import Stats from './jsm/libs/stats.module.js';
- import { GUI } from './jsm/libs/dat.gui.module.js';
- var camera, scene, renderer, stats, material;
- var mouseX = 0, mouseY = 0;
- var windowHalfX = window.innerWidth / 2;
- var windowHalfY = window.innerHeight / 2;
- init();
- animate();
- function init() {
- camera = new THREE.PerspectiveCamera( 55, window.innerWidth / window.innerHeight, 2, 2000 );
- camera.position.z = 1000;
- scene = new THREE.Scene();
- scene.fog = new THREE.FogExp2( 0x000000, 0.001 );
- var geometry = new THREE.BufferGeometry();
- var vertices = [];
- var sprite = new THREE.TextureLoader().load( 'textures/sprites/disc.png' );
- for ( var i = 0; i < 10000; i ++ ) {
- var x = 2000 * Math.random() - 1000;
- var y = 2000 * Math.random() - 1000;
- var z = 2000 * Math.random() - 1000;
- vertices.push( x, y, z );
- }
- geometry.setAttribute( 'position', new THREE.Float32BufferAttribute( vertices, 3 ) );
- material = new THREE.PointsMaterial( { size: 35, sizeAttenuation: false, map: sprite, alphaTest: 0.5, transparent: true } );
- material.color.setHSL( 1.0, 0.3, 0.7 );
- var particles = new THREE.Points( geometry, material );
- scene.add( particles );
- //
- renderer = new THREE.WebGLRenderer();
- renderer.setPixelRatio( window.devicePixelRatio );
- renderer.setSize( window.innerWidth, window.innerHeight );
- document.body.appendChild( renderer.domElement );
- //
- stats = new Stats();
- document.body.appendChild( stats.dom );
- //
- var gui = new GUI();
- gui.add( material, 'sizeAttenuation' ).onChange( function () {
- material.needsUpdate = true;
- } );
- gui.open();
- //
- document.addEventListener( 'mousemove', onDocumentMouseMove, false );
- document.addEventListener( 'touchstart', onDocumentTouchStart, false );
- document.addEventListener( 'touchmove', onDocumentTouchMove, false );
- //
- window.addEventListener( 'resize', onWindowResize, false );
- }
- function onWindowResize() {
- windowHalfX = window.innerWidth / 2;
- windowHalfY = window.innerHeight / 2;
- camera.aspect = window.innerWidth / window.innerHeight;
- camera.updateProjectionMatrix();
- renderer.setSize( window.innerWidth, window.innerHeight );
- }
- function onDocumentMouseMove( event ) {
- mouseX = event.clientX - windowHalfX;
- mouseY = event.clientY - windowHalfY;
- }
- function onDocumentTouchStart( event ) {
- if ( event.touches.length == 1 ) {
- event.preventDefault();
- mouseX = event.touches[ 0 ].pageX - windowHalfX;
- mouseY = event.touches[ 0 ].pageY - windowHalfY;
- }
- }
- function onDocumentTouchMove( event ) {
- if ( event.touches.length == 1 ) {
- event.preventDefault();
- mouseX = event.touches[ 0 ].pageX - windowHalfX;
- mouseY = event.touches[ 0 ].pageY - windowHalfY;
- }
- }
- //
- function animate() {
- requestAnimationFrame( animate );
- render();
- stats.update();
- }
- function render() {
- var time = Date.now() * 0.00005;
- camera.position.x += ( mouseX - camera.position.x ) * 0.05;
- camera.position.y += ( - mouseY - camera.position.y ) * 0.05;
- camera.lookAt( scene.position );
- var h = ( 360 * ( 1.0 + time ) % 360 ) / 360;
- material.color.setHSL( h, 0.5, 0.5 );
- renderer.render( scene, camera );
- }
- </script>
- </body>
- </html>
|