EditVoxels.js: Added color changing with number keys, better audio

This commit is contained in:
Philip Rosedale 2014-02-10 16:42:05 -08:00
parent dabf3e34d6
commit 65121aefb6

View file

@ -7,9 +7,10 @@
// //
// Captures mouse clicks and edits voxels accordingly. // Captures mouse clicks and edits voxels accordingly.
// //
// click = create a new voxel on this face, same color as old // click = create a new voxel on this face, same color as old (default color picker state)
// Alt + click = delete this voxel // right click or control + click = delete this voxel
// shift + click = recolor this voxel // shift + click = recolor this voxel
// 1 - 8 = pick new color from palette
// //
// Click and drag to create more new voxels in the same direction // Click and drag to create more new voxels in the same direction
// //
@ -18,6 +19,11 @@ function vLength(v) {
return Math.sqrt(v.x * v.x + v.y * v.y + v.z * v.z); return Math.sqrt(v.x * v.x + v.y * v.y + v.z * v.z);
} }
function vMinus(a, b) {
var rval = { x: a.x - b.x, y: a.y - b.y, z: a.z - b.z };
return rval;
}
var key_alt = false; var key_alt = false;
var key_shift = false; var key_shift = false;
var isAdding = false; var isAdding = false;
@ -37,31 +43,32 @@ colors[4] = { red: 193, green: 99, blue: 122 };
colors[5] = { red: 255, green: 54, blue: 69 }; colors[5] = { red: 255, green: 54, blue: 69 };
colors[6] = { red: 124, green: 36, blue: 36 }; colors[6] = { red: 124, green: 36, blue: 36 };
colors[7] = { red: 63, green: 35, blue: 19 }; colors[7] = { red: 63, green: 35, blue: 19 };
var numColors = 6; var numColors = 8;
var whichColor = 0; var whichColor = -1; // Starting color is 'Copy' mode
// Create sounds for adding, deleting, recoloring voxels // Create sounds for adding, deleting, recoloring voxels
var addSound = new Sound("https://s3-us-west-1.amazonaws.com/highfidelity-public/sounds/Electronic/ElectronicBurst1.raw"); var addSound = new Sound("https://s3-us-west-1.amazonaws.com/highfidelity-public/sounds/Electronic/ElectronicBurst1.raw");
var deleteSound = new Sound("https://s3-us-west-1.amazonaws.com/highfidelity-public/sounds/Bubbles/bubbles1.raw"); var deleteSound = new Sound("https://s3-us-west-1.amazonaws.com/highfidelity-public/sounds/Bubbles/bubbles1.raw");
var changeColorSound = new Sound("https://s3-us-west-1.amazonaws.com/highfidelity-public/sounds/Electronic/ElectronicBurst6.raw"); var changeColorSound = new Sound("https://s3-us-west-1.amazonaws.com/highfidelity-public/sounds/Electronic/ElectronicBurst6.raw");
var clickSound = new Sound("https://s3-us-west-1.amazonaws.com/highfidelity-public/sounds/Collisions-ballhitsandcatches/ballcatch2.raw");
var audioOptions = new AudioInjectionOptions(); var audioOptions = new AudioInjectionOptions();
audioOptions.volume = 0.5;
function mousePressEvent(event) { function mousePressEvent(event) {
var pickRay = Camera.computePickRay(event.x, event.y); var pickRay = Camera.computePickRay(event.x, event.y);
var intersection = Voxels.findRayIntersection(pickRay); var intersection = Voxels.findRayIntersection(pickRay);
audioOptions.volume = 1.0; audioOptions.position = { x: pickRay.origin.x + pickRay.direction.x,
audioOptions.position = { x: intersection.voxel.x, y: intersection.voxel.y, z: intersection.voxel.z }; y: pickRay.origin.y + pickRay.direction.y,
z: pickRay.origin.z + pickRay.direction.z };
if (intersection.intersects) { if (intersection.intersects) {
if (key_alt) { if (event.isRightButton || event.isControl) {
// Delete voxel // Delete voxel
Voxels.eraseVoxel(intersection.voxel.x, intersection.voxel.y, intersection.voxel.z, intersection.voxel.s); Voxels.eraseVoxel(intersection.voxel.x, intersection.voxel.y, intersection.voxel.z, intersection.voxel.s);
Audio.playSound(deleteSound, audioOptions); Audio.playSound(deleteSound, audioOptions);
} else if (key_shift) { } else if (event.isShifted) {
// Recolor Voxel // Recolor Voxel
whichColor++;
if (whichColor == numColors) whichColor = 0;
Voxels.setVoxel(intersection.voxel.x, Voxels.setVoxel(intersection.voxel.x,
intersection.voxel.y, intersection.voxel.y,
intersection.voxel.z, intersection.voxel.z,
@ -70,7 +77,9 @@ function mousePressEvent(event) {
Audio.playSound(changeColorSound, audioOptions); Audio.playSound(changeColorSound, audioOptions);
} else { } else {
// Add voxel on face // Add voxel on face
var newVoxel = { if (whichColor == -1) {
// Copy mode - use clicked voxel color
var newVoxel = {
x: intersection.voxel.x, x: intersection.voxel.x,
y: intersection.voxel.y, y: intersection.voxel.y,
z: intersection.voxel.z, z: intersection.voxel.z,
@ -78,6 +87,16 @@ function mousePressEvent(event) {
red: intersection.voxel.red, red: intersection.voxel.red,
green: intersection.voxel.green, green: intersection.voxel.green,
blue: intersection.voxel.blue }; blue: intersection.voxel.blue };
} else {
var newVoxel = {
x: intersection.voxel.x,
y: intersection.voxel.y,
z: intersection.voxel.z,
s: intersection.voxel.s,
red: colors[whichColor].red,
green: colors[whichColor].green,
blue: colors[whichColor].blue };
}
if (intersection.face == "MIN_X_FACE") { if (intersection.face == "MIN_X_FACE") {
newVoxel.x -= newVoxel.s; newVoxel.x -= newVoxel.s;
@ -108,7 +127,18 @@ function mousePressEvent(event) {
function keyPressEvent(event) { function keyPressEvent(event) {
key_alt = event.isAlt; key_alt = event.isAlt;
key_shift = event.isShifted; key_shift = event.isShifted;
var nVal = parseInt(event.text);
if (event.text == "0") {
print("Color = Copy");
whichColor = -1;
Audio.playSound(clickSound, audioOptions);
} else if ((nVal > 0) && (nVal <= numColors)) {
whichColor = nVal - 1;
print("Color = " + (whichColor + 1));
Audio.playSound(clickSound, audioOptions);
}
} }
function keyReleaseEvent(event) { function keyReleaseEvent(event) {
key_alt = false; key_alt = false;
key_shift = false; key_shift = false;