Skip to content

Commit

Permalink
Fixed 'backfaces' flag
Browse files Browse the repository at this point in the history
Fixed 'specular' flag
Fixed default lookAt eye.z
Tweaked default material
Fixed SceneJS.Node.insertNode
Renamed SceneJS.Texture 'baseColor' to 'color'
Better 'tick' event params
SceneJS#on and SceneJS#off
Moved plugins
Added controller extras for interaction
  • Loading branch information
xeolabs committed May 6, 2013
1 parent 21d3ac3 commit 3f8a781
Show file tree
Hide file tree
Showing 38 changed files with 1,127 additions and 544 deletions.
5 changes: 4 additions & 1 deletion build.js
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,7 @@

var distDir = "build/latest";
var distPluginDir = distDir + "/plugins";
var distExtrasDir = distDir + "/extras";

// Create sub directory for build version

Expand All @@ -258,7 +259,9 @@
function () {

// Deep-copy an existing directory
wrench.copyDirSyncRecursive("plugins", distPluginDir);
wrench.copyDirSyncRecursive("src/plugins", distPluginDir);

wrench.copyDirSyncRecursive("src/extras", distExtrasDir);

if (fileList.length > 0) {
sys.print("Writing built library: scenejs.js\n");
Expand Down
152 changes: 152 additions & 0 deletions build/latest/extras/controls/orbit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
/**
* @class Mouse control to orbit a scene's {@link SceneJS.Lookat}.
* <p>Searches the given scene depth-first for a {@link SceneJS.Lookat} and inserts one at the root if not found.</p>
* <p>Converts mouse events on the scene's canvas into orbit and zoom updates.</p>
* @param {SceneJS.Scene} scene Scene graph to control
* @param {*} options Control options
* @constructor
*/
SceneJS.OrbitControls = function (scene, options) {

options = options || {};

var yaw = options.yaw || 0;
var pitch = options.pitch || 0;
var zoom = options.zoom || 10;
var zoomSensitivity = options.zoomSensitivity || 1.0;

var lastX;
var lastY;
var dragging = false;

var lookatNode;

// Find lookat node
scene.eachNode(
function () {
if (this.get("type") == "lookAt") {
lookatNode = this;
return true;
}
}, {
depthFirst:true
});

// Insert lookat node if not found
if (!lookatNode) {
lookatNode = scene.insertNode({
type:"lookAt"
});
}

var eye = options.eye || { x:0, y:0, z:0 };
var look = options.look || { x:0, y:0, z:0};

lookatNode.set({
eye:{ x:eye.x, y:eye.y, z:-zoom },
look:{ x:look.x, y:look.y, z:look.z },
up:{ x:0, y:1, z:0 }
});

update();

var canvas = scene.getCanvas();

canvas.addEventListener('mousedown', mouseDown, true);
canvas.addEventListener('mousemove', mouseMove, true);
canvas.addEventListener('mouseup', mouseUp, true);
canvas.addEventListener('touchstart', touchStart, true);
canvas.addEventListener('touchmove', touchMove, true);
canvas.addEventListener('touchend', touchEnd, true);
canvas.addEventListener('mousewheel', mouseWheel, true);
canvas.addEventListener('DOMMouseScroll', mouseWheel, true);

function mouseDown(event) {
lastX = event.clientX;
lastY = event.clientY;
dragging = true;
}

function touchStart(event) {
lastX = event.targetTouches[0].clientX;
lastY = event.targetTouches[0].clientY;
dragging = true;
}

function mouseUp() {
dragging = false;
}

function touchEnd() {
dragging = false;
}

function mouseMove(event) {
var posX = event.clientX;
var posY = event.clientY;
actionMove(posX, posY);
}

function touchMove(event) {
var posX = event.targetTouches[0].clientX;
var posY = event.targetTouches[0].clientY;
actionMove(posX, posY);
}

function actionMove(posX, posY) {
if (dragging) {

yaw -= (posX - lastX) * 0.1;
pitch -= (posY - lastY) * 0.1;

update();

lastX = posX;
lastY = posY;
}
}

function mouseWheel(event) {
var delta = 0;
if (!event) event = window.event;
if (event.wheelDelta) {
delta = event.wheelDelta / 120;
if (window.opera) delta = -delta;
} else if (event.detail) {
delta = -event.detail / 3;
}
if (delta) {
if (delta < 0) {
zoom -= zoomSensitivity;
} else {
zoom += zoomSensitivity;
}
}
if (event.preventDefault) {
event.preventDefault();
}
event.returnValue = false;
update();

}

function update() {

var eye = [0, 0, zoom];
var look = [0, 0, 0];
var up = [0, 1, 0];

var eyeVec = SceneJS_math_subVec3(eye, look, []);
var axis = SceneJS_math_cross3Vec3(up, eyeVec, []);

var pitchMat = SceneJS_math_rotationMat4v(pitch * 0.0174532925, axis);
var yawMat = SceneJS_math_rotationMat4v(yaw * 0.0174532925, up);


var eye3 = SceneJS_math_transformPoint3(pitchMat, eye);
eye3 = SceneJS_math_transformPoint3(yawMat, eye3);

lookatNode.setEye({x:eye3[0], y:eye3[1], z:eye3[2] });
}

};
47 changes: 47 additions & 0 deletions build/latest/extras/controls/pick.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/**
* @class Mouse control to pick scene objects.
* @param {SceneJS.Scene} scene Scene graph to control
* @param {*} options Control options
* @constructor
*/
SceneJS.PickControls = function (scene, options) {

options = options || {};

var canvas = scene.getCanvas();

canvas.addEventListener('mousedown', mouseDown, true);
canvas.addEventListener('mouseup', mouseUp, true);
canvas.addEventListener('touchstart', touchStart, true);
canvas.addEventListener('touchend', touchEnd, true);

var lastX;
var lastY;
var dragging;

function mouseDown(event) {
lastX = event.clientX;
lastY = event.clientY;
dragging = true;
}

function touchStart(event) {
lastX = event.targetTouches[0].clientX;
lastY = event.targetTouches[0].clientY;
dragging = true;
}

function mouseUp(event) {
if (dragging) {
scene.pick(event.clientX, event.clientY);
}
dragging = false;
}

function touchEnd() {
if (dragging) {
scene.pick(event.targetTouches[0].clientX, event.targetTouches[0].clientY);
}
dragging = false;
}
};
4 changes: 2 additions & 2 deletions build/latest/plugins/geometry/plane.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ SceneJS.Plugins.addPlugin(
var widthSegments = cfg.widthSegments || 1;
var heightSegments = cfg.heightSegments || 1;

var coreId = "plane_" + width + "_" + height + "_" + widthSegments + "_" + heightSegments;
var coreId = "plane_" + (cfg.wire == true ? "wire_" : "") + height + "_" + widthSegments + "_" + heightSegments;

var ix, iz;
var halfWidth = width / 2;
Expand Down Expand Up @@ -140,7 +140,7 @@ SceneJS.Plugins.addPlugin(
}

return {
primitive:"triangles",
primitive:cfg.wire ? "lines" : "triangles",
coreId:coreId,
positions:new Float32Array(positions),
normals:new Float32Array(normals),
Expand Down
120 changes: 16 additions & 104 deletions build/latest/plugins/geometry/quad.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,10 @@
SceneJS.Plugins.addPlugin(

"geometry", // Node type
"sphere",
"quad",

new (function () {

var sourceService = this;

this.getSource = function () {

var created;
Expand All @@ -34,7 +32,7 @@ SceneJS.Plugins.addPlugin(

setConfigs:function (cfg) {
configs = cfg;
created(sourceService.buildQuad(cfg));
created(build(cfg));
},

getConfigs:function () {
Expand All @@ -46,107 +44,21 @@ SceneJS.Plugins.addPlugin(
};
};

this.buildQuad = function (configs) {

Quad.prototype._init = function (params) {
this.attr.type = "quad";

var solid = (params.solid != undefined) ? params.solid : true;

var x = params.xSize || 1;
var y = params.ySize || 1;

SceneJS_geometry.prototype._init.call(this, {

/* Core ID ensures that we reuse any quad that has already been created with
* these parameters instead of wasting memory
*/
coreId:params.coreId || "quad_" + x + "_" + y + (solid ? "_solid" : "_wire"),
function build(cfg) {

/* Factory function used when resource not found
*/
create:function () {
var positions = [ 1, 1, 0, -1, 1, 0, -1, -1, 0, 1, -1, 0 ];
var normals = [ -1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0 ];
var uv = [ 1, 1, 0, 1, 0, 0, 1, 0 ];
var indices = [ 0, 1, 2, 0, 2, 3 ];

var xDiv = params.xDiv || 1;
var yDiv = params.yDiv || 1;

var positions, normals, uv, indices;

if (xDiv == 1 && yDiv == 1) {
positions = [
x, y, 0,
-x, y, 0,
-x, -y, 0,
x, -y, 0
];
normals = [
0, 0, 1,
0, 0, 1,
0, 0, 1,
0, 0, 1
];
uv = [
1, 1,
0, 1,
0, 0,
1, 0
];
indices = [
2, 1, 0,
3, 2, 0
];
} else {
if (xDiv < 0) {
throw SceneJS_error.fatalError(SceneJS.errors.ILLEGAL_NODE_CONFIG, "quad xDiv should be greater than zero");
}
if (yDiv < 0) {
throw SceneJS_error.fatalError(SceneJS.errors.ILLEGAL_NODE_CONFIG, "quad yDiv should be greater than zero");
}
positions = [];
normals = [];
uv = [];
indices = [];
var xStep = (x * 2) / xDiv;
var yStep = (y * 2) / yDiv;
var xRat = 0.5 / xDiv;
var yRat = 0.5 / yDiv;
var i = 0;
for (var yi = -y, yuv = 0; yi <= y; yi += yStep, yuv += yRat) {
for (var xi = -x, xuv = 0; xi <= x; xi += xStep, xuv += xRat) {
positions.push(xi);
positions.push(yi);
positions.push(0);
normals.push(0);
normals.push(0);
normals.push(1);
uv.push(xuv);
uv.push(yuv);
if (yi < y && xi < x) { // Two triangles
indices.push(i + 2);
indices.push(i + 1);
indices.push(i);
indices.push(i + 3);
indices.push(i + 2);
indices.push(i);
}
i += 3;
}
}
}

return {
primitive:solid ? "triangles" : "lines",
positions:positions,
normals:normals,
uv:uv,
indices:indices,
colors:[]
};
}
});
return {
primitive:cfg.wire ? "lines" : "triangles",
coreId:"quad"+ (cfg.wire ? "wire" : "_solid"),
positions:new Float32Array(positions),
normals:new Float32Array(normals),
uv:new Float32Array(uv),
indices:new Uint16Array(indices)
};
}
)
()
)
;

})());
4 changes: 2 additions & 2 deletions build/latest/plugins/geometry/sphere.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,8 @@ SceneJS.Plugins.addPlugin(
}

return {
primitive : "triangles",
coreId : "sphere_" + radius + "_" + longitudeBands + "_" + latitudeBands,
primitive:cfg.wire ? "lines" : "triangles",
coreId : "sphere_" + (cfg.wire ? "wire" : "_solid") + radius + "_" + longitudeBands + "_" + latitudeBands,
positions : new Float32Array(positions),
normals: new Float32Array(normals),
uv : new Float32Array(uvs),
Expand Down
Loading

0 comments on commit 3f8a781

Please sign in to comment.