Giter VIP home page Giter VIP logo

Comments (4)

Mugen87 avatar Mugen87 commented on June 8, 2024

IMO, the use cases which require a manual bias parameter are very limited. A typical one in the past was #20522 which is about adding a LOD bias for alpha maps.

But because of the specific character of LOD bias, I don't think it's appropriate to introduce a new texture (or material) property. With a naive implementation we would end up with more uniforms that are not required most of the time. To prevent this, we would have to implement additional branches in JS and the GLSL chunks which increases complexity and maintainability.

A global setting seems problematic as well since a texture LOD bias is usually not something that is equal for all textures in an app.

Do you mind describing in more detail why you need control over what mipmap is going to be sampled? Is this control restricted to specific type of textures (like alpha maps)?

from three.js.

Aloalo avatar Aloalo commented on June 8, 2024

Sure, I need it for SSAA (implementation similar to three's SSAARenderPass).

Rendering SSAA in this manner with 2^n samples should be equivalent to rendering into a render target scaled by a factor of 2^sqrt(n) and then down scaling that render target to the original size with a linear filter.
When rendering a mesh into a render target that's scaled by a factor of 2^sqrt(n) the UV derivatives at a given pixel would be scaled by 2^-sqrt(n). The mipmap selection bias compared to rendering without scaling would then be the log of the exponent: log2(-sqrt(n))=-log2(n)/2.

So, in order to achieve the same effect as rendering into a larger render target I'd like to introduce this texture lod bias of -log2(n)/2 when rendering individual samples in SSAA.

Hopefully my explanation is clear enough

from three.js.

Mugen87 avatar Mugen87 commented on June 8, 2024

Thanks for the additional details.

I do not yet understand why you need a new texture property though. Do you modify built-in shaders for your SSAA implementation?

from three.js.

Aloalo avatar Aloalo commented on June 8, 2024

SSAA doesn't use any custom shader. It just renders the scene 2^n times with a slight camera view offset each time and averages the results. If I wanted to incorporate the load bias I'd have to modify a lot of internal shaders (pretty much every material texture sample).

The (very loose) pseudo code would be something like this:

scene.setGlobalTextureLodBias(-Math.log2(viewOffsets.length) / 2); // This is the thing I'd like to be able to do but can't currently
const viewOffsets = [...];
for (const viewOffset of viewOffsets) {
  camera.setViewOffset(viewOffset);
  
  webglRenderer.setRenderTarget(sceneRt);
  webglRenderer.clear();
  webglRenderer.render(scene, camera);

  // ...copy sceneRt into an accumulationRt with AdditiveBlending and opacity of 1.0 / viewOffsets.length
}

A way this could work is by replacing all texture2D calls in material shaders with something like:

vec4 textureSample(in sampler2D sampler, in vec2 uv) {
  return texture2D(sampler, uv, TEXTURE_LOD_BIAS);
}

Where TEXTURE_LOD_BIAS is a define that defaults to 0. Scene.setGlobalTextureLodBias would set this to the specified value.

If WebGL2 supported the TEXTURE_LOD_BIAS​ sampler parameter this would be much simpler to do as the property could be set on sampler objects and shaders wouldn't have to be touched. But alas it doesn't, and there are no extensions for it. WebGPU also douesn't support it.

from three.js.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.