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

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 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
const fieldOfView = 45
const screenAspect = innerWidth / innerHeight
const near = 0.1
const far = 10
const 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)
}

// 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)
  }
  
  // Render our scene to the default framebuffer (device screen)
  renderer.render(scene, camera)
}
              
            
!
999px

Console