読者です 読者をやめる 読者になる 読者になる

Daily Creative Coding

元「30 min. Processing」。毎日、Creative Codingします。

【Three.js】ポイントクラウド

See the Pen point cloud by aadebdeb (@aadebdeb) on CodePen.

<!DOCTYPE html>
<html lang="ja">
<head>
  <meta charset="utf-8">
  <title>[2017/04/25] point cloud</title>
  <style>
    body {
      margin: 0;
      overflow: hidden;
    }
  </style>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/stats.js/r16/Stats.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/dat-gui/0.6.3/dat.gui.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/84/three.min.js"></script>
  <script>
    window.onload = function() {
      init();
      render();
    }
    window.onresize = onResize;

    var camera, scene, renderer;
    var stats;
    var controller;


    function init() {
      rotation = new THREE.Euler();

      initGui();
      initStats();

      initScene();
      initCamera();
      initRenderer();
    }

    function initStats() {
      stats = new Stats();
      stats.setMode(0);
      stats.domElement.style.position = 'absolute';
      stats.domElement.style.left = '0px';
      stats.domElement.style.top = '0px';
      document.body.appendChild(stats.domElement);
    }

    function initGui() {
      controller = new function() {

      };

      gui = new dat.GUI();

    }

    function initRenderer() {
      renderer = new THREE.WebGLRenderer();
      renderer.setClearColor(new THREE.Color(0x000000));
      renderer.setSize(window.innerWidth, window.innerHeight);
      document.body.appendChild(renderer.domElement);
    }

    function initCamera() {
      camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.1, 1000);
      camera.position.set(0, 0, 130);
      camera.lookAt(scene.position);
    }

    function initScene() {
      scene = new THREE.Scene();

      var geometry = new THREE.Geometry();
      var material = new THREE.PointsMaterial({
        color: 0xffffff,
        size: 0.3,
        vertexColors: true
      });

      for (var i = 0; i < 200000; i++) {
        var vec = new THREE.Vector3(Math.random(), Math.random(), Math.random());
        var pos = new THREE.Vector3(
          map(vec.x, 0, 1, -30, 30),
          map(vec.y, 0, 1, -30, 30),
          map(vec.z, 0, 1, -30, 30)
        );
        geometry.vertices.push(pos);
        var color1 = new THREE.Color(0xc71585);
        var color2 = new THREE.Color(0x16c757);
        color1.lerp(color2, vec.length() / Math.sqrt(3));
        geometry.colors.push(color1);
      }

      var points = new THREE.Points(geometry, material);
      scene.add(points);
    }

    function getRandom(min, max) {
      return Math.random() * (max - min) + min;
    }

    function map(v, min1, max1, min2, max2) {
      return (v / (max1 - min1)) * (max2 - min2) + min2;
    }


    function render() {
      scene.rotation.x += 0.005;
      scene.rotation.y += 0.005;
      scene.rotation.z += 0.005;
      stats.update();
      renderer.render(scene, camera);
      requestAnimationFrame(render);
    }

    function onResize() {
      camera.aspect = window.innerWidth / window.innerHeight;
      camera.updateProjectionMatrix();
      renderer.setSize(window.innerWidth, window.innerHeight);
    }

  </script>
</head>
<body>
</body>
</html>
f:id:aa_debdeb:20170323090931p:plain