-
Notifications
You must be signed in to change notification settings - Fork 250
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
Added generateNormals stage #82
Conversation
@lilleyse consider just using the function in Cesium: https://github.com/AnalyticalGraphicsInc/cesium/blob/master/Source/Core/GeometryPipeline.js#L1059-L1192 Or improve the version in Cesium and use that. |
Yeah I'll likely go that route. @JoshuaStorm is going to write a gltf primitive <-> Geometry converter so that we can use Cesium functions like these easily. |
@lilleyse: In |
I'm not sure if the normals in the |
I think I might be misunderstanding: These are just vertex normals in the geometric sense, correct? If that is the case, shouldn't they be derived, in other words be the same whether they are provided or calculated? Sorry for the pedantic question, I just want to make sure I am understanding this. The current result I am getting is shown below, which I am somewhat suspicious is the correct normal values but the indices are out of order somehow. |
var index1 = indices[j * 3 + 0]; | ||
var index2 = indices[j * 3 + 1]; | ||
var index3 = indices[j * 3 + 2]; | ||
var faceNormal = getFaceNormal(positions[index1], positions[index2], positions[index3], scratchNormal); |
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.
Maybe I'm misreading, but does this just set the vertex normal to be the face normal? That shouldn't be the correct vector.
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.
It adds all the face normals that the vertex is a part of, and then normalizes.
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.
Oh, I see thank you
@JoshuaStorm to debug you can tweak the fragment shader (even in the Model Inspector) so the output color is the normal. The computed normal may not be the same as a provided normal, as a user could provide whatever they want. |
Looking more into this, the issue does seem to be what we discussed offline @pjcozzi. In both this implementation and the I plan on looking more into the duplicating vertices at edges solution that you also mentioned, @pjcozzi, but I was wondering if I should plan on implementing it here (and not use the GeometryPipeline) or if I should plan on implementing it in |
@JoshuaStorm the implementation should go into Cesium proper, then gltf-pipeline will pick it up in the next release via npm. You can, of course, code it in this repo to test it, but you'll only want commits to go to the Cesium repo. |
I am at a bit of a loss if anyone else has any ideas they'd be appreciated! As a naive test to confirm the shared vertices issue, I iterated through the indices and in any case where there was multiple references to the same vertex, I duplicated that vertex and had the respective index refer to the new duplicate--effectively getting rid of shared vertices by simply duplicating all shared vertices. Then generating normals for that, I ended up with this box: With RGB mapped to normal value I'm wondering if it is possibly for some of the vertices to be in the incorrect winding order? |
Maybe, perhaps hack up the code so only 3 of the 6 faces are in the model. |
…ne into generate-normals
@JoshuaStorm I took a stab at this and its pretty close now. The back normals look like they might be off. Could you take a look at this? |
@lasalvavida Your box seems to mirror that of the included I could start implementing this solution into Cesium's A quick question about the code: what exactly is the purpose for finding the center of the model? |
@JoshuaStorm It was some scratch code we can probably take out. I was using it to check if the normals were facing the center of the model and to flip them out if they were. However, it never got called, so I took it out and must have missed the center calculation. I'd say before doing anything, we need an answer as to why the faces are shaded black. Is it the shader, or is there still something wrong with the data? |
I'm getting a crash when running the dragon model through: |
Yup this and ao. I'll review soon. |
var indicesAccessorId = primitive.indices; | ||
var positionAccessor = gltf.accessors[positionAccessorId]; | ||
var position = []; | ||
readAccessor(gltf, positionAccessor, position); |
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.
You can remove these three lines.
|
Here are the models used. |
I think this is finally ready for review! Thanks @lasalvavida for pointing out the issue with generating normals before the combine and remove stages. |
If I pull master into this branch and run with
|
@lasalvavida What model was this produced with? Worked fine for |
The above seems to be a problem in my WebStorm, it works fine from bash so you can disregard it. edit: The above is a bug, opened #139 |
@@ -120,7 +120,7 @@ describe('gltfPipeline', function() { | |||
readGltf(gltfEmbeddedPath, options, function (gltf) { | |||
var initialUri = gltf.buffers.CesiumTexturedBoxTest.uri; | |||
processJSON(gltf, options, function () { | |||
var finalUri = gltf.buffers.CesiumTexturedBoxTest.uri; | |||
var finalUri = gltf.buffers.uri; |
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.
Why did you make this change? If you do this, you're comparing at different levels of the glTF hierarchy.
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.
Oops, this was a naive change since processFile
will now merge buffers, so gltf.buffers.CesiumTexturedBoxTest
will no longer exist.
This could now expect the old buffer to be undefined and compare initialUri with gltf.buffer_0.uri
(the only buffer that should be on the gltf), but that doesn't seem to be perfect test either. Thoughts?
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.
Since in this test, there can only be one buffer (I assume). Just get the first buffer:
var firstBufferId = Object.keys(gltf.buffers)[0];
var testBuffer = gltf.buffers[firstBufferId];
var initialUri = gltf.buffers.CesiumTexturedBoxTest.uri; | ||
var firstBufferId = Object.keys(gltf.buffers)[0]; | ||
var testBuffer = gltf.buffers[firstBufferId]; | ||
var initialUri = testBuffer.uri; |
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 don't think you need to do this here, just after processJSON gets run, right? You do know the initial bufferId is CesiumTexturedBoxTest.
I just wanted to include the above comments as a frame of reference for when we add support for generating hard normals. This looks great to me, thanks @lilleyse and @JoshuaStorm! |
Thanks for reviewing @lasalvavida |
This adds support for generating normals if they do not exist.