THREE.Group
前面已经介绍过,加载外部模型的时候,基本上都是一个组合对象,因为外部模型都是比较大的,把零散的模型组合到一块便于操作,可以使用THREE.Group
来操作一组对象,包括旋转,缩放,移动等,里面的子对象都会受到影响。
示例:http://ithanmang.com/threeJs/home/201808/20180820/01-group.html
THREE.Group
继承自THREE.Object3D
对象,并且和THREE.Object3D
对象没有任何区别,仅仅是名字上的差异。
使用方法
var geometry = new THREE.BoxBufferGeometry( 1, 1, 1 );
var material = new THREE.MeshBasicMaterial( {color: 0x00ff00} );
var cubeA = new THREE.Mesh( geometry, material );
cubeA.position.set( 100, 100, 0 );
var cubeB = new THREE.Mesh( geometry, material );
cubeB.position.set( -100, -100, 0 );
var group = new THREE.Group();
group.add( cubeA );
group.add( cubeB );
scene.add( group );
因为和THREE.Object3D
并无差异,所以var group = new THREE.Group();
可以使用
var group = new THREE.Object3D();
来代替
示例代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>THREE.Group 对象组合</title>
<style>
body {
margin: 0;
overflow: hidden; /* 溢出隐藏 */
}
</style>
<script src="../../libs/build/three-r93.js"></script>
<script src="../../libs/examples/js/Detector.js"></script>
<script src="../../libs/examples/js/libs/dat.gui.min.js"></script>
<script src="../../libs/examples/js/libs/stats.min.js"></script>
<script src="../../libs/examples/js/controls/OrbitControls.js"></script>
</head>
<body>
<script>
let scene, camera, renderer, controls, guiControls;
let stats = initStats();
/* 场景 */
function initScene() {
scene = new THREE.Scene();
}
/* 相机 */
function initCamera() {
camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.1, 10000);
camera.position.set(30, 20, 50);
camera.lookAt(new THREE.Vector3(0, 0, 0));
}
/* 渲染器 */
function initRender() {
renderer = new THREE.WebGLRenderer({antialias: true});
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
}
/* 灯光 */
function initLight() {
}
/* 控制器 */
function initControls() {
controls = new THREE.OrbitControls(camera, renderer.domElement);
/* 属性参数默认 */
}
/* 调试插件 */
function initGui() {
guiControls = new function () {
this.group = true;
this.rotation = false;
this.scale = 1;
};
let controls = new dat.GUI();
controls.add(guiControls, 'scale', 0.1, 2).onChange(function (e) {
group.scale.set(e, e, e);
});
controls.add(guiControls, 'group');
controls.add(guiControls, 'rotation');
}
/* 场景中的内容 */
let group = new THREE.Object3D();
// let group = new THREE.Group();
function initContent() {
let dir = new THREE.Vector3( 0, 2, 0 );
dir.normalize();
let origin = new THREE.Vector3( 0, 0, 0 );
let length = 5;
let hex = 0xff0000;
let arrowHelper = new THREE.ArrowHelper( dir, origin, length, hex );
scene.add( arrowHelper );
let normalMatrial = new THREE.MeshNormalMaterial();
let boxGeometry = new THREE.BoxGeometry(10, 10, 10);
let cylinderGeometry = new THREE.CylinderGeometry(5, 5, 10);
let box = new THREE.Mesh(boxGeometry, normalMatrial);
box.name = 'box';
let cylinder = new THREE.Mesh(cylinderGeometry, normalMatrial);
cylinder.name = 'cylinder';
box.position.x = -10;
cylinder.position.x = 10;
group.add(box);
group.add(cylinder);
scene.add(group);
}
/* 性能插件 */
function initStats() {
let stats = new Stats();
document.body.appendChild(stats.domElement);
return stats;
}
/* 窗口变动触发 */
function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
}
/* 数据更新 */
let step = 0.01;
function update() {
stats.update();
if (guiControls.group) {
group.rotateY(-step);
}
if (guiControls.rotation) {
scene.getObjectByName('box').rotateY(step+0.05);
scene.getObjectByName('cylinder').rotateY(step+0.05);
}
}
/* 初始化 */
function init() {
initScene();
initCamera();
initRender();
initLight();
initControls();
initContent();
initGui();
/* 监听事件 */
window.addEventListener('resize', onWindowResize, false);
}
/* 循环渲染 */
function animate() {
requestAnimationFrame(animate);
renderer.render(scene, camera);
update();
}
/* 初始加载 */
(function () {
console.log("three init start...");
init();
animate();
console.log("three init send...");
})();
</script>
</body>
</html>