123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221 |
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <title>three.js webgl - interactive particles</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="container"></div>
- <div id="info"><a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> webgl - interactive - particles</div>
- <script type="x-shader/x-vertex" id="vertexshader">
- attribute float size;
- attribute vec3 customColor;
- varying vec3 vColor;
- void main() {
- vColor = customColor;
- vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );
- gl_PointSize = size * ( 300.0 / -mvPosition.z );
- gl_Position = projectionMatrix * mvPosition;
- }
- </script>
- <script type="x-shader/x-fragment" id="fragmentshader">
- uniform vec3 color;
- uniform sampler2D pointTexture;
- varying vec3 vColor;
- void main() {
- gl_FragColor = vec4( color * vColor, 1.0 );
- gl_FragColor = gl_FragColor * texture2D( pointTexture, gl_PointCoord );
- if ( gl_FragColor.a < ALPHATEST ) discard;
- }
- </script>
- <script type="module">
- import * as THREE from '../build/three.module.js';
- import Stats from './jsm/libs/stats.module.js';
- var renderer, scene, camera, stats;
- var particles;
- var PARTICLE_SIZE = 20;
- var raycaster, intersects;
- var mouse, INTERSECTED;
- init();
- animate();
- function init() {
- var container = document.getElementById( 'container' );
- scene = new THREE.Scene();
- camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 10000 );
- camera.position.z = 250;
- //
- var vertices = new THREE.BoxGeometry( 200, 200, 200, 16, 16, 16 ).vertices;
- var positions = new Float32Array( vertices.length * 3 );
- var colors = new Float32Array( vertices.length * 3 );
- var sizes = new Float32Array( vertices.length );
- var vertex;
- var color = new THREE.Color();
- for ( var i = 0, l = vertices.length; i < l; i ++ ) {
- vertex = vertices[ i ];
- vertex.toArray( positions, i * 3 );
- color.setHSL( 0.01 + 0.1 * ( i / l ), 1.0, 0.5 );
- color.toArray( colors, i * 3 );
- sizes[ i ] = PARTICLE_SIZE * 0.5;
- }
- var geometry = new THREE.BufferGeometry();
- geometry.setAttribute( 'position', new THREE.BufferAttribute( positions, 3 ) );
- geometry.setAttribute( 'customColor', new THREE.BufferAttribute( colors, 3 ) );
- geometry.setAttribute( 'size', new THREE.BufferAttribute( sizes, 1 ) );
- //
- var material = new THREE.ShaderMaterial( {
- uniforms: {
- color: { value: new THREE.Color( 0xffffff ) },
- pointTexture: { value: new THREE.TextureLoader().load( "textures/sprites/disc.png" ) }
- },
- vertexShader: document.getElementById( 'vertexshader' ).textContent,
- fragmentShader: document.getElementById( 'fragmentshader' ).textContent,
- alphaTest: 0.9
- } );
- //
- particles = new THREE.Points( geometry, material );
- scene.add( particles );
- //
- renderer = new THREE.WebGLRenderer();
- renderer.setPixelRatio( window.devicePixelRatio );
- renderer.setSize( window.innerWidth, window.innerHeight );
- container.appendChild( renderer.domElement );
- //
- raycaster = new THREE.Raycaster();
- mouse = new THREE.Vector2();
- //
- stats = new Stats();
- container.appendChild( stats.dom );
- //
- window.addEventListener( 'resize', onWindowResize, false );
- document.addEventListener( 'mousemove', onDocumentMouseMove, false );
- }
- function onDocumentMouseMove( event ) {
- event.preventDefault();
- mouse.x = ( event.clientX / window.innerWidth ) * 2 - 1;
- mouse.y = - ( event.clientY / window.innerHeight ) * 2 + 1;
- }
- function onWindowResize() {
- camera.aspect = window.innerWidth / window.innerHeight;
- camera.updateProjectionMatrix();
- renderer.setSize( window.innerWidth, window.innerHeight );
- }
- function animate() {
- requestAnimationFrame( animate );
- render();
- stats.update();
- }
- function render() {
- particles.rotation.x += 0.0005;
- particles.rotation.y += 0.001;
- var geometry = particles.geometry;
- var attributes = geometry.attributes;
- raycaster.setFromCamera( mouse, camera );
- intersects = raycaster.intersectObject( particles );
- if ( intersects.length > 0 ) {
- if ( INTERSECTED != intersects[ 0 ].index ) {
- attributes.size.array[ INTERSECTED ] = PARTICLE_SIZE;
- INTERSECTED = intersects[ 0 ].index;
- attributes.size.array[ INTERSECTED ] = PARTICLE_SIZE * 1.25;
- attributes.size.needsUpdate = true;
- }
- } else if ( INTERSECTED !== null ) {
- attributes.size.array[ INTERSECTED ] = PARTICLE_SIZE;
- attributes.size.needsUpdate = true;
- INTERSECTED = null;
- }
- renderer.render( scene, camera );
- }
- </script>
- </body>
- </html>
|