[three.js] パーティクル (4)

three.jsを使って、パーティクルを扱うよ。 :boy:

threejs_particle4

WebGLRendererで描画するよ。
Geometry, PointsMaterial, Points を使ってみたよ。 :bouzu:

さらに、TextureLoader, Texture も使ってみたよ。 :cake:

index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="initial-scale=1.0">
<title>パーティクル (4) | three.js</title>
<script src="js/three.min.js"></script>
<script src="js/detector.js"></script>
<script src="js/stats.min.js"></script>
<style type="text/css">
  html { overflow:hidden; }
  body { margin:0; padding:0; }
</style>
</head>
<body style="background-color:#000000">
<script>
var scene, camera, renderer, stats;
var texture;
var particles;
var radius = 500;
var angle = 90;
var degree = 0;
var depression = 30;
var radian = Math.PI/180;
var center = new THREE.Object3D();

window.onload = init;

function init() {
  if (!Detector.webgl) Detector.addGetWebGLMessage();

  scene = new THREE.Scene();
  camera = new THREE.PerspectiveCamera(60, window.innerWidth/window.innerHeight, 0.1, 1000);
  scene.add(camera);
  renderer = new THREE.WebGLRenderer({antialias: true});
  renderer.setSize(window.innerWidth, window.innerHeight);
  document.body.appendChild(renderer.domElement);
  setup();
  //initialize();
  var loader = new THREE.TextureLoader();
  loader.load("assets/particle.png", loaded);

  stats = new Stats();
  stats.setMode(0);
  stats.domElement.style.position = "fixed";
  stats.domElement.style.right = "0px";
  stats.domElement.style.top = "0px";
  document.body.appendChild(stats.domElement);

  //render();
  window.addEventListener("resize", resize, false);
}
function setup() {
  renderer.setClearColor(0x000000, 1);
  camera.position.z = radius;
}
function loaded(data) {
  texture = data;

  initialize();

  render();
}
function initialize() {
  var geometry = new THREE.Geometry();
  for (var n = 0; n < 10000; n++) {
    var vertex = new THREE.Vector3();
    vertex.x = (Math.random() 0.5)*100;
    vertex.y = (Math.random() 0.5)*100;
    vertex.z = (Math.random() 0.5)*100;
    geometry.vertices.push(vertex);
  }
  var material = new THREE.PointsMaterial({size: 10, map: texture, blending: THREE.AdditiveBlending, transparent: true, depthTest: false});
  particles = new THREE.Points(geometry, material);
  particles.sortParticles = true;
  scene.add(particles);
}
function render() {
  requestAnimationFrame(render);

  for (var n = 0; n < 10000; n++) {
    var vertex = particles.geometry.vertices[n];
    vertex.x += (Math.random() 0.5)*4;
    vertex.y += (Math.random() 0.5)*4;
    vertex.z += (Math.random() 0.5)*4;
  }
  particles.geometry.verticesNeedUpdate = true;

  angle -= 0.5;
  degree += 1;
  var dip = depression*Math.sin(degree*radian);
  camera.position.x = radius*Math.cos(angle*radian)*Math.cos(dip*radian);
  camera.position.y = radius*Math.sin(dip*radian);
  camera.position.z = radius*Math.sin(angle*radian)*Math.cos(dip*radian);
  camera.lookAt(center.position);

  renderer.render(scene, camera);

  stats.update();
}
function resize(event) {
  camera.aspect = window.innerWidth/window.innerHeight;
  camera.updateProjectionMatrix();
  renderer.setSize(window.innerWidth, window.innerHeight);
}
</script>
</body>
</html>


[修正] (13/05/04 Sat 21:47)
three.js r58 にバージョンアップ。
[修正] (14/12/01 Mon 19:27)
three.js r69 にバージョンアップ。ParticleBasicMaterial を PointCloudMaterial に、ParticleSystem を PointCloud に変更。
[修正] (14/12/03 Wed 12:50)
ImageUtils の読み込み処理方法を変更。
[修正] (14/12/07 Sun 14:22)
PerspectiveCamera の fov の値を 75 から 60 に変更。
[修正] (14/12/27 Sat 21:10)
HTML記述を変更。
[修正] (18/12/26 Wed 11:16)
three.js r99 にバージョンアップ。PointCloudMaterial を PointsMaterial に、PointCloud を Points に変更。PointsMaterial のdepthTestプロパティを追加。 particles.geometry.verticesNeedUpdate = true; も追加。