123

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.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              html,
body
  margin: 0
  padding: 0
canvas
  display: block
            
          
!
            
              const WHITE = {
  mainColor: 0xc08937,
  mainEmissive: 0,
  secondaryColor: 0x020403,
  secondaryEmissive: 0,
  detailColor: 0x0f0f11,
  detailEmissive: 0
}

class ParticleSystem {
  constructor () {
    this.time = 0.0
    let triangles = 1
    let instances = 7000
    let geometry = new THREE.InstancedBufferGeometry()

    let vertices = new THREE.BufferAttribute(new Float32Array(triangles * 3 * 3), 3)
    let unit = 0.5
    vertices.setXYZ(0, unit, -unit, 0)
    vertices.setXYZ(1, -unit, unit, 0)
    vertices.setXYZ(2, 0, 0, unit)
    geometry.addAttribute('position', vertices)

    let offsets = new THREE.InstancedBufferAttribute(new Float32Array(instances * 3), 3, 1)
    let dist = 180
    for (let i = 0, ul = offsets.count; i < ul; i++) {
      offsets.setXYZ(i, (Math.random() - 0.5) * dist, (Math.random() - 0.5) * dist, (Math.random() - 0.5) * dist)
    }
    geometry.addAttribute('offset', offsets)

    let colors = new THREE.InstancedBufferAttribute(new Float32Array(instances * 4), 4, 1)

    let threeColor = new THREE.Color()
    for (let i = 0, ul = colors.count; i < ul; i++) {
      let c = threeColor.setHex(WHITE.mainColor)
      colors.setXYZW(i, c.r, c.g, c.b, 1)
    }
    geometry.addAttribute('color', colors)

    let timeOffsets = new THREE.InstancedBufferAttribute(new Float32Array(instances * 1), 1, 1)

    for (let i = 0, ul = timeOffsets.count; i < ul; i++) {
      timeOffsets.setX(i, Math.random())
    }
    geometry.addAttribute('timeOffset', timeOffsets)

    let vector = new THREE.Vector4()
    let orientationsStart = new THREE.InstancedBufferAttribute(new Float32Array(instances * 4), 4, 1)
    for (let i = 0, ul = orientationsStart.count; i < ul; i++) {
      vector.set(Math.random() * 2 - 1, Math.random() * 2 - 1, Math.random() * 2 - 1, Math.random() * 2 - 1)
      vector.normalize()
      orientationsStart.setXYZW(i, vector.x, vector.y, vector.z, vector.w)
    }
    geometry.addAttribute('orientationStart', orientationsStart)

    let orientationsEnd = new THREE.InstancedBufferAttribute(new Float32Array(instances * 4), 4, 1)
    for (let i = 0, ul = orientationsEnd.count; i < ul; i++) {
      vector.set(Math.random() * 2 - 1, Math.random() * 2 - 1, Math.random() * 2 - 1, Math.random() * 2 - 1)
      vector.normalize()
      orientationsEnd.setXYZW(i, vector.x, vector.y, vector.z, vector.w)
    }
    geometry.addAttribute('orientationEnd', orientationsEnd)

    let material = new THREE.RawShaderMaterial({
      uniforms: {
        time: { value: 1.0 },
        sineTime: { value: 1.0 }
      },
      vertexShader: `
        precision highp float;
        uniform float time;
        uniform mat4 modelViewMatrix;
        uniform mat4 projectionMatrix;
        attribute vec3 position;
        attribute vec3 offset;
        attribute vec4 color;
        attribute vec4 orientationStart;
        attribute vec4 orientationEnd;
        attribute float timeOffset;
        varying vec4 vColor;
        varying float lifeProgress;

        void main(){

          vec3 vPosition = offset;

          lifeProgress = mod(time+timeOffset,1.0);

          vPosition = offset * lifeProgress + position;
          vec4 orientation = normalize(mix(orientationStart, orientationEnd, lifeProgress));
          vec3 vcV = cross(orientation.xyz, vPosition);
          //orientation.w *= time*5.0;
          vPosition = vcV * (2.0 * orientation.w) + (cross(orientation.xyz, vcV) * 2.0 + vPosition);
          vColor = color;
          gl_Position = projectionMatrix * modelViewMatrix * vec4( vPosition, 1.0 );
        }
        `,
      fragmentShader: `
      precision highp float;
      uniform float time;
      varying vec4 vColor;
      varying float lifeProgress;

      void main() {
        float depth = gl_FragCoord.z / gl_FragCoord.w / 5.0;
        float opacity = clamp(0.2, 1.0, depth);
        vec4 color = vColor;
        color.a = sin(lifeProgress*100.0)*opacity;
        gl_FragColor = color;
      }
      `,
      side: THREE.DoubleSide,
      transparent: true
    })

    let mesh = new THREE.Mesh(geometry, material)
    mesh.frustumCulled = false
    this.mesh = mesh
  }
  update (dt) {
    this.time += 0.0001
    this.mesh.material.uniforms.time.value = Math.sin(this.time)
  }
}

let HEIGHT = window.innerHeight
let WIDTH = window.innerWidth


const scene = new THREE.Scene()

scene.fog = new THREE.Fog(0x2e2e2e, 2, 10)


const aspectRatio = WIDTH / HEIGHT
const fieldOfView = 30
const nearPlane = 0.5
const farPlane = 10
const camera = new THREE.PerspectiveCamera(
  fieldOfView,
  aspectRatio,
  nearPlane,
  farPlane
)


const renderer = new THREE.WebGLRenderer({
  alpha: true,
  antialias: true
})

renderer.setPixelRatio(window.devicePixelRatio)
renderer.setClearColor(0x151515, 1)
renderer.setSize(WIDTH, HEIGHT)
document.documentElement.appendChild(renderer.domElement)

const ambientLight = new THREE.AmbientLight(0xffffff, 1.0)
scene.add(ambientLight)


const controls = new THREE.OrbitControls( camera, renderer.domElement );
controls.enableDamping = true;
controls.dampingFactor = 0.25;
controls.enableZoom = false;

function handleWindowResize () {

  HEIGHT = window.innerHeight
  WIDTH = window.innerWidth
  renderer.setSize(WIDTH, HEIGHT)
  camera.aspect = WIDTH / HEIGHT
  camera.updateProjectionMatrix()
}

const render = () => {
  var delta = clock.getDelta()
  var elapsed = clock.getElapsedTime()
  particles.update(delta)
  controls.update()
  renderer.render(scene, camera)
  window.requestAnimationFrame(render)
}


window.addEventListener('resize', handleWindowResize, false)


camera.position.y = 1.7
camera.position.z = 0.5
controls.target.set(0, camera.position.y, 0)


const particles = new ParticleSystem()
particles.mesh.position.y = 4
scene.add(particles.mesh)


const clock = new THREE.Clock()
render()
            
          
!
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.

Console