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

Auto Save

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; }
canvas { display: block; }
              
            
!

JS

              
                THREE.GodrayShader = {
  uniforms: {
    texture:       { type: 't', value: null },
    lightPosition: { type: 'v2', value: new THREE.Vector2(0, 0) }
  },
  vertexShader: `
    varying vec3 vPos;
    varying vec2 vUv;

    void main() {
      vPos = position;
      vUv = uv;
      gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
    }
  `,
  fragmentShader: `
    uniform sampler2D texture;
    uniform vec2 lightPosition;

    varying vec3 vPos;
    varying vec2 vUv;

    const int NUM_SAMPLES = 70;

    void main() {
      vec2 dist = vec2(vPos.xy - lightPosition.xy);
      vec2 texPos = vUv;

      float weight = 5.25;
      float illuminationDecay = 1.0;
      float decay = 0.75;
      float exposure = 0.525;
      float density = 0.996;

      dist *= 1.0 / float(NUM_SAMPLES) * density;

      vec4 blurColor = vec4(0.0, 0.0, 0.0, 0.0);

      for (int i = 0; i < NUM_SAMPLES; i += 1) {
        texPos -= dist;
        vec4 sample = texture2D(texture, texPos);
        sample *= illuminationDecay * weight;
        blurColor += sample;
        illuminationDecay *= decay;
      }

      blurColor *= exposure;
      vec4 mainColor = mix(blurColor, texture2D(texture, vUv), 0.5);
      vec4 maskColor = vec4(vPos.x, vPos.y, 0.65, 1.0);
      gl_FragColor = mix(mainColor, maskColor, 0.65);
    }
  `
}

class Particles {
  constructor (num) {
    this.num = num

    this.group = new THREE.Group()
  }
  init (parent) {
    let geometry = new THREE.PlaneBufferGeometry(5, 5)
    let material = new THREE.MeshBasicMaterial({ color: 0x444444, wireframe: true })
    for (let i = 0; i < this.num; i += 1) {
      let mesh = new THREE.Mesh(geometry, material)
      mesh.position.set(0, i * 200 + 100, 0)
      mesh.angle = Math.random() * 360
      mesh.radius = 110 + Math.random() * 200
      mesh.offset = i * 2 + 150
      mesh.speed = 0.001 + Math.random() * 0.003
      mesh.displace = -100 + Math.random() * 200
      this.group.add(mesh)
    }
    parent.add(this.group)
    return this
  }
  update (mouse) {
    this.group.children.forEach((mesh, i) => {
      mesh.position.set(
        Math.sin(mesh.angle * 0.5) * mesh.radius,
        mesh.offset,
        Math.cos(mesh.angle * 0.2) * mesh.radius
      )
      mesh.offset += mesh.speed * 100
      if (mesh.offset >= 200) mesh.offset = -200
      mesh.lookAt(scene.position)
      mesh.angle += mesh.speed
    })
    this.group.position.x = -mouse.x * 10
  }
}

class Blob {
  constructor (radius) {
    this.radius = radius
    this.uniforms = {
      time: { type: 'f', value: 0.0 }
    }
    this.vertShader = `
      uniform float time;

      void main () {
        vec3 newPosition = vec3(position.x + sin(position.y / 10.0 + time / 400.0) * 5.0, position.y, position.z + sin(position.z / 10.0 + time / 400.0) * 2.0);
        gl_Position = projectionMatrix * modelViewMatrix * vec4( newPosition, 1.0 );
      }
  `
    this.fragShader = `
    uniform float time;

    void main () {
      gl_FragColor = vec4(0.3, 0.3, 0.3, 1.0);
    }
  `

    this.mesh = null
  }
  init (scene) {
    let geometry = new THREE.SphereBufferGeometry(20, 20, 20)
    let material = new THREE.ShaderMaterial({
      uniforms: this.uniforms,
      vertexShader: this.vertShader,
      fragmentShader: this.fragShader
    })
    this.mesh = new THREE.Mesh(geometry, material)
    this.mesh.position.set(0, 0, 0)
    scene.add(this.mesh)
    return this
  }
  update (ts) {
    if (this.mesh) {
      this.uniforms.time.value = ts
    }
  }
}

let width = window.innerWidth
let height = window.innerHeight

const scene = new THREE.Scene()
const camera = new THREE.PerspectiveCamera(45, width / height)
const renderer = new THREE.WebGLRenderer()
const bufferScene = new THREE.Scene()
const bufferTexture = new THREE.WebGLRenderTarget(width / 2, height / 2, { minFilter: THREE.LinearFilter, magFilter: THREE.NearestFilter})
const composer = new THREE.EffectComposer(renderer)

let mouse

let lightMesh = new THREE.Mesh(
  new THREE.SphereBufferGeometry(23, 20, 20),
  new THREE.MeshBasicMaterial({ color: 0xFFFFFF })
)
let particleSystem = new Particles(200).init(bufferScene)
bufferScene.add(lightMesh)
lightMesh.position.z -= 10
lightMesh.target = { x: 0, y: 0 }
let blob = new Blob(20).init(bufferScene)

const getMousePos = (el) => {
  let mouse = { x: 0, y: 0 }
  el.addEventListener('mousemove', (e) => {
    mouse.x = (e.pageX - width / 2) / width
    mouse.y = (e.pageY - height / 2) / height
  }, false)
  return mouse
}

const setScene = () => {
  camera.position.set(0, 0, 100)
  camera.lookAt(new THREE.Vector3(0, 0, 0))

  renderer.setSize(width, height)
  renderer.setClearColor(0x0f0f0f)
  renderer.setPixelRatio(window.devicePixelRatio)
  document.body.appendChild(renderer.domElement)

  let bufferRender = new THREE.RenderPass(bufferScene, camera)
  let customShader = new THREE.ShaderPass( THREE.GodrayShader )
  customShader.uniforms[ 'texture' ].value = bufferTexture.texture
  customShader.uniforms[ 'lightPosition' ].value = new THREE.Vector2(lightMesh.position.x, lightMesh.position.y)


  composer.addPass(bufferRender)
  composer.addPass(customShader)
  customShader.renderToScreen = true
  //shaderPass.renderToScreen = true
  //composer.addPass(shaderPass)

  mouse = getMousePos(renderer.domElement)

  renderFrame()
}

const renderFrame = (ts) => {
  requestAnimationFrame(renderFrame)
  renderer.render(bufferScene, camera, bufferTexture)
  composer.render(scene, camera)

  lightMesh.target.x = -mouse.x * 10
  lightMesh.target.y = mouse.y * 10
  lightMesh.position.x += (lightMesh.target.x - lightMesh.position.x) * 0.5
  lightMesh.position.y += (lightMesh.target.y - lightMesh.position.y) * 0.5
  lightMesh.position.x *= 0.99
  lightMesh.position.y *= 0.99

  particleSystem.update(mouse)
  blob.update(ts)
}

const init = () => {
  setScene()
  renderFrame()
}

init()
              
            
!
999px

Console