[three.js] 平面 (4)

three.jsを使って、平面を作るよ。 :boy:

threejs_plane4

WebGLRendererで描画するよ。
PlaneGeometry, MeshLambertMaterial, Mesh を使ってみたよ。 :bouzu:

んでもって、ImageUtils, Shader も使ったりしちゃったよ。 :beer:

Group で、2つの平面重ねたけどね。 :bear:

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 textureCube;
var backscene, backcamera;
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();
  //background();
  //initialize();
  var basePath = "assets/environment/";
  var textures = [
    basePath + "right.jpg", basePath + "left.jpg",
    basePath + "top.jpg", basePath + "bottom.jpg",
    basePath + "front.jpg", basePath + "back.jpg"
  ];
  THREE.ImageUtils.loadTextureCube(textures, undefined, 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;
  renderer.autoClear = false;
}
function loaded(data) {
  textureCube = data;

  background();
  initialize();
  render();
}
function background() {
  backscene = new THREE.Scene();
  backcamera = new THREE.PerspectiveCamera(60, window.innerWidth/window.innerHeight, 1, 10000);
  var geometry = new THREE.BoxGeometry(10000, 10000, 10000);
  var shader = THREE.ShaderLib["cube"];
  var uniforms = THREE.UniformsUtils.clone(shader.uniforms);
  uniforms[ "tCube"].value = textureCube;
  var material = new THREE.ShaderMaterial({vertexShader: shader.vertexShader, fragmentShader: shader.fragmentShader, uniforms: uniforms, depthWrite: false, side: THREE.BackSide});
  var sky = new THREE.Mesh(geometry, material);
  backscene.add(sky);
}
function initialize() {
  /*
  var geometry = new THREE.PlaneGeometry(320, 320);
  var material = new THREE.MeshLambertMaterial({envMap: textureCube, side: THREE.DoubleSide, color: 0xFFFFFF});
  material.emissive.set(0xFFFFFF);
  var plane = new THREE.Mesh(geometry, material);
  scene.add(plane);
  plane.rotation.x = 90*radian;
  */

  var plane = new THREE.Group();
  scene.add(plane);
  var geometry = new THREE.PlaneGeometry(320, 320);
  var material = new THREE.MeshLambertMaterial({envMap: textureCube, color: 0xFFFFFF, side: THREE.FrontSide});
  material.emissive.set(0xFFFFFF);
  var front = new THREE.Mesh(geometry, material);
  var back = new THREE.Mesh(geometry, material);
  plane.add(front);
  plane.add(back);
  front.rotation.x = 90*radian;
  back.rotation.x = -90*radian;
}
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);
  backcamera.rotation.copy(camera.rotation);

  renderer.render(backscene, backcamera);
  renderer.render(scene, camera);

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


[修正] (13/03/16 Sat 01:05)
three.js r57 にバージョンアップ。
[修正] (13/05/04 Sat 21:41)
three.js r58 にバージョンアップ。
[修正] (14/12/01 Mon 14:29)
three.js r69 にバージョンアップ。レンダラーのsetClearColor()メソッドを使用せず。CubeGeometry を BoxGeometry に変更。
[修正] (14/12/03 Wed 12:26)
ImageUtils の読み込み処理方法を変更。
[修正] (14/12/07 Sun 15:45)
PerspectiveCamera の fov の値を 75 から 60 に変更。AmbientLight を使用しない方法に変更。
[修正] (14/12/09 Tue 16:00)
コンテナとしての Object3D を Group に変更。
[修正] (14/12/27 Sat 21:09)
HTML記述を変更。