-
Notifications
You must be signed in to change notification settings - Fork 3.6k
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
Snorm range max #4121
Merged
Merged
Snorm range max #4121
Changes from 2 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
d8c1178
Added rangeMax for specifying different levels of precision for
lasalvavida 920eb39
Added tests
lasalvavida 49f8870
Doc grammar
lasalvavida 89ec94b
Split off into new function and update CHANGES.md
lasalvavida d905434
Fix a few existing tests
lasalvavida e8b8e00
Missed CHANGES.md
lasalvavida 8c664d4
rangeMax isn't optional on octEncodeInRange and octDecodeInRange
lasalvavida 332c582
Added doc note about bit size
lasalvavida fb3a0a7
Should be rangeMax+1
lasalvavida File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,12 +2,14 @@ | |
define([ | ||
'./Cartesian2', | ||
'./Cartesian3', | ||
'./defaultValue', | ||
'./defined', | ||
'./DeveloperError', | ||
'./Math' | ||
], function( | ||
Cartesian2, | ||
Cartesian3, | ||
defaultValue, | ||
defined, | ||
DeveloperError, | ||
CesiumMath) { | ||
|
@@ -23,21 +25,22 @@ define([ | |
var AttributeCompression = {}; | ||
|
||
/** | ||
* Encodes a normalized vector into 2 SNORM values in the range of [0-255] following the 'oct' encoding. | ||
* Encodes a normalized vector into 2 SNORM values in the range of [0-rangeMax] following the 'oct' encoding. | ||
* | ||
* Oct encoding is a compact representation of unit length vectors. The encoding and decoding functions are low cost, and represent the normalized vector within 1 degree of error. | ||
* The 'oct' encoding is described in "A Survey of Efficient Representations of Independent Unit Vectors", | ||
* Cigolle et al 2014: {@link http://jcgt.org/published/0003/02/01/} | ||
* | ||
* @param {Cartesian3} vector The normalized vector to be compressed into 2 byte 'oct' encoding. | ||
* @param {Cartesian2} result The 2 byte oct-encoded unit length vector. | ||
* @param {Number} [rangeMax=255] The maximum value of the SNORM range, 255 by default. | ||
* @returns {Cartesian2} The 2 byte oct-encoded unit length vector. | ||
* | ||
* @exception {DeveloperError} vector must be normalized. | ||
* | ||
* @see AttributeCompression.octDecode | ||
*/ | ||
AttributeCompression.octEncode = function(vector, result) { | ||
AttributeCompression.octEncode = function(vector, result, rangeMax) { | ||
//>>includeStart('debug', pragmas.debug); | ||
if (!defined(vector)) { | ||
throw new DeveloperError('vector is required.'); | ||
|
@@ -50,6 +53,7 @@ define([ | |
throw new DeveloperError('vector must be normalized.'); | ||
} | ||
//>>includeEnd('debug'); | ||
rangeMax = defaultValue(rangeMax, 255); | ||
|
||
result.x = vector.x / (Math.abs(vector.x) + Math.abs(vector.y) + Math.abs(vector.z)); | ||
result.y = vector.y / (Math.abs(vector.x) + Math.abs(vector.y) + Math.abs(vector.z)); | ||
|
@@ -60,8 +64,8 @@ define([ | |
result.y = (1.0 - Math.abs(x)) * CesiumMath.signNotZero(y); | ||
} | ||
|
||
result.x = CesiumMath.toSNorm(result.x); | ||
result.y = CesiumMath.toSNorm(result.y); | ||
result.x = CesiumMath.toSNorm(result.x, rangeMax); | ||
result.y = CesiumMath.toSNorm(result.y, rangeMax); | ||
|
||
return result; | ||
}; | ||
|
@@ -72,24 +76,27 @@ define([ | |
* @param {Number} x The x component of the oct-encoded unit length vector. | ||
* @param {Number} y The y component of the oct-encoded unit length vector. | ||
* @param {Cartesian3} result The decoded and normalized vector | ||
* @param {Number} [rangeMax=255] The maximum value of the SNORM range, 255 by default. | ||
* @returns {Cartesian3} The decoded and normalized vector. | ||
* | ||
* @exception {DeveloperError} x and y must be a signed normalized integer between 0 and 255. | ||
* @exception {DeveloperError} x and y must be a signed normalized integer between 0 and rangeMax. | ||
* | ||
* @see AttributeCompression.octEncode | ||
*/ | ||
AttributeCompression.octDecode = function(x, y, result) { | ||
AttributeCompression.octDecode = function(x, y, result, rangeMax) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same comment. |
||
rangeMax = defaultValue(rangeMax, 255); | ||
|
||
//>>includeStart('debug', pragmas.debug); | ||
if (!defined(result)) { | ||
throw new DeveloperError('result is required.'); | ||
} | ||
if (x < 0 || x > 255 || y < 0 || y > 255) { | ||
throw new DeveloperError('x and y must be a signed normalized integer between 0 and 255'); | ||
if (x < 0 || x > rangeMax || y < 0 || y > rangeMax) { | ||
throw new DeveloperError('x and y must be a signed normalized integer between 0 and ' + rangeMax); | ||
} | ||
//>>includeEnd('debug'); | ||
|
||
result.x = CesiumMath.fromSNorm(x); | ||
result.y = CesiumMath.fromSNorm(y); | ||
result.x = CesiumMath.fromSNorm(x, rangeMax); | ||
result.y = CesiumMath.fromSNorm(y, rangeMax); | ||
result.z = 1.0 - (Math.abs(result.x) + Math.abs(result.y)); | ||
|
||
if (result.z < 0.0) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -218,25 +218,29 @@ define([ | |
}; | ||
|
||
/** | ||
* Converts a scalar value in the range [-1.0, 1.0] to a 8-bit 2's complement number. | ||
* Converts a scalar value in the range [-1.0, 1.0] to an SNORM in the range [0, rangeMax] | ||
* @param {Number} value The scalar value in the range [-1.0, 1.0] | ||
* @returns {Number} The 8-bit 2's complement number, where 0 maps to -1.0 and 255 maps to 1.0. | ||
* @param {Number} [rangeMax=255] The maximum value in the mapped range, 255 by default. | ||
* @returns {Number} An SNORM value, where 0 maps to -1.0 and rangeMax maps to 1.0. | ||
* | ||
* @see CesiumMath.fromSNorm | ||
*/ | ||
CesiumMath.toSNorm = function(value) { | ||
return Math.round((CesiumMath.clamp(value, -1.0, 1.0) * 0.5 + 0.5) * 255.0); | ||
CesiumMath.toSNorm = function(value, rangeMax) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Update CHANGES.md since this is in the public API. |
||
rangeMax = defaultValue(rangeMax, 255); | ||
return Math.round((CesiumMath.clamp(value, -1.0, 1.0) * 0.5 + 0.5) * rangeMax); | ||
}; | ||
|
||
/** | ||
* Converts a SNORM value in the range [0, 255] to a scalar in the range [-1.0, 1.0]. | ||
* Converts an SNORM value in the range [0, rangeMax] to a scalar in the range [-1.0, 1.0]. | ||
* @param {Number} value SNORM value in the range [0, 255] | ||
* @param {Number} [rangeMax=255] The maximum value in the SNORM range, 255 by default. | ||
* @returns {Number} Scalar in the range [-1.0, 1.0]. | ||
* | ||
* @see CesiumMath.toSNorm | ||
*/ | ||
CesiumMath.fromSNorm = function(value) { | ||
return CesiumMath.clamp(value, 0.0, 255.0) / 255.0 * 2.0 - 1.0; | ||
CesiumMath.fromSNorm = function(value, rangeMax) { | ||
rangeMax = defaultValue(rangeMax, 255); | ||
return CesiumMath.clamp(value, 0.0, rangeMax) / rangeMax * 2.0 - 1.0; | ||
}; | ||
|
||
/** | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
result
parameters should come last so we need to introduce a new function (and we can implement this function using that new one; no need to deprecate this).