Skip to content

Commit

Permalink
Merge pull request #4962 from AnalyticalGraphicsInc/model-z-up
Browse files Browse the repository at this point in the history
Ability to load z-up models
  • Loading branch information
pjcozzi authored Feb 9, 2017
2 parents 7b88301 + 2161c1f commit 4e45e4b
Show file tree
Hide file tree
Showing 5 changed files with 511 additions and 8 deletions.
111 changes: 111 additions & 0 deletions Source/Scene/Axis.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
/*global define*/
define([
'../Core/Check',
'../Core/freezeObject',
'../Core/Math',
'../Core/Matrix3',
'../Core/Matrix4'
], function(
Check,
freezeObject,
CesiumMath,
Matrix3,
Matrix4) {
'use strict';

/**
* An enum describing the x, y, and z axes and helper conversion functions.
*
* @exports Axis
* @private
*/
var Axis = {
/**
* 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 z-up to y-up
*
* @type {Matrix4}
* @constant
*/
Z_UP_TO_Y_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)),

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

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

/**
* Matrix used to convert from y-up to x-up
*
* @type {Matrix4}
* @constant
*/
Y_UP_TO_X_UP : Matrix4.fromRotationTranslation(Matrix3.fromRotationZ(-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) {
//>>includeStart('debug', pragmas.debug);
Check.typeOf.string('name', name);
//>>includeEnd('debug');

return Axis[name];
}
};

return freezeObject(Axis);
});
39 changes: 34 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 @@ -623,6 +624,7 @@ define([
this._pickFragmentShaderLoaded = options.pickFragmentShaderLoaded;
this._pickUniformMapLoaded = options.pickUniformMapLoaded;
this._ignoreCommands = defaultValue(options.ignoreCommands, false);
this._upAxis = defaultValue(options.upAxis, Axis.Y);

/**
* @private
Expand Down Expand Up @@ -956,6 +958,24 @@ define([
//>>includeEnd('debug');
this._distanceDisplayCondition = DistanceDisplayCondition.clone(value, this._distanceDisplayCondition);
}
},

/**
* Gets the model's up-axis.
* By default models are y-up according to the glTF spec, however geo-referenced models will typically be z-up.
*
* @memberof Model.prototype
*
* @type {Number}
* @default Axis.Y
* @readonly
*
* @private
*/
upAxis : {
get : function() {
return this._upAxis;
}
}
});

Expand Down Expand Up @@ -1233,7 +1253,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 +1309,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 +4217,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 +4340,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

0 comments on commit 4e45e4b

Please sign in to comment.