正方体物体帮助器。

This commit is contained in:
liteng 2019-01-08 21:32:38 +08:00
parent 849861b6d6
commit f2ccb44ebd
3 changed files with 88 additions and 0 deletions

View File

@ -1,4 +1,9 @@
import BaseComponent from '../BaseComponent';
import BoxShapeHelper from './helper/BoxShapeHelper';
var helpers = {
BoxShapeHelper: BoxShapeHelper,
};
/**
* 刚体组件
@ -139,6 +144,7 @@ RigidBodyComponent.prototype.render = function () {
RigidBodyComponent.prototype.onObjectSelected = function () {
this.updateUI();
this.showPhysicsShapeHelper();
};
RigidBodyComponent.prototype.onObjectChanged = function () {
@ -192,4 +198,29 @@ RigidBodyComponent.prototype.onChange = function () {
physics.inertia.z = inertiaZ.getValue();
};
// -------------------------- 物理形状帮助器 -------------------------------------
RigidBodyComponent.prototype.showPhysicsShapeHelper = function () {
if (this.selected == null) {
return;
}
if (this.helper !== undefined) {
this.app.editor.removePhysicsHelper(this.selected, this.helper);
}
var geometry = this.selected.geometry;
geometry.computeBoundingBox();
var box = geometry.boundingBox;
box.applyMatrix4(this.selected.matrixWorld);
var x = box.max.x - box.min.x;
var y = box.max.y - box.min.y;
var z = box.max.z - box.min.z;
this.helper = new BoxShapeHelper(x, y, z);
this.app.editor.addPhysicsHelper(this.selected, this.helper);
};
export default RigidBodyComponent;

View File

@ -0,0 +1,29 @@
/**
* 立方体形状帮助器
* @param {*} x
* @param {*} y
* @param {*} z
*/
function BoxShapeHelper(x = 1.0, y = 1.0, z = 1.0) {
var geometry = new THREE.BoxBufferGeometry(x, y, z);
var material = new THREE.MeshBasicMaterial({
color: 0xffff00
});
material.wireframe = true;
material.wireframeLinewidth = 10;
material.polygonOffset = true;
material.polygonOffsetFactor = -1;
material.polygonOffsetUnits = -1;
THREE.Mesh.call(this, geometry, material);
};
BoxShapeHelper.prototype = Object.create(THREE.Mesh.prototype);
BoxShapeHelper.prototype.constructor = BoxShapeHelper;
BoxShapeHelper.prototype.update = function () {
};
export default BoxShapeHelper;

View File

@ -319,6 +319,34 @@ Editor.prototype.onOptionsChanged = function (options) { // 帮助器改变事
});
};
Editor.prototype.addPhysicsHelper = function (object, helper) { // 添加物理帮助器
var geometry = new THREE.SphereBufferGeometry(2, 4, 2);
var material = new THREE.MeshBasicMaterial({
color: 0xff0000,
visible: false
});
var picker = new THREE.Mesh(geometry, material);
picker.name = 'picker';
picker.userData.object = object;
helper.add(picker);
this.sceneHelpers.add(helper);
this.helpers[object.id] = helper;
this.objects.push(picker);
};
Editor.prototype.removePhysicsHelper = function (object, helper) { // 移除物理帮助器
if (this.helpers[object.id] !== undefined) {
var helper = this.helpers[object.id];
helper.parent.remove(helper);
delete this.helpers[object.id];
var objects = this.objects;
objects.splice(objects.indexOf(helper.getObjectByName('picker')), 1);
}
};
// ------------------------ 脚本 ----------------------------
Editor.prototype.addScript = function (object, script) { // 添加脚本