From 2f5800a4ccc268507c4436265eea62de766d687d Mon Sep 17 00:00:00 2001 From: samcake Date: Tue, 23 Feb 2016 18:31:38 -0800 Subject: [PATCH] IMproving the shading model and the loading, added the roughness, needt to clean up --- libraries/fbx/src/FBXReader_Material.cpp | 2 +- libraries/model/src/model/Material.slh | 19 +++++++++++++ libraries/render-utils/src/DeferredBuffer.slh | 5 +++- .../render-utils/src/DeferredBufferWrite.slh | 8 ++---- .../render-utils/src/DeferredLighting.slh | 2 -- .../render-utils/src/MaterialTextures.slh | 24 ++++++++++++---- .../render-utils/src/MeshPartPayload.cpp | 28 +++++++++---------- .../model_lightmap_normal_specular_map.slf | 6 ++-- .../src/model_lightmap_specular_map.slf | 2 +- .../src/model_normal_specular_map.slf | 19 +++++++++---- .../render-utils/src/model_specular_map.slf | 10 +++++-- libraries/render/src/render/ShapePipeline.cpp | 2 ++ libraries/render/src/render/ShapePipeline.h | 3 ++ 13 files changed, 89 insertions(+), 41 deletions(-) diff --git a/libraries/fbx/src/FBXReader_Material.cpp b/libraries/fbx/src/FBXReader_Material.cpp index cce2f2162a..2604d83423 100644 --- a/libraries/fbx/src/FBXReader_Material.cpp +++ b/libraries/fbx/src/FBXReader_Material.cpp @@ -152,6 +152,7 @@ void FBXReader::consolidateFBXMaterials() { glm::vec2 lightmapParams(0.f, 1.f); lightmapParams.x = _lightmapOffset; lightmapParams.y = _lightmapLevel; + FBXTexture ambientTexture; QString ambientTextureID = ambientTextures.value(material.materialID); if (_loadLightmaps && !ambientTextureID.isNull()) { @@ -179,7 +180,6 @@ void FBXReader::consolidateFBXMaterials() { // FIXME: Do not use the Specular Factor yet as some FBX models have it set to 0 // metallic *= material.specularFactor; material._material->setMetallic(metallic); - } if (material.opacity <= 0.0f) { diff --git a/libraries/model/src/model/Material.slh b/libraries/model/src/model/Material.slh index 02bd9568dc..ac5d750bdd 100644 --- a/libraries/model/src/model/Material.slh +++ b/libraries/model/src/model/Material.slh @@ -37,5 +37,24 @@ float getMaterialMetallic(Material m) { return m._fresnelMetallic.a; } float getMaterialShininess(Material m) { return 1.0 - getMaterialRoughness(m); } +int getMaterialKey(Material m) { return floatBitsToInt(m._spare.w); } + +const int EMISSIVE_VAL_BIT = 0x00000001; +const int ALBEDO_VAL_BIT = 0x00000002; +const int METALLIC_VAL_BIT = 0x00000004; +const int GLOSSY_VAL_BIT = 0x00000008; +const int TRANSPARENT_VAL_BIT = 0x00000010; + + +const int EMISSIVE_MAP_BIT = 0x00000020; +const int ALBEDO_MAP_BIT = 0x00000040; +const int METALLIC_MAP_BIT = 0x00000080; +const int ROUGHNESS_MAP_BIT = 0x00000100; +const int TRANSPARENT_MAP_BIT = 0x00000200; +const int NORMAL_MAP_BIT = 0x00000400; + +const int LIGHTMAP_MAP_BIT = 0x00000800; + + <@endif@> diff --git a/libraries/render-utils/src/DeferredBuffer.slh b/libraries/render-utils/src/DeferredBuffer.slh index fe9b1a2a47..f35fc12d1f 100755 --- a/libraries/render-utils/src/DeferredBuffer.slh +++ b/libraries/render-utils/src/DeferredBuffer.slh @@ -120,7 +120,9 @@ DeferredFragment unpackDeferredFragmentNoPosition(vec2 texcoord) { // } frag.metallic = frag.diffuseVal.a; - if (frag.metallic > 0.5) { + frag.diffuse = frag.diffuseVal.xyz; + frag.specular = vec3(frag.metallic); + /* if (frag.metallic > 0.5) { frag.diffuse = vec3(0); //frag.metallic = length(frag.specularVal); frag.specular = frag.diffuseVal.xyz; @@ -130,6 +132,7 @@ DeferredFragment unpackDeferredFragmentNoPosition(vec2 texcoord) { //frag.metallic = length(frag.specularVal); frag.specular = vec3(0.03); } + */ // frag.diffuse = frag.diffuseVal.xyz; //frag.metallic = length(frag.specularVal); //frag.specular = frag.specularVal.xyz; diff --git a/libraries/render-utils/src/DeferredBufferWrite.slh b/libraries/render-utils/src/DeferredBufferWrite.slh index 21d579cb3a..a92ce1d26a 100755 --- a/libraries/render-utils/src/DeferredBufferWrite.slh +++ b/libraries/render-utils/src/DeferredBufferWrite.slh @@ -50,8 +50,8 @@ void packDeferredFragment(vec3 normal, float alpha, vec3 albedo, float roughness if (alpha != 1.0) { discard; } - vec3 baseColor = ((metallic < 0.5) ? albedo : fresnel); - _fragColor0 = vec4(baseColor, metallic); + // vec3 baseColor = ((metallic < 0.5) ? albedo : fresnel); + _fragColor0 = vec4(albedo, metallic); _fragColor1 = vec4(bestFitNormal(normal), 0.5 * clamp(roughness, 0.0, 1.0)); _fragColor2 = vec4(fresnel, roughness); } @@ -60,9 +60,7 @@ void packDeferredFragmentLightmap(vec3 normal, float alpha, vec3 albedo, float r if (alpha != 1.0) { discard; } - - vec3 baseColor = ((metallic < 0.5) ? albedo : fresnel); - _fragColor0 = vec4(baseColor, metallic); + _fragColor0 = vec4(albedo, metallic); _fragColor1 = vec4(bestFitNormal(normal), 0.5 + 0.5 * clamp(roughness, 0.0, 1.0)); _fragColor2 = vec4(emissive, roughness); } diff --git a/libraries/render-utils/src/DeferredLighting.slh b/libraries/render-utils/src/DeferredLighting.slh index eb821cccac..ca3989256a 100755 --- a/libraries/render-utils/src/DeferredLighting.slh +++ b/libraries/render-utils/src/DeferredLighting.slh @@ -29,8 +29,6 @@ float specularDistribution(float roughness, vec3 normal, vec3 halfDir) { // Frag Shading returns the diffuse amount as W and the specular rgb as xyz vec4 evalPBRShading(vec3 fragNormal, vec3 fragLightDir, vec3 fragEyeDir, float metallic, vec3 fresnel, float roughness) { // Diffuse Lighting - //float diffuseDot = dot(fragNormal, fragLightDir); - //float facingLight = step(0.0, diffuseDot); float diffuse = clamp(dot(fragNormal, fragLightDir), 0.0, 1.0); // Specular Lighting diff --git a/libraries/render-utils/src/MaterialTextures.slh b/libraries/render-utils/src/MaterialTextures.slh index 47eed14a82..ab58b7cff5 100644 --- a/libraries/render-utils/src/MaterialTextures.slh +++ b/libraries/render-utils/src/MaterialTextures.slh @@ -20,14 +20,12 @@ vec4 fetchAlbedoMap(vec2 uv) { } <@endif@> - uniform sampler2D roughnessMap; float fetchRoughnessMap(vec2 uv) { return texture(roughnessMap, uv).r; } <@endif@> -!> <@if withNormal@> uniform sampler2D normalMap; @@ -38,8 +36,8 @@ vec3 fetchNormalMap(vec2 uv) { <@if withMetallic@> uniform sampler2D specularMap; -vec3 fetchMetallicMap(vec2 uv) { - return texture(specularMap, uv).rgb; +float fetchMetallicMap(vec2 uv) { + return texture(specularMap, uv).r; } <@endif@> @@ -51,13 +49,13 @@ vec3 fetchMetallicMap(vec2 uv) { vec4 <$albedo$> = fetchAlbedoMap(<$texcoord0$>); <@endif@> <@if roughness@> -float <$roughness$> = 1.0; //fetchRoughnessMap(<$texcoord0$>); + float <$roughness$> = fetchRoughnessMap(<$texcoord0$>); <@endif@> <@if normal@> vec3 <$normal$> = fetchNormalMap(<$texcoord0$>); <@endif@> <@if metallic@> - vec3 <$metallic$> = fetchMetallicMap(<$texcoord0$>); + float <$metallic$> = fetchMetallicMap(<$texcoord0$>); <@endif@> <@endfunc@> @@ -85,4 +83,18 @@ vec3 fetchLightmapMap(vec2 uv) { } <@endfunc@> + +<@func evalMaterialRoughness(fetchedRoughness, materialRoughness, matKey, roughness)@> +{ + <$roughness$> = (((<$matKey$> & ROUGHNESS_MAP_BIT) != 0) ? <$fetchedRoughness$> : <$materialRoughness$>); +} +<@endfunc@> + +<@func evalMaterialMetallic(fetchedMetallic, materialMetallic, matKey, metallic)@> +{ + <$metallic$> = (((<$matKey$> & METALLIC_MAP_BIT) != 0) ? <$fetchedMetallic$> : <$materialMetallic$>); +} +<@endfunc@> + + <@endif@> \ No newline at end of file diff --git a/libraries/render-utils/src/MeshPartPayload.cpp b/libraries/render-utils/src/MeshPartPayload.cpp index e15603cca5..ab2a969637 100644 --- a/libraries/render-utils/src/MeshPartPayload.cpp +++ b/libraries/render-utils/src/MeshPartPayload.cpp @@ -161,6 +161,20 @@ void MeshPartPayload::bindMaterial(gpu::Batch& batch, const ShapePipeline::Locat batch.setResourceTexture(ShapePipeline::Slot::ALBEDO_MAP, textureCache->getWhiteTexture()); } + // Roughness map + if (materialKey.isRoughnessMap()) { + auto roughnessMap = textureMaps[model::MaterialKey::ROUGHNESS_MAP]; + if (roughnessMap && roughnessMap->isDefined()) { + batch.setResourceTexture(ShapePipeline::Slot::ROUGHNESS_MAP, roughnessMap->getTextureView()); + + // texcoord are assumed to be the same has albedo + } else { + batch.setResourceTexture(ShapePipeline::Slot::ROUGHNESS_MAP, textureCache->getWhiteTexture()); + } + } else { + batch.setResourceTexture(ShapePipeline::Slot::ROUGHNESS_MAP, textureCache->getWhiteTexture()); + } + // Normal map if (materialKey.isNormalMap()) { auto normalMap = textureMaps[model::MaterialKey::NORMAL_MAP]; @@ -175,20 +189,6 @@ void MeshPartPayload::bindMaterial(gpu::Batch& batch, const ShapePipeline::Locat batch.setResourceTexture(ShapePipeline::Slot::NORMAL_MAP, nullptr); } - // Roughness map - /* if (materialKey.isRoughnessMap()) { - auto roughnessMap = textureMaps[model::MaterialKey::ROUGHNESS_MAP]; - if (roughnessMap && roughnessMap->isDefined()) { - batch.setResourceTexture(ShapePipeline::Slot::ROUGHNESS_MAP, roughnessMap->getTextureView()); - - // texcoord are assumed to be the same has albedo - } else { - batch.setResourceTexture(ShapePipeline::Slot::ROUGHNESS_MAP, textureCache->getBlackTexture()); - } - } else { - batch.setResourceTexture(ShapePipeline::Slot::ROUGHNESS_MAP, nullptr); - }*/ - // Metallic map if (materialKey.isMetallicMap()) { auto specularMap = textureMaps[model::MaterialKey::METALLIC_MAP]; diff --git a/libraries/render-utils/src/model_lightmap_normal_specular_map.slf b/libraries/render-utils/src/model_lightmap_normal_specular_map.slf index 513670fe89..93bb3a793e 100755 --- a/libraries/render-utils/src/model_lightmap_normal_specular_map.slf +++ b/libraries/render-utils/src/model_lightmap_normal_specular_map.slf @@ -28,7 +28,7 @@ in vec3 _tangent; in vec3 _color; void main(void) { - <$fetchMaterialTextures(_texCoord0, albedo, roughness, normalTexel, specular)$> + <$fetchMaterialTextures(_texCoord0, albedo, roughness, normalTexel, metalllicTex)$> <$fetchMaterialLightmap(_texCoord1, lightmapVal)$> Material mat = getMaterial(); @@ -41,7 +41,7 @@ void main(void) { evalOpaqueFinalAlpha(getMaterialOpacity(mat), albedo.a), getMaterialAlbedo(mat) * albedo.rgb * _color, getMaterialRoughness(mat) * roughness, - getMaterialMetallic(mat), - specular, // no use of getMaterialFresnel(mat) + getMaterialMetallic(mat) * metalllicTex, + /*specular, // no use of */ getMaterialFresnel(mat), lightmapVal); } diff --git a/libraries/render-utils/src/model_lightmap_specular_map.slf b/libraries/render-utils/src/model_lightmap_specular_map.slf index 48db5a40dc..23d3a8e2b3 100755 --- a/libraries/render-utils/src/model_lightmap_specular_map.slf +++ b/libraries/render-utils/src/model_lightmap_specular_map.slf @@ -38,6 +38,6 @@ void main(void) { getMaterialAlbedo(mat) * albedo.rgb * _color, getMaterialRoughness(mat) * roughness, getMaterialMetallic(mat), - specular, // no use of getMaterialFresnel(mat) + /*specular, // no use of */getMaterialFresnel(mat), lightmapVal); } diff --git a/libraries/render-utils/src/model_normal_specular_map.slf b/libraries/render-utils/src/model_normal_specular_map.slf index c4d5b22efe..66fe0cd67e 100755 --- a/libraries/render-utils/src/model_normal_specular_map.slf +++ b/libraries/render-utils/src/model_normal_specular_map.slf @@ -27,19 +27,28 @@ in vec3 _color; void main(void) { - <$fetchMaterialTextures(_texCoord0, albedo, roughness, normalTexel, specular)$> + <$fetchMaterialTextures(_texCoord0, albedo, roughnessTex, normalTexel, metallicTex)$> Material mat = getMaterial(); + int matKey = getMaterialKey(mat); + + float roughness = getMaterialRoughness(mat); + <$evalMaterialRoughness(roughnessTex, roughness, matKey, roughness)$>; + vec3 viewNormal; <$tangentToViewSpace(normalTexel, _normal, _tangent, viewNormal)$> - + + float metallic = getMaterialMetallic(mat); + <$evalMaterialMetallic(metallicTex, metallic, matKey, metallic)$>; + + packDeferredFragment( normalize(viewNormal.xyz), evalOpaqueFinalAlpha(getMaterialOpacity(mat), albedo.a), getMaterialAlbedo(mat) * albedo.rgb * _color, - getMaterialRoughness(mat) * roughness, - getMaterialMetallic(mat), - vec3(specular) //getMaterialFresnel(mat) + roughness, + metallic, + /*vec3(specular) //*/getMaterialFresnel(mat) ); } diff --git a/libraries/render-utils/src/model_specular_map.slf b/libraries/render-utils/src/model_specular_map.slf index 99f0364520..cfdf023ae3 100755 --- a/libraries/render-utils/src/model_specular_map.slf +++ b/libraries/render-utils/src/model_specular_map.slf @@ -27,16 +27,20 @@ in vec3 _color; void main(void) { - <$fetchMaterialTextures(_texCoord0, albedo, roughness, _SCRIBE_NULL, specular)$> + <$fetchMaterialTextures(_texCoord0, albedo, roughness, _SCRIBE_NULL, metallicTex)$> Material mat = getMaterial(); + int matKey = getMaterialKey(mat); + + float metallic = getMaterialMetallic(mat); + <$evalMaterialMetallic(metallicTex, metallic, matKey, metallic)$>; packDeferredFragment( normalize(_normal), evalOpaqueFinalAlpha(getMaterialOpacity(mat), albedo.a), getMaterialAlbedo(mat) * albedo.rgb * _color, getMaterialRoughness(mat) * roughness, - getMaterialMetallic(mat), - vec3(specular) //getMaterialFresnel(mat) + metallic, + /*vec3(specular) //*/ getMaterialFresnel(mat) ); } diff --git a/libraries/render/src/render/ShapePipeline.cpp b/libraries/render/src/render/ShapePipeline.cpp index 1103507bf5..68c0b94766 100644 --- a/libraries/render/src/render/ShapePipeline.cpp +++ b/libraries/render/src/render/ShapePipeline.cpp @@ -54,6 +54,7 @@ void ShapePlumber::addPipeline(const Filter& filter, const gpu::ShaderPointer& p slotBindings.insert(gpu::Shader::Binding(std::string("skinClusterBuffer"), Slot::SKINNING_GPU)); slotBindings.insert(gpu::Shader::Binding(std::string("materialBuffer"), Slot::MATERIAL_GPU)); slotBindings.insert(gpu::Shader::Binding(std::string("albedoMap"), Slot::ALBEDO_MAP)); + slotBindings.insert(gpu::Shader::Binding(std::string("roughnessMap"), Slot::ROUGHNESS_MAP)); slotBindings.insert(gpu::Shader::Binding(std::string("normalMap"), Slot::NORMAL_MAP)); slotBindings.insert(gpu::Shader::Binding(std::string("specularMap"), Slot::SPECULAR_MAP)); slotBindings.insert(gpu::Shader::Binding(std::string("emissiveMap"), Slot::LIGHTMAP_MAP)); @@ -67,6 +68,7 @@ void ShapePlumber::addPipeline(const Filter& filter, const gpu::ShaderPointer& p locations->emissiveParams = program->getUniforms().findLocation("emissiveParams"); locations->normalFittingMapUnit = program->getTextures().findLocation("normalFittingMap"); locations->albedoTextureUnit = program->getTextures().findLocation("albedoMap"); + locations->roughnessTextureUnit = program->getTextures().findLocation("roughnessMap"); locations->normalTextureUnit = program->getTextures().findLocation("normalMap"); locations->specularTextureUnit = program->getTextures().findLocation("specularMap"); locations->emissiveTextureUnit = program->getTextures().findLocation("emissiveMap"); diff --git a/libraries/render/src/render/ShapePipeline.h b/libraries/render/src/render/ShapePipeline.h index 4b6247aa54..b4fb622b39 100644 --- a/libraries/render/src/render/ShapePipeline.h +++ b/libraries/render/src/render/ShapePipeline.h @@ -199,6 +199,8 @@ public: static const int NORMAL_MAP = 1; static const int SPECULAR_MAP = 2; static const int LIGHTMAP_MAP = 3; + static const int ROUGHNESS_MAP = 4; + static const int LIGHT_BUFFER = 4; static const int NORMAL_FITTING_MAP = 10; }; @@ -208,6 +210,7 @@ public: int texcoordMatrices; int albedoTextureUnit; int normalTextureUnit; + int roughnessTextureUnit; int specularTextureUnit; int emissiveTextureUnit; int emissiveParams;