checking for bounds and modified paint script to start a new line entity if user paints outside of line entity's bounding box

This commit is contained in:
ericrius1 2015-06-18 18:14:03 -07:00
parent 8183e7393d
commit 830a8b6909
2 changed files with 49 additions and 33 deletions

View file

@ -13,14 +13,15 @@
// //
Script.include('lineRider.js') Script.include('lineRider.js')
var MAX_POINTS_PER_LINE = 30; var MAX_POINTS_PER_LINE = 30;
var LINE_LIFETIME = 60 * 5 //5 minute lifetime var DRAWING_DISTANCE = 5;
var LINE_DIMENSIONS = 5;
var colorPalette = [{ var colorPalette = [{
red: 236, red: 236,
green: 208, green: 208,
blue: 120 blue: 120
}, { }, {
red: 217, red: 214,
green: 91, green: 91,
blue: 67 blue: 67
}, { }, {
@ -69,7 +70,7 @@ function hydraCheck() {
//************ Mouse Paint ************************** //************ Mouse Paint **************************
function MousePaint() { function MousePaint() {
var DRAWING_DISTANCE = 2;
var lines = []; var lines = [];
var deletedLines = []; var deletedLines = [];
var isDrawing = false; var isDrawing = false;
@ -92,7 +93,7 @@ function MousePaint() {
var points = []; var points = [];
var BRUSH_SIZE = 0.02; var BRUSH_SIZE = .05;
var brush = Entities.addEntity({ var brush = Entities.addEntity({
type: 'Sphere', type: 'Sphere',
@ -111,45 +112,46 @@ function MousePaint() {
function newLine(point) { function newLine(point) {
if (!point) {
return;
}
line = Entities.addEntity({ line = Entities.addEntity({
position: MyAvatar.position, position: point,
type: "Line", type: "Line",
color: currentColor, color: currentColor,
dimensions: { dimensions: {
x: 10, x: LINE_DIMENSIONS,
y: 10, y: LINE_DIMENSIONS,
z: 10 z: LINE_DIMENSIONS
}, },
lineWidth: LINE_WIDTH, lineWidth: LINE_WIDTH
lifetime: LINE_LIFETIME
}); });
points = []; points = [];
if (point) {
points.push(point); points.push(point);
path.push(point); path.push(point);
}
lines.push(line); lines.push(line);
} }
function mouseMoveEvent(event) { function mouseMoveEvent(event) {
var point = computePoint(event)
Entities.editEntity(brush, {
position: point
});
if (!isDrawing) { if (!isDrawing) {
return; return;
} }
var pickRay = Camera.computePickRay(event.x, event.y);
var addVector = Vec3.multiply(Vec3.normalize(pickRay.direction), DRAWING_DISTANCE);
var point = Vec3.sum(Camera.getPosition(), addVector);
points.push(point); points.push(point);
path.push(point); path.push(point);
Entities.editEntity(line, { var success = Entities.setAllPoints(line, points);
linePoints: points if (!success) {
}); //We're out of bounds of entity bounding box, so start a new line
Entities.editEntity(brush, { newLine(point)
position: point return;
}); }
if (points.length === MAX_POINTS_PER_LINE) { if (points.length === MAX_POINTS_PER_LINE) {
@ -158,6 +160,8 @@ function MousePaint() {
} }
} }
function undoStroke() { function undoStroke() {
var deletedLine = lines.pop(); var deletedLine = lines.pop();
var deletedLineProps = Entities.getEntityProperties(deletedLine); var deletedLineProps = Entities.getEntityProperties(deletedLine);
@ -172,13 +176,14 @@ function MousePaint() {
} }
function mousePressEvent(event) { function mousePressEvent(event) {
if(!event.isLeftButton) { if (!event.isLeftButton) {
isDrawing = false; isDrawing = false;
return; return;
} }
lineRider.mousePressEvent(event); lineRider.mousePressEvent(event);
path = []; path = [];
newLine(); var point = computePoint(event);
newLine(point);
isDrawing = true; isDrawing = true;
@ -198,14 +203,14 @@ function MousePaint() {
if (event.text === "z") { if (event.text === "z") {
undoStroke(); undoStroke();
} }
if(event.text === "x") { if (event.text === "x") {
redoStroke(); redoStroke();
} }
} }
function cleanup() { function cleanup() {
lines.forEach(function(line) { lines.forEach(function(line) {
Entities.deleteEntity(line); // Entities.deleteEntity(line);
}); });
Entities.deleteEntity(brush); Entities.deleteEntity(brush);
lineRider.cleanup(); lineRider.cleanup();
@ -255,6 +260,7 @@ function HydraPaint() {
var maxLineWidth = 10; var maxLineWidth = 10;
var currentLineWidth = minLineWidth; var currentLineWidth = minLineWidth;
var MIN_PAINT_TRIGGER_THRESHOLD = .01; var MIN_PAINT_TRIGGER_THRESHOLD = .01;
var LINE_LIFETIME = 20;
var COLOR_CHANGE_TIME_FACTOR = 0.1; var COLOR_CHANGE_TIME_FACTOR = 0.1;
var RIGHT_BUTTON_1 = 7 var RIGHT_BUTTON_1 = 7
@ -331,7 +337,7 @@ function HydraPaint() {
z: 10 z: 10
}, },
lineWidth: 5, lineWidth: 5,
lifetime: LINE_LIFETIME // lifetime: LINE_LIFETIME
}); });
this.points = []; this.points = [];
if (point) { if (point) {
@ -482,6 +488,12 @@ function HydraPaint() {
} }
function computePoint(event) {
var pickRay = Camera.computePickRay(event.x, event.y);
var addVector = Vec3.multiply(Vec3.normalize(pickRay.direction), DRAWING_DISTANCE);
return Vec3.sum(Camera.getPosition(), addVector);
}
function randFloat(low, high) { function randFloat(low, high) {
return low + Math.random() * (high - low); return low + Math.random() * (high - low);
} }

View file

@ -86,17 +86,21 @@ bool LineEntityItem::setProperties(const EntityItemProperties& properties) {
} }
bool LineEntityItem::setLinePoints(const QVector<glm::vec3>& points) { bool LineEntityItem::setLinePoints(const QVector<glm::vec3>& points) {
int invalidPoints = 0;
for (int i = 0; i < points.size(); i++) { for (int i = 0; i < points.size(); i++) {
glm::vec3 point = points.at(i); glm::vec3 point = points.at(i);
// Make sure all of our points are valid numbers. // Make sure all of our points are valid numbers.
// Must be greater than 0 because vector component is set to 0 if it is invalid data. Also should never be greater than TREE_SCALE // Must be greater than 0 because vector component is set to 0 if it is invalid data. Also should never be greater than TREE_SCALE
if ( (point.x <= 0 || point.x >= TREE_SCALE) || (point.y <= 0 || point.y >= TREE_SCALE) || (point.z <= 0 || point.z >= TREE_SCALE) ) { if ( (point.x <= 0 || point.x >= TREE_SCALE) || (point.y <= 0 || point.y >= TREE_SCALE) || (point.z <= 0 || point.z >= TREE_SCALE) ) {
qDebug() << "Point is outside domain bounds";
return false; return false;
} }
glm::vec3 pos = getPosition();
glm::vec3 halfBox = getDimensions() * 0.5f;
if ( (point.x < pos.x - halfBox.x || point.x > pos.x + halfBox.x) || (point.y < pos.y - halfBox.y || point.y > pos.y + halfBox.y) || (point.z < pos.z - halfBox.z || point.z > pos.z + halfBox.z) ) {
qDebug() << "Point is outside entity's bounding box";
return false;
} }
if (invalidPoints > 0) {
qDebug() << "Line with" << invalidPoints << "INVALID POINTS";
} }
_points = points; _points = points;
_pointsChanged = true; _pointsChanged = true;