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

Daily Creative Coding

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

【Three.js】マウスでブロックの高さを制御する

See the Pen [Three.js]blocks controlled height by mouse by aadebdeb (@aadebdeb) on CodePen.

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>[2017/03/22]blocks controlled height by mouse</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;
    window.onmousemove = onMouseMove;

    var BLOCK_NUM = 5;
    var BLOCK_WIDTH = 40;
    var BLOCK_MIN_HEIGHT = 5;
    var BLOCK_MAX_HEIGHT = 200;

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

    function init() {

      mouseX = 0;
      mouseY = 0;

      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(0xcccccc));
      renderer.setSize(window.innerWidth, window.innerHeight);
      document.body.appendChild(renderer.domElement);
      renderer.domElement.addEventListener('mousemove', onMouseMove);
    }


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

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

      group = new THREE.Group();
      for (var x = 1; x <= BLOCK_NUM; x++) {
        for (var z = 1; z <= BLOCK_NUM; z++) {
          var block = new THREE.Mesh(
            new THREE.BoxGeometry(BLOCK_WIDTH, BLOCK_MIN_HEIGHT, BLOCK_WIDTH),
            new THREE.MeshPhongMaterial(0xffffff)
          );
          block.position.set(
            BLOCK_WIDTH * (x - BLOCK_NUM / 2),
            0,
            BLOCK_WIDTH * (z - BLOCK_NUM / 2)
          );
          group.add(block);
        }
      }
      scene.add(group);

      var ambientLight = new THREE.AmbientLight(0xdddddd);
      scene.add(ambientLight);

      var pointLight = new THREE.PointLight(0xffffff);
      pointLight.position.set(100, 100, 100);
      scene.add(pointLight);

    }

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


    function render() {
      stats.update();
      renderer.render(scene, camera);
      requestAnimationFrame(render);
    }

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

    function onMouseMove(e) {
      var mouseX = map(e.offsetX, 0, renderer.domElement.width, -BLOCK_WIDTH * BLOCK_NUM / 2, BLOCK_WIDTH * BLOCK_NUM / 2);
      var mouseZ = map(e.offsetY, 0, renderer.domElement.height, -BLOCK_WIDTH * BLOCK_NUM / 2, BLOCK_WIDTH * BLOCK_NUM / 2);
      group.traverse(function(obj) {
        if (obj instanceof THREE.Mesh) {
          var d = Math.sqrt(Math.pow(obj.position.x - mouseX, 2) + Math.pow(obj.position.z - mouseZ, 2));
          d = d > 100 ? 100 : d;
          group.remove(obj);
          var h = map(d, 0, 100, BLOCK_MAX_HEIGHT, BLOCK_MIN_HEIGHT);
          var block = new THREE.Mesh(
            new THREE.BoxGeometry(BLOCK_WIDTH, h, BLOCK_WIDTH),
            new THREE.MeshPhongMaterial(0xffffff)
          );
          block.position.set(
            obj.position.x,
            0,
            obj.position.z
          );
          group.add(block);

        }
      });
    }


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

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