Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <body>
    <div id="container"></div>
    
    <script src="https://unpkg.com/fast-simplex-noise@2.2.0/main.js"></script>
    <script src="https://unpkg.com/three@0.97.0/build/three.js"></script>
    <script src="https://unpkg.com/three@0.97.0/examples/js/controls/OrbitControls.js"></script>
</body>
              
            
!

CSS

              
                *{
    margin: 0;
    padding: 0;
}

body {
    overflow: hidden;
    background: #fb3550;
}
              
            
!

JS

              
                let w = window.innerWidth,
    h = window.innerHeight,
    spaceSize = 700;

let container, renderer, scene, camera, control, agents;
let simplex = new FastSimplexNoise( {
    min: 0,
    max: 1
} );

const nbAgents = 5000;

class Agent{
    constructor( x, y, z ){
        this.pos = new THREE.Vector3(x, y, z);
        this.vel = new THREE.Vector3();
        this.acc = new THREE.Vector3();
        this.mass = 1;
        this.maxSpeed = 3;
        this.maxForce = 0.3;
        this.theta = 0;
        this.phi = 0;

        this.material = new THREE.MeshPhongMaterial({
            color: [0xfb3550,0xfb3550,0xfb3550,0xffffff,0x000000][Math.random()*5|0],
            shading: THREE.FlatShading
        });

        let geometry = new THREE.CylinderGeometry(0, 5, 30, 8);
        geometry.rotateX(Math.PI / 2);
        this.mesh = new THREE.Mesh(geometry, this.material);
        this.mesh.position.copy(this.pos);
        this.mesh.up.set(0, 0, 1);
        scene.add(this.mesh);
    }

    seek( target ){
        let desired = target.clone().sub( this.pos );

        let d = desired.length();
        if( d < 20 ) {
            let m = d / 40 * this.maxSpeed;
            desired.setLength( m );
        } else {
            desired.setLength( this.maxSpeed );
        }

        let steer = desired.clone().sub( this.vel );
        if ( steer.lengthSq() > this.maxSpeed * this.maxSpeed ) steer.setLength( this.maxForce );

        this.applyForce( steer );
    }

    wander( t ){
        let wanderR = 25;
        let wanderD = 80;
        let change = 0.2 * Math.PI * 4;
        this.theta = simplex.in3D( t / 10000 + this.pos.x / 500, this.pos.y / 800, this.pos.z / 720 ) * change;
        this.phi = simplex.in3D( t / 10000 + this.pos.y / 500, this.pos.z / 800, this.pos.x / 720 ) * change;

        let sphereloc = this.vel.clone();
        sphereloc.setLength(wanderD);
        sphereloc.add(this.pos);

        let sphereOffSet = new THREE.Vector3(
            wanderR * Math.sin(this.theta) * Math.cos(this.phi),
            wanderR * Math.sin(this.theta) * Math.sin(this.phi),
            wanderR * Math.cos(this.theta)
        );
        sphereOffSet.lerp(this.vel, 0.5);
        let target = sphereloc.add(sphereOffSet);
        this.seek(target);
    }

    applyForce( force ){
        let f = force.clone();
        f.divideScalar( this.mass );
        this.acc.add( f );
    }

    border(){
        let margin = 10;
        if( this.pos.lengthSq() > spaceSize * spaceSize + margin * margin ){
            this.pos.multiplyScalar( -1 ).setLength( spaceSize );
        }
    }

    update( t ){
        this.vel.add( this.acc );
        if( this.vel.lengthSq() > this.maxSpeed * this.maxSpeed ) this.vel.setLength( this.maxSpeed );
        this.pos.add( this.vel );

        // reset acceleration
        this.acc.set( 0, 0, 0 );

        this.mesh.position.copy( this.pos );
        this.mesh.lookAt( this.pos.clone().add( this.vel ) );
    }
}



( function init(){
    // renderer
    renderer = new THREE.WebGLRenderer({
        antialias: true
    });
    renderer.setPixelRatio(window.devicePixelRatio);
    renderer.setSize(w, h);
    container = document.getElementById('container');
    container.appendChild(renderer.domElement);

    // world
    scene = new THREE.Scene();
    scene.fog = new THREE.FogExp2(0x1E2630, 0.0012);
    renderer.setClearColor(scene.fog.color);

    // camera
    camera = new THREE.PerspectiveCamera(60, w / h, 1, 2000);
    camera.position.x = 0;
    camera.position.y = 400;
    camera.position.z = 500;
    controls = new THREE.OrbitControls(camera, renderer.domElement);

    // lights
    light = new THREE.DirectionalLight(0xffffff);
    light.position.set(1, 1, 1);
    scene.add(light);
    light = new THREE.DirectionalLight(0x002288);
    light.position.set(-1, -1, -1);
    scene.add(light);
    light = new THREE.AmbientLight(0x222222);
    scene.add(light);

    // Dome
    geometry = new THREE.IcosahedronGeometry(spaceSize, 1);
    let domeMaterial = new THREE.MeshPhongMaterial({
        color: 0x1E2630,
        shading: THREE.FlatShading,
        side: THREE.BackSide
    });
    let dome = new THREE.Mesh(geometry, domeMaterial);
    scene.add(dome);

    agents = (new Array(nbAgents)).fill(0).map(d =>
        new Agent(
            (Math.random() * 2 - 1) * spaceSize,
            (Math.random() * 2 - 1) * spaceSize,
            (Math.random() * 2 - 1) * spaceSize
        )
    );

    window.addEventListener('resize', e => {
        w = window.innerWidth;
        h = window.innerHeight;

        camera.aspect = w / h;
        camera.updateProjectionMatrix();
        renderer.setSize( w, h );
    }, false);
} )();

( function animate( t ){
    requestAnimationFrame( animate );

    agents.forEach( agent => {
        agent.wander( t );
        agent.border();
        agent.update( t );
    } );

    renderer.render( scene, camera );
} )( 0 );

              
            
!
999px

Console