mirror of
https://github.com/openglobus/openglobus.git
synced 2025-12-08 19:25:27 +00:00
Merge pull request #939 from openglobus/938-child-entity-remove
Child Entity remove bug
This commit is contained in:
commit
3e995895b1
@ -12,104 +12,127 @@ import {
|
||||
|
||||
let renderer = new Renderer("frame", {
|
||||
msaa: 8,
|
||||
controls: [new control.SimpleNavigation({ speed: 0.01 })],
|
||||
controls: [
|
||||
new control.SimpleNavigation({ speed: 0.01 }),
|
||||
new control.GeoObjectEditor()
|
||||
],
|
||||
autoActivate: true
|
||||
});
|
||||
|
||||
window.renderer = renderer;
|
||||
|
||||
class MyScene extends RenderNode {
|
||||
constructor() {
|
||||
super("MyScene");
|
||||
}
|
||||
|
||||
init() {
|
||||
const baseObj = Object3d.createCube(10, 10, 10).translate(new Vec3(0, 0, 0)).setMaterial({
|
||||
ambient: "#c2c2c2",
|
||||
diffuse: "#ffffff",
|
||||
shininess: 1
|
||||
});
|
||||
|
||||
window.test = () => {
|
||||
parentEntity.setPitch(0);
|
||||
cube2.setPitch(0);
|
||||
cube3.setPitch(0);
|
||||
const baseObj = Object3d
|
||||
.createCube(0.4, 2, 0.4)
|
||||
.translate(new Vec3(0, 1, 0))
|
||||
.setMaterial({
|
||||
ambient: "#882a2a",
|
||||
diffuse: "#fb3434",
|
||||
shininess: 1
|
||||
});
|
||||
|
||||
parentEntity.setYaw(0);
|
||||
cube2.setYaw(0);
|
||||
cube3.setYaw(0);
|
||||
const frustumObj = Object3d
|
||||
.createFrustum(3, 2, 1)
|
||||
.setMaterial({
|
||||
ambient: "#236028",
|
||||
diffuse: "#1cdd23",
|
||||
shininess: 1
|
||||
});
|
||||
|
||||
this.renderer.activeCamera.set(new Vec3(10.5, 0, 0), new Vec3(0, 0, 0), new Vec3(0, 1, 0));
|
||||
}
|
||||
const cylinderObj = Object3d
|
||||
.createCylinder(1, 0, 3)
|
||||
.applyMat4(new Mat4().setRotation(new Vec3(1, 0, 0), 90 * Math.PI / 180))
|
||||
.setMaterial({
|
||||
ambient: "#773381",
|
||||
diffuse: "#ef00ff",
|
||||
shininess: 1
|
||||
});
|
||||
|
||||
let parentEntity = new Entity({
|
||||
cartesian: new Vec3(0, 0, 0),
|
||||
cartesian: new Vec3(1, 1, 1),
|
||||
independentPicking: true,
|
||||
//yaw: 45 * Math.PI / 180,
|
||||
//pitch: 45 * Math.PI / 180,
|
||||
geoObject: {
|
||||
color: "rgb(90,90,90)",
|
||||
scale: 1,
|
||||
instanced: true,
|
||||
tag: `baseObj`,
|
||||
object3d: baseObj
|
||||
object3d: baseObj,
|
||||
}
|
||||
});
|
||||
|
||||
let cube2 = new Entity({
|
||||
cartesian: new Vec3(45, 0, 5),
|
||||
let childEntity = new Entity({
|
||||
cartesian: new Vec3(0, 1, 0),
|
||||
independentPicking: true,
|
||||
//yaw: 45 * Math.PI / 180,
|
||||
//pitch: 45 * Math.PI / 180,
|
||||
relativePosition: true,
|
||||
geoObject: {
|
||||
color: "rgb(90,90,90)",
|
||||
scale: 1,
|
||||
instanced: true,
|
||||
tag: `baseObj`,
|
||||
object3d: baseObj
|
||||
tag: `frustumObj`,
|
||||
object3d: frustumObj,
|
||||
}
|
||||
});
|
||||
|
||||
let cube3 = new Entity({
|
||||
cartesian: new Vec3(-1, 3, 1),
|
||||
let childEntity2 = new Entity({
|
||||
cartesian: new Vec3(0, 1, 0),
|
||||
independentPicking: true,
|
||||
//yaw: 45 * Math.PI / 180,
|
||||
pitch: 45 * Math.PI / 180,
|
||||
relativePosition: true,
|
||||
yaw: 90 * Math.PI / 180,
|
||||
geoObject: {
|
||||
color: "rgb(90,90,90)",
|
||||
scale: 1,
|
||||
color: "rgb(255,255,255)",
|
||||
instanced: true,
|
||||
tag: `baseObj`,
|
||||
object3d: baseObj
|
||||
tag: `frustumObj`,
|
||||
object3d: frustumObj,
|
||||
}
|
||||
});
|
||||
|
||||
let childChildEntity = new Entity({
|
||||
cartesian: new Vec3(0, 3, -1),
|
||||
independentPicking: true,
|
||||
relativePosition: true,
|
||||
geoObject: {
|
||||
color: "rgb(90,90,90)",
|
||||
instanced: true,
|
||||
tag: `cylinderObj`,
|
||||
object3d: cylinderObj,
|
||||
}
|
||||
});
|
||||
|
||||
let childChildEntity2 = new Entity({
|
||||
cartesian: new Vec3(0, 3, -1),
|
||||
independentPicking: true,
|
||||
relativePosition: true,
|
||||
geoObject: {
|
||||
color: "rgb(90,90,90)",
|
||||
instanced: true,
|
||||
tag: `cylinderObj`,
|
||||
object3d: cylinderObj,
|
||||
}
|
||||
});
|
||||
|
||||
childEntity.appendChild(childChildEntity);
|
||||
childEntity2.appendChild(childChildEntity2);
|
||||
parentEntity.appendChild(childEntity);
|
||||
parentEntity.appendChild(childEntity2);
|
||||
|
||||
let collection = new EntityCollection({
|
||||
entities: [parentEntity, cube2]
|
||||
entities: [parentEntity]
|
||||
});
|
||||
|
||||
collection.addTo(this);
|
||||
|
||||
this.renderer.activeCamera.set(new Vec3(0, 100, 100), new Vec3(0, 0, 0));
|
||||
window.collection = collection;
|
||||
|
||||
this.renderer.activeCamera.set(new Vec3(-4, 11, 13), new Vec3(1, 0, 0));
|
||||
|
||||
this.renderer.activeCamera.update();
|
||||
this.renderer.activeCamera.isOrthographic = true;
|
||||
|
||||
this.renderer.getDepthMinDistanceAsync().then((dist) => {
|
||||
if (!dist) {
|
||||
dist = this.renderer.activeCamera.eye.length();
|
||||
}
|
||||
this.renderer.activeCamera.focusDistance = dist;
|
||||
this.renderer.activeCamera.isOrthographic = true;
|
||||
});
|
||||
|
||||
|
||||
this.renderer.events.on("rclick", (e) => {
|
||||
//let dist = this.renderer.getDistanceFromPixel(e.pos);
|
||||
//let dir = this.renderer.activeCamera.unproject(e.x, e.y, dist);
|
||||
//console.log(dir);
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
renderer.addNodes([new scene.Axes(), new MyScene()]);
|
||||
|
||||
renderer.addNodes([
|
||||
new scene.Axes(),
|
||||
new MyScene()
|
||||
]);
|
||||
24
sandbox/nestedEntities2/nestedEntities.html
Normal file
24
sandbox/nestedEntities2/nestedEntities.html
Normal file
@ -0,0 +1,24 @@
|
||||
<html>
|
||||
<head>
|
||||
<title>Draco loader sample</title>
|
||||
<meta charset="utf-8"/>
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
|
||||
<script
|
||||
type="text/javascript"
|
||||
src="https://www.gstatic.com/draco/versioned/decoders/1.5.7/draco_decoder.js"
|
||||
></script>
|
||||
<script src="./nestedEntities.js" type="module"></script>
|
||||
<link rel="stylesheet" href="../../css/og.css" type="text/css"/>
|
||||
<style>
|
||||
body {
|
||||
padding: 0;
|
||||
margin: 0;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<div id="earth" style="width:100%;height:100%"></div>
|
||||
<div id="l0" class="defaultText" style="color:black"></div>
|
||||
</body>
|
||||
</html>
|
||||
91
sandbox/nestedEntities2/nestedEntities.js
Normal file
91
sandbox/nestedEntities2/nestedEntities.js
Normal file
@ -0,0 +1,91 @@
|
||||
import {
|
||||
Globe,
|
||||
Vector,
|
||||
GlobusRgbTerrain,
|
||||
OpenStreetMap,
|
||||
Bing,
|
||||
Object3d,
|
||||
Vec3,
|
||||
Entity,
|
||||
control,
|
||||
LonLat
|
||||
} from "../../lib/og.es.js";
|
||||
|
||||
let objLayer = new Vector("Obj.Layer", {
|
||||
scaleByDistance: [50, 50000, 1]
|
||||
});
|
||||
|
||||
let globe = new Globe({
|
||||
target: "earth",
|
||||
name: "Earth",
|
||||
terrain: new GlobusRgbTerrain(),
|
||||
layers: [new OpenStreetMap(), new Bing(), objLayer],
|
||||
});
|
||||
|
||||
globe.planet.addControls([
|
||||
new control.GeoObjectEditor(),
|
||||
new control.LayerSwitcher()
|
||||
]);
|
||||
|
||||
const baseObj = Object3d.createCube(0.4, 2, 0.4)
|
||||
.translate(new Vec3(0, 1, 0))
|
||||
.setMaterial({
|
||||
ambient: "#802727",
|
||||
diffuse: "#ff5252",
|
||||
});
|
||||
|
||||
const viewObj = Object3d.createFrustum(3, 2, 1)
|
||||
.setMaterial({
|
||||
ambient: "#28552b",
|
||||
diffuse: "#1cdd23",
|
||||
});
|
||||
|
||||
const viewObj2 = Object3d.createFrustum(3, 2, 1)
|
||||
.setMaterial({
|
||||
ambient: "#5f2563",
|
||||
diffuse: "#ef00ff",
|
||||
});
|
||||
|
||||
const pos = new LonLat(-105.6173319876, 39.615583413, 4057.9466);
|
||||
|
||||
let parentEntity = new Entity({
|
||||
lonlat: pos,
|
||||
independentPicking: true,
|
||||
geoObject: {
|
||||
instanced: true,
|
||||
tag: `baseObj`,
|
||||
object3d: baseObj,
|
||||
}
|
||||
});
|
||||
|
||||
let childEntity = new Entity({
|
||||
cartesian: new Vec3(0, 1, 0),
|
||||
independentPicking: true,
|
||||
relativePosition: true,
|
||||
geoObject: {
|
||||
instanced: true,
|
||||
tag: `viewObj`,
|
||||
object3d: viewObj,
|
||||
}
|
||||
});
|
||||
|
||||
let childChildEntity = new Entity({
|
||||
cartesian: new Vec3(0, 3, -1),
|
||||
independentPicking: true,
|
||||
relativePosition: true,
|
||||
geoObject: {
|
||||
instanced: true,
|
||||
tag: `viewObj2`,
|
||||
object3d: viewObj2,
|
||||
}
|
||||
});
|
||||
|
||||
childEntity.appendChild(childChildEntity);
|
||||
parentEntity.appendChild(childEntity);
|
||||
|
||||
objLayer.add(parentEntity);
|
||||
|
||||
globe.planet.camera.setLonLat(
|
||||
new LonLat(-105.61717175714179, 39.61567256262465, 4064.033358156039),
|
||||
pos, // look point
|
||||
);
|
||||
@ -560,6 +560,7 @@ class Entity {
|
||||
public remove() {
|
||||
this._layer && this._layer.removeEntity(this);
|
||||
this._entityCollection && this._entityCollection.removeEntity(this);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@ -415,7 +415,6 @@ class EntityCollection {
|
||||
|
||||
for (let i = 0; i < entity.childEntities.length; i++) {
|
||||
entity.childEntities[i]._entityCollection = this;
|
||||
entity.childEntities[i]._entityCollectionIndex = entity._entityCollectionIndex;
|
||||
this._addRecursively(entity.childEntities[i]);
|
||||
}
|
||||
}
|
||||
@ -489,40 +488,45 @@ class EntityCollection {
|
||||
}
|
||||
}
|
||||
|
||||
protected _removeEntity(entity: Entity) {
|
||||
if (entity.parent) {
|
||||
let arr = entity.parent.childEntities;
|
||||
for (let i = 0, len = arr.length; i < len; i++) {
|
||||
if (arr[i].isEqual(entity)) {
|
||||
arr.splice(i, 1);
|
||||
break;
|
||||
}
|
||||
}
|
||||
entity.parent = null;
|
||||
} else {
|
||||
this._entities.splice(entity._entityCollectionIndex, 1);
|
||||
this.reindexEntitiesArray(entity._entityCollectionIndex);
|
||||
}
|
||||
|
||||
// clear picking color
|
||||
if (this.renderNode && this.renderNode.renderer) {
|
||||
this.renderNode.renderer.clearPickingColor(entity);
|
||||
entity._pickingColor.clear();
|
||||
}
|
||||
|
||||
this._removeRecursively(entity);
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes entity from this collection.
|
||||
* @public
|
||||
* @param {Entity} entity - Entity to remove.
|
||||
*/
|
||||
public removeEntity(entity: Entity) {
|
||||
this._entities.splice(entity._entityCollectionIndex, 1);
|
||||
this.reindexEntitiesArray(entity._entityCollectionIndex);
|
||||
|
||||
// clear picking color
|
||||
if (this.renderNode && this.renderNode.renderer) {
|
||||
this.renderNode.renderer.clearPickingColor(entity);
|
||||
entity._pickingColor.clear();
|
||||
}
|
||||
|
||||
if (this.belongs(entity)) {
|
||||
this._removeRecursively(entity);
|
||||
this._removeEntity(entity);
|
||||
this.events.dispatch(this.events.entityremove, entity);
|
||||
}
|
||||
|
||||
this.events.dispatch(this.events.entityremove, entity);
|
||||
}
|
||||
|
||||
public _removeEntitySilent(entity: Entity) {
|
||||
this._entities.splice(entity._entityCollectionIndex, 1);
|
||||
this.reindexEntitiesArray(entity._entityCollectionIndex);
|
||||
|
||||
// clear picking color
|
||||
if (this.renderNode && this.renderNode.renderer) {
|
||||
this.renderNode.renderer.clearPickingColor(entity);
|
||||
entity._pickingColor.clear();
|
||||
}
|
||||
|
||||
if (this.belongs(entity)) {
|
||||
this._removeRecursively(entity);
|
||||
this._removeEntity(entity);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -692,7 +692,12 @@ export class GeoObjectHandler {
|
||||
if (tagData.numInstances === 0) {
|
||||
tagData.clear();
|
||||
this._instanceDataMap.delete(tag);
|
||||
this._instanceDataMapValues = [];
|
||||
for (let i = 0; this._instanceDataMapValues.length; i++) {
|
||||
if (this._instanceDataMapValues[i].numInstances === 0) {
|
||||
this._instanceDataMapValues.splice(i, 1);
|
||||
break;
|
||||
}
|
||||
}
|
||||
this._clearDataTagQueue();
|
||||
isEmpty = true;
|
||||
}
|
||||
|
||||
@ -427,12 +427,12 @@ class Vector extends Layer {
|
||||
|
||||
if (entity._layer && this.isEqual(entity._layer)) {
|
||||
|
||||
this._entities.splice(entity._layerIndex, 1);
|
||||
|
||||
this._reindexEntitiesArray(entity._layerIndex);
|
||||
if (!entity.parent) {
|
||||
this._entities.splice(entity._layerIndex, 1);
|
||||
this._reindexEntitiesArray(entity._layerIndex);
|
||||
}
|
||||
|
||||
entity._layer = null;
|
||||
|
||||
entity._layerIndex = -1;
|
||||
|
||||
if (entity._entityCollection) {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user