Skip to content

Commit

Permalink
zSpace tidy-ups
Browse files Browse the repository at this point in the history
  • Loading branch information
xeolabs committed Sep 15, 2016
1 parent 7f5b098 commit 8562228
Show file tree
Hide file tree
Showing 8 changed files with 91 additions and 44 deletions.
54 changes: 37 additions & 17 deletions api/latest/scenejs.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* A WebGL-based 3D scene graph from xeoLabs
* http://scenejs.org/
*
* Built on 2016-09-13
* Built on 2016-09-15
*
* MIT License
* Copyright 2016, Lindsay Kay
Expand Down Expand Up @@ -2462,9 +2462,13 @@ SceneJS_Engine.prototype.start = function () {

if (lastFrameTime > 0) {
elapsedFrameTime = frameTime - lastFrameTime;
newFPS = 1000 / elapsedFrameTime;
totalFPS += newFPS;
fpsSamples.push(newFPS);
if(elapsedFrameTime == 0) {
newFPS = 0;
} else {
newFPS = 1000 / elapsedFrameTime;
totalFPS += newFPS;
fpsSamples.push(newFPS);
}
if (fpsSamples.length >= numFPSSamples) {
totalFPS -= fpsSamples.shift();
}
Expand Down Expand Up @@ -8960,6 +8964,13 @@ new (function () {
return SceneJS._sliceArray(this._core.matrix, 0);
};

SceneJS.Camera.prototype.setMatrix = function (matrix) { // TODO: Extract clip planes from matrix
this._core.matrix = matrix;
this._core.mat = matrix;
this.publish("matrix", this._core.matrix);
this._engine.display.imageDirty = true;
};

/**
* Compiles this camera node, setting this node's state core on the display, compiling sub-nodes,
* then restoring the previous camera state core back onto the display on exit.
Expand Down Expand Up @@ -11301,6 +11312,13 @@ SceneJS.Library.prototype._compile = function(ctx) { // Bypass child nodes
return SceneJS._sliceArray(this._core.matrix, 0);
};

SceneJS.Lookat.prototype.setMatrix = function (matrix) { // TODO: Extract clip planes from matrix
this._core.matrix = matrix;
this._core.mat = matrix;
this.publish("matrix", this._core.matrix);
this._engine.display.imageDirty = true;
};

SceneJS.Lookat.prototype.getAttributes = function () {
return {
look:{
Expand Down Expand Up @@ -17778,14 +17796,10 @@ SceneJS_Display.prototype.render = function (params) {
//this._logPickList();
}

if (true || this.imageDirty || params.force) {
if (this.imageDirty || params.force) {
SceneJS_events.fireEvent(SceneJS_events.RENDER, {
forced: !!params.force
});
//if (!this._helloWebGL) {
// this._helloWebGL = new HelloWebGL(this._canvas.canvas, this._canvas.gl);
//}
// this._helloWebGL.draw();
this._doDrawList({ // Render, no pick
clear: (params.clear !== false), // Clear buffers by default
opaqueOnly: params.opaqueOnly
Expand Down Expand Up @@ -18131,7 +18145,9 @@ SceneJS_Display.prototype._logPickList = function () {

var localRayOrigin = SceneJS_math_vec3();
var localRayDir = SceneJS_math_vec3();
var pickMatrix = SceneJS_math_mat4();
var pickViewMatrix = mat4.create();
var pickProjMatrix = mat4.create();
mat4.frustum(pickProjMatrix, -1, 1, -1, 1, 0.1, 10000);

var a = SceneJS_math_vec3();
var b = SceneJS_math_vec3();
Expand Down Expand Up @@ -18292,7 +18308,7 @@ SceneJS_Display.prototype._logPickList = function () {
var look = SceneJS_math_addVec3(worldRayOrigin, worldRayDir, tempVec3);
var up = new Float32Array([0, 1, 0]); // TODO: derive from ray

SceneJS_math_lookAtMat4v(worldRayOrigin, look, up, pickMatrix);
SceneJS_math_lookAtMat4v(worldRayOrigin, look, up, pickViewMatrix);

pickBufX = canvas.clientWidth * 0.5;
pickBufY = canvas.clientHeight * 0.5;
Expand All @@ -18316,7 +18332,8 @@ SceneJS_Display.prototype._logPickList = function () {
this._doDrawList({
pickObject: true,
clear: true,
pickMatrix: worldRayPicking ? pickMatrix: null
pickViewMatrix: worldRayPicking ? pickViewMatrix: null,
pickProjMatrix: worldRayPicking ? pickProjMatrix: null
});

this._canvas.gl.finish();
Expand Down Expand Up @@ -18362,7 +18379,8 @@ SceneJS_Display.prototype._logPickList = function () {
this._doDrawList({
pickRegion: true,
object: object,
pickMatrix: worldRayPicking ? pickMatrix: null,
pickViewMatrix: worldRayPicking ? pickViewMatrix: null,
pickProjMatrix: worldRayPicking ? pickProjMatrix: null,
clear: true
});

Expand Down Expand Up @@ -18413,7 +18431,8 @@ SceneJS_Display.prototype._logPickList = function () {
this._doDrawList({
pickTriangle: true,
object: object,
pickMatrix: worldRayPicking ? pickMatrix: null,
pickViewMatrix: worldRayPicking ? pickViewMatrix: null,
pickProjMatrix: worldRayPicking ? pickProjMatrix: null,
clear: true
});

Expand Down Expand Up @@ -18808,7 +18827,8 @@ SceneJS_Display.prototype._doDrawList = function (params) {
frameCtx.bindTexture = 0;
frameCtx.bindArray = 0;

frameCtx.pickMatrix = params.pickMatrix;
frameCtx.pickViewMatrix = params.pickViewMatrix;
frameCtx.pickProjMatrix = params.pickProjMatrix;

// The extensions needs to be re-queried in case the context was lost and has been recreated.
if (SceneJS.WEBGL_INFO.SUPPORTED_EXTENSIONS["OES_element_index_uint"]) {
Expand Down Expand Up @@ -21015,7 +21035,7 @@ SceneJS_ChunkFactory.prototype.webglRestored = function () {
var gl = this.program.gl;

if (this._uPMatrixPick) {
this._uPMatrixPick.setValue(this.core.mat);
this._uPMatrixPick.setValue(frameCtx.pickProjMatrix || this.core.mat);
}

if (frameCtx.rayPick) { // Z-pick pass: feed near and far clip planes into shader
Expand Down Expand Up @@ -21851,7 +21871,7 @@ SceneJS_ChunkFactory.createChunkType({
pick : function(frameCtx) {

if (this._uvMatrixPick) {
this._uvMatrixPick.setValue(frameCtx.pickMatrix || this.core.mat);
this._uvMatrixPick.setValue(frameCtx.pickViewMatrix || this.core.mat);
}

frameCtx.viewMat = this.core.mat;
Expand Down
18 changes: 9 additions & 9 deletions api/latest/scenejs.min.js

Large diffs are not rendered by default.

27 changes: 17 additions & 10 deletions examples/js/zspace/zSpaceEffect.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
A ZSpace effect for SceneJS.
Dependencies: zSpace.js and glmatrix
Dependencies: zSpace.js and glmatrix.js
@param {*} cfg
@param {SceneJS.Scene} cfg.scene The SceneJS scene
Expand Down Expand Up @@ -40,6 +40,14 @@ SceneJS.ZSpaceEffect = function (cfg) {
var lookat = cfg.lookat;
var camera = cfg.camera;

if (!lookat) {
throw "Param missing: lookat";
}

if (!camera) {
throw "Param missing: lookat";
}

//----------------------------------------------------------------------
// Set up zSpace
//----------------------------------------------------------------------
Expand All @@ -57,20 +65,19 @@ SceneJS.ZSpaceEffect = function (cfg) {
// Gets World-space stylus position and direction from zSpace
//----------------------------------------------------------------------

var stylusWorldPos = new Float32Array(3);
var stylusWorldDir = new Float32Array(3);
var stylusWorldPos = vec3.create();
var stylusWorldDir = vec3.create();

var processStylus = (function () {

var invViewMat = new Float32Array(16);
var stylusWorldMat = new Float32Array(16);
var invViewMat = mat4.create();
var stylusWorldMat = mat4.create();

return function () {

var viewMat = camera.getMatrix(); // View matrix (assuming this is the inverse of the World matrix?)

//SceneJS_math_inverseMat4(viewMat, invViewMat);
SceneJS_math_mulMat4(viewMat, zspace.stylusCameraMatrix, stylusWorldMat);
mat4.multiply(stylusWorldMat, viewMat, zspace.stylusCameraMatrix);

stylusWorldPos[0] = stylusWorldMat[12];
stylusWorldPos[1] = stylusWorldMat[13];
Expand All @@ -89,10 +96,10 @@ SceneJS.ZSpaceEffect = function (cfg) {
// the result into our SceneJS lookat node
//----------------------------------------------------------------------

var setViewMatrix = (function () { // Sets the SceneJS Lookat to vectors extracted from the given view matrix
var viewMat = new Float32Array(16);
var setViewMatrix = (function () {
var viewMat = mat4.create();
return function (mat) {
SceneJS_math_mulMat4(baseViewMat, mat, viewMat);
mat4.multiply(viewMat, baseViewMat, mat);
lookat.setMatrix(viewMat);
};
})();
Expand Down
2 changes: 1 addition & 1 deletion src/core/display/chunks/cameraChunk.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ SceneJS_ChunkFactory.createChunkType({
var gl = this.program.gl;

if (this._uPMatrixPick) {
this._uPMatrixPick.setValue(this.core.mat);
this._uPMatrixPick.setValue(frameCtx.pickProjMatrix || this.core.mat);
}

if (frameCtx.rayPick) { // Z-pick pass: feed near and far clip planes into shader
Expand Down
2 changes: 1 addition & 1 deletion src/core/display/chunks/lookAtChunk.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ SceneJS_ChunkFactory.createChunkType({
pick : function(frameCtx) {

if (this._uvMatrixPick) {
this._uvMatrixPick.setValue(frameCtx.pickMatrix || this.core.mat);
this._uvMatrixPick.setValue(frameCtx.pickViewMatrix || this.core.mat);
}

frameCtx.viewMat = this.core.mat;
Expand Down
18 changes: 12 additions & 6 deletions src/core/display/display.js
Original file line number Diff line number Diff line change
Expand Up @@ -1000,7 +1000,9 @@ SceneJS_Display.prototype._logPickList = function () {

var localRayOrigin = SceneJS_math_vec3();
var localRayDir = SceneJS_math_vec3();
var pickMatrix = SceneJS_math_mat4();
var pickViewMatrix = mat4.create();
var pickProjMatrix = mat4.create();
mat4.frustum(pickProjMatrix, -1, 1, -1, 1, 0.1, 10000);

var a = SceneJS_math_vec3();
var b = SceneJS_math_vec3();
Expand Down Expand Up @@ -1161,7 +1163,7 @@ SceneJS_Display.prototype._logPickList = function () {
var look = SceneJS_math_addVec3(worldRayOrigin, worldRayDir, tempVec3);
var up = new Float32Array([0, 1, 0]); // TODO: derive from ray

SceneJS_math_lookAtMat4v(worldRayOrigin, look, up, pickMatrix);
SceneJS_math_lookAtMat4v(worldRayOrigin, look, up, pickViewMatrix);

pickBufX = canvas.clientWidth * 0.5;
pickBufY = canvas.clientHeight * 0.5;
Expand All @@ -1185,7 +1187,8 @@ SceneJS_Display.prototype._logPickList = function () {
this._doDrawList({
pickObject: true,
clear: true,
pickMatrix: worldRayPicking ? pickMatrix: null
pickViewMatrix: worldRayPicking ? pickViewMatrix: null,
pickProjMatrix: worldRayPicking ? pickProjMatrix: null
});

this._canvas.gl.finish();
Expand Down Expand Up @@ -1231,7 +1234,8 @@ SceneJS_Display.prototype._logPickList = function () {
this._doDrawList({
pickRegion: true,
object: object,
pickMatrix: worldRayPicking ? pickMatrix: null,
pickViewMatrix: worldRayPicking ? pickViewMatrix: null,
pickProjMatrix: worldRayPicking ? pickProjMatrix: null,
clear: true
});

Expand Down Expand Up @@ -1282,7 +1286,8 @@ SceneJS_Display.prototype._logPickList = function () {
this._doDrawList({
pickTriangle: true,
object: object,
pickMatrix: worldRayPicking ? pickMatrix: null,
pickViewMatrix: worldRayPicking ? pickViewMatrix: null,
pickProjMatrix: worldRayPicking ? pickProjMatrix: null,
clear: true
});

Expand Down Expand Up @@ -1677,7 +1682,8 @@ SceneJS_Display.prototype._doDrawList = function (params) {
frameCtx.bindTexture = 0;
frameCtx.bindArray = 0;

frameCtx.pickMatrix = params.pickMatrix;
frameCtx.pickViewMatrix = params.pickViewMatrix;
frameCtx.pickProjMatrix = params.pickProjMatrix;

// The extensions needs to be re-queried in case the context was lost and has been recreated.
if (SceneJS.WEBGL_INFO.SUPPORTED_EXTENSIONS["OES_element_index_uint"]) {
Expand Down
7 changes: 7 additions & 0 deletions src/core/scene/camera.js
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,13 @@
return SceneJS._sliceArray(this._core.matrix, 0);
};

SceneJS.Camera.prototype.setMatrix = function (matrix) { // TODO: Extract clip planes from matrix
this._core.matrix = matrix;
this._core.mat = matrix;
this.publish("matrix", this._core.matrix);
this._engine.display.imageDirty = true;
};

/**
* Compiles this camera node, setting this node's state core on the display, compiling sub-nodes,
* then restoring the previous camera state core back onto the display on exit.
Expand Down
7 changes: 7 additions & 0 deletions src/core/scene/lookAt.js
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,13 @@
return SceneJS._sliceArray(this._core.matrix, 0);
};

SceneJS.Lookat.prototype.setMatrix = function (matrix) { // TODO: Extract clip planes from matrix
this._core.matrix = matrix;
this._core.mat = matrix;
this.publish("matrix", this._core.matrix);
this._engine.display.imageDirty = true;
};

SceneJS.Lookat.prototype.getAttributes = function () {
return {
look:{
Expand Down

0 comments on commit 8562228

Please sign in to comment.