mrdoob / three.js

JavaScript 3D Library.
https://threejs.org/
MIT License
102.79k stars 35.38k forks source link

Allow specifying mipmap bias for textures #10482

Open jampekka opened 7 years ago

jampekka commented 7 years ago

Currently there doesn't seem to be any way to enable mipmap bias for textures. For some uses (eg ground/road textures) a negative mipmap bias is often used to tune blur/aliasing trade-off.

I can hack the bias globally by adding it as a third parameter to texture2D in eg. map_fragment.glsl, but unfortunately I have no idea how to nicely implement this for three.js.

Below is an example of adding such bias (rendered using THREE.LinearMipMapLinearFilter and 16x anisotropy, 3x blowup): mipmapbias

makc commented 7 years ago

you can always do this via shader materials, I guess. the situation in your image is dealt with like this.

mrdoob commented 7 years ago

Is that supposed to be a way of faking anisotropy?

jampekka commented 7 years ago

@makc I'm already using anisotropy (the screenshot is a zoom to a "far-away" segment). With shader material one would have to probably implement the whole shader "from scratch"?

@mrdoob It's used usually in conjunction with anisotropy. I think it's quite widely supported by 3D engines, and eg the COLLADA spec has a mip-bias option for textures.

I could take a shot at implementing it, but I don't understand very well how it should be integrated. There's no easy way such as gl.texParameterf type method of specifying it, in contrast to eg anisotropy, so it has to go to the shader. I guess the proper way would be to have a GLSL preprocessor directive to turn such a uniform on when needed? Eg something like:

#ifdef USE_MAP

#ifdef USE_MIPMAP_BIAS
        vec4 texelColor = texture2D( map, vUv, mipmapBias );
#else
    vec4 texelColor = texture2D( map, vUv );
#endif

    texelColor = mapTexelToLinear( texelColor );
    diffuseColor *= texelColor;

#endif

and a conditional declaration of mipmapBias in eg map_pars_fragment.glsl, something like

#ifdef USE_MAP

    uniform sampler2D map;

#ifdef USE_MIPMAP_BIAS
        uniform float mipmapBias;
#endif

#endif

Would this make any sense?