cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

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

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

Save Automatically?

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.

            
              <div id="container"></div>
            
          
!
            
              body {
				color: #cccccc;
				font-family:Monospace;
				font-size:13px;
				text-align:center;

				background-color: #000000;
				margin: 0px;
				overflow: hidden;
			}

			#info {
				position: absolute;
				top: 0px; width: 100%;
				padding: 5px;
			}

			a {
				color: #0080ff;
			}
            
          
!
            
              //SPOTLIGHTS
			var renderSpot1 = true;
			var renderSpot2 = true;
			var renderSpot3 = false;
			var doAnimate = true;
			var doShadows = false;
			var speedMin = 1500;
			var speedVariance = 3250;
			var matFloor = new THREE.MeshPhongMaterial();
			var geoFloor = new THREE.BoxGeometry( 2000, 1, 2000 );
			var mshFloor = new THREE.Mesh( geoFloor, matFloor );
			var ambient = new THREE.AmbientLight( 0x000000 );
			var spotLight1 = createSpotlight( 0xFF7F00 );
			var spotLight2 = createSpotlight( 0x00FF7F );

			var floorY = 0;

			var lightHelper1, lightHelper2;

			var spot1Y = 750;
			var spot1Dist = 1030;

			var spot2Y = 1000;
			var spot2Dist = 1500;


			if ( ! Detector.webgl ) Detector.addGetWebGLMessage();

			//PARTICLES
			var group;
			var container, stats;
			var particlesData = [];
			var camera, scene, renderer;
			var positions, colors;
			var particles;
			var pointCloud;
			var particlePositions;
			var linesMesh;

			var minDistance = 150;
			var maxConnections = 30;
			var maxParticleCount = 150;
			var particleCount = 50;
			var r = 800;
			var rHalf = r / 2;

			//MH - custom vars
			var gui;
			var fov = 45;
			var posY = 1500;
			var enableControls = true;
			var pColor = 0XFFFFFF;
			var pSize = 10;
			var pSizeMax = 30;
			var pSizeMin = 10;
			var rotationSpeed = .01;

			var controls; //reference to orbit controls
			var effectController;
			var pMaterial; //reference to point material

			init();
			initGUI();
			animate();
			animateSpotlights();

			function initGUI() {

				gui = new dat.GUI();
				var f0 = gui.addFolder('Particles');

				effectController = {
					showDots: true,
					showLines: true,
					minDistance: 150,
					limitConnections: true,
					maxConnections: maxConnections,
					particleCount: 50,
					'camera y': posY,
					fov: fov,
					'enable controls': enableControls,
					'point color': pMaterial.color.getHex(),
					'point size': pSize,
					"r": r,
					'rotation speed': rotationSpeed,
				};

				f0.add( effectController, "minDistance", 50, 300 ).step(10);
				f0.add( effectController, "maxConnections", 1, 50).step(1);
				f0.add( effectController, "particleCount", 0, maxParticleCount, 1 ).step(10).onChange( function( value ) {
					particleCount = parseInt( value );
					particles.setDrawRange( 0, particleCount );
				});

				f0.add( effectController, "camera y", 400, 1500 ).step(50).onChange( function( value ) {
					camera.position.y = value;
				});

				f0.add( effectController, "fov", 0, 90 ).onChange( function( value ) {
					camera.fov = value;
					camera.updateProjectionMatrix();
				});

				f0.add( effectController, "enable controls" ).onChange( function( value ) {
					enableControls = value;
					controls.enabled = value;
				});

				f0.addColor( effectController, "point color" ).onChange( function( value ) {
					pMaterial.color.setHex(value);
				});

				f0.add( effectController, "point size", pSizeMin,pSizeMax).step(1).onChange( function( value ) {
					pMaterial.size = value;
				});

				f0.add( effectController, "r", 100,4000).step(100).onChange( function( value ) {
					r = value;
					rHalf = r/2;
				});

				f0.add( effectController, "rotation speed", -1,1).step(.1).onChange( function( value ) {
					rotationSpeed = value;
				});


				//SPOTLIGHTS
				var f1 = gui.addFolder('Light1');
				var f2 = gui.addFolder('Light2');
				var f3 = gui.addFolder('Misc');


				var params = {
					'light color': spotLight1.color.getHex(),
					intensity: spotLight1.intensity,
					distance: spot1Dist,
					angle: spotLight1.angle,
					penumbra: spotLight1.penumbra,
					decay: spotLight1.decay,
					x: spotLight1.position.x,
					y: spot1Y,
					z: spotLight1.position.z,
				}

				//LIGHT 1
				f1.addColor( params, 'light color' ).onChange( function ( val ) {
					spotLight1.color.setHex( val );
				} );

				f1.add( params, 'intensity', 0, 2 ).onChange( function ( val ) {
					spotLight1.intensity = val;
				} );


				f1.add( params, 'distance', 500, 1500 ).step(10).onChange( function ( val ) {
					spotLight1.distance = val;
				} );

				f1.add( params, 'angle', 0, Math.PI / 3 ).onChange( function ( val ) {
					spotLight1.angle = val;
				} );

				f1.add( params, 'penumbra', 0, 1 ).onChange( function ( val ) {
					spotLight1.penumbra = val;
				} );

				f1.add( params, 'decay', 1, 2 ).onChange( function ( val ) {
					spotLight1.decay = val;
				} );

				f1.add( params, 'x', -150, 150 ).step(10).onChange( function ( val ) {
					doAnimate = false;
					spotLight1.position.x = val;
				} );

				f1.add( params, 'y', 300, 1000 ).step(50).onChange( function ( val ) {
					doAnimate = false;
					spotLight1.position.y = val;

				} );

				f1.add( params, 'z', -150, 150 ).step(10).onChange( function ( val ) {
					doAnimate = false;
					spotLight1.position.z = val;
				} );


				//LIGHT 2

				var params2 = {
					'light color': spotLight2.color.getHex(),
					intensity: spotLight2.intensity,
					distance: spot2Dist,
					angle: spotLight2.angle,
					penumbra: spotLight2.penumbra,
					decay: spotLight2.decay,
					x: spotLight2.position.x,
					y: spot2Y,
					z: spotLight2.position.z,
				}

				f2.addColor( params2, 'light color' ).onChange( function ( val ) {
					spotLight2.color.setHex( val );
				} );

				f2.add( params2, 'intensity', 0, 2 ).onChange( function ( val ) {
					spotLight2.intensity = val;
				} );


				f2.add( params2, 'distance', 1000, 2000 ).step(20).onChange( function ( val ) {
					spotLight2.distance = val;
				} );

				f2.add( params2, 'angle', 0, Math.PI / 3 ).onChange( function ( val ) {
					spotLight2.angle = val;
				} );

				f2.add( params2, 'penumbra', 0, 1 ).onChange( function ( val ) {
					spotLight2.penumbra = val;
				} );

				f2.add( params2, 'decay', 1, 2 ).onChange( function ( val ) {
					spotLight2.decay = val;
				} );
				f2.add( params, 'x', -150, 150 ).step(10).onChange( function ( val ) {
					doAnimate = false;
					spotLight2.position.x = val;
				} );

				f2.add( params, 'y', 500, 1500 ).step(25).onChange( function ( val ) {
					doAnimate = false;
					spotLight2.position.y = val;
				} );

				f2.add( params, 'z', -150, 150 ).step(10).onChange( function ( val ) {
					doAnimate = false;
					spotLight2.position.z = val;
				} );


				//AMBIENT
				var params3 = {
					'ambient light': ambient.color.getHex(),
					'do animate' : doAnimate,
					'anim time min': speedMin,
					'anim time var': speedVariance,
					'floor y' : floorY,
				}

				f3.addColor( params3, 'ambient light' ).onChange( function ( val ) {
					ambient.color.setHex( val );
				} );

				f3.add( params3, 'do animate' ).onChange( function ( val ) {
					doAnimate = val;
					if (doAnimate){
						setTimeout( animate, 5000 );
					}
				} );

				f3.add( params3, 'anim time min',0,1750 ).onChange( function ( val ) {
					speedMin = val;
				} );

				f3.add( params3, 'anim time var',0,3250 ).onChange( function ( val ) {
					speedVariance = val;
				} );

				f3.add( params3, 'floor y',-200,200 ).step(10).onChange( function ( val ) {
					mshFloor.position.y = val;
				} );


 				gui.close();

				//END SPOTLIGHTS


			}

			function init() {

				container = document.getElementById( 'container' );

				//

				camera = new THREE.PerspectiveCamera( fov, window.innerWidth / window.innerHeight, 1, 4000 ); //fov, aspect, near, far
				//camera.position.z = posZ;
				camera.position.set(0,posY,0); //MH - from spotlights

				controls = new THREE.OrbitControls( camera, container );
				controls.enabled = enableControls;

				scene = new THREE.Scene();


				group = new THREE.Group();
				scene.add( group );

				var segments = maxParticleCount * maxParticleCount;

				positions = new Float32Array( segments * 3 );
				colors = new Float32Array( segments * 3 );

				pMaterial = new THREE.PointsMaterial( {
					color: pColor,
					size: pSize,
					blending: THREE.AdditiveBlending,
					transparent: true,
					sizeAttenuation: true,
				} );

				particles = new THREE.BufferGeometry();
				particlePositions = new Float32Array( maxParticleCount * 3 );

				for ( var i = 0; i < maxParticleCount; i++ ) {

					var x = Math.random() * r - r / 2;
					var y = Math.random() * r - r / 2;
					var z = Math.random() * r - r / 2;

					particlePositions[ i * 3     ] = x;
					particlePositions[ i * 3 + 1 ] = y;
					particlePositions[ i * 3 + 2 ] = z;

					// add it to the geometry
					particlesData.push( {
						velocity: new THREE.Vector3( -1 + Math.random() * 2, -1 + Math.random() * 2,  -1 + Math.random() * 2 ),
						numConnections: 0
					} );

				}

				particles.setDrawRange( 0, particleCount );
				particles.addAttribute( 'position', new THREE.BufferAttribute( particlePositions, 3 ).setDynamic( true ) );

				// create the particle system
				pointCloud = new THREE.Points( particles, pMaterial );
				group.add( pointCloud );

				var geometry = new THREE.BufferGeometry();

				geometry.addAttribute( 'position', new THREE.BufferAttribute( positions, 3 ).setDynamic( true ) );
				geometry.addAttribute( 'color', new THREE.BufferAttribute( colors, 3 ).setDynamic( true ) );

				geometry.computeBoundingSphere();

				geometry.setDrawRange( 0, 0 );

				var material = new THREE.LineBasicMaterial( {
					vertexColors: THREE.VertexColors,
					blending: THREE.AdditiveBlending,
					transparent: true
				} );

				linesMesh = new THREE.LineSegments( geometry, material );
				group.add( linesMesh );

				//

				renderer = new THREE.WebGLRenderer( { antialias: true } );
				renderer.shadowMap.enabled = doShadows;
				renderer.shadowMap.type = THREE.PCFSoftShadowMap;
				renderer.setPixelRatio( window.devicePixelRatio );
				renderer.setSize( window.innerWidth, window.innerHeight );

				renderer.gammaInput = true;
				renderer.gammaOutput = true;

				//SPOTLIGHTS
				spotLight1.position.set( 15, spot1Y, 45 );
				spotLight2.position.set( 0, spot2Y, 35 );
				spotLight1.distance = spot1Dist;
				spotLight2.distance = spot2Dist;

				matFloor.color.set( 0x808080 );

				mshFloor.receiveShadow = doShadows;
				mshFloor.position.set( 0, 0, 50 );

				scene.add( mshFloor );
				scene.add( ambient );


				if (renderSpot1){
					scene.add(spotLight1);
				}

				if (renderSpot2){
					scene.add(spotLight2);
				}

				lightHelper1 = new THREE.SpotLightHelper( spotLight1 );
				lightHelper2 = new THREE.SpotLightHelper( spotLight2 );

				/*var axis = new THREE.AxisHelper( 500 );
				axis.position.set( 0, 500, 0 );
				scene.add( axis );*/

				//scene.add( lightHelper1, lightHelper2); //MH


				controls.target.set( 0, 100, 0 ); //MH - from spotlights
				controls.maxPolarAngle = Math.PI / 2;
				controls.update();
				//console.log(camera);

				container.appendChild( renderer.domElement );

				//

				window.addEventListener( 'resize', onWindowResize, false );



			}

			function onResize() {

				camera.aspect = window.innerWidth / window.innerHeight;
				camera.updateProjectionMatrix();
				renderer.setSize( window.innerWidth, window.innerHeight );

			}

			function tween( light ) {

				new TWEEN.Tween( light ).to( {
					angle: ( Math.random() * 0.7 ) + 0.1,
					penumbra: Math.random() + 1
				}, Math.random() * speedVariance + speedMin )
				.easing( TWEEN.Easing.Quadratic.Out ).start();

				new TWEEN.Tween( light.position ).to( {
					x: ( Math.random() * 300 ) - 150,
					z: ( Math.random() * 300 ) - 150,
				}, Math.random() * speedVariance + speedMin )
				.easing( TWEEN.Easing.Quadratic.Out ).start();

			}

			function createSpotlight( color ) {

				var newObj = new THREE.SpotLight( color, 2 );

				newObj.castShadow = doShadows;
				newObj.angle = 0.3;
				newObj.penumbra = 0.2;
				newObj.decay = 2;
				newObj.distance = 50;

				newObj.shadow.mapSize.width = 1024;
				newObj.shadow.mapSize.height = 1024;

				return newObj;

			}

			function onWindowResize() {

				camera.aspect = window.innerWidth / window.innerHeight;
				camera.updateProjectionMatrix();

				renderer.setSize( window.innerWidth, window.innerHeight );

			}

			function animateSpotlights(){

				//SPOTLIGHTS

				if (doAnimate){
					if (renderSpot1){
					tween( spotLight1 );
					}

					if (renderSpot2){
						tween( spotLight2 );
					}

					setTimeout( animateSpotlights, 5000 );
				}

				//END SPOTLIGHTS

			}

			function animate() {

				var vertexpos = 0;
				var colorpos = 0;
				var numConnected = 0;

				for ( var i = 0; i < particleCount; i++ )
					particlesData[ i ].numConnections = 0;

				for ( var i = 0; i < particleCount; i++ ) {

					// get the particle
					var particleData = particlesData[i];

					particlePositions[ i * 3     ] += particleData.velocity.x;
					particlePositions[ i * 3 + 1 ] += particleData.velocity.y;
					particlePositions[ i * 3 + 2 ] += particleData.velocity.z;

					if ( particlePositions[ i * 3 + 1 ] < -rHalf || particlePositions[ i * 3 + 1 ] > rHalf )
						particleData.velocity.y = -particleData.velocity.y;

					if ( particlePositions[ i * 3 ] < -rHalf || particlePositions[ i * 3 ] > rHalf )
						particleData.velocity.x = -particleData.velocity.x;

					if ( particlePositions[ i * 3 + 2 ] < -rHalf || particlePositions[ i * 3 + 2 ] > rHalf )
						particleData.velocity.z = -particleData.velocity.z;

					if ( effectController.limitConnections && particleData.numConnections >= effectController.maxConnections )
						continue;

					// Check collision
					for ( var j = i + 1; j < particleCount; j++ ) {

						var particleDataB = particlesData[ j ];
						if ( effectController.limitConnections && particleDataB.numConnections >= effectController.maxConnections )
							continue;

						var dx = particlePositions[ i * 3     ] - particlePositions[ j * 3     ];
						var dy = particlePositions[ i * 3 + 1 ] - particlePositions[ j * 3 + 1 ];
						var dz = particlePositions[ i * 3 + 2 ] - particlePositions[ j * 3 + 2 ];
						var dist = Math.sqrt( dx * dx + dy * dy + dz * dz );

						if ( dist < effectController.minDistance ) {

							particleData.numConnections++;
							particleDataB.numConnections++;

							var alpha = 1.0 - dist / effectController.minDistance;

							positions[ vertexpos++ ] = particlePositions[ i * 3     ];
							positions[ vertexpos++ ] = particlePositions[ i * 3 + 1 ];
							positions[ vertexpos++ ] = particlePositions[ i * 3 + 2 ];

							positions[ vertexpos++ ] = particlePositions[ j * 3     ];
							positions[ vertexpos++ ] = particlePositions[ j * 3 + 1 ];
							positions[ vertexpos++ ] = particlePositions[ j * 3 + 2 ];

							colors[ colorpos++ ] = alpha;
							colors[ colorpos++ ] = alpha;
							colors[ colorpos++ ] = alpha;

							colors[ colorpos++ ] = alpha;
							colors[ colorpos++ ] = alpha;
							colors[ colorpos++ ] = alpha;

							numConnected++;
						}
					}
				}


				linesMesh.geometry.setDrawRange( 0, numConnected * 2 );
				linesMesh.geometry.attributes.position.needsUpdate = true;
				linesMesh.geometry.attributes.color.needsUpdate = true;

				pointCloud.geometry.attributes.position.needsUpdate = true;

				requestAnimationFrame( animate );

				render();

			}

			function render() {

				if (doAnimate){
					TWEEN.update();
				}

				var time = Date.now() * 0.001;

				group.rotation.y = time * rotationSpeed;
				renderer.render( scene, camera );

				//console.log(camera.scale)
				

			}
            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.
Loading ..................

Console