123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159 |
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <title>three.js webgl - buffergeometry - lines</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 - lines</div>
- <script type="importmap">
- {
- "imports": {
- "three": "../build/three.module.js",
- "three/addons/": "./jsm/"
- }
- }
- </script>
- <script type="module">
- import * as THREE from 'three';
- import Stats from 'three/addons/libs/stats.module.js';
- let container, stats, clock;
- let camera, scene, renderer;
- let line;
- const segments = 10000;
- const r = 800;
- let t = 0;
- init();
- function init() {
- container = document.getElementById( 'container' );
- //
- camera = new THREE.PerspectiveCamera( 27, window.innerWidth / window.innerHeight, 1, 4000 );
- camera.position.z = 2750;
- scene = new THREE.Scene();
- clock = new THREE.Clock();
- const geometry = new THREE.BufferGeometry();
- const material = new THREE.LineBasicMaterial( { vertexColors: true } );
- const positions = [];
- const colors = [];
- for ( let i = 0; i < segments; i ++ ) {
- const x = Math.random() * r - r / 2;
- const y = Math.random() * r - r / 2;
- const z = Math.random() * r - r / 2;
- // positions
- positions.push( x, y, z );
- // colors
- colors.push( ( x / r ) + 0.5 );
- colors.push( ( y / r ) + 0.5 );
- colors.push( ( z / r ) + 0.5 );
- }
- geometry.setAttribute( 'position', new THREE.Float32BufferAttribute( positions, 3 ) );
- geometry.setAttribute( 'color', new THREE.Float32BufferAttribute( colors, 3 ) );
- generateMorphTargets( geometry );
- geometry.computeBoundingSphere();
- line = new THREE.Line( geometry, material );
- scene.add( line );
- //
- renderer = new THREE.WebGLRenderer();
- renderer.setPixelRatio( window.devicePixelRatio );
- renderer.setSize( window.innerWidth, window.innerHeight );
- renderer.setAnimationLoop( animate );
- 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() {
- const delta = clock.getDelta();
- const time = clock.getElapsedTime();
- line.rotation.x = time * 0.25;
- line.rotation.y = time * 0.5;
- t += delta * 0.5;
- line.morphTargetInfluences[ 0 ] = Math.abs( Math.sin( t ) );
- renderer.render( scene, camera );
- stats.update();
- }
- function generateMorphTargets( geometry ) {
- const data = [];
- for ( let i = 0; i < segments; i ++ ) {
- const x = Math.random() * r - r / 2;
- const y = Math.random() * r - r / 2;
- const z = Math.random() * r - r / 2;
- data.push( x, y, z );
- }
- const morphTarget = new THREE.Float32BufferAttribute( data, 3 );
- morphTarget.name = 'target1';
- geometry.morphAttributes.position = [ morphTarget ];
- }
- </script>
- </body>
- </html>
|