191 lines
5.0 KiB
JavaScript
Raw Permalink Normal View History

2015-12-01 10:15:47 -08:00
//
2015-12-17 11:32:48 -08:00
// lightTrails.js
2015-12-01 10:15:47 -08:00
// examples
//
// Created by Eric Levin on 5/14/15.
2015-12-21 19:08:34 -08:00
// Copyright 2015 High Fidelity, Inc.
2015-12-01 10:15:47 -08:00
//
2015-12-17 11:32:48 -08:00
// This script creates light trails as you move your hydra hands
2015-12-01 10:15:47 -08:00
//
//
// Distributed under the Apache License, Version 2.0.
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
//
2015-12-02 15:58:30 -08:00
2015-12-02 18:10:45 -08:00
Script.include("../libraries/utils.js");
2015-12-02 15:58:30 -08:00
var eraseTrail = true;
var ugLSD = 25;
// var eraseTrail = false;
2015-12-01 10:15:47 -08:00
var LEFT = 0;
var RIGHT = 1;
2015-12-01 16:22:11 -08:00
var MAX_POINTS_PER_LINE = 50;
2015-12-01 10:15:47 -08:00
var LIFETIME = 6000;
2015-12-03 13:46:29 -08:00
var DRAWING_DEPTH = 0.8;
2015-12-14 09:01:45 -08:00
var LINE_DIMENSIONS = 100;
2015-12-01 10:15:47 -08:00
2015-12-02 15:58:30 -08:00
var MIN_POINT_DISTANCE = 0.02;
2015-12-01 10:15:47 -08:00
var colorPalette = [{
red: 250,
2015-12-02 18:10:45 -08:00
green: 137,
blue: 162
2015-12-01 10:15:47 -08:00
}, {
2015-12-02 18:10:45 -08:00
red: 204,
green: 244,
blue: 249
2015-12-01 10:15:47 -08:00
}, {
2015-12-02 18:10:45 -08:00
red: 146,
green: 206,
blue: 116
2015-12-01 10:15:47 -08:00
}, {
2015-12-02 18:10:45 -08:00
red: 240,
green: 87,
blue: 129
2015-12-01 10:15:47 -08:00
}];
2015-12-03 13:46:29 -08:00
var STROKE_WIDTH = 0.04;
2015-12-01 10:15:47 -08:00
function controller(side, triggerAction) {
this.triggerHeld = false;
this.triggerThreshold = 0.9;
this.side = side;
this.triggerAction = triggerAction;
2015-12-02 16:44:47 -08:00
var texture = "https://s3.amazonaws.com/hifi-public/eric/textures/paintStrokes/trails.png";
2015-12-02 15:58:30 -08:00
2015-12-02 18:10:45 -08:00
this.light = Entities.addEntity({
type: 'Light',
position: MyAvatar.position,
dimensions: {
2015-12-14 09:01:45 -08:00
x: 30,
y: 30,
z: 30
2015-12-02 18:10:45 -08:00
},
color: colorPalette[randInt(0, colorPalette.length)],
2015-12-03 13:46:29 -08:00
intensity: 5
2015-12-02 18:10:45 -08:00
});
2015-12-02 15:58:30 -08:00
2015-12-01 10:50:25 -08:00
this.trail = Entities.addEntity({
type: "PolyLine",
2015-12-01 10:15:47 -08:00
dimensions: {
2015-12-01 10:50:25 -08:00
x: LINE_DIMENSIONS,
y: LINE_DIMENSIONS,
z: LINE_DIMENSIONS
},
2015-12-03 13:46:29 -08:00
color: {red: 255, green: 255, blue: 255},
2015-12-02 16:44:47 -08:00
textures: texture,
2015-12-01 10:50:25 -08:00
lifetime: LIFETIME
2015-12-01 10:15:47 -08:00
});
2015-12-01 10:50:25 -08:00
this.points = [];
this.normals = [];
this.strokeWidths = [];
var self = this;
2015-12-02 15:58:30 -08:00
this.trailEraseInterval = Script.setInterval(function() {
if (self.points.length > 0 && eraseTrail) {
self.points.shift();
self.normals.shift();
self.strokeWidths.shift();
Entities.editEntity(self.trail, {
linePoints: self.points,
strokeWidths: self.strokeWidths,
normals: self.normals
});
}
}, ugLSD);
2015-12-01 10:15:47 -08:00
2015-12-01 10:50:25 -08:00
this.setTrailPosition = function(position) {
this.trailPosition = position;
Entities.editEntity(this.trail, {
position: this.trailPosition
2015-12-01 10:15:47 -08:00
});
}
2015-12-01 10:50:25 -08:00
2015-12-01 10:15:47 -08:00
this.update = function(deltaTime) {
this.updateControllerState();
2015-12-01 10:50:25 -08:00
var newTrailPosOffset = Vec3.multiply(Vec3.normalize(Vec3.subtract(this.tipPosition, this.palmPosition)), DRAWING_DEPTH);
var newTrailPos = Vec3.sum(this.palmPosition, newTrailPosOffset);
2015-12-02 18:10:45 -08:00
Entities.editEntity(this.light, {
position: newTrailPos
});
2015-12-01 10:15:47 -08:00
2015-12-01 16:22:11 -08:00
if (!this.drawing) {
2015-12-01 10:50:25 -08:00
this.setTrailPosition(newTrailPos);
2015-12-01 10:15:47 -08:00
this.drawing = true;
}
2015-12-01 10:50:25 -08:00
if (this.drawing) {
var localPoint = Vec3.subtract(newTrailPos, this.trailPosition);
2015-12-01 10:15:47 -08:00
if (Vec3.distance(localPoint, this.points[this.points.length - 1]) < MIN_POINT_DISTANCE) {
//Need a minimum distance to avoid binormal NANs
return;
}
2015-12-01 10:50:25 -08:00
if (this.points.length === MAX_POINTS_PER_LINE) {
this.points.shift();
this.normals.shift();
this.strokeWidths.shift();
}
2015-12-01 10:15:47 -08:00
this.points.push(localPoint);
2015-12-01 10:50:25 -08:00
var normal = computeNormal(newTrailPos, Camera.getPosition());
2015-12-01 10:15:47 -08:00
this.normals.push(normal);
2015-12-02 18:10:45 -08:00
this.strokeWidths.push(STROKE_WIDTH + Math.random() * 0.01);
2015-12-01 10:50:25 -08:00
Entities.editEntity(this.trail, {
2015-12-01 10:15:47 -08:00
linePoints: this.points,
normals: this.normals,
strokeWidths: this.strokeWidths,
});
}
}
this.updateControllerState = function() {
this.palmPosition = this.side == RIGHT ? MyAvatar.rightHandPose.translation : MyAvatar.leftHandPose.translation;
this.tipPosition = this.side == RIGHT ? MyAvatar.rightHandTipPose.translation : MyAvatar.leftHandTipPose.translation;
2015-12-01 10:50:25 -08:00
this.triggerValue = Controller.getActionValue(this.triggerAction);
2015-12-01 10:15:47 -08:00
}
2015-12-01 16:22:11 -08:00
this.cleanup = function() {
Entities.deleteEntity(this.trail);
2015-12-02 18:10:45 -08:00
Entities.deleteEntity(this.light);
2015-12-02 15:58:30 -08:00
Script.clearInterval(this.trailEraseInterval);
2015-12-01 16:22:11 -08:00
}
2015-12-01 10:15:47 -08:00
}
function computeNormal(p1, p2) {
return Vec3.normalize(Vec3.subtract(p2, p1));
}
function update(deltaTime) {
leftController.update(deltaTime);
rightController.update(deltaTime);
}
function scriptEnding() {
2015-12-01 10:50:25 -08:00
leftController.cleanup();
2015-12-01 10:15:47 -08:00
rightController.cleanup();
}
function vectorIsZero(v) {
return v.x === 0 && v.y === 0 && v.z === 0;
}
var rightController = new controller(RIGHT, Controller.findAction("RIGHT_HAND_CLICK"));
var leftController = new controller(LEFT, Controller.findAction("LEFT_HAND_CLICK"));
Script.update.connect(update);
Script.scriptEnding.connect(scriptEnding);
function map(value, min1, max1, min2, max2) {
return min2 + (max2 - min2) * ((value - min1) / (max1 - min1));
}