mirror of
https://github.com/overte-org/overte.git
synced 2025-07-23 14:04:23 +02:00
laser-poiner script for mouse
This commit is contained in:
parent
3471c0a44f
commit
1161f33b45
1 changed files with 76 additions and 0 deletions
76
examples/pointer.js
Normal file
76
examples/pointer.js
Normal file
|
@ -0,0 +1,76 @@
|
||||||
|
|
||||||
|
var lineEntityID = null;
|
||||||
|
var lineIsRezzed = false;
|
||||||
|
|
||||||
|
|
||||||
|
function nearLinePoint(targetPosition) {
|
||||||
|
var handPosition = MyAvatar.getRightPalmPosition();
|
||||||
|
var along = Vec3.subtract(targetPosition, handPosition);
|
||||||
|
along = Vec3.normalize(along);
|
||||||
|
along = Vec3.multiply(along, 0.4);
|
||||||
|
return Vec3.sum(handPosition, along);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
function removeLine() {
|
||||||
|
if (lineIsRezzed) {
|
||||||
|
Entities.deleteEntity(lineEntityID);
|
||||||
|
lineEntityID = null;
|
||||||
|
lineIsRezzed = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
function createOrUpdateLine(event) {
|
||||||
|
var pickRay = Camera.computePickRay(event.x, event.y);
|
||||||
|
var intersection = Entities.findRayIntersection(pickRay, true); // accurate picking
|
||||||
|
|
||||||
|
if (lineIsRezzed) {
|
||||||
|
Entities.editEntity(lineEntityID, {
|
||||||
|
position: nearLinePoint(intersection.intersection),
|
||||||
|
dimensions: Vec3.subtract(intersection.intersection, nearLinePoint(intersection.intersection)),
|
||||||
|
lifetime: 30 // renew lifetime
|
||||||
|
});
|
||||||
|
|
||||||
|
} else {
|
||||||
|
lineIsRezzed = true;
|
||||||
|
lineEntityID = Entities.addEntity({
|
||||||
|
type: "Line",
|
||||||
|
position: nearLinePoint(intersection.intersection),
|
||||||
|
dimensions: Vec3.subtract(intersection.intersection, nearLinePoint(intersection.intersection)),
|
||||||
|
color: { red: 255, green: 255, blue: 255 },
|
||||||
|
lifetime: 30 // if someone crashes while pointing, don't leave the line there forever.
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
function mousePressEvent(event) {
|
||||||
|
if (!event.isLeftButton) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (lineIsRezzed) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
createOrUpdateLine(event);
|
||||||
|
if (lineIsRezzed) {
|
||||||
|
Controller.mouseMoveEvent.connect(mouseMoveEvent);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
function mouseMoveEvent(event) {
|
||||||
|
createOrUpdateLine(event);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
function mouseReleaseEvent() {
|
||||||
|
if (lineIsRezzed) {
|
||||||
|
Controller.mouseMoveEvent.disconnect(mouseMoveEvent);
|
||||||
|
}
|
||||||
|
removeLine();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Controller.mousePressEvent.connect(mousePressEvent);
|
||||||
|
Controller.mouseReleaseEvent.connect(mouseReleaseEvent);
|
Loading…
Reference in a new issue