123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180 |
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <title>three.js webgl - buffer geometry custom attributes - 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 - buffergeometry custom attributes - particles</div>
- <script type="x-shader/x-vertex" id="vertexshader">
- attribute float size;
- varying vec3 vColor;
- void main() {
- vColor = color;
- 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 sampler2D pointTexture;
- varying vec3 vColor;
- void main() {
- gl_FragColor = vec4( vColor, 1.0 );
- gl_FragColor = gl_FragColor * texture2D( pointTexture, gl_PointCoord );
- }
- </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 particleSystem, uniforms, geometry;
- var particles = 100000;
- init();
- animate();
- function init() {
- camera = new THREE.PerspectiveCamera( 40, window.innerWidth / window.innerHeight, 1, 10000 );
- camera.position.z = 300;
- scene = new THREE.Scene();
- uniforms = {
- pointTexture: { value: new THREE.TextureLoader().load( "textures/sprites/spark1.png" ) }
- };
- var shaderMaterial = new THREE.ShaderMaterial( {
- uniforms: uniforms,
- vertexShader: document.getElementById( 'vertexshader' ).textContent,
- fragmentShader: document.getElementById( 'fragmentshader' ).textContent,
- blending: THREE.AdditiveBlending,
- depthTest: false,
- transparent: true,
- vertexColors: THREE.VertexColors
- } );
- var radius = 200;
- geometry = new THREE.BufferGeometry();
- var positions = [];
- var colors = [];
- var sizes = [];
- var color = new THREE.Color();
- for ( var i = 0; i < particles; i ++ ) {
- positions.push( ( Math.random() * 2 - 1 ) * radius );
- positions.push( ( Math.random() * 2 - 1 ) * radius );
- positions.push( ( Math.random() * 2 - 1 ) * radius );
- color.setHSL( i / particles, 1.0, 0.5 );
- colors.push( color.r, color.g, color.b );
- sizes.push( 20 );
- }
- geometry.setAttribute( 'position', new THREE.Float32BufferAttribute( positions, 3 ) );
- geometry.setAttribute( 'color', new THREE.Float32BufferAttribute( colors, 3 ) );
- geometry.setAttribute( 'size', new THREE.Float32BufferAttribute( sizes, 1 ).setUsage( THREE.DynamicDrawUsage ) );
- particleSystem = new THREE.Points( geometry, shaderMaterial );
- scene.add( particleSystem );
- renderer = new THREE.WebGLRenderer();
- renderer.setPixelRatio( window.devicePixelRatio );
- renderer.setSize( window.innerWidth, window.innerHeight );
- var container = document.getElementById( 'container' );
- container.appendChild( renderer.domElement );
- stats = new Stats();
- container.appendChild( stats.dom );
- //
- window.addEventListener( 'resize', onWindowResize, false );
- }
- 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() {
- var time = Date.now() * 0.005;
- particleSystem.rotation.z = 0.01 * time;
- var sizes = geometry.attributes.size.array;
- for ( var i = 0; i < particles; i ++ ) {
- sizes[ i ] = 10 * ( 1 + Math.sin( 0.1 * i + time ) );
- }
- geometry.attributes.size.needsUpdate = true;
- renderer.render( scene, camera );
- }
- </script>
- </body>
- </html>
|