-
Notifications
You must be signed in to change notification settings - Fork 3.5k
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
Fix oct-encoded normal upsampling. #1961
Merged
kring
merged 12 commits into
CesiumGS:master
from
abwood:upsampleQuantizedMeshNormalsFix
Jul 27, 2014
Merged
Changes from 1 commit
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
a678cd9
Fix normal upsampling. Cannot linearly interpolate oct encoded norma…
abwood a59d9ee
Moved octEncoding into Core. Moved signNotZero and toSNorm into Cesi…
abwood 393df80
Minor cleanup. Added new conversion function to Math.js, fromSNorm.
abwood e670ac7
Minor cleanup.
abwood 7129514
Merge remote-tracking branch 'agi/master' into upsampleQuantizedMeshN…
abwood 93fdefa
Doc updates.
abwood 95ef5bb
Cleaned up doc for Oct.
abwood fc60338
Fixed typo in doc.
abwood fae0d2f
Documented exceptions in Oct.
abwood c2d8b85
Mark Oct class as private.
abwood 05058ad
Merge remote-tracking branch 'agi/master' into upsampleQuantizedMeshN…
abwood 9630474
Minor cleanup.
abwood 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 |
---|---|---|
@@ -1,6 +1,7 @@ | ||
/*global define*/ | ||
define([ | ||
'../Core/BoundingSphere', | ||
'../Core/Cartesian2', | ||
'../Core/Cartesian3', | ||
'../Core/Cartographic', | ||
'../Core/defined', | ||
|
@@ -11,6 +12,7 @@ define([ | |
'./createTaskProcessorWorker' | ||
], function( | ||
BoundingSphere, | ||
Cartesian2, | ||
Cartesian3, | ||
Cartographic, | ||
defined, | ||
|
@@ -372,18 +374,82 @@ define([ | |
return CesiumMath.lerp(this.first.getV(), this.second.getV(), this.ratio); | ||
}; | ||
|
||
function signNotZero(v) { | ||
return v < 0.0 ? -1.0 : 1.0; | ||
} | ||
|
||
function toSNorm(f) { | ||
return Math.round((f * 0.5 + 0.5) * 255.0); | ||
} | ||
|
||
function octDecode(x, y, result) { | ||
result.x = x / 255.0 * 2.0 - 1.0; | ||
result.y = y / 255.0 * 2.0 - 1.0; | ||
result.z = 1.0 - (Math.abs(result.x) + Math.abs(result.y)); | ||
|
||
if (result.z < 0.0) | ||
{ | ||
var oldVX = x / 255.0 * 2.0 - 1.0; | ||
result.x = (1.0 - Math.abs(result.y)) * signNotZero(oldVX); | ||
result.y = (1.0 - Math.abs(oldVX)) * signNotZero(result.y); | ||
} | ||
|
||
return Cartesian3.normalize(result, result); | ||
} | ||
|
||
function octEncode(vector, out) { | ||
out.x = vector.x / (Math.abs(vector.x) + Math.abs(vector.y) + Math.abs(vector.z)); | ||
out.y = vector.y / (Math.abs(vector.x) + Math.abs(vector.y) + Math.abs(vector.z)); | ||
if (vector.z < 0) { | ||
var x = out.x; | ||
var y = out.y; | ||
out.x = (1.0 - Math.abs(y)) * signNotZero(x); | ||
out.y = (1.0 - Math.abs(x)) * signNotZero(y); | ||
} | ||
|
||
out.x = toSNorm(out.x); | ||
out.y = toSNorm(out.y); | ||
} | ||
|
||
var encodedScratch = new Cartesian2(); | ||
// An upsampled triangle may be clipped twice before it is assigned an index | ||
// In this case, we need a buffer to handle the recursion of getNormalX() and getNormalY(). | ||
var depth = -1; | ||
var cartesianScratch1 = [new Cartesian3(), new Cartesian3()]; | ||
var cartesianScratch2 = [new Cartesian3(), new Cartesian3()]; | ||
function lerpOctEncodedNormal(vertex, result) { | ||
depth += 1; | ||
|
||
var first = cartesianScratch1[depth]; | ||
var second = cartesianScratch2[depth]; | ||
|
||
first = octDecode(vertex.first.getNormalX(), vertex.first.getNormalY(), first); | ||
second = octDecode(vertex.second.getNormalX(), vertex.second.getNormalY(), second); | ||
cartesian3Scratch = Cartesian3.lerp(first, second, vertex.ratio, cartesian3Scratch); | ||
|
||
octEncode(cartesian3Scratch, result); | ||
|
||
depth -= 1; | ||
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. Nitpicky, but why not |
||
|
||
return result; | ||
} | ||
|
||
Vertex.prototype.getNormalX = function() { | ||
if (defined(this.index)) { | ||
return this.normalBuffer[this.index * 2]; | ||
} | ||
return Math.round(CesiumMath.lerp(this.first.getNormalX(), this.second.getNormalX(), this.ratio)); | ||
|
||
encodedScratch = lerpOctEncodedNormal(this, encodedScratch); | ||
return encodedScratch.x; | ||
}; | ||
|
||
Vertex.prototype.getNormalY = function() { | ||
if (defined(this.index)) { | ||
return this.normalBuffer[this.index * 2 + 1]; | ||
} | ||
return Math.round(CesiumMath.lerp(this.first.getNormalY(), this.second.getNormalY(), this.ratio)); | ||
|
||
encodedScratch = lerpOctEncodedNormal(this, encodedScratch); | ||
return encodedScratch.y; | ||
}; | ||
|
||
var polygonVertices = []; | ||
|
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.
Can you make this a generic unit-tested function (even if it is
@private
) since we are going to want it for the geometry pipeline, which we are going to start work on again soon for KML? Perhaps it is a functionCartesian3
? Or perhaps it is better to make a new oct class and put static decode and encode functions in it.signNotZero
andtoSNorm
could probably go inMath.js
.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.
I'd be happy to. I was on the fence about moving these into Core in this pull request since it is currently only used in this location.
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.
At least the geometry pipeline will also use them. Probably dynamic buffers too if it is a win (I bet yes) ( #932). I could also see billboards using something similar for vertex compression ( #419). 3D models will also use this (but won't need the code since it will be done server-side).