Skip to content
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

Normalmapping for errourneous UV coordinates #8901

Closed
3 of 4 tasks
throni3git opened this issue May 17, 2016 · 2 comments
Closed
3 of 4 tasks

Normalmapping for errourneous UV coordinates #8901

throni3git opened this issue May 17, 2016 · 2 comments

Comments

@throni3git
Copy link
Contributor

Description of the problem

Hello,

i've got customer models of furniture objects that are buggy in their uv coordinates. This is of course less an issue of threejs, more an issue of the models. Anyway, i'd like to check out if there is a way to handle it with the shader. The Material is MeshPhongMaterial and the problem occurs only if a normalmap is activated and the uv coordinates don't represent triangles but lines.

The code responsible for the normalmapping is found in perturbNormal2Arb. I think the problem might be the dFdy/dFdy of vUv, where the errourneous uv coodrinates deliver vec2(0, 0). However, if i add

st0.xy += 0.0000001;

the artifacts are gone.

I don't know if it really is an issue of threejs, but maybe it is a solution for others to come...

#ifdef USE_NORMALMAP
    uniform sampler2D normalMap;
    uniform vec2 normalScale;
    vec3 perturbNormal2Arb( vec3 eye_pos, vec3 surf_norm ) {
        vec3 q0 = dFdx( eye_pos.xyz );
        vec3 q1 = dFdy( eye_pos.xyz );
        vec2 st0 = dFdx( vUv.st );
        vec2 st1 = dFdy( vUv.st );
        vec3 S = normalize( q0 * st1.t - q1 * st0.t );
        vec3 T = normalize( -q0 * st1.s + q1 * st0.s );
        vec3 N = normalize( surf_norm );
        vec3 mapN = texture2D( normalMap, vUv ).xyz * 2.0 - 1.0;
        mapN.xy = normalScale * mapN.xy;
        mat3 tsn = mat3( S, T, N );
        return normalize( tsn * mapN );
    }
#endif

uv_fail

Three.js version
  • r75
Browser
  • Chrome
  • Firefox
OS
  • Windows
@mrdoob
Copy link
Owner

mrdoob commented May 17, 2016

Maybe @tschw knows something about this.

@tschw
Copy link
Contributor

tschw commented May 17, 2016

More of a stackoverflow question, actually:

The tangential space is derived at run time from the UV map. When there is a zero gradient (one value is always the same), the resulting space collapses into singularity. If you can't use a proper UV map, you can use a custom shader that only considers .s ("U") or .t ("V") which then results in missing S or T in the above code. You can set it to an axis orthogonal to the other and N using a normalized cross product.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants