Javascript 帆布鸟密度
我有一个取自“three.js”项目的bird脚本。现在,有一个问题:有这么多的鸟在飞,这已经不现实了。我试着调整了很多选择,但似乎找不到合适的。请注意,我是这段代码中的noobJavascript 帆布鸟密度,javascript,html,canvas,html5-canvas,Javascript,Html,Canvas,Html5 Canvas,我有一个取自“three.js”项目的bird脚本。现在,有一个问题:有这么多的鸟在飞,这已经不现实了。我试着调整了很多选择,但似乎找不到合适的。请注意,我是这段代码中的noob var Boid = function() { var vector = new THREE.Vector3(), _acceleration, _width = 500, _height = 500, _depth = 200, _goal, _neighborhoodRad
var Boid = function() {
var vector = new THREE.Vector3(),
_acceleration, _width = 500, _height = 500, _depth = 200, _goal, _neighborhoodRadius = 50,
_maxSpeed = 4, _maxSteerForce = 0.1, _avoidWalls = false;
this.position = new THREE.Vector3();
this.velocity = new THREE.Vector3();
_acceleration = new THREE.Vector3();
this.setGoal = function ( target ) {
_goal = target;
}
this.setAvoidWalls = function ( value ) {
_avoidWalls = value;
}
this.setWorldSize = function ( width, height, depth ) {
_width = width;
_height = height;vector
_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.addSelf( vector );
vector.set( _width, this.position.y, this.position.z );
vector = this.avoid( vector );
vector.multiplyScalar( 5 );
_acceleration.addSelf( vector );
vector.set( this.position.x, - _height, this.position.z );
vector = this.avoid( vector );
vector.multiplyScalar( 5 );
_acceleration.addSelf( vector );
vector.set( this.position.x, _height, this.position.z );
vector = this.avoid( vector );
vector.multiplyScalar( 5 );
_acceleration.addSelf( vector );
vector.set( this.position.x, this.position.y, - _depth );
vector = this.avoid( vector );
vector.multiplyScalar( 5 );
_acceleration.addSelf( vector );
vector.set( this.position.x, this.position.y, _depth );
vector = this.avoid( vector );
vector.multiplyScalar( 5 );
_acceleration.addSelf( vector );
}/* else {
this.checkBounds();
}
*/
if ( Math.random() > 0.5 ) {
this.flock( boids );
}
this.move();
}
this.flock = function ( boids ) {
if ( _goal ) {
_acceleration.addSelf( this.reach( _goal, 0.005 ) );
}
_acceleration.addSelf( this.alignment( boids ) );
_acceleration.addSelf( this.cohesion( boids ) );
_acceleration.addSelf( this.separation( boids ) );
}
this.move = function () {
this.velocity.addSelf( _acceleration );
var l = this.velocity.length();
if ( l > _maxSpeed ) {
this.velocity.divideScalar( l / _maxSpeed );
}
this.position.addSelf( this.velocity );
_acceleration.set( 0, 0, 0 );
}
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.subSelf( 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.sub( this.position, target );
steer.multiplyScalar( 0.5 / distance );
_acceleration.addSelf( steer );
}
}
this.reach = function ( target, amount ) {
var steer = new THREE.Vector3();
steer.sub( target, this.position );
steer.multiplyScalar( amount );
return steer;
}
this.alignment = function ( boids ) {
var boid, 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.addSelf( 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.addSelf( boid.position );
count++;
}
}
if ( count > 0 ) {
posSum.divideScalar( count );
}
steer.sub( 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.sub( this.position, boid.position );
repulse.normalize();
repulse.divideScalar( distance );
posSum.addSelf( repulse );
}
}
return posSum;
}
}
</script>
<script>
var SCREEN_WIDTH = window.innerWidth,
SCREEN_HEIGHT = window.innerHeight,
SCREEN_WIDTH_HALF = SCREEN_WIDTH / 2,
SCREEN_HEIGHT_HALF = SCREEN_HEIGHT / 2;
var camera, scene, renderer,
birds, bird;
var boid, boids;
init();
animate();
function init() {
camera = new THREE.PerspectiveCamera( 75, SCREEN_WIDTH / SCREEN_HEIGHT, 1, 10000 );
camera.position.z = 450;
scene = new THREE.Scene();
birds = [];
boids = [];
for ( var i = 0; i < 200; 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( 500, 500, 400 );
bird = birds[ i ] = new THREE.Mesh( new Bird(), new THREE.MeshBasicMaterial( { color:Math.random() * 0xffffff, side: THREE.DoubleSide } ) );
bird.phase = Math.floor( Math.random() * 62.83 );
bird.position = boids[ i ].position;
scene.add( bird );
}
renderer = new THREE.CanvasRenderer();
// renderer.autoClear = false;
renderer.setSize( SCREEN_WIDTH, SCREEN_HEIGHT );
document.addEventListener( 'mousemove', onDocumentMouseMove, false );
document.body.appendChild( renderer.domElement );
//
window.addEventListener( 'resize', onWindowResize, false );
}
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 - SCREEN_WIDTH_HALF, - event.clientY + SCREEN_HEIGHT_HALF, 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 );
render();
stats.update();
}
function render() {
for ( var i = 0, il = birds.length; i < il; i++ ) {
boid = boids[ i ];
boid.run( boids );
bird = birds[ i ];
color = bird.material.color;
color.r = color.g = color.b = ( 500 - bird.position.z ) / 1000;
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;
bird.geometry.vertices[ 5 ].y = bird.geometry.vertices[ 4 ].y = Math.sin( bird.phase ) * 5;
}
renderer.render( scene, camera );
}
var Boid=function(){
var vector=new THREE.Vector3(),
_加速度,_宽度=500,_高度=500,_深度=200,_目标,_邻里半径=50,
_maxSpeed=4,_maxSteerForce=0.1,_avoidWalls=false;
this.position=新的3.Vector3();
this.velocity=新的3.Vector3();
_加速度=新的3.Vector3();
this.setGoal=函数(目标){
_目标=目标;
}
this.setAvoidWalls=函数(值){
_避免墙=价值;
}
this.setWorldSize=函数(宽度、高度、深度){
_宽度=宽度;
_高度=高度;向量
_深度=深度;
}
this.run=函数(boids){
如果(避免墙壁){
向量集(-u宽度,此位置.y,此位置.z);
向量=这个。避免(向量);
向量多重标度(5);
_加速度。addSelf(向量);
向量集(_width,this.position.y,this.position.z);
向量=这个。避免(向量);
向量多重标度(5);
_加速度。addSelf(向量);
vector.set(此位置.x,-u高度,此位置.z);
向量=这个。避免(向量);
向量多重标度(5);
_加速度。addSelf(向量);
vector.set(this.position.x,_height,this.position.z);
向量=这个。避免(向量);
向量多重标度(5);
_加速度。addSelf(向量);
vector.set(此.position.x,此.position.y,--u深度);
向量=这个。避免(向量);
向量多重标度(5);
_加速度。addSelf(向量);
vector.set(this.position.x,this.position.y,_深度);
向量=这个。避免(向量);
向量多重标度(5);
_加速度。addSelf(向量);
}/*否则{
this.checkBounds();
}
*/
if(Math.random()>0.5){
这是羊群;
}
这个。移动();
}
this.flock=函数(boids){
如果(_目标){
_加速.addSelf(这个.reach(_目标,0.005));
}
_加速度.addSelf(this.alignment(boids));
_acceleration.addSelf(this.ne聚力(boids));
_加速度.addSelf(this.separation(boids));
}
this.move=函数(){
这个.velocity.addSelf(_加速度);
var l=这个.velocity.length();
如果(l>\u最大速度){
此.velocity.divideScalar(l/_maxSpeed);
}
this.position.addSelf(this.velocity);
_加速度设置(0,0,0);
}
this.checkBounds=函数(){
如果(this.position.x>\u width)this.position.x=-\u width;
如果(this.position.x<-\u width)this.position.x=\u width;
如果(this.position.y>\u height)this.position.y=-\u height;
如果(this.position.y<-\u高度)this.position.y=\u高度;
如果(this.position.z>_depth)this.position.z=-_depth;
如果(this.position.z<-\u depth)this.position.z=\u depth;
}
//
this.avoid=函数(目标){
var steer=new THREE.Vector3();
转向。复制(此位置);
转向。自底(目标);
转向多刻度(1/此位置距离方形(目标));
返回转向;
}
this.shuffer=函数(目标){
var距离=此.position.distanceTo(目标);
如果(距离<150){
var steer=new THREE.Vector3();
转向接头(该位置、目标);
转向倍率标度(0.5/距离);
_加速。添加自我(转向);
}
}
this.reach=功能(目标、金额){
var steer=new THREE.Vector3();
转向接头(目标,本位置);
转向倍数标度(数量);
返回转向;
}
this.alignment=函数(boids){
var boid,velSum=new THREE.Vector3(),
计数=0;
对于(var i=0,il=boids.length;i0.6)继续;
boid=boid[i];
距离=锅炉位置距离(此位置);
如果(距离>0&&距离0){
velSum.divideScalar(计数);
var l=velSum.length();
如果(l>\u maxSteerForce){
velSum.divideScalar(l/_maxSteerForce);
}
}
返回velSum;
}
this.ne聚力=函数(boids){
var boid,距离,
posSum=new-THREE.Vector3(),
转向=新的三个。矢量3(),
计数=0;
对于(var i=0,il=boids.length;i0.6)继续;
boid=boid[i];
距离=锅炉位置距离(此位置);
如果(距离>0&&距离0){
分保(计数);
}
转向接头(负鼠,此位置);
var l=转向长度();
如果(l>\u maxSteerForce){
steer.divideScalar(l/_最大值
for ( var i = 0; i < 200; i ++ ) { <-- reduce the 200