Support red-green normal maps

pull/3235/head
Alexei Kotov 9 months ago
parent 819aace891
commit 58afe1ba23

@ -152,6 +152,7 @@ CSMWorld::Data::Data(ToUTF8::FromType encoding, const Files::PathContainer& data
mResourceSystem
= std::make_unique<Resource::ResourceSystem>(mVFS.get(), expiryDelay, &mEncoder.getStatelessEncoder());
// FIXME: this is severely out of date (see #7595)
Shader::ShaderManager::DefineMap defines
= mResourceSystem->getSceneManager()->getShaderManager().getGlobalDefines();
Shader::ShaderManager::DefineMap shadowDefines = SceneUtil::ShadowManager::getShadowsDisabledDefines();

@ -446,6 +446,7 @@ namespace MWRender
globalDefines["useOVR_multiview"] = "0";
globalDefines["numViews"] = "1";
globalDefines["disableNormals"] = "1";
globalDefines["reconstructNormalZ"] = "0";
for (auto itr = lightDefines.begin(); itr != lightDefines.end(); itr++)
globalDefines[itr->first] = itr->second;

@ -78,8 +78,7 @@ namespace Resource
}
break;
}
// not bothering with checks for other compression formats right now, we are unlikely to ever use those
// anyway
// not bothering with checks for other compression formats right now
default:
return true;
}

@ -184,6 +184,7 @@ namespace Shader
, mAdditiveBlending(false)
, mDiffuseHeight(false)
, mNormalHeight(false)
, mReconstructNormalZ(false)
, mTexStageRequiringTangents(-1)
, mSoftParticles(false)
, mNode(nullptr)
@ -429,6 +430,7 @@ namespace Shader
normalMapTex->setFilter(osg::Texture::MAG_FILTER, diffuseMap->getFilter(osg::Texture::MAG_FILTER));
normalMapTex->setMaxAnisotropy(diffuseMap->getMaxAnisotropy());
normalMapTex->setName("normalMap");
normalMap = normalMapTex;
int unit = texAttributes.size();
if (!writableStateSet)
@ -440,6 +442,23 @@ namespace Shader
mRequirements.back().mNormalHeight = normalHeight;
}
}
if (normalMap != nullptr && normalMap->getImage(0))
{
// Special handling for red-green normal maps (e.g. BC5 or R8G8).
switch (normalMap->getImage(0)->getPixelFormat())
{
case GL_RG:
case GL_RG_INTEGER:
case GL_COMPRESSED_RED_GREEN_RGTC2_EXT:
case GL_COMPRESSED_SIGNED_RED_GREEN_RGTC2_EXT:
{
mRequirements.back().mReconstructNormalZ = true;
mRequirements.back().mNormalHeight = false;
}
}
}
if (mAutoUseSpecularMaps && diffuseMap != nullptr && specularMap == nullptr && diffuseMap->getImage(0))
{
std::string specularMapFileName = diffuseMap->getImage(0)->getFileName();
@ -629,6 +648,7 @@ namespace Shader
defineMap["diffuseParallax"] = reqs.mDiffuseHeight ? "1" : "0";
defineMap["parallax"] = reqs.mNormalHeight ? "1" : "0";
defineMap["reconstructNormalZ"] = reqs.mReconstructNormalZ ? "1" : "0";
writableStateSet->addUniform(new osg::Uniform("colorMode", reqs.mColorMode));
addedState->addUniform("colorMode");

@ -110,6 +110,7 @@ namespace Shader
bool mDiffuseHeight; // true if diffuse map has height info in alpha channel
bool mNormalHeight; // true if normal map has height info in alpha channel
bool mReconstructNormalZ; // used for red-green normal maps (e.g. BC5)
// -1 == no tangents required
int mTexStageRequiringTangents;

@ -77,6 +77,9 @@ void main()
vec3 specularColor = getSpecularColor().xyz;
#if @normalMap
vec4 normalTex = texture2D(normalMap, normalMapUV);
#if @reconstructNormalZ
normalTex.z = sqrt(1.0 - dot(normalTex.xy, normalTex.xy));
#endif
vec3 viewNormal = normalToView(normalTex.xyz * 2.0 - 1.0);
specularColor *= normalTex.a;
#else

@ -59,7 +59,11 @@ void main()
gl_FragData[0].a = alphaTest(gl_FragData[0].a, alphaRef);
#if @normalMap
vec3 viewNormal = normalToView(texture2D(normalMap, normalMapUV).xyz * 2.0 - 1.0);
vec4 normalTex = texture2D(normalMap, normalMapUV);
#if @reconstructNormalZ
normalTex.z = sqrt(1.0 - dot(normalTex.xy, normalTex.xy));
#endif
vec3 viewNormal = normalToView(normalTex.xyz * 2.0 - 1.0);
#else
vec3 viewNormal = normalToView(normalize(passNormal));
#endif

@ -167,7 +167,11 @@ vec2 screenCoords = gl_FragCoord.xy / screenRes;
gl_FragData[0].a = alphaTest(gl_FragData[0].a, alphaRef);
#if @normalMap
vec3 viewNormal = normalToView(texture2D(normalMap, normalMapUV + offset).xyz * 2.0 - 1.0);
vec4 normalTex = texture2D(normalMap, normalMapUV + offset);
#if @reconstructNormalZ
normalTex.z = sqrt(1.0 - dot(normalTex.xy, normalTex.xy));
#endif
vec3 viewNormal = normalToView(normalTex.xyz * 2.0 - 1.0);
#else
vec3 viewNormal = normalize(gl_NormalMatrix * passNormal);
#endif

@ -63,7 +63,11 @@ void main()
#endif
#if @normalMap
vec3 viewNormal = normalToView(texture2D(normalMap, adjustedUV).xyz * 2.0 - 1.0);
vec4 normalTex = texture2D(normalMap, adjustedUV);
#if @reconstructNormalZ
normalTex.z = sqrt(1.0 - dot(normalTex.xy, normalTex.xy));
#endif
vec3 viewNormal = normalToView(normalTex.xyz * 2.0 - 1.0);
#else
vec3 viewNormal = normalize(gl_NormalMatrix * passNormal);
#endif

Loading…
Cancel
Save