Pen Settings

HTML

CSS

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. You can use the CSS from another Pen by using it's URL and the proper URL extention.

+ 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

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.

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

              
                <!--
TODO
- nozzles -> particle+cones+lensflare
- texture -> ??
- starship lights
-->
<script type='x-shader/x-vertex' id='vShader'>
varying vec3 vPosition ;
varying vec3 vNormal ;
	
void main(){
	vNormal=normal;
	vPosition=position;
	
	gl_Position = projectionMatrix * modelViewMatrix * vec4(position,1.);
	
}
</script>
<script type='x-shader/x-fragment' id='fShader'>
	
uniform float time ;
uniform float maxZ ;
uniform vec3 light ;
	
varying vec3 vPosition;
varying vec3 vNormal ;
	
void main () {
	
	float lightCoeff = ( dot( vNormal, light ) + .5 ) ;
	vec3 color = vec3(.16,.2,.0) * ( .1 + lightCoeff );
	if( vPosition.y > 3.7 && vPosition.y < 3.78 && abs( vPosition.x ) > 1.25 && abs( vPosition.x ) < 1.3 ) color = vec3( 1. );
	
	float transition = ( maxZ - time + 5. ) - ( vPosition.z + cos( gl_FragCoord.y / 10. + time * 3. ) / 10. ) ;
	
	vec4 result = vec4( color, transition ) ;
	
	float positiveBlue = max( ( cos( transition / 4. - .08 ) - .99995 ), .0 );
	
	if ( transition < .0 ) {
		color = vec3( .15) ;
		transition = .15 ;
		result = vec4( color, transition );
	}
	
	//result += vec4(.2, 0., 1., 1.) * positiveBlue * 35000. * ( 1. + sin( dot( gl_FragCoord.xy , vec2( 12.9898, 78.233 ) ) * 100. ) );
	/*
	if( cos( vPosition.x * 10. ) > .99 ) result.x *= 3.;
	if( cos( vPosition.y * 10. ) > .99 ) result.y *= 3.;
	if( cos( vPosition.z * 10. ) > .99 ) result.z *= 3.;
	if( cos( vPosition.x ) > .97 ) result.x *= 3.;
	if( cos( vPosition.y ) > .97 ) result.y *= 3.;
	if( cos( vPosition.z ) > .97 ) result.z *= 3.;*/
			
	gl_FragColor = result;
	
}
</script>
	
<script type='x-shader/x-vertex' id='starVShader'>
void main(){
	gl_Position = projectionMatrix * modelViewMatrix * vec4(position,1.);
}
</script>
<script type='x-shader/x-fragment' id='starFShader'>
uniform float width;
uniform float height;
#define OCTAVES 5

vec3 mod289(vec3 x){
    return x - floor(x * (1.0 / 289.0)) * 289.0;
}
vec2 mod289(vec2 x) {
    return x - floor(x * (1.0 / 289.0)) * 289.0;
}
vec3 permute(vec3 x) {
    return mod289(((x*34.0)+1.0)*x);
}

// Simplex noise 
// https://github.com/ashima/webgl-noise 
// Copyright (C) 2011 Ashima Arts. All rights reserved.
float snoise(vec2 v) {
    const vec4 C = vec4(0.211324865405187, 
                        0.366025403784439, 
                        -0.577350269189626, 
                        0.024390243902439); 
    vec2 i  = floor(v + dot(v, C.yy) );
    vec2 x0 = v -   i + dot(i, C.xx);

    vec2 i1;
    i1 = (x0.x > x0.y) ? vec2(1.0, 0.0) : vec2(0.0, 1.0);
    vec4 x12 = x0.xyxy + C.xxzz;
    x12.xy -= i1;

    i = mod289(i);
    vec3 p = permute( permute( i.y + vec3(0.0, i1.y, 1.0 ))
                     + i.x + vec3(0.0, i1.x, 1.0 ));

    vec3 m = max(0.5 - vec3(dot(x0,x0), dot(x12.xy,x12.xy), dot(x12.zw,x12.zw)), 0.0);
    m = m*m ;
    m = m*m ;

    vec3 x = 2.0 * fract(p * C.www) - 1.0;
    vec3 h = abs(x) - 0.5;
    vec3 ox = floor(x + 0.5);
    vec3 a0 = x - ox;

    m *= 1.79284291400159 - 0.85373472095314 * ( a0*a0 + h*h );

    vec3 g;
    g.x  = a0.x  * x0.x  + h.x  * x0.y;
    g.yz = a0.yz * x12.xz + h.yz * x12.yw;
    return 130.0 * dot(m, g);
}

float fractalNoise(in vec2 coord, in float persistence, in float lacunarity){    
    float n = 0.;
    float frequency = 1.;
    float amplitude = 2.;
    for (int o = 0; o < OCTAVES ; ++o) {
        n += amplitude * snoise(coord * frequency);
        amplitude *= persistence;
        frequency *= lacunarity;
    }
    return n;
}

vec3 fractalNebula(in vec2 coord, vec3 color, float transparency){
    float n = fractalNoise(coord, .5, 3.);
    return n * color * transparency;
}

vec3 correct(in vec3 v){
    return vec3(max(.0,v.x),max(.0,v.y),max(.0,v.z));
}

void main(){
    
    vec2 b=gl_FragCoord.xy/vec2(height,height);
    
    float stars=max((fract(dot(sin(gl_FragCoord.xy),gl_FragCoord.xy))-.99)*90.,.0);
    
    vec3 galaxyArea=vec3(1.)*.5-length(b-vec2(.5,.5));
    galaxyArea*=max(.0,100.*(sin(b.y*3.14+.05*cos(b.x*15.))-.8));
    
    vec3 galaxy=correct(fractalNebula(2.*b+vec2(1.8,1.5),vec3(.4,.4,.6),.03)); 
    galaxy+=correct(fractalNebula(2.*b+vec2(1.2,1.3),vec3(.4,.4,.6),.03)); 
    
    vec3 result=vec3(stars)+vec3(1.)*galaxyArea*galaxy;

    gl_FragColor=vec4(result,1.);
}
</script>
<div>
	<button>Toggle visibility</button>
</div>
              
            
!

CSS

              
                body{
	margin:0;
	overflow:hidden;
}
div{
	position:absolute;
	width:100%;
	top:100%;
	text-align:center;
}
button{
	bottom:100px;
	position:relative;
	width:250px;
	height:75px;
	border-radius:20px;
	border:1px solid #aaf;
	padding:5px 35px;
	background:black;
	color:#aaf;
	font-family:Arial;
	cursor:pointer;
	letter-spacing:1px;
	transition:all 180ms ease;
	outline:0;
}
button:hover{
	letter-spacing:3px;
}
button:active{
	background:#fff;
}
              
            
!

JS

              
                var renderer, scene, camera, controls, spaceship, shipMaterial, size, stats ;
var starScene ;
var toggle=false,t=0;

setScene();

addStarfield(2048);

loadStarship();

animate();

/** FUNCTIONS **/

function animate(){
	if( typeof size !== undefined){
		if( toggle && t < (23 * 17 / size ) ) {
			t+=.07
		}else if(t>0){
			t-=.07
		}
	}
	shipMaterial.uniforms.time.value=t;
	//scene.rotation.y+=.001;
	requestAnimationFrame(animate);
	controls.update();
	starCamera.position.copy( new THREE.Vector3().subVectors( camera.position, controls.target ).normalize().multiplyScalar( 15 ));
	starCamera.up.copy(camera.up);
	starCamera.lookAt(scene.position);
	renderer.render(starScene,starCamera);
	renderer.render(scene,camera);
	stats.update();
}

function setScene(){
	renderer=new THREE.WebGLRenderer();
	renderer.autoClear=false;
	renderer.setSize(innerWidth,innerHeight);
	document.body.appendChild(renderer.domElement);
	scene=new THREE.Scene();
	starScene=new THREE.Scene();
	camera=new THREE.PerspectiveCamera(50,innerWidth/innerHeight,1,110);
	camera.position.set(0,0,15);
	starCamera=new THREE.PerspectiveCamera(50,innerWidth/innerHeight,2,10000);
	starCamera.position.set(0,0,15);
	controls=new THREE.TrackballControls(camera,renderer.domElement);
	controls.rotateSpeed=5;
	controls.minDistance=10;
	controls.maxDistance=100;
	
	window.addEventListener('resize',function(){
		camera.aspect=innerWidth/innerHeight;
		starCamera.aspect=innerWidth/innerHeight;
		camera.updateProjectionMatrix();
		starCamera.updateProjectionMatrix();
		renderer.setSize(innerWidth,innerHeight);
	},false);
}

function addStarfield(){
	var starfield=new THREE.Mesh(
		new THREE.SphereGeometry(9000,30,30),
		new THREE.MeshBasicMaterial({
			map:getStarfieldTexture(),
			side:THREE.DoubleSide
		})
	);
	starScene.add(starfield);
}

function getStarfieldTexture(){
	var textureScene=new THREE.Scene();
	textureScene.add(new THREE.Mesh(
		new THREE.PlaneBufferGeometry(1,1,1,1),
		new THREE.ShaderMaterial({
			vertexShader:document.querySelector('#starVShader').textContent,
			fragmentShader:document.querySelector('#starFShader').textContent,
			uniforms:{
				width:{type:'f',value:innerWidth*1},
				height:{type:'f',value:innerHeight*3}
			}
		})
	));
	var starCamera=new THREE.OrthographicCamera(-.5,.5,.5,-.5,1,3);
	starCamera.position.z=2;
	var starTexture=new THREE.WebGLRenderTarget(2048,2048);
	var date=performance.now();
	renderer.render(textureScene,starCamera,starTexture);
	console.log('background created in '+(performance.now()-date).toFixed(0)+'ms');
	return starTexture;
}

function loadStarship(){
	shipMaterial = new THREE.ShaderMaterial({
			vertexShader:document.querySelector('#vShader').textContent,
			fragmentShader:document.querySelector('#fShader').textContent,
			uniforms:{
				time:{type:'f',value:0},
				maxZ:{type:'f',value:0},
				light:{type:'v3',value:new THREE.Vector3(-1,4,100).normalize()}
			},
			transparent:true,
			side:THREE.DoubleSide
	});

	new THREE.JSONLoader().load('https://api.myjson.com/bins/4mtg8',function(geo){
		//flatShaded geometry : need to copy the rest
		for(var i=0;i<geo.faces.length;i++){
			var normal=new THREE.Vector3();
			var vNormals=geo.faces[i].vertexNormals;
			normal.x=(vNormals[0].x+vNormals[1].x+vNormals[2].x)/3;
			normal.y=(vNormals[0].y+vNormals[1].y+vNormals[2].y)/3;
			normal.z=(vNormals[0].z+vNormals[1].z+vNormals[2].z)/3;
			for(var j=0;j<vNormals.length;j++)geo.faces[i].vertexNormals[j]=normal;
		}
		spaceship=new THREE.Mesh(geo,shipMaterial);
		spaceship.geometry.computeBoundingBox();
		var maxZ=shipMaterial.uniforms.maxZ.value=spaceship.geometry.boundingBox.max.z;
		var minZ=spaceship.geometry.boundingBox.min.z;
		spaceship.rotation.x=.3;
		scene.add(spaceship);
		size=maxZ-minZ;
		document.querySelector('button').addEventListener('click',function(){toggle=!toggle},false);
	});
}
              
            
!
999px

Console