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

              
                html,
body
  margin: 0
  padding: 0
canvas
  display: block
  
  
              
            
!

JS

              
                
const PARTICLE_SIZE = 0.006

class Snowfall {
  constructor (numParticles, height = 10, width = 10, depth = 10) {
    this.numParticles = numParticles

    this.height = height
    this.width = width
    this.depth = depth
    
    // variable properties
    this.opts = {
      color: 0xFFFFFF,
      speedV: 0.2,
      speedH: 2.0,
      radius: 0.02,
      scale: 2.0,
      opacity: 0.2
    }

    var textureLoader = new THREE.TextureLoader()
    textureLoader.load('https://s3-us-west-2.amazonaws.com/s.cdpn.io/126747/snowflake.png', (texture) => {
      this.texture = texture
      this.system.material.uniforms.texture.value = texture
    })
    
    this._size = -1
    this.setParticleSize()
    window.addEventListener('resize', x => this.setParticleSize())
    
    this.init()
  }
  
  setParticleSize () {
    this._size = window.innerHeight * PARTICLE_SIZE * window.devicePixelRatio
  }

  init () {
    const systemGeometry = new THREE.BufferGeometry()
    const systemMaterial = new THREE.ShaderMaterial({
      uniforms: {
        color: { type: 'c', value: new THREE.Color(this.opts.color) },
        height: { type: 'f', value: this.height },
        speedV: { type: 'f', value: this.opts.speedV },
        speedH: { type: 'f', value: this.opts.speedH },
        elapsedTime: { type: 'f', value: 0.0 },
        radius: { type: 'f', value: this.opts.radius },
        scale: { type: 'f', value: this.opts.scale },
        size: { type: 'f', value: this._size },
        opacity: { type: 'f', value: this.opts.opacity },
        texture: { type: 't', value: null }
      },
      vertexShader: `
        uniform float height;
        uniform float elapsedTime;
        uniform float speedV;
        uniform float speedH;
        uniform float radius;
        uniform float scale;
        uniform float size;
        attribute float uniqueness;
        void main() {
          vec3 pos = position;
          pos.x += cos((elapsedTime - position.z - uniqueness) * speedH) * radius;
          pos.y = mod(position.y - elapsedTime * speedV, height);
          pos.z += sin((elapsedTime - position.x - uniqueness) * speedH) * radius;
          vec4 mvPosition = modelViewMatrix * vec4( pos, 1.0 );
          gl_PointSize = size * ( scale / length( mvPosition.xyz ) );
          gl_Position = projectionMatrix * mvPosition;
        }
      `,
      fragmentShader: `
        uniform vec3 color;
        uniform float opacity;
        uniform sampler2D texture;
        void main() {
          vec4 texColor = texture2D( texture, gl_PointCoord );
          gl_FragColor = texColor * vec4( color, opacity );
        }
      `,
      transparent: true,
      depthTest: false,
      blending: THREE.AdditiveBlending
    })

    const positions = new Float32Array(this.numParticles * 3)
    const uniqueness = new Float32Array(this.numParticles)
    for (let i = 0; i < positions.length; i += 3) {
      // x
      positions[i] = this.randCenter(this.width)
      // y
      positions[i + 1] = Math.random() * this.height
      // z
      positions[i + 2] = this.randCenter(this.depth)
    }

    // push some uniqueness - because snowflakes...
    for (let i = 0; i < this.numParticles; i++) {
      uniqueness[i] = Math.random()
    }

    // Set attributes for shaders to access
    systemGeometry.addAttribute('position', new THREE.BufferAttribute(positions, 3))
    systemGeometry.addAttribute('uniqueness', new THREE.BufferAttribute(uniqueness, 1))

    // must compute bounding box for bufferGeomtry
    systemGeometry.computeBoundingSphere()

    // create particle system
    this.system = new THREE.Points(systemGeometry, systemMaterial)

    // help THREE place snow on top of transparent objects
    this.system.renderOrder = 1
  }

  update (delta, elapsed) {
    this.system.material.uniforms.elapsedTime.value = elapsed
    this.system.material.uniforms.size.value = this._size
  }
  
  updateProp (opt) {
    this.system.material.uniforms[opt].value = this.opts[opt]
  }
  
  updateColor (opt) {
    this.system.material.uniforms[opt].value = new THREE.Color(this.opts[opt])
  }

  randCenter (v) {
    return (v * (Math.random() - 0.5))
  }
}


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

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

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

// Create the camera
const aspectRatio = WIDTH / HEIGHT
const fieldOfView = 60
const nearPlane = 0.1
const farPlane = 30
const camera = new THREE.PerspectiveCamera(
  fieldOfView,
  aspectRatio,
  nearPlane,
  farPlane
)

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

renderer.setPixelRatio(window.devicePixelRatio)
renderer.setClearColor(0xc6ccff, 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 () {
  // update height and width of the renderer and the camera
  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()
  snowfall.update(delta, elapsed)
  controls.update()
  renderer.render(scene, camera)
  window.requestAnimationFrame(render)
}


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

//set camera at eyes height
camera.position.y = 1.7
camera.position.z = 0.5
controls.target.set(0, camera.position.y, 0)

//create ground
var geometry = new THREE.PlaneGeometry(30, 30)
var material = new THREE.MeshBasicMaterial({color:0xffffff})
var plane = new THREE.Mesh( geometry, material )
plane.rotation.x = -Math.PI / 2
scene.add(plane)

// create snow
const snowfall = new Snowfall(200000)
scene.add(snowfall.system)

// start render
const clock = new THREE.Clock()
render()




// DAT GUI
////////////////////////

function add(opt, min, max) {
  return gui.add(snowfall.opts, opt, min, max)
     .onChange(x => snowfall.updateProp(opt))
}

function addColor(opt) {
  return gui.addColor(snowfall.opts, opt)
     .onChange(x => snowfall.updateColor(opt))
}

const gui = new dat.GUI()
add('speedV', 0, 1).step(0.01)
add('speedH', 0, 10).step(0.01)
add('radius', 0, 1).step(0.001)
add('scale', 0, 10).step(0.01)
add('opacity', 0, 1).step(0.01)
addColor('color')
gui.close()


              
            
!
999px

Console