import * as THREE from 'three'; import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; import { MeshLine, MeshLineMaterial } from '../../src/THREE.MeshLine.js'; import { colors } from './colors.js'; // the colors of the palette that stand out against the background var palette = [ 0, 2, 3, 5, 6, 10, 12, 13, 14 ].map( function ( i ) { return colors[ i ]; } ); var scene = new THREE.Scene(); var camera, controls, renderer, birds, bird; var boid, boids; function birdColor() { // a color of the palette, a bit lighter or darker var color = new THREE.Color( palette[ Math.floor( Math.random() * palette.length ) ] ); return color.offsetHSL( 0, 0, ( Math.random() - .5 ) * .12 ); } function createBird() { var geometry = new THREE.BufferGeometry(); geometry.setAttribute( 'position', new THREE.Float32BufferAttribute( [ 5, 0, 0, - 5, - 2, 1, - 5, 0, 0, - 5, - 2, - 1, 0, 2, - 6, 0, 2, 6, 2, 0, 0, - 3, 0, 0 ], 3 ) ); geometry.setIndex( [ 0, 2, 1, 4, 7, 6, 5, 6, 7 ] ); return geometry; } // Based on http://www.openprocessing.org/visuals/?visualID=6910 var Boid = function() { var self = this; var vector = new THREE.Vector3(), _acceleration, _width = 500, _height = 500, _depth = 200, _goal, _neighborhoodRadius = 100, _maxSpeed = 4, _maxSteerForce = 0.1, _avoidWalls = false; this.position = new THREE.Vector3(); this.velocity = new THREE.Vector3(); _acceleration = new THREE.Vector3(); this.trail_initialized = false; // delay so trails grow organically setTimeout(function () { self.initTrail(); }, 250); this.initTrail = function () { // Create the line geometry used for storing verticies this.trail_geometry = []; for (var i = 0; i < 100; i++) { // must initialize it to the number of positions it will keep this.trail_geometry.push(this.position.clone()); } // Create the line mesh this.trail_line = new MeshLine(); this.trail_line.setPoints( this.trail_geometry, function( p ) { return p; } ); // makes width taper // Create the line material this.trail_material = new MeshLineMaterial( { color: new THREE.Color( "rgb(255, 2, 2)" ), opacity: 1, sizeAttenuation: 1, lineWidth: 1, depthTest: false, blending: THREE.AdditiveBlending, transparent: false, side: THREE.DoubleSide }); this.trail_mesh = new THREE.Mesh( this.trail_line, this.trail_material ); this.trail_mesh.frustumCulled = false; scene.add( this.trail_mesh ); this.trail_initialized = true; }; this.setGoal = function ( target ) { _goal = target; }; this.setAvoidWalls = function ( value ) { _avoidWalls = value; }; this.setWorldSize = function ( width, height, depth ) { _width = width; _height = height; _depth = depth; }; this.run = function ( boids ) { if ( _avoidWalls ) { vector.set( - _width, this.position.y, this.position.z ); vector = this.avoid( vector ); vector.multiplyScalar( 5 ); _acceleration.add( vector ); vector.set( _width, this.position.y, this.position.z ); vector = this.avoid( vector ); vector.multiplyScalar( 5 ); _acceleration.add( vector ); vector.set( this.position.x, - _height, this.position.z ); vector = this.avoid( vector ); vector.multiplyScalar( 5 ); _acceleration.add( vector ); vector.set( this.position.x, _height, this.position.z ); vector = this.avoid( vector ); vector.multiplyScalar( 5 ); _acceleration.add( vector ); vector.set( this.position.x, this.position.y, - _depth ); vector = this.avoid( vector ); vector.multiplyScalar( 5 ); _acceleration.add( vector ); vector.set( this.position.x, this.position.y, _depth ); vector = this.avoid( vector ); vector.multiplyScalar( 5 ); _acceleration.add( vector ); }/* else { this.checkBounds(); } */ if ( Math.random() > 0.5 ) { this.flock( boids ); } this.move(); }; this.flock = function ( boids ) { if ( _goal ) { _acceleration.add( this.reach( _goal, 0.005 ) ); } _acceleration.add( this.alignment( boids ) ); _acceleration.add( this.cohesion( boids ) ); _acceleration.add( this.separation( boids ) ); }; this.move = function () { this.velocity.add( _acceleration ); var l = this.velocity.length(); if ( l > _maxSpeed ) { this.velocity.divideScalar( l / _maxSpeed ); } this.position.add( this.velocity ); _acceleration.set( 0, 0, 0 ); // Advance the trail by one position if (this.trail_initialized) this.trail_line.advance( this.position ); }; this.checkBounds = function () { if ( this.position.x > _width ) this.position.x = - _width; if ( this.position.x < - _width ) this.position.x = _width; if ( this.position.y > _height ) this.position.y = - _height; if ( this.position.y < - _height ) this.position.y = _height; if ( this.position.z > _depth ) this.position.z = - _depth; if ( this.position.z < - _depth ) this.position.z = _depth; }; // this.avoid = function ( target ) { var steer = new THREE.Vector3(); steer.copy( this.position ); steer.sub( target ); steer.multiplyScalar( 1 / this.position.distanceToSquared( target ) ); return steer; }; this.repulse = function ( target ) { var distance = this.position.distanceTo( target ); if ( distance < 150 ) { var steer = new THREE.Vector3(); steer.subVectors( this.position, target ); steer.multiplyScalar( 0.5 / distance ); _acceleration.add( steer ); } }; this.reach = function ( target, amount ) { var steer = new THREE.Vector3(); steer.subVectors( target, this.position ); steer.multiplyScalar( amount ); return steer; }; this.alignment = function ( boids ) { var boid, distance, velSum = new THREE.Vector3(), count = 0; for ( var i = 0, il = boids.length; i < il; i++ ) { if ( Math.random() > 0.6 ) continue; boid = boids[ i ]; distance = boid.position.distanceTo( this.position ); if ( distance > 0 && distance <= _neighborhoodRadius ) { velSum.add( boid.velocity ); count++; } } if ( count > 0 ) { velSum.divideScalar( count ); var l = velSum.length(); if ( l > _maxSteerForce ) { velSum.divideScalar( l / _maxSteerForce ); } } return velSum; }; this.cohesion = function ( boids ) { var boid, distance, posSum = new THREE.Vector3(), steer = new THREE.Vector3(), count = 0; for ( var i = 0, il = boids.length; i < il; i ++ ) { if ( Math.random() > 0.6 ) continue; boid = boids[ i ]; distance = boid.position.distanceTo( this.position ); if ( distance > 0 && distance <= _neighborhoodRadius ) { posSum.add( boid.position ); count++; } } if ( count > 0 ) { posSum.divideScalar( count ); } steer.subVectors( posSum, this.position ); var l = steer.length(); if ( l > _maxSteerForce ) { steer.divideScalar( l / _maxSteerForce ); } return steer; }; this.separation = function ( boids ) { var boid, distance, posSum = new THREE.Vector3(), repulse = new THREE.Vector3(); for ( var i = 0, il = boids.length; i < il; i ++ ) { if ( Math.random() > 0.6 ) continue; boid = boids[ i ]; distance = boid.position.distanceTo( this.position ); if ( distance > 0 && distance <= _neighborhoodRadius ) { repulse.subVectors( this.position, boid.position ); repulse.normalize(); repulse.divideScalar( distance ); posSum.add( repulse ); } } return posSum; } } init(); animate(); function init() { camera = new THREE.PerspectiveCamera( 60, window.innerWidth / window.innerHeight, .1, 1000 ); camera.position.set( 0, 0, -500 ); var container = document.getElementById( 'container' ); renderer = new THREE.WebGLRenderer( { antialias: true, alpha: true }); renderer.setPixelRatio( window.devicePixelRatio ); renderer.setSize( window.innerWidth, window.innerHeight ); container.appendChild( renderer.domElement ); birds = []; boids = []; for ( var i = 0; i < 100; i ++ ) { boid = boids[ i ] = new Boid(); boid.position.x = Math.random() * 400 - 200; boid.position.y = Math.random() * 400 - 200; boid.position.z = Math.random() * 400 - 200; boid.velocity.x = Math.random() * 2 - 1; boid.velocity.y = Math.random() * 2 - 1; boid.velocity.z = Math.random() * 2 - 1; boid.setAvoidWalls( true ); boid.setWorldSize( 300, 300, 300 ); bird = birds[ i ] = new THREE.Mesh( createBird(), new THREE.MeshBasicMaterial( { color: birdColor(), side: THREE.DoubleSide } ) ); bird.phase = Math.floor( Math.random() * 62.83 ); scene.add( bird ); } controls = new OrbitControls( camera, renderer.domElement ); controls.enableDamping = true; controls.dampingFactor = 0.25; controls.enableZoom = false; document.addEventListener( 'mousemove', onDocumentMouseMove, false ); window.addEventListener( 'resize', onWindowResize, false ); onWindowResize(); } function onWindowResize() { camera.aspect = window.innerWidth / window.innerHeight; camera.updateProjectionMatrix(); renderer.setSize( window.innerWidth, window.innerHeight ); } function onDocumentMouseMove( event ) { var vector = new THREE.Vector3( event.clientX - window.innerWidth / 2, - event.clientY + window.innerHeight / 2, 0 ); for ( var i = 0, il = boids.length; i < il; i++ ) { boid = boids[ i ]; vector.z = boid.position.z; boid.repulse( vector ); } } // function animate() { requestAnimationFrame( animate ); controls.update(); for ( var i = 0, il = birds.length; i < il; i++ ) { boid = boids[ i ]; boid.run( boids ); bird = birds[ i ]; bird.position.copy( boids[ i ].position ); // the trail has the color of its bird if (boid.trail_initialized) boid.trail_material.color = bird.material.color; bird.rotation.y = Math.atan2( - boid.velocity.z, boid.velocity.x ); bird.rotation.z = Math.asin( boid.velocity.y / boid.velocity.length() ); bird.phase = ( bird.phase + ( Math.max( 0, bird.rotation.z ) + 0.1 ) ) % 62.83; // the tips of the wings var position = bird.geometry.attributes.position; var y = Math.sin( bird.phase ) * 5; position.setY( 4, y ); position.setY( 5, y ); position.needsUpdate = true; } render(); } function render() { renderer.render( scene, camera ); }