diff --git a/ShadowEditor.Web/src/component/physics/RigidBodyComponent.js b/ShadowEditor.Web/src/component/physics/RigidBodyComponent.js index 3b5670d8..7f54761d 100644 --- a/ShadowEditor.Web/src/component/physics/RigidBodyComponent.js +++ b/ShadowEditor.Web/src/component/physics/RigidBodyComponent.js @@ -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; \ No newline at end of file diff --git a/ShadowEditor.Web/src/component/physics/helper/BoxShapeHelper.js b/ShadowEditor.Web/src/component/physics/helper/BoxShapeHelper.js index e69de29b..6888ecaf 100644 --- a/ShadowEditor.Web/src/component/physics/helper/BoxShapeHelper.js +++ b/ShadowEditor.Web/src/component/physics/helper/BoxShapeHelper.js @@ -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; \ No newline at end of file diff --git a/ShadowEditor.Web/src/editor/Editor.js b/ShadowEditor.Web/src/editor/Editor.js index 8fcb9454..c0fad426 100644 --- a/ShadowEditor.Web/src/editor/Editor.js +++ b/ShadowEditor.Web/src/editor/Editor.js @@ -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) { // 添加脚本