Open ghost opened 4 years ago
@BogdanCrav Thanks for opening this request.
Let's start by trying to see if we can agree on an implementation approach then that could be an external PR or done by the core team.
@tsherif @igorDykhta Do you have input on this?
Would it be appropriate to add an extra tangent attribute to the primitives or is this better done with shader derivatives or other techniques?
Definitely doable, and the technique is general, so having one calculateTangents
function would allow us to do it for all shapes. And you only need to calculate the tangents, as the bi-tangent can be calculated as a simple cross product in the shader.
My only suggestion would be to make it "opt-in". It's not an insignificant amount of time/memory to generate them and they're only useful for certain techniques. Wouldn't want users paying for them if they're not using them.
I worked on this tangent calculation algo in SceneJS: https://github.com/xeolabs/scenejs/blob/master/src/core/math.js#L2655-L2710 Not saying it's the best implementation, but might be useful as a reference.
One approach is to add one vec4 vertex attribute with tangent vec3 and a value to restore bitangent. Here we have some stuff to setup with more per vertex data, probably less stuff to do in fragment shader.
Second solution is to calculate tangents based on tex coords derivatives in FS, no additional attributes, also not sure about performance issues, as used only first approach.
@ibgreen @tarek If this issue has not been assigned yet, I can offer some time to add this to luma.gl.
Background
Can you add Tangets for geometry and calculation of the TBN matrix in shaders?
The best way to do it is by using UV differential algorithm like this: http://www.opengl-tutorial.org/intermediate-tutorials/tutorial-13-normal-mapping/
This would allow for correct normals transformation, which is useful for such effects like Normal Mapping, Displacement Mapping, etc.
Also, Tangets and BiTangets can be useful when you want to achieve some custom effects in shaders that require you to know vectors of the direction of U and V in model space, for example.