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. 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

JavaScript

Babel is required to process package imports. If you need a different preprocessor remove all packages first.

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

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

              
                <script src="https://unpkg.com/three@0.112.0/build/three.min.js"></script>
<script src="https://unpkg.com/three@0.112.0/examples/js/controls/OrbitControls.js"></script>

<script src="https://unpkg.com/three@0.112.0/examples/js/postprocessing/EffectComposer.js"></script>
<script src="https://unpkg.com/three@0.112.0/examples/js/postprocessing/RenderPass.js"></script>
<script src="https://unpkg.com/three@0.112.0/examples/js/postprocessing/ShaderPass.js"></script>
<script src="https://unpkg.com/three@0.112.0/examples/js/shaders/CopyShader.js"></script>
<script src="https://unpkg.com/three@0.112.0/examples/js/shaders/LuminosityHighPassShader.js"></script>
<script src="https://unpkg.com/three@0.112.0/examples/js/postprocessing/UnrealBloomPass.js"></script>
<script>
  var noise = `

  // Original noise code from https://www.shadertoy.com/view/4sc3z2
  #define MOD3 vec3(.1031,.11369,.13787)
  vec3 hash33(vec3 p3)
  {
    p3 = fract(p3 * MOD3);
      p3 += dot(p3, p3.yxz+19.19);
      return -1.0 + 2.0 * fract(vec3((p3.x + p3.y)*p3.z, (p3.x+p3.z)*p3.y, (p3.y+p3.z)*p3.x));
  }

  float simplex_noise(vec3 p)
  {
      const float K1 = 0.333333333;
      const float K2 = 0.166666667;

      vec3 i = floor(p + (p.x + p.y + p.z) * K1);
      vec3 d0 = p - (i - (i.x + i.y + i.z) * K2);

      vec3 e = step(vec3(0.0), d0 - d0.yzx);
    vec3 i1 = e * (1.0 - e.zxy);
    vec3 i2 = 1.0 - e.zxy * (1.0 - e);

      vec3 d1 = d0 - (i1 - 1.0 * K2);
      vec3 d2 = d0 - (i2 - 2.0 * K2);
      vec3 d3 = d0 - (1.0 - 3.0 * K2);

      vec4 h = max(0.6 - vec4(dot(d0, d0), dot(d1, d1), dot(d2, d2), dot(d3, d3)), 0.0);
      vec4 n = h * h * h * h * vec4(dot(d0, hash33(i)), dot(d1, hash33(i + i1)), dot(d2, hash33(i + i2)), dot(d3, hash33(i + 1.0)));

      return dot(vec4(31.316), n);
  }

`;
</script>

<script type="x-shader/x-vertex" id="vertexshader">
  varying vec2 vUv; void main() { vUv = uv; gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 ); }
</script>

<script type="x-shader/x-fragment" id="fragmentshader">
  uniform sampler2D baseTexture; uniform sampler2D bloomTexture; varying vec2 vUv; vec4 getTexture( sampler2D texture ) { return mapTexelToLinear( texture2D( texture , vUv ) ); } void main() { gl_FragColor = ( getTexture( baseTexture ) + vec4( 1.0 ) * getTexture(
  bloomTexture ) ); }
</script>

<script src="https://w.soundcloud.com/player/api.js"></script>
<div id="veil" style="position:absolute;width:100%; height:100%; background-color:#201;">
</div>
<iframe id="scWidget" scrolling="no" frameborder="no" allow="autoplay" src="https://w.soundcloud.com/player/?url=https%3A//api.soundcloud.com/tracks/380953541&color=%2300ffff&auto_play=false&hide_related=true&show_comments=false&show_user=true&show_reposts=false&show_teaser=true&visual=true"></iframe>
              
            
!

CSS

              
                body {
  overflow: hidden;
  margin: 0;
}

#scWidget {
  position: absolute;
  width: 50vh;
  height: 50vh;
  left: calc(50vw - 25vh);
  top: calc(50vh - 25vh);
}
              
            
!

JS

              
                // soundcloud widget section
var widget = SC.Widget(scWidget);
widget.bind(SC.Widget.Events.PLAY, ()=>{
  sequence();
  veil.style.display = "none";
  scWidget.style.display = "none";
});
////////////////////////////

// Nocturnal texture
var noxTexture = new THREE.TextureLoader().load("https://cywarr.github.io/small-shop/NocturnalTexture.png");

//var backColor = 0x441133;
var backColor = 0x000000;
//console.log(new THREE.Color(backColor));
var params = {
  exposure: 0.8,
  bloomStrength: 1.5,
  bloomThreshold: 0,
  bloomRadius: 0.5
};
var uniforms = {
  globalBloom: { value: 0 },
  time: { value: 0 }
};

var scene = new THREE.Scene();
var camera = new THREE.PerspectiveCamera(
  60,
  window.innerWidth / window.innerHeight,
  1,
  1000
);
camera.position.set(0, 30, 50).setLength(50);
var renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setClearColor(backColor);
document.body.appendChild(renderer.domElement);
window.addEventListener("resize", onWindowResize, false);

//scene.add(new THREE.GridHelper(200, 10));

var renderScene = new THREE.RenderPass(scene, camera);
var bloomPass = new THREE.UnrealBloomPass(
  new THREE.Vector2(window.innerWidth, window.innerHeight),
  1.5,
  0.4,
  0.85
);
bloomPass.threshold = params.bloomThreshold;
bloomPass.strength = params.bloomStrength;
bloomPass.radius = params.bloomRadius;
var bloomComposer = new THREE.EffectComposer(renderer);
bloomComposer.renderToScreen = false;
bloomComposer.setSize(
  window.innerWidth * window.devicePixelRatio,
  window.innerHeight * window.devicePixelRatio
);
bloomComposer.addPass(renderScene);
bloomComposer.addPass(bloomPass);
var finalPass = new THREE.ShaderPass(
  new THREE.ShaderMaterial({
    uniforms: {
      baseTexture: { value: null },
      bloomTexture: { value: bloomComposer.renderTarget2.texture }
    },
    vertexShader: document.getElementById("vertexshader").textContent,
    fragmentShader: document.getElementById("fragmentshader").textContent,
    defines: {}
  }),
  "baseTexture"
);
finalPass.needsSwap = true;
var finalComposer = new THREE.EffectComposer(renderer);
finalComposer.setSize(
  window.innerWidth * window.devicePixelRatio,
  window.innerHeight * window.devicePixelRatio
);
finalComposer.addPass(renderScene);
finalComposer.addPass(finalPass);

var controls = new THREE.OrbitControls(camera, renderer.domElement);
controls.enablePan = false;
controls.enableKeys = false;
controls.maxPolarAngle = THREE.Math.degToRad(90);
controls.enableDamping = true;
controls.dampingFactor = 0.1;
controls.minDistance = 25;
controls.maxDistance = 75;
controls.target.set(0, 20, 0);
controls.update();

// light
var light = new THREE.HemisphereLight(0xff0088, 0x0088ff, 0.37);
light.position.set(0, 200, 0);
scene.add(light);
var dirLight = new THREE.DirectionalLight(0xffddbb, 0.05);
dirLight.position.set(0, 0.75, -1.5);
scene.add(dirLight);

// background

var bkGeom = new THREE.CylinderBufferGeometry(250, 250, 500, 72, 72, true);
//bkGeom.rotateY(Math.PI * 0.5);
var bkMat = new THREE.ShaderMaterial({
  uniforms: {
    globalBloom: uniforms.globalBloom,
    time: uniforms.time,
    noxTexture: {value: noxTexture}
  },
  vertexShader: `
  varying vec3 vPos;
  varying vec2 vUv;
  void main() {
    vPos = position;
    vUv = uv;
    gl_Position = projectionMatrix * modelViewMatrix * vec4(position,1.0);
  }
`,
  fragmentShader: `
  uniform float globalBloom;
  uniform float time;
  uniform sampler2D noxTexture;

  varying vec3 vPos;
  varying vec2 vUv;
  
  ${noise}
  
  // simple random
  float rand(vec2 c) {
    return fract(sin(dot(c.xy, vec2( 12.9898, 78.233))) * 43758.5453);
  }

  void main() {

    vec2 uv = vUv;
    
    // data from texture
    vec2 noxC = vec2(0.5, 0.7);
    vec2 noxS = vec2(2, 2) * 0.075;
    
    vec2 noxUV = (uv - (noxC - (noxS * 0.5))) / noxS;
    noxUV.x = 1. - noxUV.x;
    
    vec2 noxData = texture2D(noxTexture, noxUV).rg;
    
    ////////////////////

    vec3 pos = vec3(vPos) / 500.;
    float e = length(fwidth(vUv));
    float nVal = simplex_noise(vec3(pos.x, pos.y, pos.z - time * 0.25));

    vec3 col = vec3(0);
    
    // cityscape
    // buildings
    vec2 csUv = uv;
    csUv.x *= 144.;
    float i = floor(csUv.x);
    float f = fract(csUv.x);
    float cs = simplex_noise(vec3(i, 0, 0));
    float cs2 = simplex_noise(vec3(i + 1.0, i, 0));
    float flt = 0.6;
    float rmp = 0.65;
    float flatBuildings = step( flt + cs * 0.025, uv.y );
    float rampBuildings = step( mix(rmp + cs * 0.02, rmp + cs2 * 0.02, f), uv.y) + smoothstep(rmp - 0.025, rmp + 0.025, uv.y);
    col = mix(vec3(1, 0.25, 0.75) * 0.125, col, rampBuildings);
    col = mix(vec3(1, 0.75, 1) * 0.0156, col, flatBuildings);

    // fog
    col = mix(vec3(0.25, 0.75, 1.) * (1. - smoothstep(0.45, 0.5, uv.y) * 0.5), col, smoothstep(0.5, 0.65 + nVal * 0.025, uv.y)); 
    
    // non-bloomed objects before this line /////////////////////////////////////////////////////////////////////////////////////
    col = floor(globalBloom + 0.1) > 0.5 ? vec3(0) : col;
    /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

    //col = mix(col, vec3(0.25, 0.75, 1), smoothstep(-e, 0., nVal) - smoothstep(0., e, nVal));
    
    // city lights / windows
    vec2 wUv = uv; 
    wUv *= vec2(720, 360)*0.75;
    vec2 wcUv = fract(wUv);
    vec2 wiUv = floor(wUv);
    wcUv -= 0.5;
    float e2 = length(fwidth(wUv));
    float wF = smoothstep(e2, 0., length(wcUv));

    float twinkle = smoothstep(-1., 1., simplex_noise(vec3(wiUv, (time * 0.125))));
    twinkle = smoothstep(0.8, 0.85, twinkle) * 0.75;
    float lightBand = smoothstep(0.5, 0.525, uv.y) - smoothstep(0.525, 0.65, uv.y);

    vec3 winColor = mix(vec3(1, 0.5, 1), vec3(1, 0.75, 0.5), twinkle);
    col = mix(col, winColor, wF * twinkle * lightBand);
    

    // Nocturnal inner
    vec2 niUV = noxUV * vec2(1, 24);
    niUV.y -= time;
    float niF = sin(niUV.y * 3.1415926 * 2.) * 0.5 + 0.5;
    niF = smoothstep(0.5, 0.95, niF);
    col = mix(col, vec3(1, 0.5, 0.75)*0.175, niF * noxData.r);

    // Nocturnal outer
    float noBlink = step(0.0156, rand(vec2(floor(time * 50.), 0)));
    col = mix(col, vec3(1, 0.5, 1), noxData.g * noBlink);
    
    // final colour
    gl_FragColor = vec4(col, 1.0);
  }
`,
  extensions: {derivatives: true},
  side: THREE.BackSide
});

var bk = new THREE.Mesh(bkGeom, bkMat);
scene.add(bk);


// scene stuff
var boxGeom = new THREE.BoxBufferGeometry(2, 2, 2, 9, 9, 9); // base geometry
var boxPositions = new THREE.PlaneBufferGeometry(200, 200, 20, 20);
boxPositions.rotateX(Math.PI * -0.5);
var instGeom = new THREE.InstancedBufferGeometry();
instGeom.copy(boxGeom);
instGeom.setAttribute(
  "instPosition",
  new THREE.InstancedBufferAttribute(boxPositions.attributes.position.array, 3)
);
instGeom.setAttribute(
  "instUV",
  new THREE.InstancedBufferAttribute(boxPositions.attributes.uv.array, 2)
);

var boxMat = new THREE.MeshStandardMaterial({
  color: 0xffffff,
  wireframe: false,
  metalness: 0.5,
  roughness: 0.5
});
boxMat.defines = { USE_UV: "" };
boxMat.onBeforeCompile = shader => {
  shader.uniforms.globalBloom = uniforms.globalBloom;
  shader.uniforms.time = uniforms.time;
  shader.vertexShader =
    `
  uniform float globalBloom;
  uniform float time;

  attribute vec3 instPosition;
  attribute vec2 instUV;

  varying vec3 vPos;
  varying float vNoiseVal;
  varying float vTintVal;

  ${noise}

` + shader.vertexShader;
  shader.vertexShader = shader.vertexShader.replace(
    `#include <begin_vertex>`,
    `#include <begin_vertex>

    vec3 boxSize = vec3(5., 10., 5.);
    float radius = 2.;

    vec3 signs = sign(position);
    
    vec3 box = boxSize - vec3(radius);
    box = vec3(max(0.0, box.x), max(0.0, box.y), max(0.0, box.z));
    vec3 p = signs * box;

    transformed = signs * box + normalize(position) * radius;
    vPos = vec3(transformed);

    // re-compute normals for correct shadows and reflections
    objectNormal = all(equal(p, transformed)) ? normal : normalize(position); 
    transformedNormal = normalize(normalMatrix * objectNormal);
    vNormal = transformedNormal;

    vec3 posShift = instPosition;
    vec2 iUV = instUV * 4.;
    float noiseVal = simplex_noise(vec3(iUV.x, iUV.y + time * 0.5, 0.));
    float noiseSign = sign(noiseVal);
    //noiseVal = pow(abs(noiseVal), 1.) * noiseSign;
    vNoiseVal = noiseVal;
    posShift.y += noiseVal * 10.;
    
    vec2 tintUV = instUV * 2.;
    float tintVal = simplex_noise(vec3(tintUV, time * 0.5));
    vTintVal = tintVal;

    transformed += posShift;
`
  );
  //console.log(shader.fragmentShader);
  shader.fragmentShader =
    `
  uniform float globalBloom;
  uniform float time;

  varying vec3 vPos;
  varying float vNoiseVal;
  varying float vTintVal;
` + shader.fragmentShader;
  shader.fragmentShader = shader.fragmentShader.replace(
    `#include <dithering_fragment>`,
    `#include <dithering_fragment>
    
    float gb = floor(globalBloom + 0.1);
    gl_FragColor.rgb = mix(gl_FragColor.rgb, vec3(0), step(0.5, gb));

    // shining cap
    float hVal = smoothstep(9.25, 9.5, vPos.y) - smoothstep(9.5, 9.75, vPos.y);
    float nVal = smoothstep(0.125, 0.25, vNoiseVal);
    vec3 tint = mix(vec3(1, 0.5, 0.25), vec3(0.25, 0.5, 1), smoothstep(-1., 1., vTintVal));
    gl_FragColor.rgb = mix(gl_FragColor.rgb, tint, hVal * nVal);

    // corner dots
    vec2 uv = vUv;
    uv *= 4.;
    vec2 gUv = fract(uv);
    gUv -= 0.5;
    gUv *= 4.;
    gl_FragColor.rgb = mix(vec3(0.25, 0.75, 1), gl_FragColor.rgb, clamp( length(gUv) , 0., 1.) );

    
`
  );
};

var boxes = new THREE.Mesh(instGeom, boxMat);
boxes.frustumCulled = false;
scene.add(boxes);

function sequence(){
  clock.start();
  fullRender = true;
}

var clock = new THREE.Clock(false);
var fullRender = false;

renderer.setAnimationLoop(() => {
  let t = clock.getDelta();
  uniforms.time.value = clock.getElapsedTime() * 0.75;
  
  controls.update();
  
  if (!fullRender) {
    renderer.render(scene, camera);
  } else {
    //render scene with bloom
    renderBloom();
    // render the entire scene, then render bloom scene on top
    finalComposer.render();
  }
});

function renderBloom() {
  darkenNonBloomed();
  bloomComposer.render();
  restoreMaterial();
}
function darkenNonBloomed() {
  // non-bloomed stuff must be black, including scene background
  uniforms.globalBloom.value = 1;
  //renderer.setClearColor(0x000000);
}
function restoreMaterial() {
  uniforms.globalBloom.value = 0;
  //renderer.setClearColor(backColor);
}

function onWindowResize() {
  var width = window.innerWidth;
  var height = window.innerHeight;
  camera.aspect = width / height;
  camera.updateProjectionMatrix();
  renderer.setSize(width, height);
  bloomComposer.setSize(
    width * window.devicePixelRatio,
    height * window.devicePixelRatio
  );
  finalComposer.setSize(
    width * window.devicePixelRatio,
    height * window.devicePixelRatio
  );
}

              
            
!
999px

Console