123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238 |
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <title>three.js webgl - particles - waves</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 particles waves example
- </div>
- <script type="x-shader/x-vertex" id="vertexshader">
- attribute float scale;
- void main() {
- vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );
- gl_PointSize = scale * ( 300.0 / - mvPosition.z );
- gl_Position = projectionMatrix * mvPosition;
- }
- </script>
- <script type="x-shader/x-fragment" id="fragmentshader">
- uniform vec3 color;
- void main() {
- if ( length( gl_PointCoord - vec2( 0.5, 0.5 ) ) > 0.475 ) discard;
- gl_FragColor = vec4( color, 1.0 );
- }
- </script>
- <script type="module">
- import * as THREE from '../build/three.module.js';
- import Stats from './jsm/libs/stats.module.js';
- var SEPARATION = 100, AMOUNTX = 50, AMOUNTY = 50;
- var container, stats;
- var camera, scene, renderer;
- var particles, count = 0;
- var mouseX = 0, mouseY = 0;
- var windowHalfX = window.innerWidth / 2;
- var windowHalfY = window.innerHeight / 2;
- init();
- animate();
- function init() {
- container = document.createElement( 'div' );
- document.body.appendChild( container );
- camera = new THREE.PerspectiveCamera( 75, window.innerWidth / window.innerHeight, 1, 10000 );
- camera.position.z = 1000;
- scene = new THREE.Scene();
- //
- var numParticles = AMOUNTX * AMOUNTY;
- var positions = new Float32Array( numParticles * 3 );
- var scales = new Float32Array( numParticles );
- var i = 0, j = 0;
- for ( var ix = 0; ix < AMOUNTX; ix ++ ) {
- for ( var iy = 0; iy < AMOUNTY; iy ++ ) {
- positions[ i ] = ix * SEPARATION - ( ( AMOUNTX * SEPARATION ) / 2 ); // x
- positions[ i + 1 ] = 0; // y
- positions[ i + 2 ] = iy * SEPARATION - ( ( AMOUNTY * SEPARATION ) / 2 ); // z
- scales[ j ] = 1;
- i += 3;
- j ++;
- }
- }
- var geometry = new THREE.BufferGeometry();
- geometry.setAttribute( 'position', new THREE.BufferAttribute( positions, 3 ) );
- geometry.setAttribute( 'scale', new THREE.BufferAttribute( scales, 1 ) );
- var material = new THREE.ShaderMaterial( {
- uniforms: {
- color: { value: new THREE.Color( 0xffffff ) },
- },
- vertexShader: document.getElementById( 'vertexshader' ).textContent,
- fragmentShader: document.getElementById( 'fragmentshader' ).textContent
- } );
- //
- particles = new THREE.Points( geometry, material );
- scene.add( particles );
- //
- renderer = new THREE.WebGLRenderer( { antialias: true } );
- renderer.setPixelRatio( window.devicePixelRatio );
- renderer.setSize( window.innerWidth, window.innerHeight );
- container.appendChild( renderer.domElement );
- stats = new Stats();
- container.appendChild( stats.dom );
- 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() {
- camera.position.x += ( mouseX - camera.position.x ) * .05;
- camera.position.y += ( - mouseY - camera.position.y ) * .05;
- camera.lookAt( scene.position );
- var positions = particles.geometry.attributes.position.array;
- var scales = particles.geometry.attributes.scale.array;
- var i = 0, j = 0;
- for ( var ix = 0; ix < AMOUNTX; ix ++ ) {
- for ( var iy = 0; iy < AMOUNTY; iy ++ ) {
- positions[ i + 1 ] = ( Math.sin( ( ix + count ) * 0.3 ) * 50 ) +
- ( Math.sin( ( iy + count ) * 0.5 ) * 50 );
- scales[ j ] = ( Math.sin( ( ix + count ) * 0.3 ) + 1 ) * 8 +
- ( Math.sin( ( iy + count ) * 0.5 ) + 1 ) * 8;
- i += 3;
- j ++;
- }
- }
- particles.geometry.attributes.position.needsUpdate = true;
- particles.geometry.attributes.scale.needsUpdate = true;
- renderer.render( scene, camera );
- count += 0.1;
- }
- </script>
- </body>
- </html>
|