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

Add shader textureBias #4088

Merged
merged 16 commits into from
Mar 8, 2022
Merged
5,549 changes: 5,537 additions & 12 deletions examples/package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/graphics/program-lib/chunks/ao.frag.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ void applyAO() {
dAo = 1.0;

#ifdef MAPTEXTURE
dAo *= texture2D(texture_aoMap, $UV).$CH;
dAo *= texture2D(texture_aoMap, $UV, textureBias).$CH;
#endif

#ifdef MAPVERTEX
Expand Down
2 changes: 2 additions & 0 deletions src/graphics/program-lib/chunks/base.frag.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ uniform vec3 view_position;

uniform vec3 light_globalAmbient;

uniform float textureBias;

float square(float x) {
return x*x;
}
Expand Down
2 changes: 1 addition & 1 deletion src/graphics/program-lib/chunks/diffuse.frag.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ void getAlbedo() {
#endif

#ifdef MAPTEXTURE
dAlbedo *= gammaCorrectInput(addAlbedoDetail(texture2D(texture_diffuseMap, $UV).$CH));
dAlbedo *= gammaCorrectInput(addAlbedoDetail(texture2D(texture_diffuseMap, $UV, textureBias).$CH));
#endif

#ifdef MAPVERTEX
Expand Down
2 changes: 1 addition & 1 deletion src/graphics/program-lib/chunks/metalness.frag.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ void getSpecularity() {
#endif

#ifdef MAPTEXTURE
metalness *= texture2D(texture_metalnessMap, $UV).$CH;
metalness *= texture2D(texture_metalnessMap, $UV, textureBias).$CH;
#endif

#ifdef MAPVERTEX
Expand Down
2 changes: 1 addition & 1 deletion src/graphics/program-lib/chunks/normalDetailMap.frag.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ vec3 blendNormals(vec3 n1, vec3 n2) {

vec3 addNormalDetail(vec3 normalMap) {
#ifdef MAPTEXTURE
vec3 normalDetailMap = unpackNormal(texture2D(texture_normalDetailMap, $UV));
vec3 normalDetailMap = unpackNormal(texture2D(texture_normalDetailMap, $UV, textureBias));
normalDetailMap = normalize(mix(vec3(0.0, 0.0, 1.0), normalDetailMap, material_normalDetailMapBumpiness));
return blendNormals(normalMap, normalDetailMap);
#else
Expand Down
2 changes: 1 addition & 1 deletion src/graphics/program-lib/chunks/normalMap.frag.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ uniform sampler2D texture_normalMap;
uniform float material_bumpiness;

void getNormal() {
vec3 normalMap = unpackNormal(texture2D(texture_normalMap, $UV));
vec3 normalMap = unpackNormal(texture2D(texture_normalMap, $UV, textureBias));
normalMap = normalize(mix(vec3(0.0, 0.0, 1.0), normalMap, material_bumpiness));
dNormalMap = addNormalDetail(normalMap);
dNormalW = dTBN * dNormalMap;
Expand Down
2 changes: 1 addition & 1 deletion src/graphics/program-lib/chunks/normalMapFast.frag.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ export default /* glsl */`
uniform sampler2D texture_normalMap;

void getNormal() {
vec3 normalMap = unpackNormal(texture2D(texture_normalMap, $UV));
vec3 normalMap = unpackNormal(texture2D(texture_normalMap, $UV, textureBias));
dNormalMap = addNormalDetail(normalMap);
dNormalW = dTBN * dNormalMap;
}
Expand Down
2 changes: 1 addition & 1 deletion src/graphics/program-lib/chunks/opacity.frag.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ void getOpacity() {
#endif

#ifdef MAPTEXTURE
dAlpha *= texture2D(texture_opacityMap, $UV).$CH;
dAlpha *= texture2D(texture_opacityMap, $UV, textureBias).$CH;
#endif

#ifdef MAPVERTEX
Expand Down
2 changes: 1 addition & 1 deletion src/graphics/program-lib/chunks/parallax.frag.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ uniform float material_heightMapFactor;
void getParallax() {
float parallaxScale = material_heightMapFactor;

float height = texture2D(texture_heightMap, $UV).$CH;
float height = texture2D(texture_heightMap, $UV, textureBias).$CH;
height = height * parallaxScale - parallaxScale*0.5;
vec3 viewDirT = dViewDirW * dTBN;

Expand Down
2 changes: 1 addition & 1 deletion src/graphics/program-lib/chunks/reflectionEnv.frag.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ float shinyMipLevel(vec2 uv) {
// calculate min of both sets of dF to handle discontinuity at the azim edge
float maxd = min(max(dot(dx, dx), dot(dy, dy)), max(dot(dx2, dx2), dot(dy2, dy2)));

return clamp(0.5 * log2(maxd) - 1.0, 0.0, 6.0);
return clamp(0.5 * log2(maxd) - 1.0 + textureBias, 0.0, 6.0);
}

vec3 calcReflection(vec3 tReflDirW, float tGlossiness) {
Expand Down
2 changes: 1 addition & 1 deletion src/graphics/program-lib/chunks/specular.frag.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ void getSpecularity() {
#endif

#ifdef MAPTEXTURE
dSpecularity *= texture2D(texture_specularMap, $UV).$CH;
dSpecularity *= texture2D(texture_specularMap, $UV, textureBias).$CH;
#endif

#ifdef MAPVERTEX
Expand Down
5 changes: 5 additions & 0 deletions src/graphics/webgl/webgl-graphics-device.js
Original file line number Diff line number Diff line change
Expand Up @@ -568,6 +568,9 @@ class WebglGraphicsDevice extends GraphicsDevice {
}

this.constantTexSource = this.scope.resolve("source");
this.textureBias = this.scope.resolve("textureBias");

this.textureBias.setValue(0.0);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should have a public API for this. Ideally the viewer code you test it on would not do
this.scope.resolve("textureBias").SetValue(...);

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This transform feedback example already uses this API. This uniform management is long overdue a redesign.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's true, but transform feedback example is accessing this API to set uniforms in its own shader.
But here we're adding new functionality to the engine that the Viewer wants to use .. and that should be different API, allowing us to even rename the variable later if needed. Maybe `Scene.standardMaterialTextureBias' or a shorter version of it? Something we publicly document.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd prefer something like Scene#textureBias and then potentially add Texture#bias which would take precedence.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could be a scene setting... though I would prefer creating and exposing public API in followup PR.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fine by me.


if (this.extTextureFloat) {
if (this.webgl2) {
Expand Down Expand Up @@ -763,6 +766,7 @@ class WebglGraphicsDevice extends GraphicsDevice {
// Note that Firefox exposes EXT_disjoint_timer_query under WebGL2 rather than
// EXT_disjoint_timer_query_webgl2
this.extDisjointTimerQuery = getExtension('EXT_disjoint_timer_query_webgl2', 'EXT_disjoint_timer_query');
this.extDepthTexture = true;
} else {
this.extBlendMinmax = getExtension("EXT_blend_minmax");
this.extDrawBuffers = getExtension('EXT_draw_buffers');
Expand Down Expand Up @@ -791,6 +795,7 @@ class WebglGraphicsDevice extends GraphicsDevice {
}
this.extColorBufferFloat = null;
this.extDisjointTimerQuery = null;
this.extDepthTexture = gl.getExtension('WEBGL_depth_texture');
slimbuck marked this conversation as resolved.
Show resolved Hide resolved
}

this.extDebugRendererInfo = getExtension('WEBGL_debug_renderer_info');
Expand Down
6 changes: 3 additions & 3 deletions src/resources/parser/glb-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -899,7 +899,7 @@ const createMaterial = function (gltfMaterial, textures) {
"#endif",
"",
"#ifdef MAPTEXTURE",
" dGlossiness *= texture2D(texture_glossMap, $UV).$CH;",
" dGlossiness *= texture2D(texture_glossMap, $UV, textureBias).$CH;",
"#endif",
"",
"#ifdef MAPVERTEX",
Expand Down Expand Up @@ -929,7 +929,7 @@ const createMaterial = function (gltfMaterial, textures) {
" #endif",
"",
" #ifdef MAPTEXTURE",
" vec3 srgb = texture2D(texture_specularMap, $UV).$CH;",
" vec3 srgb = texture2D(texture_specularMap, $UV, textureBias).$CH;",
" dSpecularity *= vec3(pow(srgb.r, 2.2), pow(srgb.g, 2.2), pow(srgb.b, 2.2));",
" #endif",
"",
Expand All @@ -956,7 +956,7 @@ const createMaterial = function (gltfMaterial, textures) {
"#endif",
"",
"#ifdef MAPTEXTURE",
" ccGlossiness *= texture2D(texture_clearCoatGlossMap, $UV).$CH;",
" ccGlossiness *= texture2D(texture_clearCoatGlossMap, $UV, textureBias).$CH;",
"#endif",
"",
"#ifdef MAPVERTEX",
Expand Down