From 6b9b4d4e6e38d6c8428261c1699f3074ecedc570 Mon Sep 17 00:00:00 2001 From: Ryan Huffman Date: Mon, 26 Oct 2015 10:44:39 -0700 Subject: [PATCH 1/7] Add libraries/line.js --- examples/libraries/line.js | 149 +++++++++++++++++++++++++++++++++++++ 1 file changed, 149 insertions(+) create mode 100644 examples/libraries/line.js diff --git a/examples/libraries/line.js b/examples/libraries/line.js new file mode 100644 index 0000000000..62811e50bc --- /dev/null +++ b/examples/libraries/line.js @@ -0,0 +1,149 @@ +function error(message) { + print("[ERROR] " + message); +} + +// PolyLine +var LINE_DIMENSIONS = { x: 2000, y: 2000, z: 2000 }; +var MAX_LINE_LENGTH = 40; // This must be 2 or greater; +var PolyLine = function(position, color, defaultStrokeWidth) { + this.position = position; + this.color = color; + this.defaultStrokeWidth = 0.10; + this.points = [ + { x: 0, y: 0, z: 0 } + ]; + this.strokeWidths = [ + this.defaultStrokeWidth + ]; + this.normals = [ + { x: 1, y: 0, z: 0 } + ] + this.entityID = Entities.addEntity({ + type: "PolyLine", + position: position, + linePoints: this.points, + normals: this.normals, + strokeWidths: this.strokeWidths, + dimensions: LINE_DIMENSIONS, + color: color, + lifetime: 20, + }); +}; + +PolyLine.prototype.enqueuePoint = function(position) { + if (this.isFull()) { + error("Hit max PolyLine size"); + return; + } + + position = Vec3.subtract(position, this.position); + this.points.push(position); + this.normals.push({ x: 1, y: 0, z: 0 }); + this.strokeWidths.push(this.defaultStrokeWidth * Math.min(1.0, this.points.length / 10)); + Entities.editEntity(this.entityID, { + linePoints: this.points, + normals: this.normals, + strokeWidths: this.strokeWidths, + }); +}; + +PolyLine.prototype.dequeuePoint = function() { + if (this.points.length == 0) { + error("Hit min PolyLine size"); + return; + } + + this.points = this.points.slice(1); + this.normals = this.normals.slice(1); + this.strokeWidths = this.strokeWidths.slice(1); + + Entities.editEntity(this.entityID, { + linePoints: this.points, + normals: this.normals, + strokeWidths: this.strokeWidths, + }); +}; + +PolyLine.prototype.getFirstPoint = function() { + return Vec3.sum(this.position, this.points[0]); +}; + +PolyLine.prototype.getLastPoint = function() { + return Vec3.sum(this.position, this.points[this.points.length - 1]); +}; + +PolyLine.prototype.getSize = function() { + return this.points.length; +} + +PolyLine.prototype.isFull = function() { + return this.points.length >= MAX_LINE_LENGTH; +}; + +PolyLine.prototype.destroy = function() { + Entities.deleteEntity(this.entityID); + this.points = []; +}; + + + +// InfiniteLine +InfiniteLine = function(position, color) { + this.position = position; + this.color = color; + this.lines = [new PolyLine(position, color)]; + this.size = 0; +}; + +InfiniteLine.prototype.enqueuePoint = function(position) { + var currentLine; + + if (this.lines.length == 0) { + currentLine = new PolyLine(position, this.color); + this.lines.push(currentLine); + } else { + currentLine = this.lines[this.lines.length - 1]; + } + + if (currentLine.isFull()) { + var newLine = new PolyLine(currentLine.getLastPoint(), this.color); + this.lines.push(newLine); + currentLine = newLine; + } + + currentLine.enqueuePoint(position); + + ++this.size; +}; + +InfiniteLine.prototype.dequeuePoint = function() { + if (this.lines.length == 0) { + error("Trying to dequeue from InfiniteLine when no points are left"); + return; + } + + var lastLine = this.lines[0]; + lastLine.dequeuePoint(); + + if (lastLine.getSize() <= 1) { + this.lines = this.lines.slice(1); + } + + --this.size; +}; + +InfiniteLine.prototype.getFirstPoint = function() { + return this.lines.length > 0 ? this.lines[0].getFirstPoint() : null; +}; + +InfiniteLine.prototype.getLastPoint = function() { + return this.lines.length > 0 ? this.lines[lines.length - 1].getLastPoint() : null; +}; + +InfiniteLine.prototype.destroy = function() { + for (var i = 0; i < this.lines.length; ++i) { + this.lines[i].destroy(); + } + + this.size = 0; +}; From d6230fbed329f5db5780adca536012939d035a4c Mon Sep 17 00:00:00 2001 From: Ryan Huffman Date: Mon, 26 Oct 2015 10:46:36 -0700 Subject: [PATCH 2/7] Remove trailing commas in line.js --- examples/libraries/line.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/examples/libraries/line.js b/examples/libraries/line.js index 62811e50bc..0834c6176e 100644 --- a/examples/libraries/line.js +++ b/examples/libraries/line.js @@ -26,7 +26,7 @@ var PolyLine = function(position, color, defaultStrokeWidth) { strokeWidths: this.strokeWidths, dimensions: LINE_DIMENSIONS, color: color, - lifetime: 20, + lifetime: 20 }); }; @@ -43,7 +43,7 @@ PolyLine.prototype.enqueuePoint = function(position) { Entities.editEntity(this.entityID, { linePoints: this.points, normals: this.normals, - strokeWidths: this.strokeWidths, + strokeWidths: this.strokeWidths }); }; @@ -60,7 +60,7 @@ PolyLine.prototype.dequeuePoint = function() { Entities.editEntity(this.entityID, { linePoints: this.points, normals: this.normals, - strokeWidths: this.strokeWidths, + strokeWidths: this.strokeWidths }); }; From 1a1ab29978e04302fb49aff32a2afcfd3611b147 Mon Sep 17 00:00:00 2001 From: Ryan Huffman Date: Mon, 26 Oct 2015 16:45:43 -0700 Subject: [PATCH 3/7] Add lineExample.js --- examples/example/lineExample.js | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 examples/example/lineExample.js diff --git a/examples/example/lineExample.js b/examples/example/lineExample.js new file mode 100644 index 0000000000..6642e499fb --- /dev/null +++ b/examples/example/lineExample.js @@ -0,0 +1,12 @@ +Script.include("../libraries/line.js"); + +var basePosition = MyAvatar.position; +var line = new InfiniteLine(basePosition); + +for (var i = 0; i < (16 * Math.PI); i += 0.05) { + var x = 0 + var y = 0.25 * Math.sin(i); + var z = i / 10; + + line.enqueuePoint(Vec3.sum(basePosition, { x: x, y: y, z: z })); +} From 2abb6a2fd5921f474a999eba368296fb00def4b4 Mon Sep 17 00:00:00 2001 From: Ryan Huffman Date: Mon, 26 Oct 2015 16:45:58 -0700 Subject: [PATCH 4/7] Clean up line.js --- examples/libraries/line.js | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/examples/libraries/line.js b/examples/libraries/line.js index 0834c6176e..991b691ae8 100644 --- a/examples/libraries/line.js +++ b/examples/libraries/line.js @@ -8,15 +8,12 @@ var MAX_LINE_LENGTH = 40; // This must be 2 or greater; var PolyLine = function(position, color, defaultStrokeWidth) { this.position = position; this.color = color; - this.defaultStrokeWidth = 0.10; + this.defaultStrokeWidth = defaultStrokeWidth; this.points = [ - { x: 0, y: 0, z: 0 } ]; this.strokeWidths = [ - this.defaultStrokeWidth ]; this.normals = [ - { x: 1, y: 0, z: 0 } ] this.entityID = Entities.addEntity({ type: "PolyLine", @@ -39,7 +36,7 @@ PolyLine.prototype.enqueuePoint = function(position) { position = Vec3.subtract(position, this.position); this.points.push(position); this.normals.push({ x: 1, y: 0, z: 0 }); - this.strokeWidths.push(this.defaultStrokeWidth * Math.min(1.0, this.points.length / 10)); + this.strokeWidths.push(this.defaultStrokeWidth); Entities.editEntity(this.entityID, { linePoints: this.points, normals: this.normals, @@ -91,7 +88,7 @@ PolyLine.prototype.destroy = function() { InfiniteLine = function(position, color) { this.position = position; this.color = color; - this.lines = [new PolyLine(position, color)]; + this.lines = [new PolyLine(position, color, 0.01)]; this.size = 0; }; @@ -99,14 +96,15 @@ InfiniteLine.prototype.enqueuePoint = function(position) { var currentLine; if (this.lines.length == 0) { - currentLine = new PolyLine(position, this.color); + currentLine = new PolyLine(position, this.color, 0.01); this.lines.push(currentLine); } else { currentLine = this.lines[this.lines.length - 1]; } if (currentLine.isFull()) { - var newLine = new PolyLine(currentLine.getLastPoint(), this.color); + var newLine = new PolyLine(currentLine.getLastPoint(), this.color, 0.01); + newLine.enqueuePoint(currentLine.getLastPoint()); this.lines.push(newLine); currentLine = newLine; } From 983c551ce35c0ff5c153d31fa4294a89f38d422c Mon Sep 17 00:00:00 2001 From: Ryan Huffman Date: Tue, 27 Oct 2015 09:13:16 -0700 Subject: [PATCH 5/7] Clean up line.js and make lifetime settable --- examples/example/lineExample.js | 7 +++++-- examples/libraries/line.js | 27 +++++++++++++++------------ 2 files changed, 20 insertions(+), 14 deletions(-) diff --git a/examples/example/lineExample.js b/examples/example/lineExample.js index 6642e499fb..e6c3d90cba 100644 --- a/examples/example/lineExample.js +++ b/examples/example/lineExample.js @@ -1,12 +1,15 @@ Script.include("../libraries/line.js"); var basePosition = MyAvatar.position; -var line = new InfiniteLine(basePosition); +var color = { red: 128, green: 220, blue: 190 }; +var strokeWidth = 0.01; +var line = new InfiniteLine(basePosition, color, 20); for (var i = 0; i < (16 * Math.PI); i += 0.05) { var x = 0 var y = 0.25 * Math.sin(i); var z = i / 10; - line.enqueuePoint(Vec3.sum(basePosition, { x: x, y: y, z: z })); + var position = Vec3.sum(basePosition, { x: x, y: y, z: z }); + line.enqueuePoint(position, strokeWidth); } diff --git a/examples/libraries/line.js b/examples/libraries/line.js index 991b691ae8..77cb13c124 100644 --- a/examples/libraries/line.js +++ b/examples/libraries/line.js @@ -5,10 +5,13 @@ function error(message) { // PolyLine var LINE_DIMENSIONS = { x: 2000, y: 2000, z: 2000 }; var MAX_LINE_LENGTH = 40; // This must be 2 or greater; -var PolyLine = function(position, color, defaultStrokeWidth) { +var DEFAULT_STROKE_WIDTH = 0.1; +var DEFAULT_LIFETIME = 20; +var DEFAULT_COLOR = { red: 255, green: 255, blue: 255 }; +var PolyLine = function(position, color, lifetime) { this.position = position; this.color = color; - this.defaultStrokeWidth = defaultStrokeWidth; + this.lifetime = lifetime === undefined ? DEFAULT_LIFETIME : lifetime; this.points = [ ]; this.strokeWidths = [ @@ -23,11 +26,11 @@ var PolyLine = function(position, color, defaultStrokeWidth) { strokeWidths: this.strokeWidths, dimensions: LINE_DIMENSIONS, color: color, - lifetime: 20 + lifetime: lifetime }); }; -PolyLine.prototype.enqueuePoint = function(position) { +PolyLine.prototype.enqueuePoint = function(position, strokeWidth) { if (this.isFull()) { error("Hit max PolyLine size"); return; @@ -36,7 +39,7 @@ PolyLine.prototype.enqueuePoint = function(position) { position = Vec3.subtract(position, this.position); this.points.push(position); this.normals.push({ x: 1, y: 0, z: 0 }); - this.strokeWidths.push(this.defaultStrokeWidth); + this.strokeWidths.push(strokeWidth); Entities.editEntity(this.entityID, { linePoints: this.points, normals: this.normals, @@ -83,33 +86,33 @@ PolyLine.prototype.destroy = function() { }; - // InfiniteLine -InfiniteLine = function(position, color) { +InfiniteLine = function(position, color, lifetime) { this.position = position; this.color = color; + this.lifetime = lifetime === undefined ? DEFAULT_LIFETIME : lifetime; this.lines = [new PolyLine(position, color, 0.01)]; this.size = 0; }; -InfiniteLine.prototype.enqueuePoint = function(position) { +InfiniteLine.prototype.enqueuePoint = function(position, strokeWidth) { var currentLine; if (this.lines.length == 0) { - currentLine = new PolyLine(position, this.color, 0.01); + currentLine = new PolyLine(position, this.color, this.lifetime); this.lines.push(currentLine); } else { currentLine = this.lines[this.lines.length - 1]; } if (currentLine.isFull()) { - var newLine = new PolyLine(currentLine.getLastPoint(), this.color, 0.01); - newLine.enqueuePoint(currentLine.getLastPoint()); + var newLine = new PolyLine(currentLine.getLastPoint(), this.color, this.lifetime); + newLine.enqueuePoint(currentLine.getLastPoint(), strokeWidth); this.lines.push(newLine); currentLine = newLine; } - currentLine.enqueuePoint(position); + currentLine.enqueuePoint(position, strokeWidth); ++this.size; }; From 2cd2af2b13421341224910654921e9413bccd6f6 Mon Sep 17 00:00:00 2001 From: Ryan Huffman Date: Tue, 27 Oct 2015 09:49:52 -0700 Subject: [PATCH 6/7] Add headers to line.js and lineExample.js --- examples/example/lineExample.js | 11 +++++++++++ examples/libraries/line.js | 11 +++++++++++ 2 files changed, 22 insertions(+) diff --git a/examples/example/lineExample.js b/examples/example/lineExample.js index e6c3d90cba..d424d4f9f3 100644 --- a/examples/example/lineExample.js +++ b/examples/example/lineExample.js @@ -1,3 +1,14 @@ +// +// lineExample.js +// examples/example +// +// Created by Ryan Huffman on October 27, 2015 +// Copyright 2015 High Fidelity, Inc. +// +// Distributed under the Apache License, Version 2.0. +// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html +// + Script.include("../libraries/line.js"); var basePosition = MyAvatar.position; diff --git a/examples/libraries/line.js b/examples/libraries/line.js index 77cb13c124..d31a34867b 100644 --- a/examples/libraries/line.js +++ b/examples/libraries/line.js @@ -1,3 +1,14 @@ +// +// line.js +// examples/libraries +// +// Created by Ryan Huffman on October 27, 2015 +// Copyright 2015 High Fidelity, Inc. +// +// Distributed under the Apache License, Version 2.0. +// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html +// + function error(message) { print("[ERROR] " + message); } From 77e0023b43ddcfe6db2fd7b9852cba2b0977cdd2 Mon Sep 17 00:00:00 2001 From: Ryan Huffman Date: Fri, 30 Oct 2015 16:31:15 -0700 Subject: [PATCH 7/7] Fix lifetime bug with first PolyLine in InfiniteLine --- examples/libraries/line.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/libraries/line.js b/examples/libraries/line.js index d31a34867b..c21bf2f3ad 100644 --- a/examples/libraries/line.js +++ b/examples/libraries/line.js @@ -102,7 +102,7 @@ InfiniteLine = function(position, color, lifetime) { this.position = position; this.color = color; this.lifetime = lifetime === undefined ? DEFAULT_LIFETIME : lifetime; - this.lines = [new PolyLine(position, color, 0.01)]; + this.lines = []; this.size = 0; };