mirror of
https://github.com/overte-org/overte.git
synced 2025-08-08 04:57:23 +02:00
Cleanup edit.js entity creation
This commit is contained in:
parent
81e80996f7
commit
0b25fc335e
1 changed files with 106 additions and 122 deletions
144
examples/edit.js
144
examples/edit.js
|
@ -291,22 +291,18 @@ var toolBar = (function () {
|
||||||
var RESIZE_TIMEOUT = 120000; // 2 minutes
|
var RESIZE_TIMEOUT = 120000; // 2 minutes
|
||||||
var RESIZE_MAX_CHECKS = RESIZE_TIMEOUT / RESIZE_INTERVAL;
|
var RESIZE_MAX_CHECKS = RESIZE_TIMEOUT / RESIZE_INTERVAL;
|
||||||
function addModel(url) {
|
function addModel(url) {
|
||||||
var position;
|
var entityID = createNewEntity({
|
||||||
|
|
||||||
position = Vec3.sum(MyAvatar.position, Vec3.multiply(Quat.getFront(MyAvatar.orientation), SPAWN_DISTANCE));
|
|
||||||
|
|
||||||
if (position.x > 0 && position.y > 0 && position.z > 0) {
|
|
||||||
var entityId = Entities.addEntity({
|
|
||||||
type: "Model",
|
type: "Model",
|
||||||
position: grid.snapToSurface(grid.snapToGrid(position, false, DEFAULT_DIMENSIONS), DEFAULT_DIMENSIONS),
|
|
||||||
dimensions: DEFAULT_DIMENSIONS,
|
dimensions: DEFAULT_DIMENSIONS,
|
||||||
modelURL: url
|
modelURL: url
|
||||||
});
|
}, false);
|
||||||
|
|
||||||
|
if (entityID) {
|
||||||
print("Model added: " + url);
|
print("Model added: " + url);
|
||||||
|
|
||||||
var checkCount = 0;
|
var checkCount = 0;
|
||||||
function resize() {
|
function resize() {
|
||||||
var entityProperties = Entities.getEntityProperties(entityId);
|
var entityProperties = Entities.getEntityProperties(entityID);
|
||||||
var naturalDimensions = entityProperties.naturalDimensions;
|
var naturalDimensions = entityProperties.naturalDimensions;
|
||||||
|
|
||||||
checkCount++;
|
checkCount++;
|
||||||
|
@ -318,21 +314,41 @@ var toolBar = (function () {
|
||||||
print("Resize failed: timed out waiting for model (" + url + ") to load");
|
print("Resize failed: timed out waiting for model (" + url + ") to load");
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
Entities.editEntity(entityId, { dimensions: naturalDimensions });
|
Entities.editEntity(entityID, { dimensions: naturalDimensions });
|
||||||
|
|
||||||
// Reset selection so that the selection overlays will be updated
|
// Reset selection so that the selection overlays will be updated
|
||||||
selectionManager.setSelections([entityId]);
|
selectionManager.setSelections([entityID]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
selectionManager.setSelections([entityId]);
|
selectionManager.setSelections([entityID]);
|
||||||
|
|
||||||
Script.setTimeout(resize, RESIZE_INTERVAL);
|
Script.setTimeout(resize, RESIZE_INTERVAL);
|
||||||
} else {
|
|
||||||
Window.alert("Can't add model: Model would be out of bounds.");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function createNewEntity(properties, dragOnCreate) {
|
||||||
|
// Default to true if not passed in
|
||||||
|
dragOnCreate = dragOnCreate == undefined ? true : dragOnCreate;
|
||||||
|
|
||||||
|
var dimensions = properties.dimensions ? properties.dimensions : DEFAULT_DIMENSIONS;
|
||||||
|
var position = getPositionToCreateEntity();
|
||||||
|
var entityID = null;
|
||||||
|
if (position != null) {
|
||||||
|
position = grid.snapToSurface(grid.snapToGrid(position, false, dimensions), dimensions),
|
||||||
|
properties.position = position;
|
||||||
|
|
||||||
|
entityID = Entities.addEntity(properties);
|
||||||
|
if (dragOnCreate) {
|
||||||
|
placingEntityID = entityID;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
Window.alert("Can't create " + properties.type + ": " + properties.type + " would be out of bounds.");
|
||||||
|
}
|
||||||
|
|
||||||
|
return entityID;
|
||||||
|
}
|
||||||
|
|
||||||
var newModelButtonDown = false;
|
var newModelButtonDown = false;
|
||||||
var browseMarketplaceButtonDown = false;
|
var browseMarketplaceButtonDown = false;
|
||||||
that.mousePressEvent = function (event) {
|
that.mousePressEvent = function (event) {
|
||||||
|
@ -363,45 +379,28 @@ var toolBar = (function () {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (newCubeButton === toolBar.clicked(clickedOverlay)) {
|
if (newCubeButton === toolBar.clicked(clickedOverlay)) {
|
||||||
var position = getPositionToCreateEntity();
|
createNewEntity({
|
||||||
|
|
||||||
if (position.x > 0 && position.y > 0 && position.z > 0) {
|
|
||||||
placingEntityID = Entities.addEntity({
|
|
||||||
type: "Box",
|
type: "Box",
|
||||||
position: grid.snapToSurface(grid.snapToGrid(position, false, DEFAULT_DIMENSIONS), DEFAULT_DIMENSIONS),
|
|
||||||
dimensions: DEFAULT_DIMENSIONS,
|
dimensions: DEFAULT_DIMENSIONS,
|
||||||
color: { red: 255, green: 0, blue: 0 }
|
color: { red: 255, green: 0, blue: 0 }
|
||||||
|
|
||||||
});
|
});
|
||||||
} else {
|
|
||||||
Window.alert("Can't create box: Box would be out of bounds.");
|
|
||||||
}
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (newSphereButton === toolBar.clicked(clickedOverlay)) {
|
if (newSphereButton === toolBar.clicked(clickedOverlay)) {
|
||||||
var position = getPositionToCreateEntity();
|
createNewEntity({
|
||||||
|
|
||||||
if (position.x > 0 && position.y > 0 && position.z > 0) {
|
|
||||||
placingEntityID = Entities.addEntity({
|
|
||||||
type: "Sphere",
|
type: "Sphere",
|
||||||
position: grid.snapToSurface(grid.snapToGrid(position, false, DEFAULT_DIMENSIONS), DEFAULT_DIMENSIONS),
|
|
||||||
dimensions: DEFAULT_DIMENSIONS,
|
dimensions: DEFAULT_DIMENSIONS,
|
||||||
color: { red: 255, green: 0, blue: 0 }
|
color: { red: 255, green: 0, blue: 0 }
|
||||||
});
|
});
|
||||||
} else {
|
|
||||||
Window.alert("Can't create sphere: Sphere would be out of bounds.");
|
|
||||||
}
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (newLightButton === toolBar.clicked(clickedOverlay)) {
|
if (newLightButton === toolBar.clicked(clickedOverlay)) {
|
||||||
var position = getPositionToCreateEntity();
|
createNewEntity({
|
||||||
|
|
||||||
if (position.x > 0 && position.y > 0 && position.z > 0) {
|
|
||||||
placingEntityID = Entities.addEntity({
|
|
||||||
type: "Light",
|
type: "Light",
|
||||||
position: grid.snapToSurface(grid.snapToGrid(position, false, DEFAULT_LIGHT_DIMENSIONS), DEFAULT_LIGHT_DIMENSIONS),
|
|
||||||
dimensions: DEFAULT_LIGHT_DIMENSIONS,
|
dimensions: DEFAULT_LIGHT_DIMENSIONS,
|
||||||
isSpotlight: false,
|
isSpotlight: false,
|
||||||
color: { red: 150, green: 150, blue: 150 },
|
color: { red: 150, green: 150, blue: 150 },
|
||||||
|
@ -412,78 +411,50 @@ var toolBar = (function () {
|
||||||
exponent: 0,
|
exponent: 0,
|
||||||
cutoff: 180, // in degrees
|
cutoff: 180, // in degrees
|
||||||
});
|
});
|
||||||
} else {
|
|
||||||
Window.alert("Can't create Light: Light would be out of bounds.");
|
|
||||||
}
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
if (newTextButton === toolBar.clicked(clickedOverlay)) {
|
if (newTextButton === toolBar.clicked(clickedOverlay)) {
|
||||||
var position = getPositionToCreateEntity();
|
createNewEntity({
|
||||||
|
|
||||||
if (position.x > 0 && position.y > 0 && position.z > 0) {
|
|
||||||
placingEntityID = Entities.addEntity({
|
|
||||||
type: "Text",
|
type: "Text",
|
||||||
position: grid.snapToSurface(grid.snapToGrid(position, false, DEFAULT_DIMENSIONS), DEFAULT_DIMENSIONS),
|
|
||||||
dimensions: { x: 0.65, y: 0.3, z: 0.01 },
|
dimensions: { x: 0.65, y: 0.3, z: 0.01 },
|
||||||
backgroundColor: { red: 64, green: 64, blue: 64 },
|
backgroundColor: { red: 64, green: 64, blue: 64 },
|
||||||
textColor: { red: 255, green: 255, blue: 255 },
|
textColor: { red: 255, green: 255, blue: 255 },
|
||||||
text: "some text",
|
text: "some text",
|
||||||
lineHeight: 0.06
|
lineHeight: 0.06
|
||||||
});
|
});
|
||||||
} else {
|
|
||||||
Window.alert("Can't create box: Text would be out of bounds.");
|
|
||||||
}
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (newWebButton === toolBar.clicked(clickedOverlay)) {
|
if (newWebButton === toolBar.clicked(clickedOverlay)) {
|
||||||
var position = getPositionToCreateEntity();
|
createNewEntity({
|
||||||
|
|
||||||
if (position.x > 0 && position.y > 0 && position.z > 0) {
|
|
||||||
placingEntityID = Entities.addEntity({
|
|
||||||
type: "Web",
|
type: "Web",
|
||||||
position: grid.snapToSurface(grid.snapToGrid(position, false, DEFAULT_DIMENSIONS), DEFAULT_DIMENSIONS),
|
|
||||||
dimensions: { x: 1.6, y: 0.9, z: 0.01 },
|
dimensions: { x: 1.6, y: 0.9, z: 0.01 },
|
||||||
sourceUrl: "https://highfidelity.com/",
|
sourceUrl: "https://highfidelity.com/",
|
||||||
});
|
});
|
||||||
} else {
|
|
||||||
Window.alert("Can't create Web Entity: would be out of bounds.");
|
|
||||||
}
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (newZoneButton === toolBar.clicked(clickedOverlay)) {
|
if (newZoneButton === toolBar.clicked(clickedOverlay)) {
|
||||||
var position = getPositionToCreateEntity();
|
createNewEntity({
|
||||||
|
|
||||||
if (position.x > 0 && position.y > 0 && position.z > 0) {
|
|
||||||
placingEntityID = Entities.addEntity({
|
|
||||||
type: "Zone",
|
type: "Zone",
|
||||||
position: grid.snapToSurface(grid.snapToGrid(position, false, DEFAULT_DIMENSIONS), DEFAULT_DIMENSIONS),
|
|
||||||
dimensions: { x: 10, y: 10, z: 10 },
|
dimensions: { x: 10, y: 10, z: 10 },
|
||||||
});
|
});
|
||||||
} else {
|
|
||||||
Window.alert("Can't create box: Text would be out of bounds.");
|
|
||||||
}
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (newPolyVoxButton === toolBar.clicked(clickedOverlay)) {
|
if (newPolyVoxButton === toolBar.clicked(clickedOverlay)) {
|
||||||
var position = getPositionToCreateEntity();
|
createNewEntity({
|
||||||
|
|
||||||
if (position.x > 0 && position.y > 0 && position.z > 0) {
|
|
||||||
placingEntityID = Entities.addEntity({
|
|
||||||
type: "PolyVox",
|
type: "PolyVox",
|
||||||
position: grid.snapToSurface(grid.snapToGrid(position, false, DEFAULT_DIMENSIONS),
|
|
||||||
DEFAULT_DIMENSIONS),
|
|
||||||
dimensions: { x: 10, y: 10, z: 10 },
|
dimensions: { x: 10, y: 10, z: 10 },
|
||||||
voxelVolumeSize: {x:16, y:16, z:16},
|
voxelVolumeSize: {x:16, y:16, z:16},
|
||||||
voxelSurfaceStyle: 1
|
voxelSurfaceStyle: 1
|
||||||
});
|
});
|
||||||
} else {
|
|
||||||
Window.alert("Can't create PolyVox: would be out of bounds.");
|
|
||||||
}
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1038,17 +1009,29 @@ function handeMenuEvent(menuItem) {
|
||||||
tooltip.show(false);
|
tooltip.show(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// This function tries to find a reasonable position to place a new entity based on the camera
|
||||||
|
// position. If a reasonable position within the world bounds can't be found, `null` will
|
||||||
|
// be returned. The returned position will also take into account grid snapping settings.
|
||||||
function getPositionToCreateEntity() {
|
function getPositionToCreateEntity() {
|
||||||
var distance = cameraManager.enabled ? cameraManager.zoomDistance : DEFAULT_ENTITY_DRAG_DROP_DISTANCE;
|
var distance = cameraManager.enabled ? cameraManager.zoomDistance : DEFAULT_ENTITY_DRAG_DROP_DISTANCE;
|
||||||
var direction = Quat.getFront(Camera.orientation);
|
var direction = Quat.getFront(Camera.orientation);
|
||||||
var offset = Vec3.multiply(distance, direction);
|
var offset = Vec3.multiply(distance, direction);
|
||||||
var position = Vec3.sum(Camera.position, offset);
|
var placementPosition = Vec3.sum(Camera.position, offset);
|
||||||
|
|
||||||
position.x = Math.max(0, position.x);
|
var cameraPosition = Camera.position;
|
||||||
position.y = Math.max(0, position.y);
|
|
||||||
position.z = Math.max(0, position.z);
|
|
||||||
|
|
||||||
return position;
|
var cameraOutOfBounds = cameraPosition.x < 0 || cameraPosition.y < 0 || cameraPosition.z < 0;
|
||||||
|
var placementOutOfBounds = placementPosition.x < 0 || placementPosition.y < 0 || placementPosition.z < 0;
|
||||||
|
|
||||||
|
if (cameraOutOfBounds && placementOutOfBounds) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
placementPosition.x = Math.max(0, placementPosition.x);
|
||||||
|
placementPosition.y = Math.max(0, placementPosition.y);
|
||||||
|
placementPosition.z = Math.max(0, placementPosition.z);
|
||||||
|
|
||||||
|
return placementPosition;
|
||||||
}
|
}
|
||||||
|
|
||||||
function importSVO(importURL) {
|
function importSVO(importURL) {
|
||||||
|
@ -1064,16 +1047,17 @@ function importSVO(importURL) {
|
||||||
|
|
||||||
if (success) {
|
if (success) {
|
||||||
var VERY_LARGE = 10000;
|
var VERY_LARGE = 10000;
|
||||||
var position = { x: 0.01, y: 0.01, z: 0.01};
|
var position = { x: 0, y: 0, z: 0 };
|
||||||
if (Clipboard.getClipboardContentsLargestDimension() < VERY_LARGE) {
|
if (Clipboard.getClipboardContentsLargestDimension() < VERY_LARGE) {
|
||||||
position = getPositionToCreateEntity();
|
position = getPositionToCreateEntity();
|
||||||
}
|
}
|
||||||
if (position.x > 0 && position.y > 0 && position.z > 0) {
|
if (position != null) {
|
||||||
var pastedEntityIDs = Clipboard.pasteEntities(position);
|
var pastedEntityIDs = Clipboard.pasteEntities(position);
|
||||||
|
|
||||||
if (isActive) {
|
if (isActive) {
|
||||||
selectionManager.setSelections(pastedEntityIDs);
|
selectionManager.setSelections(pastedEntityIDs);
|
||||||
}
|
}
|
||||||
|
|
||||||
Window.raiseMainWindow();
|
Window.raiseMainWindow();
|
||||||
} else {
|
} else {
|
||||||
Window.alert("Can't import objects: objects would be out of bounds.");
|
Window.alert("Can't import objects: objects would be out of bounds.");
|
||||||
|
|
Loading…
Reference in a new issue