-
-
Notifications
You must be signed in to change notification settings - Fork 35.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
BufferGeometryUtils: Fixes for MikkTSpace tangents #23802
BufferGeometryUtils: Fixes for MikkTSpace tangents #23802
Conversation
- Avoid top-level await. - Fix error on non-indexed geometry.
@@ -10,10 +10,15 @@ import { | |||
TrianglesDrawMode, | |||
Vector3, | |||
} from 'three'; | |||
import { generateTangents } from '../libs/mikktspace.module.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 think this import can stay now that there is no top-level await anymore? The wasm
variable will contain the instance once the request has been fulfilled.
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 technically could stay, it's a question of the preferred API / usage:
import * as MikkTSpace from 'three/examples/jsm/libs/mikktspace.module.js';
import { computeTangents } from 'three/examples/jsm/utils/BufferGeometryUtils.js';
await MikkTSpace.ready;
// (A)
geometry = computeTangents( geometry, MikkTSpace );
// (B)
geometry = computeTangents( geometry );
I was thinking (A) might be better because it is more explicit – you have to know about this MikkTSpace module, because you're passing it into the function, so there's perhaps less chance of forgetting to check .ready
and having a subtle race condition. This also allows users to provide the mikktspace
npm package instead, which is more optimized for modern bundlers (it doesn't embed the WASM as a base64 string as required here).
I'm open to either (A) or (B), though.
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.
Right, if it makes users able to use the wasm version, it makes sense.
Works great! I recommend having a code blurb in the documentation showing how to import the MikkTSpace object since it wasn't immediately clear to me: import * as MikkTSpace from './lib/mikktspace.module.js';
import { computeTangents } from './lib/BufferGeometryUtils.js';
// ...
await MikktSpace.ready;
computeTangents( geometry, MikktSpace ); Here's a demo of a model that had to have tangents generated to use the normal map correctly: |
Also not sure if this is a requirement of MikkTSpace computation or not but it would be nice if vertices were not implicitly unmerged while computing tangents. But I guess it's not a huge deal since you can remerge after. |
MikkTSpace does require non-indexed input, although re-indexing after computing tangents is fine. Would it be better to throw an error for indexed input rather than rebuilding the input geometry? |
I find it confusing to have two methods called |
No I think as-is is fine. Just thought I'd mention it in case there was a better way to handle it. I think requiring the user to merge after makes sense. I'd just make sure it's well-noted in the docs. |
@gkjohnson Since the docs are not clear as to when the existing method may not work, can you please use |
The model above just does not have tangents at all. For my use case (path traced ao map baking) I need tangents in order to correctly use the normal map and the existing view-space generation doesn't work. I want to generate tangents in-shader for this use case, as well, (which I think is doable) but that's a longer term effort. |
@gkjohnson But you compute tangents, and the existing method is inadequate in your example. Right? I'd like to know where the existing method fails. |
Oh tbh I didn't realize that
But here's an image with the different compute tangent methods. Notice on the BufferGeometry.computeTangents that the normal map seems inverted -- this is likely due to the
|
It might also be worth throwing a more human-readable error if there are no normals / uvs present on the geometry since they're required for generating tangents. |
@mrdoob we might want to publish a patch with this.. Some bundlers are affected even if not importing directly from BufferGeometryUtils. |
Thanks! |
@marcofugaro Done. Cherry-picked this PR into |
Made both changes in #23815, for r140. |
@mrdoob I'm not sure that this PR was successfully included in |
Looks like the old version, still: https://unpkg.com/browse/[email protected]/examples/jsm/utils/BufferGeometryUtils.js |
Eek... I cherry-picked the wrong PR... |
- Avoid top-level await. - Fix error on non-indexed geometry.
Alright, |
- Avoid top-level await. - Fix error on non-indexed geometry.
Related issues:
Description
/cc @gkjohnson do you mind checking that this works with your Parcel setup?