123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106 |
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <title>three.js webgl - trails</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>
- <script type="module">
- import * as THREE from '../build/three.module.js';
- import Stats from './jsm/libs/stats.module.js';
- let stats;
- let camera, scene, renderer, clock;
- init();
- animate();
- function init() {
- const container = document.createElement( 'div' );
- document.body.appendChild( container );
- camera = new THREE.PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 0.1, 10 );
- camera.position.set( 0, 0, 1 );
- clock = new THREE.Clock();
- scene = new THREE.Scene();
- const colorArray = [ new THREE.Color( 0xff0080 ), new THREE.Color( 0xffffff ), new THREE.Color( 0x8000ff ) ];
- const positions = [];
- const colors = [];
- for ( let i = 0; i < 100; i ++ ) {
- positions.push( Math.random() - 0.5, Math.random() - 0.5, Math.random() - 0.5 );
- const clr = colorArray[ Math.floor( Math.random() * colorArray.length ) ];
- colors.push( clr.r, clr.g, clr.b );
- }
- const geometry = new THREE.BufferGeometry();
- geometry.setAttribute( 'position', new THREE.Float32BufferAttribute( positions, 3 ) );
- geometry.setAttribute( 'color', new THREE.Float32BufferAttribute( colors, 3 ) );
- const material = new THREE.PointsMaterial( { size: 4, vertexColors: true, depthTest: false, sizeAttenuation: false } );
- const mesh = new THREE.Points( geometry, material );
- scene.add( mesh );
- renderer = new THREE.WebGLRenderer( { preserveDrawingBuffer: true } );
- renderer.setPixelRatio( window.devicePixelRatio );
- renderer.setSize( window.innerWidth, window.innerHeight );
- renderer.autoClearColor = false;
- container.appendChild( renderer.domElement );
- stats = new Stats();
- container.appendChild( stats.dom );
- //
- window.addEventListener( 'resize', onWindowResize );
- }
- 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() {
- const elapsedTime = clock.getElapsedTime();
- scene.rotation.y = elapsedTime * 0.5;
- renderer.render( scene, camera );
- }
- </script>
- </body>
- </html>
|