Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ability to load z-up models #4962

Merged
merged 5 commits into from
Feb 9, 2017
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 73 additions & 0 deletions Source/Scene/Axis.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*global define*/
define([
'../Core/freezeObject',
'../Core/Math',
'../Core/Matrix3',
'../Core/Matrix4'
], function(
freezeObject,
CesiumMath,
Matrix3,
Matrix4) {
'use strict';

/**
* An enum describing the x, y, and z axes and helper conversion functions.
*
* @exports Axis
* @private
*/
var Axis = {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mark this @private.

/**
* Denotes the x-axis.
*
* @type {Number}
* @constant
*/
X : 0,

/**
* Denotes the y-axis.
*
* @type {Number}
* @constant
*/
Y : 1,

/**
* Denotes the z-axis.
*
* @type {Number}
* @constant
*/
Z : 2,

/**
* Matrix used to convert from y-up to z-up
*
* @type {Matrix4}
* @constant
*/
Y_UP_TO_Z_UP : Matrix4.fromRotationTranslation(Matrix3.fromRotationX(CesiumMath.PI_OVER_TWO)),

/**
* Matrix used to convert from x-up to z-up
*
* @type {Matrix4}
* @constant
*/
X_UP_TO_Z_UP : Matrix4.fromRotationTranslation(Matrix3.fromRotationY(CesiumMath.PI_OVER_TWO)),

/**
* Gets the axis by name
*
* @param {String} name The name of the axis.
* @returns {Number} The axis enum.
*/
fromName : function(name) {
return Axis[name];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Really sorry, but name is required so this should throw an exception, right?

}
};

return freezeObject(Axis);
});
29 changes: 24 additions & 5 deletions Source/Scene/Model.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ define([
'../ThirdParty/gltfDefaults',
'../ThirdParty/Uri',
'../ThirdParty/when',
'./Axis',
'./BlendingState',
'./ColorBlendMode',
'./getAttributeOrUniformBySemantic',
Expand Down Expand Up @@ -118,6 +119,7 @@ define([
gltfDefaults,
Uri,
when,
Axis,
BlendingState,
ColorBlendMode,
getAttributeOrUniformBySemantic,
Expand All @@ -139,7 +141,6 @@ define([
return {};
}

var yUpToZUp = Matrix4.fromRotationTranslation(Matrix3.fromRotationX(CesiumMath.PI_OVER_TWO));
var boundingSphereCartesian3Scratch = new Cartesian3();

var ModelState = {
Expand Down Expand Up @@ -624,6 +625,15 @@ define([
this._pickUniformMapLoaded = options.pickUniformMapLoaded;
this._ignoreCommands = defaultValue(options.ignoreCommands, false);


/**
* By default models are y-up according to the glTF spec, however geo-referenced models will typically be z-up
*
* @private
* @readonly
*/
this.upAxis = defaultValue(options.upAxis, Axis.Y);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this user an explicit getter so readonly is enforced?


/**
* @private
* @readonly
Expand Down Expand Up @@ -1233,7 +1243,7 @@ define([
};
}

function computeBoundingSphere(gltf) {
function computeBoundingSphere(model, gltf) {
var gltfNodes = gltf.nodes;
var gltfMeshes = gltf.meshes;
var rootNodes = gltf.scenes[gltf.scene].nodes;
Expand Down Expand Up @@ -1289,7 +1299,12 @@ define([
}

var boundingSphere = BoundingSphere.fromCornerPoints(min, max);
return BoundingSphere.transformWithoutScale(boundingSphere, yUpToZUp, boundingSphere);
if (model.upAxis === Axis.Y) {
BoundingSphere.transformWithoutScale(boundingSphere, Axis.Y_UP_TO_Z_UP, boundingSphere);
} else if (model.upAxis === Axis.X) {
BoundingSphere.transformWithoutScale(boundingSphere, Axis.X_UP_TO_Z_UP, boundingSphere);
}
return boundingSphere;
}

///////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -4192,7 +4207,7 @@ define([

this._state = ModelState.LOADING;

this._boundingSphere = computeBoundingSphere(this.gltf);
this._boundingSphere = computeBoundingSphere(this, this.gltf);
this._initialRadius = this._boundingSphere.radius;

checkSupportedExtensions(this);
Expand Down Expand Up @@ -4315,7 +4330,11 @@ define([
var scale = getScale(this, frameState);
var computedModelMatrix = this._computedModelMatrix;
Matrix4.multiplyByUniformScale(modelMatrix, scale, computedModelMatrix);
Matrix4.multiplyTransformation(computedModelMatrix, yUpToZUp, computedModelMatrix);
if (this.upAxis === Axis.Y) {
Matrix4.multiplyTransformation(computedModelMatrix, Axis.Y_UP_TO_Z_UP, computedModelMatrix);
} else if (this.upAxis === Axis.X) {
Matrix4.multiplyTransformation(computedModelMatrix, Axis.X_UP_TO_Z_UP, computedModelMatrix);
}
}

// Update modelMatrix throughout the graph as needed
Expand Down
Loading