Daily Creative Coding

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

【Three.js】点で構成された球

See the Pen sphere of points by aadebdeb (@aadebdeb) on CodePen.

<!DOCTYPE html>
<html lang="ja">
<head>
  <meta charset="utf-8">
  <title>[2017/03/28] sphere of points</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;
    var points;

    var SPACE_SIZE = 1000;

    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() {
        this.size = 3.0;
        this.opacity = 1.0;
        this.color = '#ffffff';
      };

      gui = new dat.GUI();
      gui.add(controller, 'size', 0.0, 5.0).onChange(updateMaterial);
      gui.add(controller, 'opacity', 0.0, 1.0).onChange(updateMaterial);
      gui.addColor(controller, 'color').onChange(updateMaterial);
    }

    function updateMaterial() {
      points.material.size = controller.size;
      points.material.opacity = controller.opacity;
      points.material.color = new THREE.Color(controller.color);
    }

    function initRenderer() {
      renderer = new THREE.WebGLRenderer();
      renderer.setClearColor(new THREE.Color(0xcccccc));
      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, 100);
      camera.lookAt(scene.position);
    }

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

      var geometry = new THREE.SphereGeometry(30, 64, 32);
      material = new THREE.PointsMaterial({
        color: controller.color,
        size: controller.size,
        transparent: true,
        opacity: controller.opacity
      });

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

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


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

    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:20170325210856p:plain