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

              
                
              
            
!

CSS

              
                * { margin: 0; padding: 0; }
              
            
!

JS

              
                console.clear()

const OBJECTS_COUNT = 100

// Create orthographic camera, so we can look at our fullscreen quads
let orthoCamera
{
  const left = -innerWidth / 2
  const right = innerWidth / 2
  const top = -innerHeight / 2
  const bottom = innerHeight / 2
  const near = -100
  const far = 100
  orthoCamera = new THREE.OrthographicCamera(left, right, top, bottom, near, far)
  orthoCamera.position.z = -10
  orthoCamera.lookAt(new THREE.Vector3(0, 0, 0))
}

const fullscreenQuadGeometry = new THREE.PlaneGeometry(innerWidth, innerHeight)

// const uvMatrix = new THREE.Matrix3()
// uvMatrix.setUvTransform(0.0, 0, 1, 1, Math.PI, 0.5, 0.5)
 // tx : Float, ty : Float, sx : Float, sy : Float, rotation : Float, cx : Float, cy : Float

const fadeMaterial = new THREE.ShaderMaterial({
  uniforms: {
    inputTexture: { value: null },
    // uvMatrix: { value: uvMatrix }
  },
  vertexShader: `
    varying vec2 vUv;
    void main () {
      gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);
      vUv = uv;
    }
  `,
  fragmentShader: `
    uniform sampler2D inputTexture;
    varying vec2 vUv;
    void main () {
      float dist = distance(vUv, vec2(0.5));
      vec4 texColor = texture2D(inputTexture, vUv * vec2(0.9925));
      vec4 fadeColor = vec4(0.0, 0.0, 0.0, 1.0);
      gl_FragColor = mix(texColor, fadeColor, 0.015);
    }
  `
})
const fadePlane = new THREE.Mesh(
  fullscreenQuadGeometry,
  fadeMaterial
)

const resultMaterial = new THREE.MeshBasicMaterial({ map: null })
const resultPlane = new THREE.Mesh(
  fullscreenQuadGeometry,
  resultMaterial
)

// Create WebGLRenderer and append its canvas to DOM
const renderer = new THREE.WebGLRenderer()
renderer.setSize(innerWidth, innerHeight)
renderer.setPixelRatio(devicePixelRatio || 1)
document.body.appendChild(renderer.domElement)

// Create PerspectiveCamera
let camera
{
  const fieldOfView = 45
  const screenAspect = innerWidth / innerHeight
  const near = 0.1
  const far = 10
  camera = new THREE.PerspectiveCamera(fieldOfView, screenAspect, near, far)
  camera.position.z = 8
}

// Create Scene
const scene = new THREE.Scene()

// Programatically create a checkerbox texture for each object
const texture = new THREE.DataTexture(new Uint8Array([  // data
  0xFF, 0xCC, 0xFF, 0xCC, 0xFF, 0xCC, 0xFF, 0xCC,
  0xCC, 0xFF, 0xCC, 0xFF, 0xCC, 0xFF, 0xCC, 0xFF,
  0xFF, 0xCC, 0xFF, 0xCC, 0xFF, 0xCC, 0xFF, 0xCC,
  0xCC, 0xFF, 0xCC, 0xFF, 0xCC, 0xFF, 0xCC, 0xFF,
  0xFF, 0xCC, 0xFF, 0xCC, 0xFF, 0xCC, 0xFF, 0xCC,
  0xCC, 0xFF, 0xCC, 0xFF, 0xCC, 0xFF, 0xCC, 0xFF,
  0xFF, 0xCC, 0xFF, 0xCC, 0xFF, 0xCC, 0xFF, 0xCC,
  0xCC, 0xFF, 0xCC, 0xFF, 0xCC, 0xFF, 0xCC, 0xFF,
]), 8, 8, THREE.LuminanceFormat)

// Create objects to render
const boxGeometry = new THREE.BoxGeometry()
const sphereGeometry = new THREE.SphereGeometry()
const torusGeometry = new THREE.TorusGeometry()
const coneGeometry = new THREE.ConeGeometry()

const meshes = []
const meshTransforms = []

const sharedUniforms = {
  checkerTexture: { value: texture }
}

for (let i = 0; i < OBJECTS_COUNT; i++) {
  const randGeometryPicker = Math.random()
  let geometry = boxGeometry
  if (randGeometryPicker >= 0.25) {
    geometry = sphereGeometry
  }
  if (randGeometryPicker >= 0.5) {
    geometry = torusGeometry
  }
  if (randGeometryPicker >= 0.75) {
    geometry = coneGeometry
  }
  
  const randR = Math.random()
  const randG = Math.random()
  const randB = Math.random()
  const multColor = new THREE.Vector4(randR, randG, randB, 1)

  const material = new THREE.ShaderMaterial({
    uniforms: {
      ...sharedUniforms,
      multColor: { value: multColor }
    },
    vertexShader: `
      varying vec2 vUv;
      void main () {
        gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);
        vUv = uv;
      }
    `,
    fragmentShader: `
      uniform sampler2D checkerTexture;
      uniform vec4 multColor;
      varying vec2 vUv;
      void main () {
        gl_FragColor = texture2D(checkerTexture, vUv) * multColor;
      }
    `
  })
  
  const mesh = new THREE.Mesh(geometry, material)
  
  // Assign random positions and scale to each object
  meshTransforms.push({
    x: (Math.random() - 0.5) * 5,
    y: (Math.random() - 0.5) * 5,
    z: (Math.random() - 0.5) * 2,
    scale: Math.random() * 0.2 + 0.1
  })
  meshes.push(mesh)
  
  scene.add(mesh)
}

// Create two extra framebuffers manually
// It is important we use let instead of const variables, as we will need to swap them
let framebuffer1 = new THREE.WebGLRenderTarget(innerWidth, innerHeight)
let framebuffer2 = new THREE.WebGLRenderTarget(innerWidth, innerHeight)

renderer.setClearColor(0x111111)
renderer.setRenderTarget(framebuffer1)
renderer.clearColor()
renderer.setRenderTarget(framebuffer2)
renderer.clearColor()

// Start render loop
renderer.setAnimationLoop(drawFrame)

function drawFrame (timeElapsed) {
  timeElapsed /= 1000
  
  // Animate our objects around the scene
  for (let i = 0; i < OBJECTS_COUNT; i++) {
    const mesh = meshes[i]
    const meshTransform = meshTransforms[i]
    
    const moveRadius = 0.4
    const x = meshTransform.x + Math.cos(timeElapsed + meshTransform.y * 2) * moveRadius
    const y = meshTransform.y + Math.sin(timeElapsed + meshTransform.x * 2) * moveRadius
    const z = meshTransform.z + Math.cos(timeElapsed - meshTransform.x * 2) * moveRadius
    
    const rotAddSpeed = timeElapsed
    
    const rotX = x + rotAddSpeed
    const rotY = y + rotAddSpeed
    const rotZ = z + rotAddSpeed
    
    mesh.position.set(x, y, z)
    mesh.rotation.set(rotX, rotY, rotZ)
    mesh.scale.set(meshTransform.scale, meshTransform.scale, meshTransform.scale)
  }
  
  renderer.autoClearColor = false
  fadePlane.material.uniforms.inputTexture.value = framebuffer1.texture
  renderer.setRenderTarget(framebuffer2)
  renderer.render(fadePlane, orthoCamera)
  renderer.render(scene, camera)
  
  renderer.setRenderTarget(null)
  resultPlane.material.map = framebuffer2.texture
  renderer.render(resultPlane, orthoCamera)
  
  const swap = framebuffer1
  framebuffer1 = framebuffer2
  framebuffer2 = swap
}
              
            
!
999px

Console