A Pen by Steven Morasky on CodePen.
Created
September 7, 2018 21:25
-
-
Save smor25/73e1ac81e9537214a68d084fb0ae33bb to your computer and use it in GitHub Desktop.
Three.js Particle Blown Up
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div id="canvas"></div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var camera, scene, renderer, particles, particle, material, particleCount, points, texture; | |
var xSpeed, ySpeed; | |
xSpeed = 0.0005; | |
ySpeed = 0.001; | |
var winWidth, winHeight; | |
winWidth = window.innerWidth; | |
winHeight = window.innerHeight; | |
init(); | |
animate(); | |
function init(){ | |
scene = new THREE.Scene(); | |
scene.fog = new THREE.FogExp2('#222', 0.001); | |
camera = new THREE.PerspectiveCamera(75, winWidth/winHeight, 1, 1000); | |
camera.position.z = 500; | |
// particle | |
// transparentとblendingたぶん効いてない | |
material = new THREE.PointsMaterial({ | |
color: 0xffffff, | |
size: 3.5, | |
transparent: true, | |
blending: THREE.AdditiveBlending | |
}); | |
particleCount = 50000; | |
particles = new THREE.Geometry(); | |
for (var i = 0; i < particleCount; i++) { | |
var px = Math.random() * 2000 - 1000; | |
var py = Math.random() * 2000 - 1000; | |
var pz = Math.random() * 2000 - 1000; | |
particle = new THREE.Vector3(px, py, pz); | |
particle.velocity = new THREE.Vector3(0, Math.random(), 0); | |
particles.vertices.push(particle); | |
} | |
points = new THREE.Points(particles, material); | |
points.sortParticles = true; | |
scene.add(points); | |
renderer = new THREE.WebGLRenderer({ antialias: true }); | |
renderer.setSize(winWidth, winHeight); | |
renderer.setClearColor('#222', 1); | |
document.getElementById('canvas').appendChild(renderer.domElement); | |
} | |
function animate(){ | |
requestAnimationFrame(animate); | |
scene.rotation.y += xSpeed; | |
// パーティクル上下移動 | |
var i = particleCount; | |
while(i--){ | |
var particle = particles.vertices[i]; | |
// y | |
if(particle.y > 1000){ | |
particle.y = -1000; | |
particle.velocity.y = Math.random(); | |
} | |
particle.velocity.y += Math.random() * ySpeed; | |
particle.add(particle.velocity); | |
} | |
points.geometry.verticesNeedUpdate = true; | |
render(); | |
} | |
function render(){ | |
camera.lookAt(scene.position); | |
renderer.render(scene, camera); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r72/three.min.js"></script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
body { | |
padding: 0; | |
margin: 0; | |
overflow: hidden; | |
height:100%; | |
width:100%; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment