three.jsはある物体をめぐって回転することを実現します。
4585 ワード
話が多くないので、コードを見てください。
右上の角をドラッグして変化を観察できます。
右上の角をドラッグして変化を観察できます。
<!DOCTYPE html>
<html lang="en" style="width: 100%; height:100%;">
<head>
<meta charset="UTF-8">
<title>Document</title>
<script src="http://cdn.bootcss.com/three.js/r83/three.min.js"></script>
<script src="http://cooklife.cn/html/node_modules/dat.gui/build/dat.gui.min.js"></script>
</head>
<body onload="threeExcute()" style="width: 100%; height:100%;">
<div id="box"></div>
</body>
<!-- Three.js :
1. three.js
2. camera
3. scene
4. light
5. object
-->
<script>
// 1. three.js
var renderer;
function initThree(){
width = document.getElementById("box").clientWidth;
height = document.getElementById("box").clientHeight;
renderer = new THREE.WebGLRenderer({
antialias:true
});/* ( : )*/
renderer.setSize(width,height);
document.getElementById("box").appendChild(renderer.domElement);
/* canvas (clearColor) (clearAlpha) */
renderer.setClearColor(0xFFFF00,1.0);
}
// 2. camera
var camera;
function initCamera(){
camera = new THREE.PerspectiveCamera(45,width/height,1,10000);
camera.position.x = 1000;
camera.position.y = 1000;
camera.position.z = 1000;
camera.up.x = 0;
camera.up.y = 0;
camera.up.z = 100;
camera.lookAt({x:0,y:0,z:0}); //
}
// 3.
var scene;
function initScene(){
scene = new THREE.Scene();
}
// 4. light
var light;
function initLight(){
light = new THREE.DirectionalLight(0xFF00FF, 1.0, 0); //
light.position.set(100,100, 200); //
scene.add(light); //
}
//5.
var sphereMesh;
var cubeMesh;
var cubeMesh2;
var cubeMesh3;
var cubeMesh4;
var cubeMesh5;
var cubeMesh6;
function initObject(){
cubeMesh = new THREE.Mesh(new THREE.BoxGeometry(80,80,80),new THREE.MeshLambertMaterial({color:0xff0000})/*
*/);
cubeMesh2 = new THREE.Mesh(new THREE.BoxGeometry(80,80,80),new THREE.MeshLambertMaterial({color:0xff0000})/*
*/);
cubeMesh3 = new THREE.Mesh(new THREE.BoxGeometry(80,80,80),new THREE.MeshLambertMaterial({color:0xff0000})/*
*/);
sphereMesh = new THREE.Mesh(new THREE.SphereGeometry(200,200,200),new THREE.MeshLambertMaterial({color:0xff00FF})/* */); //
sphereMesh.position.set(0,0,0); /* */
cubeMesh2.position.set(400,0,0);
cubeMesh.position.set(390,150,0);
cubeMesh3.position.set(380,100,0);
/*
* 。。。
*/
var pivotPoint = new THREE.Object3D();
pivotPoint.add(cubeMesh);
pivotPoint.add(cubeMesh2);
pivotPoint.add(cubeMesh3);
sphereMesh.add(pivotPoint);
scene.add(sphereMesh);
sphereMesh.name = 'cube'
}
control = new function () {
this.rotationSpeedX = 0.001;
this.rotationSpeedY = 0.001;
this.rotationSpeedZ = 0.001;
};
function addController(){
var gui = new dat.GUI();
gui.add(control, 'rotationSpeedX', -0.2, 0.2);
gui.add(control, 'rotationSpeedY', -0.2, 0.2);
gui.add(control, 'rotationSpeedZ', -0.2, 0.2);
}
function render(){
renderer.render(scene, camera);
scene.getObjectByName('cube').rotation.x += control.rotationSpeedX;
scene.getObjectByName('cube').rotation.y += control.rotationSpeedY;
scene.getObjectByName('cube').rotation.z += control.rotationSpeedZ;
requestAnimationFrame(render);
}
function threeExcute(){
initThree();
initCamera();
initScene();
initLight();
initObject();
renderer.clear();
addController();
render();
}
</script>
<style type="text/css">
div#box{
border: none;
cursor: move;
width: 100%;
height: 100%;
background-color: #EEEEEE;
}
</style>
</html>
以上が本文の全部です。本文の内容は皆さんの学習や仕事に一定の助けをもたらしてくれると同時に、私達を応援してください。