From 508dee458049886cfe31187d864e7e43cbefcb74 Mon Sep 17 00:00:00 2001 From: David Rowe Date: Mon, 29 May 2017 13:35:18 +1200 Subject: [PATCH 01/12] Increase entity creation distance to 2m --- scripts/system/edit.js | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/scripts/system/edit.js b/scripts/system/edit.js index f39165f3df..a270c6dd15 100644 --- a/scripts/system/edit.js +++ b/scripts/system/edit.js @@ -1409,14 +1409,15 @@ function handeMenuEvent(menuItem) { } function getPositionToCreateEntity() { var HALF_TREE_SCALE = 16384; - var direction = Quat.getForward(MyAvatar.orientation); - var distance = 1; - var position = Vec3.sum(MyAvatar.position, Vec3.multiply(direction, distance)); - + var CREATE_DISTANCE = 2; + var position; if (Camera.mode === "entity" || Camera.mode === "independent") { - position = Vec3.sum(Camera.position, Vec3.multiply(Quat.getForward(Camera.orientation), distance)); + position = Vec3.sum(Camera.position, Vec3.multiply(Quat.getForward(Camera.orientation), CREATE_DISTANCE)); + } else { + position = Vec3.sum(MyAvatar.position, Vec3.multiply(Quat.getForward(MyAvatar.orientation), CREATE_DISTANCE)); } position.y += 0.5; + if (position.x > HALF_TREE_SCALE || position.y > HALF_TREE_SCALE || position.z > HALF_TREE_SCALE) { return null; } From 9b679325ff2a37aa78b91239fc040aed1d5f04b7 Mon Sep 17 00:00:00 2001 From: David Rowe Date: Mon, 29 May 2017 13:35:54 +1200 Subject: [PATCH 02/12] Don't raise creation position if in independent or entity view mode --- scripts/system/edit.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/system/edit.js b/scripts/system/edit.js index a270c6dd15..2ec76863f9 100644 --- a/scripts/system/edit.js +++ b/scripts/system/edit.js @@ -1415,8 +1415,8 @@ function getPositionToCreateEntity() { position = Vec3.sum(Camera.position, Vec3.multiply(Quat.getForward(Camera.orientation), CREATE_DISTANCE)); } else { position = Vec3.sum(MyAvatar.position, Vec3.multiply(Quat.getForward(MyAvatar.orientation), CREATE_DISTANCE)); + position.y += 0.5; } - position.y += 0.5; if (position.x > HALF_TREE_SCALE || position.y > HALF_TREE_SCALE || position.z > HALF_TREE_SCALE) { return null; From f0785cd39eeb21804e7b9aa5811b54b0e29e5b0e Mon Sep 17 00:00:00 2001 From: David Rowe Date: Mon, 29 May 2017 13:52:36 +1200 Subject: [PATCH 03/12] Adjust creation position so that bounding box is creation distance away --- scripts/system/edit.js | 51 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/scripts/system/edit.js b/scripts/system/edit.js index 2ec76863f9..d3137f5aea 100644 --- a/scripts/system/edit.js +++ b/scripts/system/edit.js @@ -229,11 +229,62 @@ var toolBar = (function () { systemToolbar = null, tablet = null; + function adjustPositionPerBoundingBox(position, direction, registration, dimensions, orientation) { + // Adjust the position such that the bounding box lies behind the original position. + var CORNERS = [ + { x: 0, y: 0, z: 0 }, + { x: 0, y: 0, z: 1 }, + { x: 0, y: 1, z: 0 }, + { x: 0, y: 1, z: 1 }, + { x: 1, y: 0, z: 0 }, + { x: 1, y: 0, z: 1 }, + { x: 1, y: 1, z: 0 }, + { x: 1, y: 1, z: 1 }, + ]; + + // Go through all corners and find least (most negative) distance in front of position. + var distance = 0; + for (var i = 0, length = CORNERS.length; i < length; i++) { + var cornerVector = + Vec3.multiplyQbyV(orientation, Vec3.multiplyVbyV(Vec3.subtract(CORNERS[i], registration), dimensions)); + var cornerDistance = Vec3.dot(cornerVector, direction); + distance = Math.min(cornerDistance, distance); + } + position = Vec3.sum(Vec3.multiply(distance, direction), position); + + return position; + } + function createNewEntity(properties) { var dimensions = properties.dimensions ? properties.dimensions : DEFAULT_DIMENSIONS; var position = getPositionToCreateEntity(); var entityID = null; if (position !== null && position !== undefined) { + var ADJUST_ENTITY_TYPES = ["Box", "Sphere", "Shape", "Text", "Web"]; + if (ADJUST_ENTITY_TYPES.indexOf(properties.type) !== -1) { + var direction; + if (Camera.mode === "entity" || Camera.mode === "independent") { + direction = Camera.orientation; + } else { + direction = MyAvatar.orientation; + } + direction = Vec3.multiplyQbyV(direction, Vec3.UNIT_Z); + + var registration = properties.registration; + if (registration === undefined) { + var DEFAULT_REGISTRATION = { x: 0.5, y: 0.5, z: 0.5 }; + registration = DEFAULT_REGISTRATION; + } + + var orientation = properties.orientation; + if (orientation === undefined) { + var DEFAULT_ORIENTATION = Quat.fromPitchYawRollDegrees(0, 0, 0); + orientation = DEFAULT_ORIENTATION; + } + + position = adjustPositionPerBoundingBox(position, direction, registration, dimensions, orientation); + } + position = grid.snapToSurface(grid.snapToGrid(position, false, dimensions), dimensions); properties.position = position; if (Menu.isOptionChecked(GRABBABLE_ENTITIES_MENU_ITEM)) { From 6c890d90ef7b88d35d6c3f459864eb51c8e1afa9 Mon Sep 17 00:00:00 2001 From: David Rowe Date: Mon, 29 May 2017 17:48:01 +1200 Subject: [PATCH 04/12] Adjust position of model after its been resized --- scripts/system/edit.js | 46 +++++++++++++++++++++++++++++++++--------- 1 file changed, 37 insertions(+), 9 deletions(-) diff --git a/scripts/system/edit.js b/scripts/system/edit.js index d3137f5aea..b5ef6dd395 100644 --- a/scripts/system/edit.js +++ b/scripts/system/edit.js @@ -260,16 +260,17 @@ var toolBar = (function () { var position = getPositionToCreateEntity(); var entityID = null; if (position !== null && position !== undefined) { - var ADJUST_ENTITY_TYPES = ["Box", "Sphere", "Shape", "Text", "Web"]; - if (ADJUST_ENTITY_TYPES.indexOf(properties.type) !== -1) { - var direction; - if (Camera.mode === "entity" || Camera.mode === "independent") { - direction = Camera.orientation; - } else { - direction = MyAvatar.orientation; - } - direction = Vec3.multiplyQbyV(direction, Vec3.UNIT_Z); + var direction; + if (Camera.mode === "entity" || Camera.mode === "independent") { + direction = Camera.orientation; + } else { + direction = MyAvatar.orientation; + } + direction = Vec3.multiplyQbyV(direction, Vec3.UNIT_Z); + var PRE_ADJUST_ENTITY_TYPES = ["Box", "Sphere", "Shape", "Text", "Web"]; + if (PRE_ADJUST_ENTITY_TYPES.indexOf(properties.type) !== -1) { + // Adjust position of entity per bounding box prior to creating it. var registration = properties.registration; if (registration === undefined) { var DEFAULT_REGISTRATION = { x: 0.5, y: 0.5, z: 0.5 }; @@ -294,6 +295,32 @@ var toolBar = (function () { if (properties.type == "ParticleEffect") { selectParticleEntity(entityID); } + + var POST_ADJUST_ENTITY_TYPES = ["Model"]; + if (POST_ADJUST_ENTITY_TYPES.indexOf(properties.type) !== -1) { + // Adjust position of entity per bounding box after it has been created and auto-resized. + var initialDimensions = Entities.getEntityProperties(entityID, ["dimensions"]).dimensions; + var DIMENSIONS_CHECK_INTERVAL = 200; + var MAX_DIMENSIONS_CHECKS = 10; + var dimensionsCheckCount = 0; + var dimensionsCheckFunction = function () { + dimensionsCheckCount++; + var properties = Entities.getEntityProperties(entityID, ["dimensions", "registrationPoint", "orientation", "rotation"]); + if (!Vec3.equal(properties.dimensions, initialDimensions)) { + position = adjustPositionPerBoundingBox(position, direction, properties.registrationPoint, + properties.dimensions, properties.rotation); + position = grid.snapToSurface(grid.snapToGrid(position, false, properties.dimensions), + properties.dimensions); + Entities.editEntity(entityID, { + position: position + }); + selectionManager._update(); + } else if (dimensionsCheckCount < MAX_DIMENSIONS_CHECKS) { + Script.setTimeout(dimensionsCheckFunction, DIMENSIONS_CHECK_INTERVAL); + } + }; + Script.setTimeout(dimensionsCheckFunction, DIMENSIONS_CHECK_INTERVAL); + } } else { Window.notifyEditError("Can't create " + properties.type + ": " + properties.type + " would be out of bounds."); @@ -1458,6 +1485,7 @@ function handeMenuEvent(menuItem) { } tooltip.show(false); } + function getPositionToCreateEntity() { var HALF_TREE_SCALE = 16384; var CREATE_DISTANCE = 2; From 31df18ff5b10ad6ab8d989bea7f6580a578b7736 Mon Sep 17 00:00:00 2001 From: David Rowe Date: Mon, 29 May 2017 18:43:49 +1200 Subject: [PATCH 05/12] Fix snap-to-grid elevation --- scripts/system/libraries/gridTool.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/scripts/system/libraries/gridTool.js b/scripts/system/libraries/gridTool.js index 0290674a0f..11384ad91b 100644 --- a/scripts/system/libraries/gridTool.js +++ b/scripts/system/libraries/gridTool.js @@ -161,9 +161,9 @@ Grid = function(opts) { if (data.origin) { var pos = data.origin; - pos.x = pos.x === undefined ? origin.x : pos.x; - pos.y = pos.y === undefined ? origin.y : pos.y; - pos.z = pos.z === undefined ? origin.z : pos.z; + pos.x = pos.x === undefined ? origin.x : parseFloat(pos.x); + pos.y = pos.y === undefined ? origin.y : parseFloat(pos.y); + pos.z = pos.z === undefined ? origin.z : parseFloat(pos.z); that.setPosition(pos, true); } From 18558dd7950b0ae7db44e8e11e190c4539e23a6d Mon Sep 17 00:00:00 2001 From: David Rowe Date: Tue, 30 May 2017 13:34:17 +1200 Subject: [PATCH 06/12] Make JSON import distance 2m --- scripts/system/edit.js | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/scripts/system/edit.js b/scripts/system/edit.js index b5ef6dd395..3a9a7fc106 100644 --- a/scripts/system/edit.js +++ b/scripts/system/edit.js @@ -1486,8 +1486,9 @@ function handeMenuEvent(menuItem) { tooltip.show(false); } +var HALF_TREE_SCALE = 16384; + function getPositionToCreateEntity() { - var HALF_TREE_SCALE = 16384; var CREATE_DISTANCE = 2; var position; if (Camera.mode === "entity" || Camera.mode === "independent") { @@ -1503,22 +1504,20 @@ function getPositionToCreateEntity() { return position; } -function getPositionToImportEntity() { - var dimensions = Clipboard.getContentsDimensions(); - var HALF_TREE_SCALE = 16384; - var direction = Quat.getForward(MyAvatar.orientation); - var longest = 1; - longest = Math.sqrt(Math.pow(dimensions.x, 2) + Math.pow(dimensions.z, 2)); - var position = Vec3.sum(MyAvatar.position, Vec3.multiply(direction, longest)); - +function getPositionToImportEntities() { + var CREATE_DISTANCE = 2; + var distance = CREATE_DISTANCE + Clipboard.getClipboardContentsLargestDimension() / 2; + var position; if (Camera.mode === "entity" || Camera.mode === "independent") { - position = Vec3.sum(Camera.position, Vec3.multiply(Quat.getForward(Camera.orientation), longest)); + position = Vec3.sum(Camera.position, Vec3.multiply(Quat.getForward(Camera.orientation), distance)); + } else { + position = Vec3.sum(MyAvatar.position, Vec3.multiply(Quat.getForward(MyAvatar.orientation), distance)); + position.y += 0.5; } if (position.x > HALF_TREE_SCALE || position.y > HALF_TREE_SCALE || position.z > HALF_TREE_SCALE) { return null; } - return position; } function importSVO(importURL) { @@ -1544,7 +1543,7 @@ function importSVO(importURL) { z: 0 }; if (Clipboard.getClipboardContentsLargestDimension() < VERY_LARGE) { - position = getPositionToImportEntity(); + position = getPositionToImportEntities(); } if (position !== null && position !== undefined) { var pastedEntityIDs = Clipboard.pasteEntities(position); From 4c0296daaa9f3eab8006e1abdc95a60e7ee45ffe Mon Sep 17 00:00:00 2001 From: David Rowe Date: Tue, 30 May 2017 14:29:10 +1200 Subject: [PATCH 07/12] Reposition imported JSON entities so that they start at import distance --- scripts/system/edit.js | 103 ++++++++++++++++++++++++++++------------- 1 file changed, 70 insertions(+), 33 deletions(-) diff --git a/scripts/system/edit.js b/scripts/system/edit.js index 3a9a7fc106..b84fd86f3f 100644 --- a/scripts/system/edit.js +++ b/scripts/system/edit.js @@ -217,6 +217,32 @@ function hideMarketplace() { // } // } +function adjustPositionPerBoundingBox(position, direction, registration, dimensions, orientation) { + // Adjust the position such that the bounding box (registration, dimenions, and orientation) lies behind the original + // position in the given direction. + var CORNERS = [ + { x: 0, y: 0, z: 0 }, + { x: 0, y: 0, z: 1 }, + { x: 0, y: 1, z: 0 }, + { x: 0, y: 1, z: 1 }, + { x: 1, y: 0, z: 0 }, + { x: 1, y: 0, z: 1 }, + { x: 1, y: 1, z: 0 }, + { x: 1, y: 1, z: 1 }, + ]; + + // Go through all corners and find least (most negative) distance in front of position. + var distance = 0; + for (var i = 0, length = CORNERS.length; i < length; i++) { + var cornerVector = + Vec3.multiplyQbyV(orientation, Vec3.multiplyVbyV(Vec3.subtract(CORNERS[i], registration), dimensions)); + var cornerDistance = Vec3.dot(cornerVector, direction); + distance = Math.min(cornerDistance, distance); + } + position = Vec3.sum(Vec3.multiply(distance, direction), position); + return position; +} + var TOOLS_PATH = Script.resolvePath("assets/images/tools/"); var GRABBABLE_ENTITIES_MENU_CATEGORY = "Edit"; var GRABBABLE_ENTITIES_MENU_ITEM = "Create Entities As Grabbable"; @@ -229,33 +255,9 @@ var toolBar = (function () { systemToolbar = null, tablet = null; - function adjustPositionPerBoundingBox(position, direction, registration, dimensions, orientation) { - // Adjust the position such that the bounding box lies behind the original position. - var CORNERS = [ - { x: 0, y: 0, z: 0 }, - { x: 0, y: 0, z: 1 }, - { x: 0, y: 1, z: 0 }, - { x: 0, y: 1, z: 1 }, - { x: 1, y: 0, z: 0 }, - { x: 1, y: 0, z: 1 }, - { x: 1, y: 1, z: 0 }, - { x: 1, y: 1, z: 1 }, - ]; - - // Go through all corners and find least (most negative) distance in front of position. - var distance = 0; - for (var i = 0, length = CORNERS.length; i < length; i++) { - var cornerVector = - Vec3.multiplyQbyV(orientation, Vec3.multiplyVbyV(Vec3.subtract(CORNERS[i], registration), dimensions)); - var cornerDistance = Vec3.dot(cornerVector, direction); - distance = Math.min(cornerDistance, distance); - } - position = Vec3.sum(Vec3.multiply(distance, direction), position); - - return position; - } - function createNewEntity(properties) { + + var dimensions = properties.dimensions ? properties.dimensions : DEFAULT_DIMENSIONS; var position = getPositionToCreateEntity(); var entityID = null; @@ -1520,6 +1522,7 @@ function getPositionToImportEntities() { } return position; } + function importSVO(importURL) { if (!Entities.canRez() && !Entities.canRezTmp()) { Window.notifyEditError(INSUFFICIENT_PERMISSIONS_IMPORT_ERROR_MSG); @@ -1537,22 +1540,56 @@ function importSVO(importURL) { if (success) { var VERY_LARGE = 10000; - var position = { - x: 0, - y: 0, - z: 0 - }; - if (Clipboard.getClipboardContentsLargestDimension() < VERY_LARGE) { + var isLargeImport = Clipboard.getClipboardContentsLargestDimension() >= VERY_LARGE; + var position = Vec3.ZERO; + if (!isLargeImport) { position = getPositionToImportEntities(); } if (position !== null && position !== undefined) { var pastedEntityIDs = Clipboard.pasteEntities(position); + if (!isLargeImport) { + // The first entity in Clipboard gets the specified position with the rest being relative to it. Therefore, move + // entities after they're imported so that they're all the correct distance in front of and with geometric mean + // centered on the avatar/camera direction. + + var targetDirection; + if (Camera.mode === "entity" || Camera.mode === "independent") { + targetDirection = Camera.orientation; + } else { + targetDirection = MyAvatar.orientation; + } + targetDirection = Vec3.multiplyQbyV(targetDirection, Vec3.UNIT_Z); + + var targetPosition = getPositionToCreateEntity(); + var deltaParallel = HALF_TREE_SCALE; // Distance to move entities parallel to targetDirection. + var deltaPerpendicular = Vec3.ZERO; // Distance to move entities perpendicular to targetDirection. + var entityPositions = []; + for (var i = 0, length = pastedEntityIDs.length; i < length; i++) { + var properties = Entities.getEntityProperties(pastedEntityIDs[i], ["position", "dimensions", + "registrationPoint", "rotation"]); + var adjustedPosition = adjustPositionPerBoundingBox(targetPosition, targetDirection, properties.registrationPoint, + properties.dimensions, properties.rotation); + var delta = Vec3.subtract(adjustedPosition, properties.position); + var distance = Vec3.dot(delta, targetDirection); + deltaParallel = Math.min(distance, deltaParallel); + deltaPerpendicular = Vec3.sum(Vec3.subtract(delta, Vec3.multiply(distance, targetDirection)), deltaPerpendicular); + entityPositions[i] = properties.position; + } + deltaPerpendicular = Vec3.multiply(1 / pastedEntityIDs.length, deltaPerpendicular); + + var deltaPosition = Vec3.sum(Vec3.multiply(deltaParallel, targetDirection), deltaPerpendicular); + for (var i = 0, length = pastedEntityIDs.length; i < length; i++) { + Entities.editEntity(pastedEntityIDs[i], { + position: Vec3.sum(deltaPosition, entityPositions[i]) + }); + } + } if (isActive) { selectionManager.setSelections(pastedEntityIDs); } } else { - Window.notifyEditError("Can't import objects: objects would be out of bounds."); + Window.notifyEditError("Can't import entities: entities would be out of bounds."); } } else { Window.notifyEditError("There was an error importing the entity file."); From b176dbe54425990fbdcef98bd88c027a2998e44a Mon Sep 17 00:00:00 2001 From: David Rowe Date: Tue, 30 May 2017 14:35:59 +1200 Subject: [PATCH 08/12] Refactoring --- scripts/system/edit.js | 26 +++++--------------------- 1 file changed, 5 insertions(+), 21 deletions(-) diff --git a/scripts/system/edit.js b/scripts/system/edit.js index b84fd86f3f..293402f618 100644 --- a/scripts/system/edit.js +++ b/scripts/system/edit.js @@ -1490,30 +1490,14 @@ function handeMenuEvent(menuItem) { var HALF_TREE_SCALE = 16384; -function getPositionToCreateEntity() { +function getPositionToCreateEntity(extra) { var CREATE_DISTANCE = 2; var position; + var delta = extra !== undefined ? extra : 0; if (Camera.mode === "entity" || Camera.mode === "independent") { - position = Vec3.sum(Camera.position, Vec3.multiply(Quat.getForward(Camera.orientation), CREATE_DISTANCE)); + position = Vec3.sum(Camera.position, Vec3.multiply(Quat.getForward(Camera.orientation), CREATE_DISTANCE + delta)); } else { - position = Vec3.sum(MyAvatar.position, Vec3.multiply(Quat.getForward(MyAvatar.orientation), CREATE_DISTANCE)); - position.y += 0.5; - } - - if (position.x > HALF_TREE_SCALE || position.y > HALF_TREE_SCALE || position.z > HALF_TREE_SCALE) { - return null; - } - return position; -} - -function getPositionToImportEntities() { - var CREATE_DISTANCE = 2; - var distance = CREATE_DISTANCE + Clipboard.getClipboardContentsLargestDimension() / 2; - var position; - if (Camera.mode === "entity" || Camera.mode === "independent") { - position = Vec3.sum(Camera.position, Vec3.multiply(Quat.getForward(Camera.orientation), distance)); - } else { - position = Vec3.sum(MyAvatar.position, Vec3.multiply(Quat.getForward(MyAvatar.orientation), distance)); + position = Vec3.sum(MyAvatar.position, Vec3.multiply(Quat.getForward(MyAvatar.orientation), CREATE_DISTANCE + delta)); position.y += 0.5; } @@ -1543,7 +1527,7 @@ function importSVO(importURL) { var isLargeImport = Clipboard.getClipboardContentsLargestDimension() >= VERY_LARGE; var position = Vec3.ZERO; if (!isLargeImport) { - position = getPositionToImportEntities(); + position = getPositionToCreateEntity(Clipboard.getClipboardContentsLargestDimension() / 2); } if (position !== null && position !== undefined) { var pastedEntityIDs = Clipboard.pasteEntities(position); From e02021bddf473ef75c5f939b6a4d23617a5703da Mon Sep 17 00:00:00 2001 From: David Rowe Date: Tue, 30 May 2017 14:38:59 +1200 Subject: [PATCH 09/12] Tidying --- scripts/system/edit.js | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/scripts/system/edit.js b/scripts/system/edit.js index 293402f618..9f98c553bf 100644 --- a/scripts/system/edit.js +++ b/scripts/system/edit.js @@ -256,8 +256,6 @@ var toolBar = (function () { tablet = null; function createNewEntity(properties) { - - var dimensions = properties.dimensions ? properties.dimensions : DEFAULT_DIMENSIONS; var position = getPositionToCreateEntity(); var entityID = null; @@ -307,7 +305,7 @@ var toolBar = (function () { var dimensionsCheckCount = 0; var dimensionsCheckFunction = function () { dimensionsCheckCount++; - var properties = Entities.getEntityProperties(entityID, ["dimensions", "registrationPoint", "orientation", "rotation"]); + var properties = Entities.getEntityProperties(entityID, ["dimensions", "registrationPoint", "rotation"]); if (!Vec3.equal(properties.dimensions, initialDimensions)) { position = adjustPositionPerBoundingBox(position, direction, properties.registrationPoint, properties.dimensions, properties.rotation); @@ -1551,12 +1549,13 @@ function importSVO(importURL) { for (var i = 0, length = pastedEntityIDs.length; i < length; i++) { var properties = Entities.getEntityProperties(pastedEntityIDs[i], ["position", "dimensions", "registrationPoint", "rotation"]); - var adjustedPosition = adjustPositionPerBoundingBox(targetPosition, targetDirection, properties.registrationPoint, - properties.dimensions, properties.rotation); + var adjustedPosition = adjustPositionPerBoundingBox(targetPosition, targetDirection, + properties.registrationPoint, properties.dimensions, properties.rotation); var delta = Vec3.subtract(adjustedPosition, properties.position); var distance = Vec3.dot(delta, targetDirection); deltaParallel = Math.min(distance, deltaParallel); - deltaPerpendicular = Vec3.sum(Vec3.subtract(delta, Vec3.multiply(distance, targetDirection)), deltaPerpendicular); + deltaPerpendicular = Vec3.sum(Vec3.subtract(delta, Vec3.multiply(distance, targetDirection)), + deltaPerpendicular); entityPositions[i] = properties.position; } deltaPerpendicular = Vec3.multiply(1 / pastedEntityIDs.length, deltaPerpendicular); From 1e8698aee113540f61a71f9fcc3e972ece37e915 Mon Sep 17 00:00:00 2001 From: David Rowe Date: Tue, 30 May 2017 15:23:18 +1200 Subject: [PATCH 10/12] Snap imported JSON to grid --- scripts/system/edit.js | 11 ++++++++++- scripts/system/libraries/gridTool.js | 16 ++++++++++++---- 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/scripts/system/edit.js b/scripts/system/edit.js index 9f98c553bf..510aab1e7a 100644 --- a/scripts/system/edit.js +++ b/scripts/system/edit.js @@ -1559,8 +1559,17 @@ function importSVO(importURL) { entityPositions[i] = properties.position; } deltaPerpendicular = Vec3.multiply(1 / pastedEntityIDs.length, deltaPerpendicular); - var deltaPosition = Vec3.sum(Vec3.multiply(deltaParallel, targetDirection), deltaPerpendicular); + + if (grid.getSnapToGrid()) { + var properties = Entities.getEntityProperties(pastedEntityIDs[0], ["position", "dimensions", + "registrationPoint"]); + var position = Vec3.sum(deltaPosition, properties.position); + position = grid.snapToSurface(grid.snapToGrid(position, false, properties.dimensions, + properties.registrationPoint), properties.dimensions, properties.registrationPoint); + deltaPosition = Vec3.subtract(position, properties.position); + } + for (var i = 0, length = pastedEntityIDs.length; i < length; i++) { Entities.editEntity(pastedEntityIDs[i], { position: Vec3.sum(deltaPosition, entityPositions[i]) diff --git a/scripts/system/libraries/gridTool.js b/scripts/system/libraries/gridTool.js index 11384ad91b..08eb96a2bc 100644 --- a/scripts/system/libraries/gridTool.js +++ b/scripts/system/libraries/gridTool.js @@ -79,7 +79,7 @@ Grid = function(opts) { } } - that.snapToSurface = function(position, dimensions) { + that.snapToSurface = function(position, dimensions, registration) { if (!snapToGrid) { return position; } @@ -88,14 +88,18 @@ Grid = function(opts) { dimensions = { x: 0, y: 0, z: 0 }; } + if (registration === undefined) { + registration = { x: 0.5, y: 0.5, z: 0.5 }; + } + return { x: position.x, - y: origin.y + (dimensions.y / 2), + y: origin.y + (registration.y * dimensions.y), z: position.z }; } - that.snapToGrid = function(position, majorOnly, dimensions) { + that.snapToGrid = function(position, majorOnly, dimensions, registration) { if (!snapToGrid) { return position; } @@ -104,6 +108,10 @@ Grid = function(opts) { dimensions = { x: 0, y: 0, z: 0 }; } + if (registration === undefined) { + registration = { x: 0.5, y: 0.5, z: 0.5 }; + } + var spacing = majorOnly ? majorGridEvery : minorGridEvery; position = Vec3.subtract(position, origin); @@ -112,7 +120,7 @@ Grid = function(opts) { position.y = Math.round(position.y / spacing) * spacing; position.z = Math.round(position.z / spacing) * spacing; - return Vec3.sum(Vec3.sum(position, Vec3.multiply(0.5, dimensions)), origin); + return Vec3.sum(Vec3.sum(position, Vec3.multiplyVbyV(registration, dimensions)), origin); } that.snapToSpacing = function(delta, majorOnly) { From f5066aac5120b64da88d9726211f62fd5950ac3b Mon Sep 17 00:00:00 2001 From: David Rowe Date: Tue, 30 May 2017 15:53:03 +1200 Subject: [PATCH 11/12] Don't adjust position of imported entities of particular types --- scripts/system/edit.js | 63 +++++++++++++++++++++++------------------- 1 file changed, 35 insertions(+), 28 deletions(-) diff --git a/scripts/system/edit.js b/scripts/system/edit.js index 510aab1e7a..c1ce5321b4 100644 --- a/scripts/system/edit.js +++ b/scripts/system/edit.js @@ -1533,33 +1533,38 @@ function importSVO(importURL) { // The first entity in Clipboard gets the specified position with the rest being relative to it. Therefore, move // entities after they're imported so that they're all the correct distance in front of and with geometric mean // centered on the avatar/camera direction. + var deltaPosition = Vec3.ZERO; - var targetDirection; - if (Camera.mode === "entity" || Camera.mode === "independent") { - targetDirection = Camera.orientation; - } else { - targetDirection = MyAvatar.orientation; - } - targetDirection = Vec3.multiplyQbyV(targetDirection, Vec3.UNIT_Z); + var properties = Entities.getEntityProperties(pastedEntityIDs[0], ["type"]); + var NO_ADJUST_ENTITY_TYPES = ["Zone", "Light", "ParticleEffect"]; + if (NO_ADJUST_ENTITY_TYPES.indexOf(properties.type) === -1) { + var targetDirection; + if (Camera.mode === "entity" || Camera.mode === "independent") { + targetDirection = Camera.orientation; + } else { + targetDirection = MyAvatar.orientation; + } + targetDirection = Vec3.multiplyQbyV(targetDirection, Vec3.UNIT_Z); - var targetPosition = getPositionToCreateEntity(); - var deltaParallel = HALF_TREE_SCALE; // Distance to move entities parallel to targetDirection. - var deltaPerpendicular = Vec3.ZERO; // Distance to move entities perpendicular to targetDirection. - var entityPositions = []; - for (var i = 0, length = pastedEntityIDs.length; i < length; i++) { - var properties = Entities.getEntityProperties(pastedEntityIDs[i], ["position", "dimensions", - "registrationPoint", "rotation"]); - var adjustedPosition = adjustPositionPerBoundingBox(targetPosition, targetDirection, - properties.registrationPoint, properties.dimensions, properties.rotation); - var delta = Vec3.subtract(adjustedPosition, properties.position); - var distance = Vec3.dot(delta, targetDirection); - deltaParallel = Math.min(distance, deltaParallel); - deltaPerpendicular = Vec3.sum(Vec3.subtract(delta, Vec3.multiply(distance, targetDirection)), - deltaPerpendicular); - entityPositions[i] = properties.position; + var targetPosition = getPositionToCreateEntity(); + var deltaParallel = HALF_TREE_SCALE; // Distance to move entities parallel to targetDirection. + var deltaPerpendicular = Vec3.ZERO; // Distance to move entities perpendicular to targetDirection. + var entityPositions = []; + for (var i = 0, length = pastedEntityIDs.length; i < length; i++) { + var properties = Entities.getEntityProperties(pastedEntityIDs[i], ["position", "dimensions", + "registrationPoint", "rotation"]); + var adjustedPosition = adjustPositionPerBoundingBox(targetPosition, targetDirection, + properties.registrationPoint, properties.dimensions, properties.rotation); + var delta = Vec3.subtract(adjustedPosition, properties.position); + var distance = Vec3.dot(delta, targetDirection); + deltaParallel = Math.min(distance, deltaParallel); + deltaPerpendicular = Vec3.sum(Vec3.subtract(delta, Vec3.multiply(distance, targetDirection)), + deltaPerpendicular); + entityPositions[i] = properties.position; + } + deltaPerpendicular = Vec3.multiply(1 / pastedEntityIDs.length, deltaPerpendicular); + deltaPosition = Vec3.sum(Vec3.multiply(deltaParallel, targetDirection), deltaPerpendicular); } - deltaPerpendicular = Vec3.multiply(1 / pastedEntityIDs.length, deltaPerpendicular); - var deltaPosition = Vec3.sum(Vec3.multiply(deltaParallel, targetDirection), deltaPerpendicular); if (grid.getSnapToGrid()) { var properties = Entities.getEntityProperties(pastedEntityIDs[0], ["position", "dimensions", @@ -1570,10 +1575,12 @@ function importSVO(importURL) { deltaPosition = Vec3.subtract(position, properties.position); } - for (var i = 0, length = pastedEntityIDs.length; i < length; i++) { - Entities.editEntity(pastedEntityIDs[i], { - position: Vec3.sum(deltaPosition, entityPositions[i]) - }); + if (!Vec3.equal(deltaPosition, Vec3.ZERO)) { + for (var i = 0, length = pastedEntityIDs.length; i < length; i++) { + Entities.editEntity(pastedEntityIDs[i], { + position: Vec3.sum(deltaPosition, entityPositions[i]) + }); + } } } From 8de93d9651f0924c9630ca9a25b819575c2e6ae0 Mon Sep 17 00:00:00 2001 From: David Rowe Date: Tue, 30 May 2017 15:53:58 +1200 Subject: [PATCH 12/12] Fix typo noticed in passing --- libraries/shared/src/Extents.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/shared/src/Extents.h b/libraries/shared/src/Extents.h index 850735dd5d..444575b7d3 100644 --- a/libraries/shared/src/Extents.h +++ b/libraries/shared/src/Extents.h @@ -69,7 +69,7 @@ public: glm::vec3 size() const { return maximum - minimum; } float largestDimension() const { return glm::compMax(size()); } - /// \return new Extents which is original rotated around orign by rotation + /// \return new Extents which is original rotated around origin by rotation Extents getRotated(const glm::quat& rotation) const { Extents temp(minimum, maximum); temp.rotate(rotation);