[three.js] 直線 (2)

three.jsを使って、直線を作るよ。 :boy:

threejs_line2

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

index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="initial-scale=1.0">
<title>直線 (2) | 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 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();

  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 initialize() {
  var geometry = new THREE.Geometry();
  for (var n = 0; n < 5; n++) {
    geometry.vertices.push(new THREE.Vector3(-160, 0, 80*n - 160));
    geometry.vertices.push(new THREE.Vector3(160, 0, 80*n - 160));
    geometry.vertices.push(new THREE.Vector3(80*n - 160, 0, -160));
    geometry.vertices.push(new THREE.Vector3(80*n - 160, 0, 160));
  }
  var material = new THREE.LineBasicMaterial({color: 0xFFFFFF});
  var line = new THREE.Line(geometry, material, THREE.LinePieces);
  scene.add(line);
}
function render() {
  requestAnimationFrame(render);

  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:43)
three.js r58 にバージョンアップ。
[修正] (14/12/01 Mon 18:40)
three.js r69 にバージョンアップ。レンダラーのsetClearColor()メソッドを使用。
[修正] (14/12/07 Sun 14:10)
PerspectiveCamera の fov の値を 75 から 60 に変更。
[修正] (14/12/27 Sat 21:09)
HTML記述を変更。