123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142 |
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <title>three.js webgl - buffergeometry - 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 - particles</div>
- <script type="module">
- import * as THREE from '../build/three.module.js';
- import Stats from './jsm/libs/stats.module.js';
- var container, stats;
- var camera, scene, renderer;
- var points;
- init();
- animate();
- function init() {
- container = document.getElementById( 'container' );
- //
- camera = new THREE.PerspectiveCamera( 27, window.innerWidth / window.innerHeight, 5, 3500 );
- camera.position.z = 2750;
- scene = new THREE.Scene();
- scene.background = new THREE.Color( 0x050505 );
- scene.fog = new THREE.Fog( 0x050505, 2000, 3500 );
- //
- var particles = 500000;
- var geometry = new THREE.BufferGeometry();
- var positions = [];
- var colors = [];
- var color = new THREE.Color();
- var n = 1000, n2 = n / 2; // particles spread in the cube
- for ( var i = 0; i < particles; i ++ ) {
- // positions
- var x = Math.random() * n - n2;
- var y = Math.random() * n - n2;
- var z = Math.random() * n - n2;
- positions.push( x, y, z );
- // colors
- var vx = ( x / n ) + 0.5;
- var vy = ( y / n ) + 0.5;
- var vz = ( z / n ) + 0.5;
- color.setRGB( vx, vy, vz );
- colors.push( color.r, color.g, color.b );
- }
- geometry.setAttribute( 'position', new THREE.Float32BufferAttribute( positions, 3 ) );
- geometry.setAttribute( 'color', new THREE.Float32BufferAttribute( colors, 3 ) );
- geometry.computeBoundingSphere();
- //
- var material = new THREE.PointsMaterial( { size: 15, vertexColors: THREE.VertexColors } );
- points = new THREE.Points( geometry, material );
- scene.add( points );
- //
- renderer = new THREE.WebGLRenderer();
- renderer.setPixelRatio( window.devicePixelRatio );
- renderer.setSize( window.innerWidth, window.innerHeight );
- 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.001;
- points.rotation.x = time * 0.25;
- points.rotation.y = time * 0.5;
- renderer.render( scene, camera );
- }
- </script>
- </body>
- </html>
|