From 0ad7bacf02f359929c81e56afe72bb212c2fcb85 Mon Sep 17 00:00:00 2001 From: samcake Date: Mon, 15 Feb 2016 21:50:10 -0800 Subject: [PATCH 01/24] Changing diffuse to albedo and breaking everything... --- interface/src/ModelPackager.cpp | 6 +-- libraries/fbx/src/FBXReader.h | 2 +- libraries/fbx/src/FBXReader_Material.cpp | 4 +- libraries/fbx/src/OBJReader.cpp | 4 +- .../src/model-networking/ModelCache.cpp | 36 +++++++++--------- .../src/model-networking/ModelCache.h | 4 +- libraries/model/src/model/Material.cpp | 6 +-- libraries/model/src/model/Material.h | 36 +++++++++--------- libraries/model/src/model/Material.slh | 38 ++----------------- libraries/model/src/model/TextureMap.h | 2 +- .../render-utils/src/DebugDeferredBuffer.cpp | 14 +++---- .../render-utils/src/DebugDeferredBuffer.h | 2 +- .../src/DeferredLightingEffect.cpp | 6 +-- libraries/render-utils/src/GeometryCache.cpp | 4 +- .../render-utils/src/MeshPartPayload.cpp | 18 ++++----- libraries/render-utils/src/Model.cpp | 2 +- .../render-utils/src/RenderPipelines.cpp | 6 +-- libraries/render-utils/src/model.slf | 2 +- libraries/render-utils/src/model_emissive.slf | 2 +- libraries/render-utils/src/model_lightmap.slf | 2 +- .../src/model_lightmap_normal_map.slf | 2 +- .../model_lightmap_normal_specular_map.slf | 2 +- .../src/model_lightmap_specular_map.slf | 2 +- .../render-utils/src/model_normal_map.slf | 2 +- .../src/model_normal_specular_map.slf | 2 +- .../render-utils/src/model_specular_map.slf | 12 +++--- .../render-utils/src/model_translucent.slf | 8 ++-- .../src/model_translucent_emissive.slf | 8 ++-- .../src/skin_model_normal_map.slv | 2 +- libraries/render/src/render/ShapePipeline.cpp | 4 +- libraries/render/src/render/ShapePipeline.h | 4 +- 31 files changed, 106 insertions(+), 138 deletions(-) diff --git a/interface/src/ModelPackager.cpp b/interface/src/ModelPackager.cpp index 64ab76c64c..fa87a5f657 100644 --- a/interface/src/ModelPackager.cpp +++ b/interface/src/ModelPackager.cpp @@ -342,9 +342,9 @@ void ModelPackager::populateBasicMapping(QVariantHash& mapping, QString filename void ModelPackager::listTextures() { _textures.clear(); foreach (const FBXMaterial mat, _geometry->materials) { - if (!mat.diffuseTexture.filename.isEmpty() && mat.diffuseTexture.content.isEmpty() && - !_textures.contains(mat.diffuseTexture.filename)) { - _textures << mat.diffuseTexture.filename; + if (!mat.albedoTexture.filename.isEmpty() && mat.albedoTexture.content.isEmpty() && + !_textures.contains(mat.albedoTexture.filename)) { + _textures << mat.albedoTexture.filename; } if (!mat.normalTexture.filename.isEmpty() && mat.normalTexture.content.isEmpty() && !_textures.contains(mat.normalTexture.filename)) { diff --git a/libraries/fbx/src/FBXReader.h b/libraries/fbx/src/FBXReader.h index 51cb5baf9f..6dc8a31bc7 100644 --- a/libraries/fbx/src/FBXReader.h +++ b/libraries/fbx/src/FBXReader.h @@ -151,7 +151,7 @@ public: QString materialID; model::MaterialPointer _material; - FBXTexture diffuseTexture; + FBXTexture albedoTexture; FBXTexture opacityTexture; FBXTexture normalTexture; FBXTexture specularTexture; diff --git a/libraries/fbx/src/FBXReader_Material.cpp b/libraries/fbx/src/FBXReader_Material.cpp index e947a0356e..b82d4c0877 100644 --- a/libraries/fbx/src/FBXReader_Material.cpp +++ b/libraries/fbx/src/FBXReader_Material.cpp @@ -79,7 +79,7 @@ void FBXReader::consolidateFBXMaterials() { } } - material.diffuseTexture = diffuseTexture; + material.albedoTexture = diffuseTexture; detectDifferentUVs = (diffuseTexture.texcoordSet != 0) || (!diffuseTexture.transform.isIdentity()); } @@ -140,7 +140,7 @@ void FBXReader::consolidateFBXMaterials() { auto diffuse = material.diffuseColor; // FIXME: Do not use the Diffuse Factor yet as some FBX models have it set to 0 // diffuse *= material.diffuseFactor; - material._material->setDiffuse(diffuse); + material._material->setAlbedo(diffuse); float metallic = std::max(material.specularColor.x, std::max(material.specularColor.y, material.specularColor.z)); // FIXME: Do not use the Specular Factor yet as some FBX models have it set to 0 diff --git a/libraries/fbx/src/OBJReader.cpp b/libraries/fbx/src/OBJReader.cpp index 11164afbbb..3286e5e974 100644 --- a/libraries/fbx/src/OBJReader.cpp +++ b/libraries/fbx/src/OBJReader.cpp @@ -555,11 +555,11 @@ FBXGeometry* OBJReader::readOBJ(QByteArray& model, const QVariantHash& mapping, model::MaterialPointer modelMaterial = fbxMaterial._material; if (!objMaterial.diffuseTextureFilename.isEmpty()) { - fbxMaterial.diffuseTexture.filename = objMaterial.diffuseTextureFilename; + fbxMaterial.albedoTexture.filename = objMaterial.diffuseTextureFilename; } modelMaterial->setEmissive(fbxMaterial.emissiveColor); - modelMaterial->setDiffuse(fbxMaterial.diffuseColor); + modelMaterial->setAlbedo(fbxMaterial.diffuseColor); modelMaterial->setMetallic(glm::length(fbxMaterial.specularColor)); modelMaterial->setGloss(fbxMaterial.shininess); diff --git a/libraries/model-networking/src/model-networking/ModelCache.cpp b/libraries/model-networking/src/model-networking/ModelCache.cpp index 2f8e04890c..6551ca5a60 100644 --- a/libraries/model-networking/src/model-networking/ModelCache.cpp +++ b/libraries/model-networking/src/model-networking/ModelCache.cpp @@ -136,7 +136,7 @@ bool NetworkGeometry::isLoadedWithTextures() const { if (!_isLoadedWithTextures) { for (auto&& material : _materials) { - if ((material->diffuseTexture && !material->diffuseTexture->isLoaded()) || + if ((material->albedoTexture && !material->albedoTexture->isLoaded()) || (material->normalTexture && !material->normalTexture->isLoaded()) || (material->specularTexture && !material->specularTexture->isLoaded()) || (material->emissiveTexture && !material->emissiveTexture->isLoaded())) { @@ -154,15 +154,15 @@ void NetworkGeometry::setTextureWithNameToURL(const QString& name, const QUrl& u for (auto&& material : _materials) { auto networkMaterial = material->_material; auto oldTextureMaps = networkMaterial->getTextureMaps(); - if (material->diffuseTextureName == name) { - material->diffuseTexture = textureCache->getTexture(url, DEFAULT_TEXTURE); + if (material->albedoTextureName == name) { + material->albedoTexture = textureCache->getTexture(url, DEFAULT_TEXTURE); - auto diffuseMap = model::TextureMapPointer(new model::TextureMap()); - diffuseMap->setTextureSource(material->diffuseTexture->_textureSource); - diffuseMap->setTextureTransform( - oldTextureMaps[model::MaterialKey::DIFFUSE_MAP]->getTextureTransform()); + auto albedoMap = model::TextureMapPointer(new model::TextureMap()); + albedoMap->setTextureSource(material->albedoTexture->_textureSource); + albedoMap->setTextureTransform( + oldTextureMaps[model::MaterialKey::ALBEDO_MAP]->getTextureTransform()); - networkMaterial->setTextureMap(model::MaterialKey::DIFFUSE_MAP, diffuseMap); + networkMaterial->setTextureMap(model::MaterialKey::ALBEDO_MAP, albedoMap); } else if (material->normalTextureName == name) { material->normalTexture = textureCache->getTexture(url); @@ -200,9 +200,9 @@ void NetworkGeometry::setTextureWithNameToURL(const QString& name, const QUrl& u QStringList NetworkGeometry::getTextureNames() const { QStringList result; for (auto&& material : _materials) { - if (!material->diffuseTextureName.isEmpty() && material->diffuseTexture) { - QString textureURL = material->diffuseTexture->getURL().toString(); - result << material->diffuseTextureName + ":\"" + textureURL + "\""; + if (!material->albedoTextureName.isEmpty() && material->albedoTexture) { + QString textureURL = material->albedoTexture->getURL().toString(); + result << material->albedoTextureName + ":\"" + textureURL + "\""; } if (!material->normalTextureName.isEmpty() && material->normalTexture) { @@ -310,15 +310,15 @@ static NetworkMaterial* buildNetworkMaterial(const FBXMaterial& material, const networkMaterial->_material = material._material; - if (!material.diffuseTexture.filename.isEmpty()) { - networkMaterial->diffuseTexture = textureCache->getTexture(textureBaseUrl.resolved(QUrl(material.diffuseTexture.filename)), DEFAULT_TEXTURE, material.diffuseTexture.content); - networkMaterial->diffuseTextureName = material.diffuseTexture.name; + if (!material.albedoTexture.filename.isEmpty()) { + networkMaterial->albedoTexture = textureCache->getTexture(textureBaseUrl.resolved(QUrl(material.albedoTexture.filename)), DEFAULT_TEXTURE, material.albedoTexture.content); + networkMaterial->albedoTextureName = material.albedoTexture.name; - auto diffuseMap = model::TextureMapPointer(new model::TextureMap()); - diffuseMap->setTextureSource(networkMaterial->diffuseTexture->_textureSource); - diffuseMap->setTextureTransform(material.diffuseTexture.transform); + auto albedoMap = model::TextureMapPointer(new model::TextureMap()); + albedoMap->setTextureSource(networkMaterial->albedoTexture->_textureSource); + albedoMap->setTextureTransform(material.albedoTexture.transform); - material._material->setTextureMap(model::MaterialKey::DIFFUSE_MAP, diffuseMap); + material._material->setTextureMap(model::MaterialKey::ALBEDO_MAP, albedoMap); } if (!material.normalTexture.filename.isEmpty()) { networkMaterial->normalTexture = textureCache->getTexture(textureBaseUrl.resolved(QUrl(material.normalTexture.filename)), (material.normalTexture.isBumpmap ? BUMP_TEXTURE : NORMAL_TEXTURE), material.normalTexture.content); diff --git a/libraries/model-networking/src/model-networking/ModelCache.h b/libraries/model-networking/src/model-networking/ModelCache.h index b2d81c5900..2cb67e7e87 100644 --- a/libraries/model-networking/src/model-networking/ModelCache.h +++ b/libraries/model-networking/src/model-networking/ModelCache.h @@ -174,8 +174,8 @@ public: class NetworkMaterial { public: model::MaterialPointer _material; - QString diffuseTextureName; - QSharedPointer diffuseTexture; + QString albedoTextureName; + QSharedPointer albedoTexture; QString normalTextureName; QSharedPointer normalTexture; QString specularTextureName; diff --git a/libraries/model/src/model/Material.cpp b/libraries/model/src/model/Material.cpp index 1d0f6ee5d9..95f540e762 100755 --- a/libraries/model/src/model/Material.cpp +++ b/libraries/model/src/model/Material.cpp @@ -44,9 +44,9 @@ Material& Material::operator= (const Material& material) { Material::~Material() { } -void Material::setDiffuse(const Color& diffuse, bool isSRGB) { - _key.setDiffuse(glm::any(glm::greaterThan(diffuse, Color(0.0f)))); - _schemaBuffer.edit()._diffuse = (isSRGB ? ColorUtils::toLinearVec3(diffuse) : diffuse); +void Material::setAlbedo(const Color& albedo, bool isSRGB) { + _key.setAlbedo(glm::any(glm::greaterThan(albedo, Color(0.0f)))); + _schemaBuffer.edit()._albedo = (isSRGB ? ColorUtils::toLinearVec3(albedo) : albedo); } void Material::setMetallic(float metallic) { diff --git a/libraries/model/src/model/Material.h b/libraries/model/src/model/Material.h index cb5a285dba..d2c7a04683 100755 --- a/libraries/model/src/model/Material.h +++ b/libraries/model/src/model/Material.h @@ -28,13 +28,13 @@ class MaterialKey { public: enum FlagBit { EMISSIVE_VAL_BIT = 0, - DIFFUSE_VAL_BIT, + ALBEDO_VAL_BIT, METALLIC_VAL_BIT, GLOSS_VAL_BIT, TRANSPARENT_VAL_BIT, EMISSIVE_MAP_BIT, - DIFFUSE_MAP_BIT, + ALBEDO_MAP_BIT, METALLIC_MAP_BIT, GLOSS_MAP_BIT, TRANSPARENT_MAP_BIT, @@ -47,7 +47,7 @@ public: enum MapChannel { EMISSIVE_MAP = 0, - DIFFUSE_MAP, + ALBEDO_MAP, METALLIC_MAP, GLOSS_MAP, TRANSPARENT_MAP, @@ -71,13 +71,13 @@ public: MaterialKey build() const { return MaterialKey(_flags); } Builder& withEmissive() { _flags.set(EMISSIVE_VAL_BIT); return (*this); } - Builder& withDiffuse() { _flags.set(DIFFUSE_VAL_BIT); return (*this); } + Builder& withAlbedo() { _flags.set(ALBEDO_VAL_BIT); return (*this); } Builder& withMetallic() { _flags.set(METALLIC_VAL_BIT); return (*this); } Builder& withGloss() { _flags.set(GLOSS_VAL_BIT); return (*this); } Builder& withTransparent() { _flags.set(TRANSPARENT_VAL_BIT); return (*this); } Builder& withEmissiveMap() { _flags.set(EMISSIVE_MAP_BIT); return (*this); } - Builder& withDiffuseMap() { _flags.set(DIFFUSE_MAP_BIT); return (*this); } + Builder& withAlbedoMap() { _flags.set(ALBEDO_MAP_BIT); return (*this); } Builder& withMetallicMap() { _flags.set(METALLIC_MAP_BIT); return (*this); } Builder& withGlossMap() { _flags.set(GLOSS_MAP_BIT); return (*this); } Builder& withTransparentMap() { _flags.set(TRANSPARENT_MAP_BIT); return (*this); } @@ -86,7 +86,7 @@ public: Builder& withLightmapMap() { _flags.set(LIGHTMAP_MAP_BIT); return (*this); } // Convenient standard keys that we will keep on using all over the place - static MaterialKey opaqueDiffuse() { return Builder().withDiffuse().build(); } + static MaterialKey opaqueAlbedo() { return Builder().withAlbedo().build(); } }; void setEmissive(bool value) { _flags.set(EMISSIVE_VAL_BIT, value); } @@ -95,11 +95,11 @@ public: void setEmissiveMap(bool value) { _flags.set(EMISSIVE_MAP_BIT, value); } bool isEmissiveMap() const { return _flags[EMISSIVE_MAP_BIT]; } - void setDiffuse(bool value) { _flags.set(DIFFUSE_VAL_BIT, value); } - bool isDiffuse() const { return _flags[DIFFUSE_VAL_BIT]; } + void setAlbedo(bool value) { _flags.set(ALBEDO_VAL_BIT, value); } + bool isAlbedo() const { return _flags[ALBEDO_VAL_BIT]; } - void setDiffuseMap(bool value) { _flags.set(DIFFUSE_MAP_BIT, value); } - bool isDiffuseMap() const { return _flags[DIFFUSE_MAP_BIT]; } + void setAlbedoMap(bool value) { _flags.set(ALBEDO_MAP_BIT, value); } + bool isAlbedoMap() const { return _flags[ALBEDO_MAP_BIT]; } void setMetallic(bool value) { _flags.set(METALLIC_VAL_BIT, value); } bool isMetallic() const { return _flags[METALLIC_VAL_BIT]; } @@ -154,11 +154,11 @@ public: Builder& withoutEmissiveMap() { _value.reset(MaterialKey::EMISSIVE_MAP_BIT); _mask.set(MaterialKey::EMISSIVE_MAP_BIT); return (*this); } Builder& withEmissiveMap() { _value.set(MaterialKey::EMISSIVE_MAP_BIT); _mask.set(MaterialKey::EMISSIVE_MAP_BIT); return (*this); } - Builder& withoutDiffuse() { _value.reset(MaterialKey::DIFFUSE_VAL_BIT); _mask.set(MaterialKey::DIFFUSE_VAL_BIT); return (*this); } - Builder& withDiffuse() { _value.set(MaterialKey::DIFFUSE_VAL_BIT); _mask.set(MaterialKey::DIFFUSE_VAL_BIT); return (*this); } + Builder& withoutAlbedo() { _value.reset(MaterialKey::ALBEDO_VAL_BIT); _mask.set(MaterialKey::ALBEDO_VAL_BIT); return (*this); } + Builder& withAlbedo() { _value.set(MaterialKey::ALBEDO_VAL_BIT); _mask.set(MaterialKey::ALBEDO_VAL_BIT); return (*this); } - Builder& withoutDiffuseMap() { _value.reset(MaterialKey::DIFFUSE_MAP_BIT); _mask.set(MaterialKey::DIFFUSE_MAP_BIT); return (*this); } - Builder& withDiffuseMap() { _value.set(MaterialKey::DIFFUSE_MAP_BIT); _mask.set(MaterialKey::DIFFUSE_MAP_BIT); return (*this); } + Builder& withoutAlbedoMap() { _value.reset(MaterialKey::ALBEDO_MAP_BIT); _mask.set(MaterialKey::ALBEDO_MAP_BIT); return (*this); } + Builder& withAlbedoMap() { _value.set(MaterialKey::ALBEDO_MAP_BIT); _mask.set(MaterialKey::ALBEDO_MAP_BIT); return (*this); } Builder& withoutMetallic() { _value.reset(MaterialKey::METALLIC_VAL_BIT); _mask.set(MaterialKey::METALLIC_VAL_BIT); return (*this); } Builder& withMetallic() { _value.set(MaterialKey::METALLIC_VAL_BIT); _mask.set(MaterialKey::METALLIC_VAL_BIT); return (*this); } @@ -185,7 +185,7 @@ public: Builder& withLightmapMap() { _value.set(MaterialKey::LIGHTMAP_MAP_BIT); _mask.set(MaterialKey::LIGHTMAP_MAP_BIT); return (*this); } // Convenient standard keys that we will keep on using all over the place - static MaterialFilter opaqueDiffuse() { return Builder().withDiffuse().withoutTransparent().build(); } + static MaterialFilter opaqueAlbedo() { return Builder().withAlbedo().withoutTransparent().build(); } }; // Item Filter operator testing if a key pass the filter @@ -223,8 +223,8 @@ public: void setEmissive(const Color& emissive, bool isSRGB = true); Color getEmissive(bool SRGB = true) const { return (SRGB ? ColorUtils::toGamma22Vec3(_schemaBuffer.get()._emissive) : _schemaBuffer.get()._emissive); } - void setDiffuse(const Color& diffuse, bool isSRGB = true); - Color getDiffuse(bool SRGB = true) const { return (SRGB ? ColorUtils::toGamma22Vec3(_schemaBuffer.get()._diffuse) : _schemaBuffer.get()._diffuse); } + void setAlbedo(const Color& albedo, bool isSRGB = true); + Color getAlbedo(bool SRGB = true) const { return (SRGB ? ColorUtils::toGamma22Vec3(_schemaBuffer.get()._albedo) : _schemaBuffer.get()._albedo); } void setMetallic(float metallic); float getMetallic() const { return _schemaBuffer.get()._metallic.x; } @@ -239,7 +239,7 @@ public: class Schema { public: - glm::vec3 _diffuse{ 0.5f }; + glm::vec3 _albedo{ 0.5f }; float _opacity{1.f}; glm::vec3 _metallic{ 0.03f }; float _gloss{0.1f}; diff --git a/libraries/model/src/model/Material.slh b/libraries/model/src/model/Material.slh index f48bdfa8f1..1f9a290fe7 100644 --- a/libraries/model/src/model/Material.slh +++ b/libraries/model/src/model/Material.slh @@ -12,7 +12,7 @@ <@def MODEL_MATERIAL_SLH@> struct Material { - vec4 _diffuse; + vec4 _albedoOpacity; vec4 _specular; vec4 _emissive; vec4 _spare; @@ -26,40 +26,8 @@ Material getMaterial() { return _mat; } - 0.04045 - // constants: - // T = 0.04045 - // A = 1 / 1.055 = 0.94786729857 - // B = 0.055 * A = 0.05213270142 - // C = 1 / 12.92 = 0.0773993808 - // G = 2.4 - const float T = 0.04045; - const float A = 0.947867; - const float B = 0.052132; - const float C = 0.077399; - const float G = 2.4; - - if (cs > T) { - return pow((cs * A + B), G); - } else { - return cs * C; - } -} - -vec3 SRGBToLinear(vec3 srgb) { - return vec3(componentSRGBToLinear(srgb.x),componentSRGBToLinear(srgb.y),componentSRGBToLinear(srgb.z)); -} -vec3 getMaterialDiffuse(Material m) { return (gl_FragCoord.x < 800 ? SRGBToLinear(m._diffuse.rgb) : m._diffuse.rgb); } -*/!> - -float getMaterialOpacity(Material m) { return m._diffuse.a; } -vec3 getMaterialDiffuse(Material m) { return m._diffuse.rgb; } +float getMaterialOpacity(Material m) { return m._albedoOpacity.a; } +vec3 getMaterialAlbedo(Material m) { return m._albedoOpacity.rgb; } vec3 getMaterialSpecular(Material m) { return m._specular.rgb; } float getMaterialShininess(Material m) { return m._specular.a; } diff --git a/libraries/model/src/model/TextureMap.h b/libraries/model/src/model/TextureMap.h index 6bc5b8228c..4b625356e9 100755 --- a/libraries/model/src/model/TextureMap.h +++ b/libraries/model/src/model/TextureMap.h @@ -27,7 +27,7 @@ typedef glm::vec3 Color; class TextureUsage { public: gpu::Texture::Type _type{ gpu::Texture::TEX_2D }; - Material::MapFlags _materialUsage{ MaterialKey::DIFFUSE_MAP }; + Material::MapFlags _materialUsage{ MaterialKey::ALBEDO_MAP }; int _environmentUsage = 0; diff --git a/libraries/render-utils/src/DebugDeferredBuffer.cpp b/libraries/render-utils/src/DebugDeferredBuffer.cpp index 06d8709dc5..0be3ce422d 100644 --- a/libraries/render-utils/src/DebugDeferredBuffer.cpp +++ b/libraries/render-utils/src/DebugDeferredBuffer.cpp @@ -39,7 +39,7 @@ void DebugDeferredBufferConfig::setMode(int newMode) { } enum Slot { - Diffuse = 0, + Albedo = 0, Normal, Specular, Depth, @@ -52,9 +52,9 @@ enum Slot { -static const std::string DEFAULT_DIFFUSE_SHADER { +static const std::string DEFAULT_ALBEDO_SHADER { "vec4 getFragmentColor() {" - " return vec4(pow(texture(diffuseMap, uv).xyz, vec3(1.0 / 2.2)), 1.0);" + " return vec4(pow(texture(albedoMap, uv).xyz, vec3(1.0 / 2.2)), 1.0);" " }" }; @@ -144,8 +144,8 @@ DebugDeferredBuffer::DebugDeferredBuffer() { std::string DebugDeferredBuffer::getShaderSourceCode(Mode mode, std::string customFile) { switch (mode) { - case DiffuseMode: - return DEFAULT_DIFFUSE_SHADER; + case AlbedoMode: + return DEFAULT_ALBEDO_SHADER; case SpecularMode: return DEFAULT_SPECULAR_SHADER; case RoughnessMode: @@ -206,7 +206,7 @@ const gpu::PipelinePointer& DebugDeferredBuffer::getPipeline(Mode mode, std::str const auto program = gpu::Shader::createProgram(vs, ps); gpu::Shader::BindingSet slotBindings; - slotBindings.insert(gpu::Shader::Binding("diffuseMap", Diffuse)); + slotBindings.insert(gpu::Shader::Binding("albedoMap", Albedo)); slotBindings.insert(gpu::Shader::Binding("normalMap", Normal)); slotBindings.insert(gpu::Shader::Binding("specularMap", Specular)); slotBindings.insert(gpu::Shader::Binding("depthMap", Depth)); @@ -262,7 +262,7 @@ void DebugDeferredBuffer::run(const SceneContextPointer& sceneContext, const Ren batch.setPipeline(getPipeline(_mode, first)); - batch.setResourceTexture(Diffuse, framebufferCache->getDeferredColorTexture()); + batch.setResourceTexture(Albedo, framebufferCache->getDeferredColorTexture()); batch.setResourceTexture(Normal, framebufferCache->getDeferredNormalTexture()); batch.setResourceTexture(Specular, framebufferCache->getDeferredSpecularTexture()); batch.setResourceTexture(Depth, framebufferCache->getPrimaryDepthTexture()); diff --git a/libraries/render-utils/src/DebugDeferredBuffer.h b/libraries/render-utils/src/DebugDeferredBuffer.h index b31985d6a3..221de9a68e 100644 --- a/libraries/render-utils/src/DebugDeferredBuffer.h +++ b/libraries/render-utils/src/DebugDeferredBuffer.h @@ -47,7 +47,7 @@ protected: enum Mode : uint8_t { // Use Mode suffix to avoid collisions - DiffuseMode = 0, + AlbedoMode = 0, SpecularMode, RoughnessMode, NormalMode, diff --git a/libraries/render-utils/src/DeferredLightingEffect.cpp b/libraries/render-utils/src/DeferredLightingEffect.cpp index d0387cd3d4..d66c1ba2a3 100644 --- a/libraries/render-utils/src/DeferredLightingEffect.cpp +++ b/libraries/render-utils/src/DeferredLightingEffect.cpp @@ -525,7 +525,7 @@ static void loadLightProgram(const char* vertSource, const char* fragSource, boo gpu::ShaderPointer program = gpu::Shader::createProgram(VS, PS); gpu::Shader::BindingSet slotBindings; - slotBindings.insert(gpu::Shader::Binding(std::string("diffuseMap"), DEFERRED_BUFFER_COLOR_UNIT)); + slotBindings.insert(gpu::Shader::Binding(std::string("colorMap"), DEFERRED_BUFFER_COLOR_UNIT)); slotBindings.insert(gpu::Shader::Binding(std::string("normalMap"), DEFERRED_BUFFER_NORMAL_UNIT)); slotBindings.insert(gpu::Shader::Binding(std::string("specularMap"), DEFERRED_BUFFER_EMISSIVE_UNIT)); slotBindings.insert(gpu::Shader::Binding(std::string("depthMap"), DEFERRED_BUFFER_DEPTH_UNIT)); @@ -584,10 +584,10 @@ void DeferredLightingEffect::setAmbientLightMode(int preset) { } } -void DeferredLightingEffect::setGlobalLight(const glm::vec3& direction, const glm::vec3& diffuse, float intensity, float ambientIntensity) { +void DeferredLightingEffect::setGlobalLight(const glm::vec3& direction, const glm::vec3& color, float intensity, float ambientIntensity) { auto light = _allocatedLights.front(); light->setDirection(direction); - light->setColor(diffuse); + light->setColor(color); light->setIntensity(intensity); light->setAmbientIntensity(ambientIntensity); } diff --git a/libraries/render-utils/src/GeometryCache.cpp b/libraries/render-utils/src/GeometryCache.cpp index ed6b8f2f5b..34a02b51d2 100644 --- a/libraries/render-utils/src/GeometryCache.cpp +++ b/libraries/render-utils/src/GeometryCache.cpp @@ -504,7 +504,7 @@ GeometryCache::GeometryCache() : std::make_shared(getSimplePipeline(), nullptr, [](const render::ShapePipeline&, gpu::Batch& batch) { // Set the defaults needed for a simple program - batch.setResourceTexture(render::ShapePipeline::Slot::DIFFUSE_MAP, + batch.setResourceTexture(render::ShapePipeline::Slot::ALBEDO_MAP, DependencyManager::get()->getWhiteTexture()); batch.setResourceTexture(render::ShapePipeline::Slot::NORMAL_FITTING_MAP, DependencyManager::get()->getNormalFittingTexture()); @@ -1848,7 +1848,7 @@ void GeometryCache::bindSimpleProgram(gpu::Batch& batch, bool textured, bool cul // If not textured, set a default diffuse map if (!textured) { - batch.setResourceTexture(render::ShapePipeline::Slot::DIFFUSE_MAP, + batch.setResourceTexture(render::ShapePipeline::Slot::ALBEDO_MAP, DependencyManager::get()->getWhiteTexture()); } // Set a default normal map diff --git a/libraries/render-utils/src/MeshPartPayload.cpp b/libraries/render-utils/src/MeshPartPayload.cpp index c0003219a0..31f17c3bc2 100644 --- a/libraries/render-utils/src/MeshPartPayload.cpp +++ b/libraries/render-utils/src/MeshPartPayload.cpp @@ -145,20 +145,20 @@ void MeshPartPayload::bindMaterial(gpu::Batch& batch, const ShapePipeline::Locat auto textureMaps = _drawMaterial->getTextureMaps(); glm::mat4 texcoordTransform[2]; - // Diffuse - if (materialKey.isDiffuseMap()) { - auto diffuseMap = textureMaps[model::MaterialKey::DIFFUSE_MAP]; - if (diffuseMap && diffuseMap->isDefined()) { - batch.setResourceTexture(ShapePipeline::Slot::DIFFUSE_MAP, diffuseMap->getTextureView()); + // Albedo + if (materialKey.isAlbedoMap()) { + auto albedoMap = textureMaps[model::MaterialKey::ALBEDO_MAP]; + if (albedoMap && albedoMap->isDefined()) { + batch.setResourceTexture(ShapePipeline::Slot::ALBEDO_MAP, albedoMap->getTextureView()); - if (!diffuseMap->getTextureTransform().isIdentity()) { - diffuseMap->getTextureTransform().getMatrix(texcoordTransform[0]); + if (!albedoMap->getTextureTransform().isIdentity()) { + albedoMap->getTextureTransform().getMatrix(texcoordTransform[0]); } } else { - batch.setResourceTexture(ShapePipeline::Slot::DIFFUSE_MAP, textureCache->getGrayTexture()); + batch.setResourceTexture(ShapePipeline::Slot::ALBEDO_MAP, textureCache->getGrayTexture()); } } else { - batch.setResourceTexture(ShapePipeline::Slot::DIFFUSE_MAP, textureCache->getWhiteTexture()); + batch.setResourceTexture(ShapePipeline::Slot::ALBEDO_MAP, textureCache->getWhiteTexture()); } // Normal map diff --git a/libraries/render-utils/src/Model.cpp b/libraries/render-utils/src/Model.cpp index 91c9ec623d..2ef5b5e792 100644 --- a/libraries/render-utils/src/Model.cpp +++ b/libraries/render-utils/src/Model.cpp @@ -1197,7 +1197,7 @@ void Model::segregateMeshGroups() { if (showingCollisionHull) { if (!_collisionHullMaterial) { _collisionHullMaterial = std::make_shared(); - _collisionHullMaterial->setDiffuse(glm::vec3(1.0f, 0.5f, 0.0f)); + _collisionHullMaterial->setAlbedo(glm::vec3(1.0f, 0.5f, 0.0f)); _collisionHullMaterial->setMetallic(0.02f); _collisionHullMaterial->setGloss(1.0f); } diff --git a/libraries/render-utils/src/RenderPipelines.cpp b/libraries/render-utils/src/RenderPipelines.cpp index fce9ce1c28..fbaa7df1b1 100644 --- a/libraries/render-utils/src/RenderPipelines.cpp +++ b/libraries/render-utils/src/RenderPipelines.cpp @@ -66,7 +66,7 @@ void initStencilPipeline(gpu::PipelinePointer& pipeline) { gpu::BufferView getDefaultMaterialBuffer() { model::Material::Schema schema; - schema._diffuse = vec3(1.0f); + schema._albedo = vec3(1.0f); schema._opacity = 1.0f; schema._metallic = vec3(0.1f); schema._gloss = 10.0f; @@ -74,8 +74,8 @@ gpu::BufferView getDefaultMaterialBuffer() { } void batchSetter(const ShapePipeline& pipeline, gpu::Batch& batch) { - // Set a default diffuse map - batch.setResourceTexture(render::ShapePipeline::Slot::DIFFUSE_MAP, + // Set a default albedo map + batch.setResourceTexture(render::ShapePipeline::Slot::ALBEDO_MAP, DependencyManager::get()->getWhiteTexture()); // Set a default normal map batch.setResourceTexture(render::ShapePipeline::Slot::NORMAL_FITTING_MAP, diff --git a/libraries/render-utils/src/model.slf b/libraries/render-utils/src/model.slf index f455030f6f..353c772327 100755 --- a/libraries/render-utils/src/model.slf +++ b/libraries/render-utils/src/model.slf @@ -33,7 +33,7 @@ void main(void) { packDeferredFragment( normalize(_normal.xyz), evalOpaqueFinalAlpha(getMaterialOpacity(mat), diffuse.a), - getMaterialDiffuse(mat) * diffuse.rgb * _color, + getMaterialAlbedo(mat) * diffuse.rgb * _color, getMaterialSpecular(mat), getMaterialShininess(mat)); } diff --git a/libraries/render-utils/src/model_emissive.slf b/libraries/render-utils/src/model_emissive.slf index 25239691cd..9705897399 100644 --- a/libraries/render-utils/src/model_emissive.slf +++ b/libraries/render-utils/src/model_emissive.slf @@ -26,7 +26,7 @@ void main(void) { vec4 texel = texture(diffuseMap, _texCoord0); Material mat = getMaterial(); - vec3 fragColor = getMaterialDiffuse(mat) * texel.rgb * _color; + vec3 fragColor = getMaterialAlbedo(mat) * texel.rgb * _color; packDeferredFragmentLightmap( normalize(_normal), diff --git a/libraries/render-utils/src/model_lightmap.slf b/libraries/render-utils/src/model_lightmap.slf index ab92d2bf4c..7cfeb5da32 100755 --- a/libraries/render-utils/src/model_lightmap.slf +++ b/libraries/render-utils/src/model_lightmap.slf @@ -38,7 +38,7 @@ void main(void) { packDeferredFragmentLightmap( normalize(_normal), evalOpaqueFinalAlpha(getMaterialOpacity(mat), diffuse.a), - getMaterialDiffuse(mat) * diffuse.rgb * _color, + getMaterialAlbedo(mat) * diffuse.rgb * _color, getMaterialSpecular(mat), getMaterialShininess(mat), (vec3(emissiveParams.x) + emissiveParams.y * emissive.rgb)); diff --git a/libraries/render-utils/src/model_lightmap_normal_map.slf b/libraries/render-utils/src/model_lightmap_normal_map.slf index 78c802be51..165ef4bf04 100755 --- a/libraries/render-utils/src/model_lightmap_normal_map.slf +++ b/libraries/render-utils/src/model_lightmap_normal_map.slf @@ -51,7 +51,7 @@ void main(void) { packDeferredFragmentLightmap( normalize(viewNormal.xyz), evalOpaqueFinalAlpha(getMaterialOpacity(mat), diffuse.a), - getMaterialDiffuse(mat) * diffuse.rgb * _color, + getMaterialAlbedo(mat) * diffuse.rgb * _color, getMaterialSpecular(mat), getMaterialShininess(mat), (vec3(emissiveParams.x) + emissiveParams.y * emissive.rgb)); 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 1b7416baa5..591cc45a40 100755 --- a/libraries/render-utils/src/model_lightmap_normal_specular_map.slf +++ b/libraries/render-utils/src/model_lightmap_normal_specular_map.slf @@ -55,7 +55,7 @@ void main(void) { packDeferredFragmentLightmap( normalize(viewNormal.xyz), evalOpaqueFinalAlpha(getMaterialOpacity(mat), diffuse.a), - getMaterialDiffuse(mat) * diffuse.rgb * _color, + getMaterialAlbedo(mat) * diffuse.rgb * _color, specular, // no use of getMaterialSpecular(mat) getMaterialShininess(mat), (vec3(emissiveParams.x) + emissiveParams.y * emissive.rgb)); diff --git a/libraries/render-utils/src/model_lightmap_specular_map.slf b/libraries/render-utils/src/model_lightmap_specular_map.slf index efdfcd6be9..97c1713e96 100755 --- a/libraries/render-utils/src/model_lightmap_specular_map.slf +++ b/libraries/render-utils/src/model_lightmap_specular_map.slf @@ -43,7 +43,7 @@ void main(void) { packDeferredFragmentLightmap( normalize(_normal), evalOpaqueFinalAlpha(getMaterialOpacity(mat), diffuse.a), - getMaterialDiffuse(mat) * diffuse.rgb * _color, + getMaterialAlbedo(mat) * diffuse.rgb * _color, specular, // no use of getMaterialSpecular(mat) getMaterialShininess(mat), (vec3(emissiveParams.x) + emissiveParams.y * emissive.rgb)); diff --git a/libraries/render-utils/src/model_normal_map.slf b/libraries/render-utils/src/model_normal_map.slf index 5479cec8eb..5749a437c8 100755 --- a/libraries/render-utils/src/model_normal_map.slf +++ b/libraries/render-utils/src/model_normal_map.slf @@ -44,7 +44,7 @@ void main(void) { packDeferredFragment( normalize(viewNormal.xyz), evalOpaqueFinalAlpha(getMaterialOpacity(mat), diffuse.a), - getMaterialDiffuse(mat) * diffuse.rgb * _color, + getMaterialAlbedo(mat) * diffuse.rgb * _color, getMaterialSpecular(mat), getMaterialShininess(mat)); } diff --git a/libraries/render-utils/src/model_normal_specular_map.slf b/libraries/render-utils/src/model_normal_specular_map.slf index 2ebcdaae44..7f27747791 100755 --- a/libraries/render-utils/src/model_normal_specular_map.slf +++ b/libraries/render-utils/src/model_normal_specular_map.slf @@ -49,7 +49,7 @@ void main(void) { packDeferredFragment( normalize(viewNormal.xyz), evalOpaqueFinalAlpha(getMaterialOpacity(mat), diffuse.a), - getMaterialDiffuse(mat) * diffuse.rgb * _color, + getMaterialAlbedo(mat) * diffuse.rgb * _color, specular, //getMaterialSpecular(mat), getMaterialShininess(mat)); } diff --git a/libraries/render-utils/src/model_specular_map.slf b/libraries/render-utils/src/model_specular_map.slf index e7f639194d..7e6e8f6f57 100755 --- a/libraries/render-utils/src/model_specular_map.slf +++ b/libraries/render-utils/src/model_specular_map.slf @@ -16,8 +16,8 @@ <@include model/Material.slh@> -// the diffuse texture -uniform sampler2D diffuseMap; +// the albedo texture +uniform sampler2D albedoMap; // the specular texture uniform sampler2D specularMap; @@ -29,16 +29,16 @@ in vec3 _color; void main(void) { - // set the diffuse, normal, specular data - vec4 diffuse = texture(diffuseMap, _texCoord0); + // set the albedo, normal, specular data + vec4 albedo = texture(albedoMap, _texCoord0); vec3 specular = texture(specularMap, _texCoord0).rgb; Material mat = getMaterial(); packDeferredFragment( normalize(_normal), - evalOpaqueFinalAlpha(getMaterialOpacity(mat), diffuse.a), - getMaterialDiffuse(mat) * diffuse.rgb * _color, + evalOpaqueFinalAlpha(getMaterialOpacity(mat), albedo.a), + getMaterialAlbedo(mat) * albedo.rgb * _color, specular, //getMaterialSpecular(mat), getMaterialShininess(mat)); } diff --git a/libraries/render-utils/src/model_translucent.slf b/libraries/render-utils/src/model_translucent.slf index c6f745ed0f..1ce0d22bc5 100755 --- a/libraries/render-utils/src/model_translucent.slf +++ b/libraries/render-utils/src/model_translucent.slf @@ -40,7 +40,7 @@ vec4 evalGlobalColor(float shadowAttenuation, vec3 position, vec3 normal, vec3 d return vec4(color, opacity); } -uniform sampler2D diffuseMap; +uniform sampler2D albedoMap; in vec2 _texCoord0; in vec4 _position; @@ -51,15 +51,15 @@ in float _alpha; out vec4 _fragColor; void main(void) { - vec4 diffuse = texture(diffuseMap, _texCoord0); + vec4 albedo = texture(albedoMap, _texCoord0); Material mat = getMaterial(); vec3 fragPosition = _position.xyz; vec3 fragNormal = normalize(_normal); - vec3 fragDiffuse = getMaterialDiffuse(mat) * diffuse.rgb * _color; + vec3 fragDiffuse = getMaterialAlbedo(mat) * albedo.rgb * _color; vec3 fragSpecular = getMaterialSpecular(mat); float fragGloss = getMaterialShininess(mat) / 128; - float fragOpacity = getMaterialOpacity(mat) * diffuse.a * _alpha; + float fragOpacity = getMaterialOpacity(mat) * albedo.a * _alpha; _fragColor = evalGlobalColor(1.0, fragPosition, diff --git a/libraries/render-utils/src/model_translucent_emissive.slf b/libraries/render-utils/src/model_translucent_emissive.slf index a2c7186f6f..82faf7cf45 100644 --- a/libraries/render-utils/src/model_translucent_emissive.slf +++ b/libraries/render-utils/src/model_translucent_emissive.slf @@ -14,7 +14,7 @@ <@include model/Material.slh@> -uniform sampler2D diffuseMap; +uniform sampler2D albedoMap; in vec2 _texCoord0; in vec3 _color; @@ -23,11 +23,11 @@ in float _alpha; out vec4 _fragColor; void main(void) { - vec4 diffuse = texture(diffuseMap, _texCoord0); + vec4 albedo = texture(albedoMap, _texCoord0); Material mat = getMaterial(); - vec3 fragColor = getMaterialDiffuse(mat) * diffuse.rgb * _color; - float fragOpacity = getMaterialOpacity(mat) * diffuse.a * _alpha; + vec3 fragColor = getMaterialAlbedo(mat) * albedo.rgb * _color; + float fragOpacity = getMaterialOpacity(mat) * albedo.a * _alpha; _fragColor = vec4(fragColor, fragOpacity); } diff --git a/libraries/render-utils/src/skin_model_normal_map.slv b/libraries/render-utils/src/skin_model_normal_map.slv index cec93a024e..003b35b79b 100755 --- a/libraries/render-utils/src/skin_model_normal_map.slv +++ b/libraries/render-utils/src/skin_model_normal_map.slv @@ -34,7 +34,7 @@ void main(void) { skinPositionNormalTangent(inSkinClusterIndex, inSkinClusterWeight, inPosition, inNormal.xyz, inTangent.xyz, position, interpolatedNormal.xyz, interpolatedTangent.xyz); - // pass along the diffuse color + // pass along the color _color = colorToLinearRGB(inColor.rgb); // and the texture coordinates diff --git a/libraries/render/src/render/ShapePipeline.cpp b/libraries/render/src/render/ShapePipeline.cpp index b77c9c3451..1103507bf5 100644 --- a/libraries/render/src/render/ShapePipeline.cpp +++ b/libraries/render/src/render/ShapePipeline.cpp @@ -53,7 +53,7 @@ void ShapePlumber::addPipeline(const Filter& filter, const gpu::ShaderPointer& p gpu::Shader::BindingSet slotBindings; 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("diffuseMap"), Slot::DIFFUSE_MAP)); + slotBindings.insert(gpu::Shader::Binding(std::string("albedoMap"), Slot::ALBEDO_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)); @@ -66,7 +66,7 @@ void ShapePlumber::addPipeline(const Filter& filter, const gpu::ShaderPointer& p locations->texcoordMatrices = program->getUniforms().findLocation("texcoordMatrices"); locations->emissiveParams = program->getUniforms().findLocation("emissiveParams"); locations->normalFittingMapUnit = program->getTextures().findLocation("normalFittingMap"); - locations->diffuseTextureUnit = program->getTextures().findLocation("diffuseMap"); + locations->albedoTextureUnit = program->getTextures().findLocation("albedoMap"); 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 4fd1dc22c5..4b6247aa54 100644 --- a/libraries/render/src/render/ShapePipeline.h +++ b/libraries/render/src/render/ShapePipeline.h @@ -195,7 +195,7 @@ public: public: static const int SKINNING_GPU = 2; static const int MATERIAL_GPU = 3; - static const int DIFFUSE_MAP = 0; + static const int ALBEDO_MAP = 0; static const int NORMAL_MAP = 1; static const int SPECULAR_MAP = 2; static const int LIGHTMAP_MAP = 3; @@ -206,7 +206,7 @@ public: class Locations { public: int texcoordMatrices; - int diffuseTextureUnit; + int albedoTextureUnit; int normalTextureUnit; int specularTextureUnit; int emissiveTextureUnit; From 71863a1550e20341a39308a03f2053a3e8a5aed4 Mon Sep 17 00:00:00 2001 From: samcake Date: Tue, 16 Feb 2016 09:33:24 -0800 Subject: [PATCH 02/24] Fixing the material names --- .../entities-renderer/src/paintStroke.slf | 2 +- .../src/untextured_particle.slv | 2 +- libraries/render-utils/src/DeferredBuffer.slh | 6 ++--- .../render-utils/src/DeferredBufferWrite.slh | 12 +++++----- .../render-utils/src/DeferredGlobalLight.slh | 22 +++++++++---------- .../render-utils/src/DeferredLightingEffect.h | 2 +- libraries/render-utils/src/GeometryCache.cpp | 2 +- libraries/render-utils/src/animdebugdraw.slv | 2 +- libraries/render-utils/src/model.slf | 10 ++++----- libraries/render-utils/src/model_emissive.slf | 4 ++-- libraries/render-utils/src/model_lightmap.slf | 10 ++++----- libraries/render-utils/src/model_lightmap.slv | 2 +- .../src/model_lightmap_normal_map.slf | 10 ++++----- .../src/model_lightmap_normal_map.slv | 2 +- .../model_lightmap_normal_specular_map.slf | 12 +++++----- .../src/model_lightmap_specular_map.slf | 12 +++++----- .../render-utils/src/model_normal_map.slf | 10 ++++----- .../render-utils/src/model_normal_map.slv | 2 +- .../src/model_normal_specular_map.slf | 12 +++++----- .../render-utils/src/model_translucent.slf | 10 ++++----- libraries/render-utils/src/overlay3D.slf | 14 ++++++------ .../render-utils/src/overlay3D_emissive.slf | 6 ++--- .../src/overlay3D_translucent.slf | 14 ++++++------ .../src/overlay3D_translucent_emissive.slf | 4 ++-- libraries/render-utils/src/skin_model.slv | 2 +- 25 files changed, 93 insertions(+), 93 deletions(-) diff --git a/libraries/entities-renderer/src/paintStroke.slf b/libraries/entities-renderer/src/paintStroke.slf index fc659d5928..9b7193bbfc 100644 --- a/libraries/entities-renderer/src/paintStroke.slf +++ b/libraries/entities-renderer/src/paintStroke.slf @@ -15,7 +15,7 @@ <@include DeferredBufferWrite.slh@> -// the diffuse texture +// the albedo texture uniform sampler2D originalTexture; // the interpolated normal diff --git a/libraries/entities-renderer/src/untextured_particle.slv b/libraries/entities-renderer/src/untextured_particle.slv index ab0ea15219..85f9d438bf 100644 --- a/libraries/entities-renderer/src/untextured_particle.slv +++ b/libraries/entities-renderer/src/untextured_particle.slv @@ -16,7 +16,7 @@ out vec4 _color; void main(void) { - // pass along the diffuse color + // pass along the color _color = colorToLinearRGBA(inColor); TransformCamera cam = getTransformCamera(); diff --git a/libraries/render-utils/src/DeferredBuffer.slh b/libraries/render-utils/src/DeferredBuffer.slh index 3f9aef6dd1..40c4349ae2 100755 --- a/libraries/render-utils/src/DeferredBuffer.slh +++ b/libraries/render-utils/src/DeferredBuffer.slh @@ -12,8 +12,8 @@ <@def DEFERRED_BUFFER_SLH@> -// the diffuse texture -uniform sampler2D diffuseMap; +// the albedo texture +uniform sampler2D albedoMap; // the normal texture uniform sampler2D normalMap; @@ -83,7 +83,7 @@ DeferredFragment unpackDeferredFragment(DeferredTransform deferredTransform, vec DeferredFragment frag; frag.depthVal = texture(depthMap, texcoord).r; frag.normalVal = texture(normalMap, texcoord); - frag.diffuseVal = texture(diffuseMap, texcoord); + frag.diffuseVal = texture(albedoMap, texcoord); frag.specularVal = texture(specularMap, texcoord); frag.obscurance = texture(obscuranceMap, texcoord).x; diff --git a/libraries/render-utils/src/DeferredBufferWrite.slh b/libraries/render-utils/src/DeferredBufferWrite.slh index 1045c4afc7..5cf40fad2d 100755 --- a/libraries/render-utils/src/DeferredBufferWrite.slh +++ b/libraries/render-utils/src/DeferredBufferWrite.slh @@ -43,32 +43,32 @@ float evalOpaqueFinalAlpha(float alpha, float mapAlpha) { const vec3 DEFAULT_SPECULAR = vec3(0.1); const float DEFAULT_SHININESS = 10; -void packDeferredFragment(vec3 normal, float alpha, vec3 diffuse, vec3 specular, float shininess) { +void packDeferredFragment(vec3 normal, float alpha, vec3 albedo, vec3 specular, float shininess) { if (alpha != 1.0) { discard; } - _fragColor0 = vec4(diffuse.rgb, 1.0); // Opaque + _fragColor0 = vec4(albedo.rgb, 1.0); // Opaque _fragColor1 = vec4(bestFitNormal(normal), 1.0); _fragColor2 = vec4(specular, shininess / 128.0); } -void packDeferredFragmentLightmap(vec3 normal, float alpha, vec3 diffuse, vec3 specular, float shininess, vec3 emissive) { +void packDeferredFragmentLightmap(vec3 normal, float alpha, vec3 albedo, vec3 specular, float shininess, vec3 emissive) { if (alpha != 1.0) { discard; } - _fragColor0 = vec4(diffuse.rgb, 0.5); + _fragColor0 = vec4(albedo.rgb, 0.5); _fragColor1 = vec4(bestFitNormal(normal), 0.5); _fragColor2 = vec4(emissive, shininess / 128.0); } -void packDeferredFragmentTranslucent(vec3 normal, float alpha, vec3 diffuse, vec3 specular, float shininess) { +void packDeferredFragmentTranslucent(vec3 normal, float alpha, vec3 albedo, vec3 specular, float shininess) { if (alpha <= 0.0) { discard; } - _fragColor0 = vec4(diffuse.rgb, alpha); + _fragColor0 = vec4(albedo.rgb, alpha); // _fragColor1 = vec4(normal, 0.0) * 0.5 + vec4(0.5, 0.5, 0.5, 1.0); // _fragColor2 = vec4(specular, shininess / 128.0); } diff --git a/libraries/render-utils/src/DeferredGlobalLight.slh b/libraries/render-utils/src/DeferredGlobalLight.slh index 75fe187e46..30c52fb3b6 100755 --- a/libraries/render-utils/src/DeferredGlobalLight.slh +++ b/libraries/render-utils/src/DeferredGlobalLight.slh @@ -70,7 +70,7 @@ uniform SphericalHarmonics ambientSphere; <@include model/Light.slh@> <@func declareEvalAmbientGlobalColor()@> -vec3 evalAmbientGlobalColor(mat4 invViewMat, float shadowAttenuation, float obscurance, vec3 position, vec3 normal, vec3 diffuse, vec3 specular, float gloss) { +vec3 evalAmbientGlobalColor(mat4 invViewMat, float shadowAttenuation, float obscurance, vec3 position, vec3 normal, vec3 albedo, vec3 specular, float gloss) { // Need the light now Light light = getLight(); @@ -79,11 +79,11 @@ vec3 evalAmbientGlobalColor(mat4 invViewMat, float shadowAttenuation, float obsc vec4 fragEyeVector = invViewMat * vec4(-position, 0.0); vec3 fragEyeDir = normalize(fragEyeVector.xyz); - vec3 color = diffuse.rgb * getLightColor(light) * obscurance * getLightAmbientIntensity(light); + vec3 color = albedo * getLightColor(light) * obscurance * getLightAmbientIntensity(light); vec4 shading = evalFragShading(fragNormal, -getLightDirection(light), fragEyeDir, specular, gloss); - color += vec3(diffuse * shading.w + shading.rgb) * min(shadowAttenuation, obscurance) * getLightColor(light) * getLightIntensity(light); + color += vec3(albedo * shading.w + shading.rgb) * min(shadowAttenuation, obscurance) * getLightColor(light) * getLightIntensity(light); return color; } @@ -93,7 +93,7 @@ vec3 evalAmbientGlobalColor(mat4 invViewMat, float shadowAttenuation, float obsc <$declareSphericalHarmonics()$> -vec3 evalAmbientSphereGlobalColor(mat4 invViewMat, float shadowAttenuation, float obscurance, vec3 position, vec3 normal, vec3 diffuse, vec3 specular, float gloss) { +vec3 evalAmbientSphereGlobalColor(mat4 invViewMat, float shadowAttenuation, float obscurance, vec3 position, vec3 normal, vec3 albedo, vec3 specular, float gloss) { // Need the light now Light light = getLight(); @@ -102,11 +102,11 @@ vec3 evalAmbientSphereGlobalColor(mat4 invViewMat, float shadowAttenuation, floa vec3 fragEyeDir = normalize(fragEyeVector.xyz); vec3 ambientNormal = fragNormal.xyz; - vec3 color = diffuse.rgb * evalSphericalLight(ambientSphere, ambientNormal).xyz * obscurance * getLightAmbientIntensity(light); + vec3 color = albedo * evalSphericalLight(ambientSphere, ambientNormal).xyz * obscurance * getLightAmbientIntensity(light); vec4 shading = evalFragShading(fragNormal, -getLightDirection(light), fragEyeDir, specular, gloss); - color += vec3(diffuse * shading.w + shading.rgb) * min(shadowAttenuation, obscurance) * getLightColor(light) * getLightIntensity(light); + color += vec3(albedo * shading.w + shading.rgb) * min(shadowAttenuation, obscurance) * getLightColor(light) * getLightIntensity(light); return color; } @@ -117,7 +117,7 @@ vec3 evalAmbientSphereGlobalColor(mat4 invViewMat, float shadowAttenuation, floa <$declareSkyboxMap()$> <$declareSphericalHarmonics()$> -vec3 evalSkyboxGlobalColor(mat4 invViewMat, float shadowAttenuation, float obscurance, vec3 position, vec3 normal, vec3 diffuse, vec3 specular, float gloss) { +vec3 evalSkyboxGlobalColor(mat4 invViewMat, float shadowAttenuation, float obscurance, vec3 position, vec3 normal, vec3 albedo, vec3 specular, float gloss) { // Need the light now Light light = getLight(); @@ -125,18 +125,18 @@ vec3 evalSkyboxGlobalColor(mat4 invViewMat, float shadowAttenuation, float obscu vec4 fragEyeVector = invViewMat * vec4(-position, 0.0); vec3 fragEyeDir = normalize(fragEyeVector.xyz); - vec3 color = diffuse.rgb * evalSphericalLight(ambientSphere, fragNormal).xyz * obscurance * getLightAmbientIntensity(light); + vec3 color = albedo * evalSphericalLight(ambientSphere, fragNormal).xyz * obscurance * getLightAmbientIntensity(light); vec4 shading = evalFragShading(fragNormal, -getLightDirection(light), fragEyeDir, specular, gloss); - color += vec3(diffuse * shading.w + shading.rgb) * min(shadowAttenuation, obscurance) * getLightColor(light) * getLightIntensity(light); + color += vec3(albedo * shading.w + shading.rgb) * min(shadowAttenuation, obscurance) * getLightColor(light) * getLightIntensity(light); return color; } <@endfunc@> <@func declareEvalLightmappedColor()@> -vec3 evalLightmappedColor(mat4 invViewMat, float shadowAttenuation, float obscurance, vec3 normal, vec3 diffuse, vec3 lightmap) { +vec3 evalLightmappedColor(mat4 invViewMat, float shadowAttenuation, float obscurance, vec3 normal, vec3 albedo, vec3 lightmap) { Light light = getLight(); @@ -156,7 +156,7 @@ vec3 evalLightmappedColor(mat4 invViewMat, float shadowAttenuation, float obscur // ambient is a tiny percentage of the lightmap and only when in the shadow vec3 ambientLight = (1 - lightAttenuation) * lightmap * getLightAmbientIntensity(light); - return obscurance * diffuse * (ambientLight + diffuseLight); + return obscurance * albedo * (ambientLight + diffuseLight); } <@endfunc@> diff --git a/libraries/render-utils/src/DeferredLightingEffect.h b/libraries/render-utils/src/DeferredLightingEffect.h index 6ef5794d95..9eef530bbc 100644 --- a/libraries/render-utils/src/DeferredLightingEffect.h +++ b/libraries/render-utils/src/DeferredLightingEffect.h @@ -50,7 +50,7 @@ public: // update global lighting void setAmbientLightMode(int preset); - void setGlobalLight(const glm::vec3& direction, const glm::vec3& diffuse, float intensity, float ambientIntensity); + void setGlobalLight(const glm::vec3& direction, const glm::vec3& color, float intensity, float ambientIntensity); void setGlobalSkybox(const model::SkyboxPointer& skybox); const LightStage& getLightStage() { return _lightStage; } diff --git a/libraries/render-utils/src/GeometryCache.cpp b/libraries/render-utils/src/GeometryCache.cpp index 34a02b51d2..991397df8b 100644 --- a/libraries/render-utils/src/GeometryCache.cpp +++ b/libraries/render-utils/src/GeometryCache.cpp @@ -1846,7 +1846,7 @@ inline bool operator==(const SimpleProgramKey& a, const SimpleProgramKey& b) { void GeometryCache::bindSimpleProgram(gpu::Batch& batch, bool textured, bool culled, bool emissive, bool depthBiased) { batch.setPipeline(getSimplePipeline(textured, culled, emissive, depthBiased)); - // If not textured, set a default diffuse map + // If not textured, set a default albedo map if (!textured) { batch.setResourceTexture(render::ShapePipeline::Slot::ALBEDO_MAP, DependencyManager::get()->getWhiteTexture()); diff --git a/libraries/render-utils/src/animdebugdraw.slv b/libraries/render-utils/src/animdebugdraw.slv index 3cb356c055..ffa44b6cee 100644 --- a/libraries/render-utils/src/animdebugdraw.slv +++ b/libraries/render-utils/src/animdebugdraw.slv @@ -16,7 +16,7 @@ out vec4 _color; void main(void) { - // pass along the diffuse color + // pass along the color _color = colorToLinearRGBA(inColor.rgba); TransformCamera cam = getTransformCamera(); diff --git a/libraries/render-utils/src/model.slf b/libraries/render-utils/src/model.slf index 353c772327..d00ef917fb 100755 --- a/libraries/render-utils/src/model.slf +++ b/libraries/render-utils/src/model.slf @@ -16,7 +16,7 @@ <@include model/Material.slh@> // the diffuse texture -uniform sampler2D diffuseMap; +uniform sampler2D albedoMap; in vec4 _position; in vec3 _normal; @@ -25,15 +25,15 @@ in vec2 _texCoord0; void main(void) { - // Fetch diffuse map - vec4 diffuse = texture(diffuseMap, _texCoord0); + // Fetch albedo map + vec4 albedo = texture(albedoMap, _texCoord0); Material mat = getMaterial(); packDeferredFragment( normalize(_normal.xyz), - evalOpaqueFinalAlpha(getMaterialOpacity(mat), diffuse.a), - getMaterialAlbedo(mat) * diffuse.rgb * _color, + evalOpaqueFinalAlpha(getMaterialOpacity(mat), albedo.a), + getMaterialAlbedo(mat) * albedo.rgb * _color, getMaterialSpecular(mat), getMaterialShininess(mat)); } diff --git a/libraries/render-utils/src/model_emissive.slf b/libraries/render-utils/src/model_emissive.slf index 9705897399..ba0abc44c2 100644 --- a/libraries/render-utils/src/model_emissive.slf +++ b/libraries/render-utils/src/model_emissive.slf @@ -15,7 +15,7 @@ <@include DeferredBufferWrite.slh@> <@include model/Material.slh@> -uniform sampler2D diffuseMap; +uniform sampler2D albedoMap; in vec2 _texCoord0; in vec3 _normal; @@ -23,7 +23,7 @@ in vec3 _color; in float _alpha; void main(void) { - vec4 texel = texture(diffuseMap, _texCoord0); + vec4 texel = texture(albedoMap, _texCoord0); Material mat = getMaterial(); vec3 fragColor = getMaterialAlbedo(mat) * texel.rgb * _color; diff --git a/libraries/render-utils/src/model_lightmap.slf b/libraries/render-utils/src/model_lightmap.slf index 7cfeb5da32..ff91cc402d 100755 --- a/libraries/render-utils/src/model_lightmap.slf +++ b/libraries/render-utils/src/model_lightmap.slf @@ -16,8 +16,8 @@ <@include model/Material.slh@> -// the diffuse texture -uniform sampler2D diffuseMap; +// the albedo texture +uniform sampler2D albedoMap; // the emissive map texture and parameters uniform sampler2D emissiveMap; @@ -30,15 +30,15 @@ in vec3 _normal; in vec3 _color; void main(void) { - vec4 diffuse = texture(diffuseMap, _texCoord0); + vec4 albedo = texture(albedoMap, _texCoord0); vec4 emissive = texture(emissiveMap, _texCoord1); Material mat = getMaterial(); packDeferredFragmentLightmap( normalize(_normal), - evalOpaqueFinalAlpha(getMaterialOpacity(mat), diffuse.a), - getMaterialAlbedo(mat) * diffuse.rgb * _color, + evalOpaqueFinalAlpha(getMaterialOpacity(mat), albedo.a), + getMaterialAlbedo(mat) * albedo.rgb * _color, getMaterialSpecular(mat), getMaterialShininess(mat), (vec3(emissiveParams.x) + emissiveParams.y * emissive.rgb)); diff --git a/libraries/render-utils/src/model_lightmap.slv b/libraries/render-utils/src/model_lightmap.slv index eacc91245c..8696b70373 100755 --- a/libraries/render-utils/src/model_lightmap.slv +++ b/libraries/render-utils/src/model_lightmap.slv @@ -28,7 +28,7 @@ out vec3 _normal; out vec3 _color; void main(void) { - // pass along the diffuse color in linear space + // pass along the color in linear space _color = colorToLinearRGB(inColor.xyz); // and the texture coordinates diff --git a/libraries/render-utils/src/model_lightmap_normal_map.slf b/libraries/render-utils/src/model_lightmap_normal_map.slf index 165ef4bf04..fa6cc60e1f 100755 --- a/libraries/render-utils/src/model_lightmap_normal_map.slf +++ b/libraries/render-utils/src/model_lightmap_normal_map.slf @@ -16,8 +16,8 @@ <@include model/Material.slh@> -// the diffuse texture -uniform sampler2D diffuseMap; +// the albedo texture +uniform sampler2D albedoMap; // the normal map texture uniform sampler2D normalMap; @@ -43,15 +43,15 @@ void main(void) { normalizedBitangent * localNormal.y + normalizedNormal * localNormal.z, 0.0); // set the diffuse, normal, specular data - vec4 diffuse = texture(diffuseMap, _texCoord0); + vec4 albedo = texture(albedoMap, _texCoord0); vec4 emissive = texture(emissiveMap, _texCoord1); Material mat = getMaterial(); packDeferredFragmentLightmap( normalize(viewNormal.xyz), - evalOpaqueFinalAlpha(getMaterialOpacity(mat), diffuse.a), - getMaterialAlbedo(mat) * diffuse.rgb * _color, + evalOpaqueFinalAlpha(getMaterialOpacity(mat), albedo.a), + getMaterialAlbedo(mat) * albedo.rgb * _color, getMaterialSpecular(mat), getMaterialShininess(mat), (vec3(emissiveParams.x) + emissiveParams.y * emissive.rgb)); diff --git a/libraries/render-utils/src/model_lightmap_normal_map.slv b/libraries/render-utils/src/model_lightmap_normal_map.slv index 6046a67e10..eb8f80656d 100755 --- a/libraries/render-utils/src/model_lightmap_normal_map.slv +++ b/libraries/render-utils/src/model_lightmap_normal_map.slv @@ -29,7 +29,7 @@ out vec3 _tangent; out vec3 _color; void main(void) { - // pass along the diffuse color in linear space + // pass along the color in linear space _color = colorToLinearRGB(inColor.xyz); // and the texture coordinates 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 591cc45a40..a9e6325320 100755 --- a/libraries/render-utils/src/model_lightmap_normal_specular_map.slf +++ b/libraries/render-utils/src/model_lightmap_normal_specular_map.slf @@ -16,8 +16,8 @@ <@include model/Material.slh@> -// the diffuse texture -uniform sampler2D diffuseMap; +// the albedo texture +uniform sampler2D albedoMap; // the emissive map texture and parameters uniform sampler2D emissiveMap; @@ -45,8 +45,8 @@ void main(void) { vec4 viewNormal = vec4(normalizedTangent * localNormal.x + normalizedBitangent * localNormal.y + normalizedNormal * localNormal.z, 0.0); - // set the diffuse, normal, specular data - vec4 diffuse = texture(diffuseMap, _texCoord0); + // set the albedo, normal, specular data + vec4 albedo = texture(albedoMap, _texCoord0); vec3 specular = texture(specularMap, _texCoord0).rgb; vec4 emissive = texture(emissiveMap, _texCoord1); @@ -54,8 +54,8 @@ void main(void) { packDeferredFragmentLightmap( normalize(viewNormal.xyz), - evalOpaqueFinalAlpha(getMaterialOpacity(mat), diffuse.a), - getMaterialAlbedo(mat) * diffuse.rgb * _color, + evalOpaqueFinalAlpha(getMaterialOpacity(mat), albedo.a), + getMaterialAlbedo(mat) * albedo.rgb * _color, specular, // no use of getMaterialSpecular(mat) getMaterialShininess(mat), (vec3(emissiveParams.x) + emissiveParams.y * emissive.rgb)); diff --git a/libraries/render-utils/src/model_lightmap_specular_map.slf b/libraries/render-utils/src/model_lightmap_specular_map.slf index 97c1713e96..531e07c411 100755 --- a/libraries/render-utils/src/model_lightmap_specular_map.slf +++ b/libraries/render-utils/src/model_lightmap_specular_map.slf @@ -16,8 +16,8 @@ <@include model/Material.slh@> -// the diffuse texture -uniform sampler2D diffuseMap; +// the albedo texture +uniform sampler2D albedoMap; // the emissive map texture and parameters uniform sampler2D emissiveMap; @@ -33,8 +33,8 @@ in vec3 _normal; in vec3 _color; void main(void) { - // set the diffuse, normal, specular data - vec4 diffuse = texture(diffuseMap, _texCoord0); + // set the albedo, normal, specular data + vec4 albedo = texture(albedoMap, _texCoord0); vec3 specular = texture(specularMap, _texCoord0).rgb; vec4 emissive = texture(emissiveMap, _texCoord1); @@ -42,8 +42,8 @@ void main(void) { packDeferredFragmentLightmap( normalize(_normal), - evalOpaqueFinalAlpha(getMaterialOpacity(mat), diffuse.a), - getMaterialAlbedo(mat) * diffuse.rgb * _color, + evalOpaqueFinalAlpha(getMaterialOpacity(mat), albedo.a), + getMaterialAlbedo(mat) * albedo.rgb * _color, specular, // no use of getMaterialSpecular(mat) getMaterialShininess(mat), (vec3(emissiveParams.x) + emissiveParams.y * emissive.rgb)); diff --git a/libraries/render-utils/src/model_normal_map.slf b/libraries/render-utils/src/model_normal_map.slf index 5749a437c8..aff8049d1d 100755 --- a/libraries/render-utils/src/model_normal_map.slf +++ b/libraries/render-utils/src/model_normal_map.slf @@ -16,8 +16,8 @@ <@include model/Material.slh@> -// the diffuse texture -uniform sampler2D diffuseMap; +// the albedo texture +uniform sampler2D albedoMap; // the normal map texture uniform sampler2D normalMap; @@ -37,14 +37,14 @@ void main(void) { vec4 viewNormal = vec4(normalizedTangent * localNormal.x + normalizedBitangent * localNormal.y + normalizedNormal * localNormal.z, 0.0); - vec4 diffuse = texture(diffuseMap, _texCoord0.st); + vec4 albedo = texture(albedoMap, _texCoord0.st); Material mat = getMaterial(); packDeferredFragment( normalize(viewNormal.xyz), - evalOpaqueFinalAlpha(getMaterialOpacity(mat), diffuse.a), - getMaterialAlbedo(mat) * diffuse.rgb * _color, + evalOpaqueFinalAlpha(getMaterialOpacity(mat), albedo.a), + getMaterialAlbedo(mat) * albedo.rgb * _color, getMaterialSpecular(mat), getMaterialShininess(mat)); } diff --git a/libraries/render-utils/src/model_normal_map.slv b/libraries/render-utils/src/model_normal_map.slv index 5ed4e37278..f7711cd326 100755 --- a/libraries/render-utils/src/model_normal_map.slv +++ b/libraries/render-utils/src/model_normal_map.slv @@ -28,7 +28,7 @@ out vec3 _tangent; out vec3 _color; void main(void) { - // pass along the diffuse color + // pass along the color _color = colorToLinearRGB(inColor.xyz); // and the texture coordinates diff --git a/libraries/render-utils/src/model_normal_specular_map.slf b/libraries/render-utils/src/model_normal_specular_map.slf index 7f27747791..703648c5ed 100755 --- a/libraries/render-utils/src/model_normal_specular_map.slf +++ b/libraries/render-utils/src/model_normal_specular_map.slf @@ -16,8 +16,8 @@ <@include model/Material.slh@> -// the diffuse texture -uniform sampler2D diffuseMap; +// the albedo texture +uniform sampler2D albedoMap; // the normal map texture uniform sampler2D normalMap; @@ -40,16 +40,16 @@ void main(void) { vec4 viewNormal = vec4(normalizedTangent * localNormal.x + normalizedBitangent * localNormal.y + normalizedNormal * localNormal.z, 0.0); - // set the diffuse, normal, specular data - vec4 diffuse = texture(diffuseMap, _texCoord0); + // set the albedo, normal, specular data + vec4 albedo = texture(albedoMap, _texCoord0); vec3 specular = texture(specularMap, _texCoord0).rgb; Material mat = getMaterial(); packDeferredFragment( normalize(viewNormal.xyz), - evalOpaqueFinalAlpha(getMaterialOpacity(mat), diffuse.a), - getMaterialAlbedo(mat) * diffuse.rgb * _color, + evalOpaqueFinalAlpha(getMaterialOpacity(mat), albedo.a), + getMaterialAlbedo(mat) * albedo.rgb * _color, specular, //getMaterialSpecular(mat), getMaterialShininess(mat)); } diff --git a/libraries/render-utils/src/model_translucent.slf b/libraries/render-utils/src/model_translucent.slf index 1ce0d22bc5..d90662a574 100755 --- a/libraries/render-utils/src/model_translucent.slf +++ b/libraries/render-utils/src/model_translucent.slf @@ -20,7 +20,7 @@ <@include gpu/Transform.slh@> <$declareStandardCameraTransform()$> -vec4 evalGlobalColor(float shadowAttenuation, vec3 position, vec3 normal, vec3 diffuse, vec3 specular, float gloss, float opacity) { +vec4 evalGlobalColor(float shadowAttenuation, vec3 position, vec3 normal, vec3 albedo, vec3 specular, float gloss, float opacity) { // Need the light now Light light = getLight(); @@ -31,11 +31,11 @@ vec4 evalGlobalColor(float shadowAttenuation, vec3 position, vec3 normal, vec3 d vec3 fragEyeDir; <$transformEyeToWorldDir(cam, fragEyeVectorView, fragEyeDir)$> - vec3 color = opacity * diffuse.rgb * getLightColor(light) * getLightAmbientIntensity(light); + vec3 color = opacity * albedo * getLightColor(light) * getLightAmbientIntensity(light); vec4 shading = evalFragShading(fragNormal, -getLightDirection(light), fragEyeDir, specular, gloss); - color += vec3(diffuse * shading.w * opacity + shading.rgb) * shadowAttenuation * getLightColor(light) * getLightIntensity(light); + color += vec3(albedo * shading.w * opacity + shading.rgb) * shadowAttenuation * getLightColor(light) * getLightIntensity(light); return vec4(color, opacity); } @@ -56,7 +56,7 @@ void main(void) { Material mat = getMaterial(); vec3 fragPosition = _position.xyz; vec3 fragNormal = normalize(_normal); - vec3 fragDiffuse = getMaterialAlbedo(mat) * albedo.rgb * _color; + vec3 fragAlbedo = getMaterialAlbedo(mat) * albedo.rgb * _color; vec3 fragSpecular = getMaterialSpecular(mat); float fragGloss = getMaterialShininess(mat) / 128; float fragOpacity = getMaterialOpacity(mat) * albedo.a * _alpha; @@ -64,7 +64,7 @@ void main(void) { _fragColor = evalGlobalColor(1.0, fragPosition, fragNormal, - fragDiffuse, + fragAlbedo, fragSpecular, fragGloss, fragOpacity); diff --git a/libraries/render-utils/src/overlay3D.slf b/libraries/render-utils/src/overlay3D.slf index 66a48e95f4..8de7a23898 100644 --- a/libraries/render-utils/src/overlay3D.slf +++ b/libraries/render-utils/src/overlay3D.slf @@ -17,7 +17,7 @@ <@include gpu/Transform.slh@> <$declareStandardCameraTransform()$> -vec4 evalGlobalColor(float shadowAttenuation, vec3 position, vec3 normal, vec3 diffuse, vec3 specular, float gloss, float opacity) { +vec4 evalGlobalColor(float shadowAttenuation, vec3 position, vec3 normal, vec3 albedo, vec3 specular, float gloss, float opacity) { // Need the light now Light light = getLight(); @@ -28,11 +28,11 @@ vec4 evalGlobalColor(float shadowAttenuation, vec3 position, vec3 normal, vec3 d vec3 fragEyeDir; <$transformEyeToWorldDir(cam, fragEyeVectorView, fragEyeDir)$> - vec3 color = opacity * diffuse.rgb * getLightColor(light) * getLightAmbientIntensity(light); + vec3 color = opacity * albedo * getLightColor(light) * getLightAmbientIntensity(light); vec4 shading = evalFragShading(fragNormal, -getLightDirection(light), fragEyeDir, specular, gloss); - color += vec3(diffuse * shading.w * opacity + shading.rgb) * shadowAttenuation * getLightColor(light) * getLightIntensity(light); + color += vec3(albedo * shading.w * opacity + shading.rgb) * shadowAttenuation * getLightColor(light) * getLightIntensity(light); return vec4(color, opacity); } @@ -48,14 +48,14 @@ in float _alpha; out vec4 _fragColor; void main(void) { - vec4 diffuse = texture(originalTexture, _texCoord0); + vec4 albedo = texture(originalTexture, _texCoord0); vec3 fragPosition = _position.xyz; vec3 fragNormal = normalize(_normal); - vec3 fragDiffuse = diffuse.rgb * _color; + vec3 fragAlbedo = albedo.rgb * _color; vec3 fragSpecular = vec3(0.1); float fragGloss = 10.0 / 128.0; - float fragOpacity = diffuse.a; + float fragOpacity = albedo.a; if (fragOpacity <= 0.1) { discard; @@ -64,7 +64,7 @@ void main(void) { vec4 color = evalGlobalColor(1.0, fragPosition, fragNormal, - fragDiffuse, + fragAlbedo, fragSpecular, fragGloss, fragOpacity); diff --git a/libraries/render-utils/src/overlay3D_emissive.slf b/libraries/render-utils/src/overlay3D_emissive.slf index ad689baf91..727eb0f317 100644 --- a/libraries/render-utils/src/overlay3D_emissive.slf +++ b/libraries/render-utils/src/overlay3D_emissive.slf @@ -20,12 +20,12 @@ in vec3 _color; out vec4 _fragColor; void main(void) { - vec4 diffuse = texture(originalTexture, _texCoord0); + vec4 albedo = texture(originalTexture, _texCoord0); - if (diffuse.a <= 0.1) { + if (albedo.a <= 0.1) { discard; } - vec4 color = vec4(diffuse.rgb * _color, diffuse.a); + vec4 color = vec4(albedo.rgb * _color, albedo.a); // Apply standard tone mapping _fragColor = vec4(pow(color.xyz, vec3(1.0 / 2.2)), color.w); diff --git a/libraries/render-utils/src/overlay3D_translucent.slf b/libraries/render-utils/src/overlay3D_translucent.slf index dcc15ba25b..8e6dbb9eff 100644 --- a/libraries/render-utils/src/overlay3D_translucent.slf +++ b/libraries/render-utils/src/overlay3D_translucent.slf @@ -18,7 +18,7 @@ <@include gpu/Transform.slh@> <$declareStandardCameraTransform()$> -vec4 evalGlobalColor(float shadowAttenuation, vec3 position, vec3 normal, vec3 diffuse, vec3 specular, float gloss, float opacity) { +vec4 evalGlobalColor(float shadowAttenuation, vec3 position, vec3 normal, vec3 albedo, vec3 specular, float gloss, float opacity) { // Need the light now Light light = getLight(); @@ -29,11 +29,11 @@ vec4 evalGlobalColor(float shadowAttenuation, vec3 position, vec3 normal, vec3 d vec3 fragEyeDir; <$transformEyeToWorldDir(cam, fragEyeVectorView, fragEyeDir)$> - vec3 color = opacity * diffuse.rgb * getLightColor(light) * getLightAmbientIntensity(light); + vec3 color = opacity * albedo * getLightColor(light) * getLightAmbientIntensity(light); vec4 shading = evalFragShading(fragNormal, -getLightDirection(light), fragEyeDir, specular, gloss); - color += vec3(diffuse * shading.w * opacity + shading.rgb) * shadowAttenuation * getLightColor(light) * getLightIntensity(light); + color += vec3(albedo * shading.w * opacity + shading.rgb) * shadowAttenuation * getLightColor(light) * getLightIntensity(light); return vec4(color, opacity); } @@ -49,19 +49,19 @@ in float _alpha; out vec4 _fragColor; void main(void) { - vec4 diffuse = texture(originalTexture, _texCoord0); + vec4 albedo = texture(originalTexture, _texCoord0); vec3 fragPosition = _position.xyz; vec3 fragNormal = normalize(_normal); - vec3 fragDiffuse = diffuse.rgb * _color; + vec3 fragAlbedo = albedo.rgb * _color; vec3 fragSpecular = vec3(0.1); float fragGloss = 10.0 / 128.0; - float fragOpacity = diffuse.a * _alpha; + float fragOpacity = albedo.a * _alpha; vec4 color = evalGlobalColor(1.0, fragPosition, fragNormal, - fragDiffuse, + fragAlbedo, fragSpecular, fragGloss, fragOpacity); diff --git a/libraries/render-utils/src/overlay3D_translucent_emissive.slf b/libraries/render-utils/src/overlay3D_translucent_emissive.slf index bcfec7d588..61935f3c67 100644 --- a/libraries/render-utils/src/overlay3D_translucent_emissive.slf +++ b/libraries/render-utils/src/overlay3D_translucent_emissive.slf @@ -21,7 +21,7 @@ in float _alpha; out vec4 _fragColor; void main(void) { - vec4 diffuse = texture(originalTexture, _texCoord0); + vec4 albedo = texture(originalTexture, _texCoord0); - _fragColor = vec4(diffuse.rgb * _color, diffuse.a * _alpha); + _fragColor = vec4(albedo.rgb * _color, albedo.a * _alpha); } diff --git a/libraries/render-utils/src/skin_model.slv b/libraries/render-utils/src/skin_model.slv index 8ab475e1fd..199b97ba53 100755 --- a/libraries/render-utils/src/skin_model.slv +++ b/libraries/render-utils/src/skin_model.slv @@ -32,7 +32,7 @@ void main(void) { skinPositionNormal(inSkinClusterIndex, inSkinClusterWeight, inPosition, inNormal.xyz, position, interpolatedNormal); - // pass along the diffuse color + // pass along the color _color = colorToLinearRGB(inColor.rgb); // and the texture coordinates From b03f639e2cbb64327ddd777593b8548ca8c9e5a0 Mon Sep 17 00:00:00 2001 From: samcake Date: Tue, 16 Feb 2016 17:20:23 -0800 Subject: [PATCH 03/24] Renaming and rearranging the material fields to support PBR fbx materials --- libraries/fbx/src/FBXReader.cpp | 33 ++++++++++++-- libraries/fbx/src/FBXReader.h | 12 ++++- libraries/fbx/src/FBXReader_Material.cpp | 44 +++++++++++++++---- libraries/fbx/src/OBJReader.cpp | 2 +- libraries/model/src/model/Material.cpp | 11 +++-- libraries/model/src/model/Material.h | 11 +++-- .../render-utils/src/MeshPartPayload.cpp | 4 +- libraries/render-utils/src/Model.cpp | 2 +- .../render-utils/src/RenderPipelines.cpp | 2 +- .../render-utils/src/simple_textured.slf | 2 +- .../src/simple_textured_emisive.slf | 2 +- 11 files changed, 99 insertions(+), 26 deletions(-) diff --git a/libraries/fbx/src/FBXReader.cpp b/libraries/fbx/src/FBXReader.cpp index 1be3bbb5f6..daaa2d94be 100644 --- a/libraries/fbx/src/FBXReader.cpp +++ b/libraries/fbx/src/FBXReader.cpp @@ -865,6 +865,9 @@ FBXGeometry* FBXReader::extractFBXGeometry(const QVariantHash& mapping, const QS } } else if (object.name == "Material") { FBXMaterial material; + if (object.properties.at(1).toByteArray().contains("StingrayPBS1")) { + material.isPBSMaterial = true; + } foreach (const FBXNode& subobject, object.children) { bool properties = false; QByteArray propertyName; @@ -879,7 +882,7 @@ FBXGeometry* FBXReader::extractFBXGeometry(const QVariantHash& mapping, const QS propertyName = "P"; index = 4; } - if (properties) { + if (properties && !material.isPBSMaterial) { foreach (const FBXNode& property, subobject.children) { if (property.name == propertyName) { if (property.properties.at(0) == "DiffuseColor") { @@ -914,6 +917,22 @@ FBXGeometry* FBXReader::extractFBXGeometry(const QVariantHash& mapping, const QS #endif } } + } else if (properties && material.isPBSMaterial) { + std::vector unknowns; + foreach(const FBXNode& property, subobject.children) { + if (property.name == propertyName) { + if (property.properties.at(0) == "Maya|base_color") { + material.diffuseColor = getVec3(property.properties, index); + } else if (property.properties.at(0) == "Maya|metallic") { + material.metallic = property.properties.at(index).value(); + } else if (property.properties.at(0) == "Maya|roughness") { + material.roughness = property.properties.at(index).value(); + } else { + const QString propname = property.properties.at(0).toString(); + unknowns.push_back(propname.toStdString()); + } + } + } } #if defined(DEBUG_FBXREADER) else { @@ -1032,7 +1051,9 @@ FBXGeometry* FBXReader::extractFBXGeometry(const QVariantHash& mapping, const QS QByteArray type = connection.properties.at(3).toByteArray().toLower(); if (type.contains("diffuse")) { diffuseTextures.insert(getID(connection.properties, 2), getID(connection.properties, 1)); - + } else if (type.contains("tex_color_map")) { + qDebug() << "insert color map for diffuse!"; + diffuseTextures.insert(getID(connection.properties, 2), getID(connection.properties, 1)); } else if (type.contains("transparentcolor")) { // it should be TransparentColor... // THis is how Maya assign a texture that affect diffuse color AND transparency ? diffuseTextures.insert(getID(connection.properties, 2), getID(connection.properties, 1)); @@ -1042,7 +1063,8 @@ FBXGeometry* FBXReader::extractFBXGeometry(const QVariantHash& mapping, const QS normalTextures.insert(getID(connection.properties, 2), getID(connection.properties, 1)); } else if (type.contains("specular") || type.contains("reflection")) { specularTextures.insert(getID(connection.properties, 2), getID(connection.properties, 1)); - + } else if (type.contains("metallic")) { + metallicTextures.insert(getID(connection.properties, 2), getID(connection.properties, 1)); } else if (type == "lcl rotation") { localRotations.insert(getID(connection.properties, 2), getID(connection.properties, 1)); } else if (type == "lcl translation") { @@ -1056,8 +1078,11 @@ FBXGeometry* FBXReader::extractFBXGeometry(const QVariantHash& mapping, const QS zComponents.insert(getID(connection.properties, 2), getID(connection.properties, 1)); } else if (type.contains("shininess")) { + shininessTextures.insert(getID(connection.properties, 2), getID(connection.properties, 1)); + counter++; + } else if (type.contains("roughness")) { + roughnessTextures.insert(getID(connection.properties, 2), getID(connection.properties, 1)); counter++; - } else if (_loadLightmaps && type.contains("emissive")) { emissiveTextures.insert(getID(connection.properties, 2), getID(connection.properties, 1)); diff --git a/libraries/fbx/src/FBXReader.h b/libraries/fbx/src/FBXReader.h index 6dc8a31bc7..755a1e70f2 100644 --- a/libraries/fbx/src/FBXReader.h +++ b/libraries/fbx/src/FBXReader.h @@ -111,7 +111,8 @@ public: QString texcoordSetName; bool isBumpmap{ false }; - + bool isGlossmap{ true }; + bool isNull() const { return name.isEmpty() && filename.isEmpty() && content.isEmpty(); } }; @@ -148,6 +149,9 @@ public: float shininess = 23.0f; float opacity = 1.0f; + float metallic{ 0.0f }; + float roughness{ 1.0f }; + QString materialID; model::MaterialPointer _material; @@ -156,7 +160,10 @@ public: FBXTexture normalTexture; FBXTexture specularTexture; FBXTexture emissiveTexture; + FBXTexture metallicTexture; + FBXTexture roughnessTexture; + bool isPBSMaterial{ false }; bool needTangentSpace() const; }; @@ -402,6 +409,9 @@ public: QHash specularTextures; QHash emissiveTextures; QHash ambientTextures; + QHash metallicTextures; + QHash roughnessTextures; + QHash shininessTextures; QHash _fbxMaterials; diff --git a/libraries/fbx/src/FBXReader_Material.cpp b/libraries/fbx/src/FBXReader_Material.cpp index b82d4c0877..64ee958330 100644 --- a/libraries/fbx/src/FBXReader_Material.cpp +++ b/libraries/fbx/src/FBXReader_Material.cpp @@ -100,17 +100,38 @@ void FBXReader::consolidateFBXMaterials() { material.normalTexture = normalTexture; detectDifferentUVs |= (normalTexture.texcoordSet != 0) || (!normalTexture.transform.isIdentity()); } - - + FBXTexture specularTexture; QString specularTextureID = specularTextures.value(material.materialID); if (!specularTextureID.isNull()) { specularTexture = getTexture(specularTextureID); detectDifferentUVs |= (specularTexture.texcoordSet != 0) || (!specularTexture.transform.isIdentity()); - material.specularTexture = specularTexture; } + FBXTexture metallicTexture; + QString metallicTextureID = metallicTextures.value(material.materialID); + if (!metallicTextureID.isNull()) { + metallicTexture = getTexture(metallicTextureID); + detectDifferentUVs |= (metallicTexture.texcoordSet != 0) || (!metallicTexture.transform.isIdentity()); + material.metallicTexture = metallicTexture; + } + + FBXTexture roughnessTexture; + QString roughnessTextureID = roughnessTextures.value(material.materialID); + QString shininessTextureID = shininessTextures.value(material.materialID); + if (!roughnessTextureID.isNull()) { + roughnessTexture = getTexture(roughnessTextureID); + roughnessTexture.isGlossmap = false; + material.roughnessTexture = roughnessTexture; + detectDifferentUVs |= (roughnessTexture.texcoordSet != 0) || (!roughnessTexture.transform.isIdentity()); + } else if (!shininessTextureID.isNull()) { + roughnessTexture = getTexture(roughnessTextureID); + roughnessTexture.isGlossmap = true; + material.roughnessTexture = roughnessTexture; + detectDifferentUVs |= (roughnessTexture.texcoordSet != 0) || (!roughnessTexture.transform.isIdentity()); + } + FBXTexture emissiveTexture; glm::vec2 emissiveParams(0.f, 1.f); emissiveParams.x = _lightmapOffset; @@ -142,11 +163,18 @@ void FBXReader::consolidateFBXMaterials() { // diffuse *= material.diffuseFactor; material._material->setAlbedo(diffuse); - float metallic = std::max(material.specularColor.x, std::max(material.specularColor.y, material.specularColor.z)); - // FIXME: Do not use the Specular Factor yet as some FBX models have it set to 0 - // metallic *= material.specularFactor; - material._material->setMetallic(metallic); - material._material->setGloss(material.shininess); + if (material.isPBSMaterial) { + material._material->setRoughness(material.roughness); + material._material->setMetallic(material.metallic); + } else { + material._material->setRoughness(model::Material::shininessToRoughness(material.shininess)); + + float metallic = std::max(material.specularColor.x, std::max(material.specularColor.y, material.specularColor.z)); + // 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) { material._material->setOpacity(1.0f); diff --git a/libraries/fbx/src/OBJReader.cpp b/libraries/fbx/src/OBJReader.cpp index 3286e5e974..0e56753164 100644 --- a/libraries/fbx/src/OBJReader.cpp +++ b/libraries/fbx/src/OBJReader.cpp @@ -561,7 +561,7 @@ FBXGeometry* OBJReader::readOBJ(QByteArray& model, const QVariantHash& mapping, modelMaterial->setEmissive(fbxMaterial.emissiveColor); modelMaterial->setAlbedo(fbxMaterial.diffuseColor); modelMaterial->setMetallic(glm::length(fbxMaterial.specularColor)); - modelMaterial->setGloss(fbxMaterial.shininess); + modelMaterial->setRoughness(model::Material::shininessToRoughness(fbxMaterial.shininess)); if (fbxMaterial.opacity <= 0.0f) { modelMaterial->setOpacity(1.0f); diff --git a/libraries/model/src/model/Material.cpp b/libraries/model/src/model/Material.cpp index 95f540e762..206b95e8a1 100755 --- a/libraries/model/src/model/Material.cpp +++ b/libraries/model/src/model/Material.cpp @@ -58,12 +58,17 @@ void Material::setEmissive(const Color& emissive, bool isSRGB) { _key.setEmissive(glm::any(glm::greaterThan(emissive, Color(0.0f)))); _schemaBuffer.edit()._emissive = (isSRGB ? ColorUtils::toLinearVec3(emissive) : emissive); } - +/* void Material::setGloss(float gloss) { - _key.setGloss((gloss > 0.0f)); - _schemaBuffer.edit()._gloss = gloss; + setRoughness(1.0 - gloss); +}*/ +void Material::setRoughness(float roughness) { + roughness = std::min(1.0f, std::max(roughness, 0.0f)); + _key.setGloss((roughness < 1.0f)); + _schemaBuffer.edit()._roughness = roughness; } + void Material::setOpacity(float opacity) { _key.setTransparent((opacity < 1.0f)); _schemaBuffer.edit()._opacity = opacity; diff --git a/libraries/model/src/model/Material.h b/libraries/model/src/model/Material.h index d2c7a04683..a5281bb117 100755 --- a/libraries/model/src/model/Material.h +++ b/libraries/model/src/model/Material.h @@ -229,8 +229,10 @@ public: void setMetallic(float metallic); float getMetallic() const { return _schemaBuffer.get()._metallic.x; } - void setGloss(float gloss); - float getGloss() const { return _schemaBuffer.get()._gloss; } + // void setGloss(float gloss); + void setRoughness(float gloss); + float getGloss() const { return 1.0f - getRoughness(); } + float getRoughness() const { return _schemaBuffer.get()._roughness; } void setOpacity(float opacity); float getOpacity() const { return _schemaBuffer.get()._opacity; } @@ -242,7 +244,7 @@ public: glm::vec3 _albedo{ 0.5f }; float _opacity{1.f}; glm::vec3 _metallic{ 0.03f }; - float _gloss{0.1f}; + float _roughness{ 0.9f }; glm::vec3 _emissive{ 0.0f }; float _spare0{0.0f}; glm::vec4 _spareVec4{0.0f}; // for alignment beauty, Material size == Mat4x4 @@ -256,6 +258,9 @@ public: void setTextureMap(MapChannel channel, const TextureMapPointer& textureMap); const TextureMaps& getTextureMaps() const { return _textureMaps; } + // conversion from previous properties to PBR equivalent + static float shininessToRoughness(float shininess) { return 1.0f - shininess / 128.0f; } + protected: MaterialKey _key; diff --git a/libraries/render-utils/src/MeshPartPayload.cpp b/libraries/render-utils/src/MeshPartPayload.cpp index 31f17c3bc2..a78d384dd1 100644 --- a/libraries/render-utils/src/MeshPartPayload.cpp +++ b/libraries/render-utils/src/MeshPartPayload.cpp @@ -167,7 +167,7 @@ void MeshPartPayload::bindMaterial(gpu::Batch& batch, const ShapePipeline::Locat if (normalMap && normalMap->isDefined()) { batch.setResourceTexture(ShapePipeline::Slot::NORMAL_MAP, normalMap->getTextureView()); - // texcoord are assumed to be the same has diffuse + // texcoord are assumed to be the same has albedo } else { batch.setResourceTexture(ShapePipeline::Slot::NORMAL_MAP, textureCache->getBlueTexture()); } @@ -181,7 +181,7 @@ void MeshPartPayload::bindMaterial(gpu::Batch& batch, const ShapePipeline::Locat if (specularMap && specularMap->isDefined()) { batch.setResourceTexture(ShapePipeline::Slot::SPECULAR_MAP, specularMap->getTextureView()); - // texcoord are assumed to be the same has diffuse + // texcoord are assumed to be the same has albedo } else { batch.setResourceTexture(ShapePipeline::Slot::SPECULAR_MAP, textureCache->getBlackTexture()); } diff --git a/libraries/render-utils/src/Model.cpp b/libraries/render-utils/src/Model.cpp index 2ef5b5e792..e1646437d1 100644 --- a/libraries/render-utils/src/Model.cpp +++ b/libraries/render-utils/src/Model.cpp @@ -1199,7 +1199,7 @@ void Model::segregateMeshGroups() { _collisionHullMaterial = std::make_shared(); _collisionHullMaterial->setAlbedo(glm::vec3(1.0f, 0.5f, 0.0f)); _collisionHullMaterial->setMetallic(0.02f); - _collisionHullMaterial->setGloss(1.0f); + _collisionHullMaterial->setRoughness(0.5f); } _renderItemsSet << std::make_shared(networkMesh._mesh, partIndex, _collisionHullMaterial, transform, offset); } else { diff --git a/libraries/render-utils/src/RenderPipelines.cpp b/libraries/render-utils/src/RenderPipelines.cpp index fbaa7df1b1..1011d4081c 100644 --- a/libraries/render-utils/src/RenderPipelines.cpp +++ b/libraries/render-utils/src/RenderPipelines.cpp @@ -69,7 +69,7 @@ gpu::BufferView getDefaultMaterialBuffer() { schema._albedo = vec3(1.0f); schema._opacity = 1.0f; schema._metallic = vec3(0.1f); - schema._gloss = 10.0f; + schema._roughness = 0.9f; return gpu::BufferView(std::make_shared(sizeof(model::Material::Schema), (const gpu::Byte*) &schema)); } diff --git a/libraries/render-utils/src/simple_textured.slf b/libraries/render-utils/src/simple_textured.slf index 727c029bbe..6c99343a28 100644 --- a/libraries/render-utils/src/simple_textured.slf +++ b/libraries/render-utils/src/simple_textured.slf @@ -15,7 +15,7 @@ <@include DeferredBufferWrite.slh@> <@include model/Material.slh@> -// the diffuse texture +// the albedo texture uniform sampler2D originalTexture; // the interpolated normal diff --git a/libraries/render-utils/src/simple_textured_emisive.slf b/libraries/render-utils/src/simple_textured_emisive.slf index 1dd3d667a6..2119af17fb 100644 --- a/libraries/render-utils/src/simple_textured_emisive.slf +++ b/libraries/render-utils/src/simple_textured_emisive.slf @@ -14,7 +14,7 @@ <@include DeferredBufferWrite.slh@> -// the diffuse texture +// the albedo texture uniform sampler2D originalTexture; // the interpolated normal From 2ed7f997e3d493aad6fc3e1d1da096859ccfb0b8 Mon Sep 17 00:00:00 2001 From: samcake Date: Wed, 17 Feb 2016 10:44:15 -0800 Subject: [PATCH 04/24] A few changes in the material call back --- libraries/fbx/src/FBXReader.cpp | 2 ++ libraries/model/src/model/Material.slh | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/libraries/fbx/src/FBXReader.cpp b/libraries/fbx/src/FBXReader.cpp index daaa2d94be..f4225e6e73 100644 --- a/libraries/fbx/src/FBXReader.cpp +++ b/libraries/fbx/src/FBXReader.cpp @@ -867,6 +867,8 @@ FBXGeometry* FBXReader::extractFBXGeometry(const QVariantHash& mapping, const QS FBXMaterial material; if (object.properties.at(1).toByteArray().contains("StingrayPBS1")) { material.isPBSMaterial = true; + } else if (object.properties.at(1).toByteArray().contains("StingrayPBS2")) { + material.isPBSMaterial = true; } foreach (const FBXNode& subobject, object.children) { bool properties = false; diff --git a/libraries/model/src/model/Material.slh b/libraries/model/src/model/Material.slh index 1f9a290fe7..a190a1c552 100644 --- a/libraries/model/src/model/Material.slh +++ b/libraries/model/src/model/Material.slh @@ -29,6 +29,6 @@ Material getMaterial() { float getMaterialOpacity(Material m) { return m._albedoOpacity.a; } vec3 getMaterialAlbedo(Material m) { return m._albedoOpacity.rgb; } vec3 getMaterialSpecular(Material m) { return m._specular.rgb; } -float getMaterialShininess(Material m) { return m._specular.a; } +float getMaterialShininess(Material m) { return (1 - m._specular.a); } <@endif@> From d7edc8c2ba789aee877d835c5492e07690cf4463 Mon Sep 17 00:00:00 2001 From: samcake Date: Wed, 17 Feb 2016 16:06:30 -0800 Subject: [PATCH 05/24] Adding more fields ready for the PBR stingray materials --- libraries/fbx/src/FBXReader.cpp | 57 ++++++++++++++++++++++++--------- libraries/fbx/src/FBXReader.h | 25 +++++++++++---- 2 files changed, 60 insertions(+), 22 deletions(-) diff --git a/libraries/fbx/src/FBXReader.cpp b/libraries/fbx/src/FBXReader.cpp index f4225e6e73..a95a66f7e1 100644 --- a/libraries/fbx/src/FBXReader.cpp +++ b/libraries/fbx/src/FBXReader.cpp @@ -923,12 +923,34 @@ FBXGeometry* FBXReader::extractFBXGeometry(const QVariantHash& mapping, const QS std::vector unknowns; foreach(const FBXNode& property, subobject.children) { if (property.name == propertyName) { - if (property.properties.at(0) == "Maya|base_color") { + if (property.properties.at(0) == "Maya|use_normal_map") { + material.useNormalMap = (bool)property.properties.at(index).value(); + + } else if (property.properties.at(0) == "Maya|base_color") { material.diffuseColor = getVec3(property.properties, index); - } else if (property.properties.at(0) == "Maya|metallic") { - material.metallic = property.properties.at(index).value(); + } else if (property.properties.at(0) == "Maya|use_color_map") { + material.useAlbedoMap = (bool) property.properties.at(index).value(); + } else if (property.properties.at(0) == "Maya|roughness") { material.roughness = property.properties.at(index).value(); + } else if (property.properties.at(0) == "Maya|use_roughness_map") { + material.useRoughnessMap = (bool)property.properties.at(index).value(); + + } else if (property.properties.at(0) == "Maya|metallic") { + material.metallic = property.properties.at(index).value(); + } else if (property.properties.at(0) == "Maya|use_metallic_map") { + material.useMetallicMap = (bool)property.properties.at(index).value(); + + } else if (property.properties.at(0) == "Maya|emissive") { + material.emissiveColor = getVec3(property.properties, index); + } else if (property.properties.at(0) == "Maya|emissive_intensity") { + material.emissiveIntensity = property.properties.at(index).value(); + } else if (property.properties.at(0) == "Maya|use_emissive_map") { + material.useEmissiveMap = (bool)property.properties.at(index).value(); + + } else if (property.properties.at(0) == "Maya|use_ao_map") { + material.useOcclusionMap = (bool)property.properties.at(index).value(); + } else { const QString propname = property.properties.at(0).toString(); unknowns.push_back(propname.toStdString()); @@ -1054,7 +1076,6 @@ FBXGeometry* FBXReader::extractFBXGeometry(const QVariantHash& mapping, const QS if (type.contains("diffuse")) { diffuseTextures.insert(getID(connection.properties, 2), getID(connection.properties, 1)); } else if (type.contains("tex_color_map")) { - qDebug() << "insert color map for diffuse!"; diffuseTextures.insert(getID(connection.properties, 2), getID(connection.properties, 1)); } else if (type.contains("transparentcolor")) { // it should be TransparentColor... // THis is how Maya assign a texture that affect diffuse color AND transparency ? @@ -1063,10 +1084,25 @@ FBXGeometry* FBXReader::extractFBXGeometry(const QVariantHash& mapping, const QS bumpTextures.insert(getID(connection.properties, 2), getID(connection.properties, 1)); } else if (type.contains("normal")) { normalTextures.insert(getID(connection.properties, 2), getID(connection.properties, 1)); + } else if (type.contains("tex_normal_map")) { + normalTextures.insert(getID(connection.properties, 2), getID(connection.properties, 1)); } else if (type.contains("specular") || type.contains("reflection")) { specularTextures.insert(getID(connection.properties, 2), getID(connection.properties, 1)); - } else if (type.contains("metallic")) { + } else if (type.contains("tex_metallic_map")) { metallicTextures.insert(getID(connection.properties, 2), getID(connection.properties, 1)); + } else if (type.contains("shininess")) { + shininessTextures.insert(getID(connection.properties, 2), getID(connection.properties, 1)); + } else if (type.contains("tex_roughness_map")) { + roughnessTextures.insert(getID(connection.properties, 2), getID(connection.properties, 1)); + } else if (_loadLightmaps && type.contains("emissive")) { + emissiveTextures.insert(getID(connection.properties, 2), getID(connection.properties, 1)); + } else if (type.contains("tex_emissive_map")) { + roughnessTextures.insert(getID(connection.properties, 2), getID(connection.properties, 1)); + } else if (_loadLightmaps && type.contains("ambient")) { + ambientTextures.insert(getID(connection.properties, 2), getID(connection.properties, 1)); + } else if (_loadLightmaps && type.contains("tex_ao_map")) { + occlusionTextures.insert(getID(connection.properties, 2), getID(connection.properties, 1)); + } else if (type == "lcl rotation") { localRotations.insert(getID(connection.properties, 2), getID(connection.properties, 1)); } else if (type == "lcl translation") { @@ -1079,17 +1115,6 @@ FBXGeometry* FBXReader::extractFBXGeometry(const QVariantHash& mapping, const QS } else if (type == "d|z") { zComponents.insert(getID(connection.properties, 2), getID(connection.properties, 1)); - } else if (type.contains("shininess")) { - shininessTextures.insert(getID(connection.properties, 2), getID(connection.properties, 1)); - counter++; - } else if (type.contains("roughness")) { - roughnessTextures.insert(getID(connection.properties, 2), getID(connection.properties, 1)); - counter++; - } else if (_loadLightmaps && type.contains("emissive")) { - emissiveTextures.insert(getID(connection.properties, 2), getID(connection.properties, 1)); - - } else if (_loadLightmaps && type.contains("ambient")) { - ambientTextures.insert(getID(connection.properties, 2), getID(connection.properties, 1)); } else { QString typenam = type.data(); counter++; diff --git a/libraries/fbx/src/FBXReader.h b/libraries/fbx/src/FBXReader.h index 755a1e70f2..680cbe2fd4 100644 --- a/libraries/fbx/src/FBXReader.h +++ b/libraries/fbx/src/FBXReader.h @@ -151,17 +151,29 @@ public: float metallic{ 0.0f }; float roughness{ 1.0f }; + float emissiveIntensity{ 1.0f }; + + bool useNormalMap{ false }; + bool useAlbedoMap{ false }; + bool useOpacityMap{ false }; + bool useRoughnessMap{ false }; + bool useSpecularMap{ false }; + bool useMetallicMap{ false }; + bool useEmissiveMap{ false }; + bool useOcclusionMap{ false }; QString materialID; model::MaterialPointer _material; + FBXTexture normalTexture; FBXTexture albedoTexture; FBXTexture opacityTexture; - FBXTexture normalTexture; - FBXTexture specularTexture; - FBXTexture emissiveTexture; - FBXTexture metallicTexture; FBXTexture roughnessTexture; + FBXTexture specularTexture; + FBXTexture metallicTexture; + FBXTexture emissiveTexture; + FBXTexture occlusionTexture; + bool isPBSMaterial{ false }; bool needTangentSpace() const; @@ -407,11 +419,12 @@ public: QHash bumpTextures; QHash normalTextures; QHash specularTextures; - QHash emissiveTextures; - QHash ambientTextures; QHash metallicTextures; QHash roughnessTextures; QHash shininessTextures; + QHash emissiveTextures; + QHash ambientTextures; + QHash occlusionTextures; QHash _fbxMaterials; From 903824809c2ee76c2901016a9b71f97b8fb7b76a Mon Sep 17 00:00:00 2001 From: samcake Date: Thu, 18 Feb 2016 18:33:32 -0800 Subject: [PATCH 06/24] trying to have better support for the materials --- .../tools/render/debugFramebuffer.js | 51 +++++++++++++++++++ .../utilities/tools/render/framebuffer.qml | 40 +++++++++++++++ libraries/fbx/src/FBXReader.cpp | 12 ++--- libraries/fbx/src/FBXReader.h | 6 ++- libraries/fbx/src/FBXReader_Material.cpp | 46 +++++++++-------- .../src/model-networking/ModelCache.cpp | 46 ++++++++++++++--- .../src/model-networking/ModelCache.h | 4 ++ .../src/model-networking/TextureCache.cpp | 4 ++ .../src/model-networking/TextureCache.h | 2 +- libraries/model/src/model/TextureMap.cpp | 49 ++++++++++++++++++ libraries/model/src/model/TextureMap.h | 1 + libraries/render-utils/src/DeferredBuffer.slh | 2 +- .../render-utils/src/DeferredGlobalLight.slh | 12 ++--- .../render-utils/src/DeferredLighting.slh | 6 +-- .../render-utils/src/MeshPartPayload.cpp | 6 +-- 15 files changed, 236 insertions(+), 51 deletions(-) create mode 100644 examples/utilities/tools/render/debugFramebuffer.js create mode 100644 examples/utilities/tools/render/framebuffer.qml diff --git a/examples/utilities/tools/render/debugFramebuffer.js b/examples/utilities/tools/render/debugFramebuffer.js new file mode 100644 index 0000000000..4aad8f9488 --- /dev/null +++ b/examples/utilities/tools/render/debugFramebuffer.js @@ -0,0 +1,51 @@ +// +// debug.js +// examples/utilities/tools/render +// +// Sam Gateau created on 2/18/2016. +// Copyright 2016 High Fidelity, Inc. +// +// Distributed under the Apache License, Version 2.0. +// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html +// + +var DDB = Render.RenderDeferredTask.DebugDeferredBuffer; +DDB.enabled = true; +oldConfig = DDB.toJSON(); + + +// Set up the qml ui +var qml = Script.resolvePath('framebuffer.qml'); +var window = new OverlayWindow({ + title: 'Framebuffer Debug', + source: qml, + width: 400, height: 400, +}); +window.setPosition(25, 50); +window.closed.connect(function() { Script.stop(); }); + +// Debug buffer sizing +var resizing = false; + +Controller.mousePressEvent.connect(function (e) { + if (shouldStartResizing(e.x)) { + resizing = true; + } +}); +Controller.mouseReleaseEvent.connect(function() { resizing = false; }); +Controller.mouseMoveEvent.connect(function (e) { resizing && setDebugBufferSize(e.x); }); + + +function shouldStartResizing(eventX) { + var x = Math.abs(eventX - Window.innerWidth * (1.0 + DDB.size.x) / 2.0); + var mode = DDB.mode; + return mode !== -1 && x < 20; +} + +function setDebugBufferSize(x) { + x = (2.0 * (x / Window.innerWidth) - 1.0); // scale + x = Math.min(Math.max(-1, x), 1); // clamp + DDB.size = { x: x, y: -1, z: 1, w: 1 }; +} + +Script.scriptEnding.connect(function () { DDB.fromJSON(oldConfig); }); diff --git a/examples/utilities/tools/render/framebuffer.qml b/examples/utilities/tools/render/framebuffer.qml new file mode 100644 index 0000000000..734c62ad19 --- /dev/null +++ b/examples/utilities/tools/render/framebuffer.qml @@ -0,0 +1,40 @@ +// +// main.qml +// examples/utilities/tools/render +// +// Created by Zach Pomerantz on 2/8/2016 +// Copyright 2016 High Fidelity, Inc. +// +// Distributed under the Apache License, Version 2.0. +// See the accompanying file LICENSE or https://www.apache.org/licenses/LICENSE-2.0.html +// +import QtQuick 2.5 +import QtQuick.Controls 1.4 + +Column { + spacing: 8 + Column { + id: debug + property var config: Render.getConfig("DebugDeferredBuffer") + + function setDebugMode(mode) { + debug.config.enabled = (mode != -1); + debug.config.mode = mode; + } + + Label { text: qsTr("Debug Buffer") } + ExclusiveGroup { id: bufferGroup } + Repeater { + model: [ + "Off", "Diffuse", "Metallic", "Roughness", "Normal", "Depth", + "Lighting", "Shadow", "Pyramid Depth", "Ambient Occlusion", "Custom Shader" + ] + RadioButton { + text: qsTr(modelData) + exclusiveGroup: bufferGroup + checked: index == 0 + onCheckedChanged: if (checked) debug.setDebugMode(index - 1); + } + } + } +} diff --git a/libraries/fbx/src/FBXReader.cpp b/libraries/fbx/src/FBXReader.cpp index 7af67039ef..1778e06894 100644 --- a/libraries/fbx/src/FBXReader.cpp +++ b/libraries/fbx/src/FBXReader.cpp @@ -865,9 +865,7 @@ FBXGeometry* FBXReader::extractFBXGeometry(const QVariantHash& mapping, const QS } } else if (object.name == "Material") { FBXMaterial material; - if (object.properties.at(1).toByteArray().contains("StingrayPBS1")) { - material.isPBSMaterial = true; - } else if (object.properties.at(1).toByteArray().contains("StingrayPBS2")) { + if (object.properties.at(1).toByteArray().contains("StingrayPBS")) { material.isPBSMaterial = true; } foreach (const FBXNode& subobject, object.children) { @@ -1079,7 +1077,7 @@ FBXGeometry* FBXReader::extractFBXGeometry(const QVariantHash& mapping, const QS diffuseTextures.insert(getID(connection.properties, 2), getID(connection.properties, 1)); } else if (type.contains("transparentcolor")) { // it should be TransparentColor... // THis is how Maya assign a texture that affect diffuse color AND transparency ? - diffuseTextures.insert(getID(connection.properties, 2), getID(connection.properties, 1)); + transparentTextures.insert(getID(connection.properties, 2), getID(connection.properties, 1)); } else if (type.contains("bump")) { bumpTextures.insert(getID(connection.properties, 2), getID(connection.properties, 1)); } else if (type.contains("normal")) { @@ -1094,13 +1092,13 @@ FBXGeometry* FBXReader::extractFBXGeometry(const QVariantHash& mapping, const QS shininessTextures.insert(getID(connection.properties, 2), getID(connection.properties, 1)); } else if (type.contains("tex_roughness_map")) { roughnessTextures.insert(getID(connection.properties, 2), getID(connection.properties, 1)); - } else if (_loadLightmaps && type.contains("emissive")) { + } else if (type.contains("emissive")) { emissiveTextures.insert(getID(connection.properties, 2), getID(connection.properties, 1)); } else if (type.contains("tex_emissive_map")) { roughnessTextures.insert(getID(connection.properties, 2), getID(connection.properties, 1)); - } else if (_loadLightmaps && type.contains("ambient")) { + } else if (type.contains("ambient")) { ambientTextures.insert(getID(connection.properties, 2), getID(connection.properties, 1)); - } else if (_loadLightmaps && type.contains("tex_ao_map")) { + } else if (type.contains("tex_ao_map")) { occlusionTextures.insert(getID(connection.properties, 2), getID(connection.properties, 1)); } else if (type == "lcl rotation") { diff --git a/libraries/fbx/src/FBXReader.h b/libraries/fbx/src/FBXReader.h index 680cbe2fd4..6a59ac6aef 100644 --- a/libraries/fbx/src/FBXReader.h +++ b/libraries/fbx/src/FBXReader.h @@ -135,7 +135,7 @@ public: diffuseColor(diffuseColor), specularColor(specularColor), emissiveColor(emissiveColor), - emissiveParams(emissiveParams), + lightmapParams(emissiveParams), shininess(shininess), opacity(opacity) {} @@ -145,7 +145,6 @@ public: float specularFactor = 1.0f; glm::vec3 emissiveColor{ 0.0f }; - glm::vec2 emissiveParams{ 0.0f, 1.0f }; float shininess = 23.0f; float opacity = 1.0f; @@ -173,6 +172,8 @@ public: FBXTexture metallicTexture; FBXTexture emissiveTexture; FBXTexture occlusionTexture; + FBXTexture lightmapTexture; + glm::vec2 lightmapParams{ 0.0f, 1.0f }; bool isPBSMaterial{ false }; @@ -416,6 +417,7 @@ public: QHash diffuseTextures; + QHash transparentTextures; QHash bumpTextures; QHash normalTextures; QHash specularTextures; diff --git a/libraries/fbx/src/FBXReader_Material.cpp b/libraries/fbx/src/FBXReader_Material.cpp index 64ee958330..cce2f2162a 100644 --- a/libraries/fbx/src/FBXReader_Material.cpp +++ b/libraries/fbx/src/FBXReader_Material.cpp @@ -80,10 +80,19 @@ void FBXReader::consolidateFBXMaterials() { } material.albedoTexture = diffuseTexture; - detectDifferentUVs = (diffuseTexture.texcoordSet != 0) || (!diffuseTexture.transform.isIdentity()); } - + + + FBXTexture transparentTexture; + QString transparentTextureID = transparentTextures.value(material.materialID); + if (!transparentTextureID.isNull()) { + transparentTexture = getTexture(transparentTextureID); + + material.opacityTexture = transparentTexture; + detectDifferentUVs |= (transparentTexture.texcoordSet != 0) || (!transparentTexture.transform.isIdentity()); + } + FBXTexture normalTexture; QString bumpTextureID = bumpTextures.value(material.materialID); QString normalTextureID = normalTextures.value(material.materialID); @@ -133,25 +142,23 @@ void FBXReader::consolidateFBXMaterials() { } FBXTexture emissiveTexture; - glm::vec2 emissiveParams(0.f, 1.f); - emissiveParams.x = _lightmapOffset; - emissiveParams.y = _lightmapLevel; - QString emissiveTextureID = emissiveTextures.value(material.materialID); - QString ambientTextureID = ambientTextures.value(material.materialID); - if (_loadLightmaps && (!emissiveTextureID.isNull() || !ambientTextureID.isNull())) { - - if (!emissiveTextureID.isNull()) { - emissiveTexture = getTexture(emissiveTextureID); - emissiveParams.y = 4.0f; - } else if (!ambientTextureID.isNull()) { - emissiveTexture = getTexture(ambientTextureID); - } - - material.emissiveParams = emissiveParams; - material.emissiveTexture = emissiveTexture; - + if (!emissiveTextureID.isNull()) { + emissiveTexture = getTexture(emissiveTextureID); detectDifferentUVs |= (emissiveTexture.texcoordSet != 0) || (!emissiveTexture.transform.isIdentity()); + material.emissiveTexture = emissiveTexture; + } + + 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()) { + ambientTexture = getTexture(ambientTextureID); + detectDifferentUVs |= (ambientTexture.texcoordSet != 0) || (!ambientTexture.transform.isIdentity()); + material.lightmapTexture = ambientTexture; + material.lightmapParams = lightmapParams; } // Finally create the true material representation @@ -168,7 +175,6 @@ void FBXReader::consolidateFBXMaterials() { material._material->setMetallic(material.metallic); } else { material._material->setRoughness(model::Material::shininessToRoughness(material.shininess)); - float metallic = std::max(material.specularColor.x, std::max(material.specularColor.y, material.specularColor.z)); // FIXME: Do not use the Specular Factor yet as some FBX models have it set to 0 // metallic *= material.specularFactor; diff --git a/libraries/model-networking/src/model-networking/ModelCache.cpp b/libraries/model-networking/src/model-networking/ModelCache.cpp index 6551ca5a60..3de21e5b0e 100644 --- a/libraries/model-networking/src/model-networking/ModelCache.cpp +++ b/libraries/model-networking/src/model-networking/ModelCache.cpp @@ -139,7 +139,8 @@ bool NetworkGeometry::isLoadedWithTextures() const { if ((material->albedoTexture && !material->albedoTexture->isLoaded()) || (material->normalTexture && !material->normalTexture->isLoaded()) || (material->specularTexture && !material->specularTexture->isLoaded()) || - (material->emissiveTexture && !material->emissiveTexture->isLoaded())) { + (material->emissiveTexture && !material->emissiveTexture->isLoaded()) || + (material->lightmapTexture && !material->lightmapTexture->isLoaded())) { return false; } } @@ -180,6 +181,13 @@ void NetworkGeometry::setTextureWithNameToURL(const QString& name, const QUrl& u } else if (material->emissiveTextureName == name) { material->emissiveTexture = textureCache->getTexture(url); + auto emissiveMap = model::TextureMapPointer(new model::TextureMap()); + emissiveMap->setTextureSource(material->emissiveTexture->_textureSource); + + networkMaterial->setTextureMap(model::MaterialKey::EMISSIVE_MAP, emissiveMap); + } else if (material->lightmapTextureName == name) { + material->emissiveTexture = textureCache->getTexture(url); + auto lightmapMap = model::TextureMapPointer(new model::TextureMap()); lightmapMap->setTextureSource(material->emissiveTexture->_textureSource); lightmapMap->setTextureTransform( @@ -219,6 +227,10 @@ QStringList NetworkGeometry::getTextureNames() const { QString textureURL = material->emissiveTexture->getURL().toString(); result << material->emissiveTextureName + ":\"" + textureURL + "\""; } + if (!material->lightmapTextureName.isEmpty() && material->lightmapTexture) { + QString textureURL = material->lightmapTexture->getURL().toString(); + result << material->lightmapTextureName + ":\"" + textureURL + "\""; + } } return result; @@ -333,20 +345,38 @@ static NetworkMaterial* buildNetworkMaterial(const FBXMaterial& material, const networkMaterial->specularTexture = textureCache->getTexture(textureBaseUrl.resolved(QUrl(material.specularTexture.filename)), SPECULAR_TEXTURE, material.specularTexture.content); networkMaterial->specularTextureName = material.specularTexture.name; - auto glossMap = model::TextureMapPointer(new model::TextureMap()); - glossMap->setTextureSource(networkMaterial->specularTexture->_textureSource); + auto specularMap = model::TextureMapPointer(new model::TextureMap()); + specularMap->setTextureSource(networkMaterial->specularTexture->_textureSource); - material._material->setTextureMap(model::MaterialKey::GLOSS_MAP, glossMap); + material._material->setTextureMap(model::MaterialKey::METALLIC_MAP, specularMap); + } + if (!material.roughnessTexture.filename.isEmpty()) { + networkMaterial->roughnessTexture = textureCache->getTexture(textureBaseUrl.resolved(QUrl(material.roughnessTexture.filename)), ROUGHNESS_TEXTURE, material.roughnessTexture.content); + networkMaterial->roughnessTextureName = material.roughnessTexture.name; + + auto roughnessMap = model::TextureMapPointer(new model::TextureMap()); + roughnessMap->setTextureSource(networkMaterial->roughnessTexture->_textureSource); + + material._material->setTextureMap(model::MaterialKey::GLOSS_MAP, roughnessMap); } if (!material.emissiveTexture.filename.isEmpty()) { - networkMaterial->emissiveTexture = textureCache->getTexture(textureBaseUrl.resolved(QUrl(material.emissiveTexture.filename)), LIGHTMAP_TEXTURE, material.emissiveTexture.content); + networkMaterial->emissiveTexture = textureCache->getTexture(textureBaseUrl.resolved(QUrl(material.emissiveTexture.filename)), EMISSIVE_TEXTURE, material.emissiveTexture.content); networkMaterial->emissiveTextureName = material.emissiveTexture.name; + auto emissiveMap = model::TextureMapPointer(new model::TextureMap()); + emissiveMap->setTextureSource(networkMaterial->emissiveTexture->_textureSource); + + material._material->setTextureMap(model::MaterialKey::EMISSIVE_MAP, emissiveMap); + } + if (!material.lightmapTexture.filename.isEmpty()) { + networkMaterial->lightmapTexture = textureCache->getTexture(textureBaseUrl.resolved(QUrl(material.lightmapTexture.filename)), LIGHTMAP_TEXTURE, material.lightmapTexture.content); + networkMaterial->lightmapTextureName = material.lightmapTexture.name; + auto lightmapMap = model::TextureMapPointer(new model::TextureMap()); - lightmapMap->setTextureSource(networkMaterial->emissiveTexture->_textureSource); - lightmapMap->setTextureTransform(material.emissiveTexture.transform); - lightmapMap->setLightmapOffsetScale(material.emissiveParams.x, material.emissiveParams.y); + lightmapMap->setTextureSource(networkMaterial->lightmapTexture->_textureSource); + lightmapMap->setTextureTransform(material.lightmapTexture.transform); + lightmapMap->setLightmapOffsetScale(material.lightmapParams.x, material.lightmapParams.y); material._material->setTextureMap(model::MaterialKey::LIGHTMAP_MAP, lightmapMap); } diff --git a/libraries/model-networking/src/model-networking/ModelCache.h b/libraries/model-networking/src/model-networking/ModelCache.h index 2cb67e7e87..1d43424813 100644 --- a/libraries/model-networking/src/model-networking/ModelCache.h +++ b/libraries/model-networking/src/model-networking/ModelCache.h @@ -180,8 +180,12 @@ public: QSharedPointer normalTexture; QString specularTextureName; QSharedPointer specularTexture; + QString roughnessTextureName; + QSharedPointer roughnessTexture; QString emissiveTextureName; QSharedPointer emissiveTexture; + QString lightmapTextureName; + QSharedPointer lightmapTexture; }; diff --git a/libraries/model-networking/src/model-networking/TextureCache.cpp b/libraries/model-networking/src/model-networking/TextureCache.cpp index 8201339e93..2ec9ab4027 100644 --- a/libraries/model-networking/src/model-networking/TextureCache.cpp +++ b/libraries/model-networking/src/model-networking/TextureCache.cpp @@ -212,6 +212,10 @@ NetworkTexture::TextureLoaderFunc NetworkTexture::getTextureLoader() const { return TextureLoaderFunc(model::TextureUsage::createNormalTextureFromNormalImage); break; } + case ROUGHNESS_TEXTURE: { + return TextureLoaderFunc(model::TextureUsage::createRoughnessTextureFromImage); + break; + } case CUSTOM_TEXTURE: { return _textureLoader; break; diff --git a/libraries/model-networking/src/model-networking/TextureCache.h b/libraries/model-networking/src/model-networking/TextureCache.h index 033b4bede6..1a0d22a8d3 100644 --- a/libraries/model-networking/src/model-networking/TextureCache.h +++ b/libraries/model-networking/src/model-networking/TextureCache.h @@ -29,7 +29,7 @@ class NetworkTexture; typedef QSharedPointer NetworkTexturePointer; -enum TextureType { DEFAULT_TEXTURE, NORMAL_TEXTURE, BUMP_TEXTURE, SPECULAR_TEXTURE, EMISSIVE_TEXTURE, CUBE_TEXTURE, LIGHTMAP_TEXTURE, CUSTOM_TEXTURE }; +enum TextureType { DEFAULT_TEXTURE, NORMAL_TEXTURE, BUMP_TEXTURE, SPECULAR_TEXTURE, ROUGHNESS_TEXTURE, EMISSIVE_TEXTURE, CUBE_TEXTURE, LIGHTMAP_TEXTURE, CUSTOM_TEXTURE }; /// Stores cached textures, including render-to-texture targets. class TextureCache : public ResourceCache, public Dependency { diff --git a/libraries/model/src/model/TextureMap.cpp b/libraries/model/src/model/TextureMap.cpp index cd42419f6a..17d610e87f 100755 --- a/libraries/model/src/model/TextureMap.cpp +++ b/libraries/model/src/model/TextureMap.cpp @@ -223,6 +223,55 @@ gpu::Texture* TextureUsage::createNormalTextureFromBumpImage(const QImage& srcIm return theTexture; } +gpu::Texture* TextureUsage::createRoughnessTextureFromImage(const QImage& srcImage, const std::string& srcImageName) { + QImage image = srcImage; + if (!image.hasAlphaChannel()) { + if (image.format() != QImage::Format_RGB888) { + image = image.convertToFormat(QImage::Format_RGB888); + } + } else { + if (image.format() != QImage::Format_ARGB32) { + image = image.convertToFormat(QImage::Format_ARGB32); + } + } + + image = image.convertToFormat(QImage::Format_Grayscale8); + + /* gpu::Texture* theTexture = nullptr; + if ((image.width() > 0) && (image.height() > 0)) { + + // Actual alpha channel? + for (int y = 0; y < image.height(); ++y) { + QRgb* data = reinterpret_cast(image.scanLine(y)); + for (int x = 0; x < image.width(); ++x) { + data[x]auto alpha = q(data[x]); + if (alpha != 255) { + validAlpha = true; + break; + } + } + } + */ + + + gpu::Texture* theTexture = nullptr; + if ((image.width() > 0) && (image.height() > 0)) { + // bool isLinearRGB = true; //(_type == NORMAL_TEXTURE) || (_type == EMISSIVE_TEXTURE); + bool isLinearRGB = false; //(_type == NORMAL_TEXTURE) || (_type == EMISSIVE_TEXTURE); + + gpu::Element formatGPU = gpu::Element(gpu::SCALAR, gpu::NUINT8, gpu::RGB); + gpu::Element formatMip = gpu::Element(gpu::SCALAR, gpu::NUINT8, gpu::RGB); + + theTexture = (gpu::Texture::create2D(formatGPU, image.width(), image.height(), gpu::Sampler(gpu::Sampler::FILTER_MIN_MAG_MIP_LINEAR))); + theTexture->assignStoredMip(0, formatMip, image.byteCount(), image.constBits()); + theTexture->autoGenerateMips(-1); + + // FIXME queue for transfer to GPU and block on completion + } + + return theTexture; +} + class CubeLayout { public: int _widthRatio = 1; diff --git a/libraries/model/src/model/TextureMap.h b/libraries/model/src/model/TextureMap.h index 4b625356e9..1e7f313dc8 100755 --- a/libraries/model/src/model/TextureMap.h +++ b/libraries/model/src/model/TextureMap.h @@ -34,6 +34,7 @@ public: static gpu::Texture* create2DTextureFromImage(const QImage& image, const std::string& srcImageName); static gpu::Texture* createNormalTextureFromNormalImage(const QImage& image, const std::string& srcImageName); static gpu::Texture* createNormalTextureFromBumpImage(const QImage& image, const std::string& srcImageName); + static gpu::Texture* createRoughnessTextureFromImage(const QImage& image, const std::string& srcImageName); static gpu::Texture* createCubeTextureFromImage(const QImage& image, const std::string& srcImageName); static gpu::Texture* createLightmapTextureFromImage(const QImage& image, const std::string& srcImageName); diff --git a/libraries/render-utils/src/DeferredBuffer.slh b/libraries/render-utils/src/DeferredBuffer.slh index 40c4349ae2..339b85ec1a 100755 --- a/libraries/render-utils/src/DeferredBuffer.slh +++ b/libraries/render-utils/src/DeferredBuffer.slh @@ -105,7 +105,7 @@ DeferredFragment unpackDeferredFragment(DeferredTransform deferredTransform, vec frag.diffuse = frag.diffuseVal.xyz; frag.specular = frag.specularVal.xyz; - frag.gloss = frag.specularVal.w; + frag.gloss = (frag.specularVal.w * 125)*(frag.specularVal.w * 125); return frag; diff --git a/libraries/render-utils/src/DeferredGlobalLight.slh b/libraries/render-utils/src/DeferredGlobalLight.slh index 18a1aaffb6..aad06c9305 100755 --- a/libraries/render-utils/src/DeferredGlobalLight.slh +++ b/libraries/render-utils/src/DeferredGlobalLight.slh @@ -30,7 +30,7 @@ vec4 evalSkyboxLight(vec3 direction, float lod) { <@include model/Light.slh@> <@func declareEvalAmbientGlobalColor()@> -vec3 evalAmbientGlobalColor(mat4 invViewMat, float shadowAttenuation, float obscurance, vec3 position, vec3 normal, vec3 albedo, vec3 specular, float gloss) { +vec3 evalAmbientGlobalColor(mat4 invViewMat, float shadowAttenuation, float obscurance, vec3 position, vec3 normal, vec3 albedo, vec3 fresnel, float gloss) { // Need the light now Light light = getLight(); @@ -41,7 +41,7 @@ vec3 evalAmbientGlobalColor(mat4 invViewMat, float shadowAttenuation, float obsc vec3 color = albedo * getLightColor(light) * obscurance * getLightAmbientIntensity(light); - vec4 shading = evalFragShading(fragNormal, -getLightDirection(light), fragEyeDir, specular, gloss); + vec4 shading = evalFragShading(fragNormal, -getLightDirection(light), fragEyeDir, fresnel, gloss); color += vec3(albedo * shading.w + shading.rgb) * min(shadowAttenuation, obscurance) * getLightColor(light) * getLightIntensity(light); @@ -52,7 +52,7 @@ vec3 evalAmbientGlobalColor(mat4 invViewMat, float shadowAttenuation, float obsc <@func declareEvalAmbientSphereGlobalColor()@> -vec3 evalAmbientSphereGlobalColor(mat4 invViewMat, float shadowAttenuation, float obscurance, vec3 position, vec3 normal, vec3 albedo, vec3 specular, float gloss) { +vec3 evalAmbientSphereGlobalColor(mat4 invViewMat, float shadowAttenuation, float obscurance, vec3 position, vec3 normal, vec3 albedo, vec3 fresnel, float gloss) { // Need the light now Light light = getLight(); @@ -64,7 +64,7 @@ vec3 evalAmbientSphereGlobalColor(mat4 invViewMat, float shadowAttenuation, floa vec3 color = albedo * evalSphericalLight(getLightAmbientSphere(light), ambientNormal).xyz * obscurance * getLightAmbientIntensity(light); - vec4 shading = evalFragShading(fragNormal, -getLightDirection(light), fragEyeDir, specular, gloss); + vec4 shading = evalFragShading(fragNormal, -getLightDirection(light), fragEyeDir, fresnel, gloss); color += vec3(albedo * shading.w + shading.rgb) * min(shadowAttenuation, obscurance) * getLightColor(light) * getLightIntensity(light); @@ -76,7 +76,7 @@ vec3 evalAmbientSphereGlobalColor(mat4 invViewMat, float shadowAttenuation, floa <$declareSkyboxMap()$> -vec3 evalSkyboxGlobalColor(mat4 invViewMat, float shadowAttenuation, float obscurance, vec3 position, vec3 normal, vec3 albedo, vec3 specular, float gloss) { +vec3 evalSkyboxGlobalColor(mat4 invViewMat, float shadowAttenuation, float obscurance, vec3 position, vec3 normal, vec3 albedo, vec3 fresnel, float gloss) { // Need the light now Light light = getLight(); @@ -87,7 +87,7 @@ vec3 evalSkyboxGlobalColor(mat4 invViewMat, float shadowAttenuation, float obscu vec3 color = albedo * evalSphericalLight(getLightAmbientSphere(light), fragNormal).xyz * obscurance * getLightAmbientIntensity(light); - vec4 shading = evalFragShading(fragNormal, -getLightDirection(light), fragEyeDir, specular, gloss); + vec4 shading = evalFragShading(fragNormal, -getLightDirection(light), fragEyeDir, fresnel, gloss); color += vec3(albedo * shading.w + shading.rgb) * min(shadowAttenuation, obscurance) * getLightColor(light) * getLightIntensity(light); diff --git a/libraries/render-utils/src/DeferredLighting.slh b/libraries/render-utils/src/DeferredLighting.slh index cf03861c2f..01403a8180 100755 --- a/libraries/render-utils/src/DeferredLighting.slh +++ b/libraries/render-utils/src/DeferredLighting.slh @@ -23,8 +23,8 @@ vec4 evalPBRShading(vec3 fragNormal, vec3 fragLightDir, vec3 fragEyeDir, vec3 sp // Specular Lighting depends on the half vector and the gloss vec3 halfDir = normalize(fragEyeDir + fragLightDir); - float specularPower = pow(max(0.0, dot(halfDir, fragNormal)), gloss * 128.0); - specularPower *= (gloss * 128.0 * 0.125 + 0.25); + float specularPower = pow(max(0.0, dot(halfDir, fragNormal)), gloss); + specularPower *= (gloss * 0.125 + 0.25); float shlickPower = (1.0 - dot(fragLightDir,halfDir)); float shlickPower2 = shlickPower * shlickPower; @@ -47,7 +47,7 @@ vec4 evalBlinnShading(vec3 fragNormal, vec3 fragLightDir, vec3 fragEyeDir, vec3 // Specular Lighting depends on the half vector and the gloss vec3 halfDir = normalize(fragEyeDir + fragLightDir); - float specularPower = pow(facingLight * max(0.0, dot(halfDir, fragNormal)), gloss * 128.0); + float specularPower = pow(facingLight * max(0.0, dot(halfDir, fragNormal)), gloss); vec3 reflect = specularPower * specular * diffuse; return vec4(reflect, diffuse); diff --git a/libraries/render-utils/src/MeshPartPayload.cpp b/libraries/render-utils/src/MeshPartPayload.cpp index a78d384dd1..f7642cf008 100644 --- a/libraries/render-utils/src/MeshPartPayload.cpp +++ b/libraries/render-utils/src/MeshPartPayload.cpp @@ -175,9 +175,9 @@ void MeshPartPayload::bindMaterial(gpu::Batch& batch, const ShapePipeline::Locat batch.setResourceTexture(ShapePipeline::Slot::NORMAL_MAP, nullptr); } - // TODO: For now gloss map is used as the "specular map in the shading, we ll need to fix that - if (materialKey.isGlossMap()) { - auto specularMap = textureMaps[model::MaterialKey::GLOSS_MAP]; + // Metallic map + if (materialKey.isMetallicMap()) { + auto specularMap = textureMaps[model::MaterialKey::METALLIC_MAP]; if (specularMap && specularMap->isDefined()) { batch.setResourceTexture(ShapePipeline::Slot::SPECULAR_MAP, specularMap->getTextureView()); From e103fd364e3b95dedc0523e832e2403a3105cec8 Mon Sep 17 00:00:00 2001 From: samcake Date: Fri, 19 Feb 2016 09:24:22 -0800 Subject: [PATCH 07/24] USing metallic and not gloss map --- libraries/render-utils/src/MeshPartPayload.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/libraries/render-utils/src/MeshPartPayload.cpp b/libraries/render-utils/src/MeshPartPayload.cpp index f7642cf008..9c6c8522fd 100644 --- a/libraries/render-utils/src/MeshPartPayload.cpp +++ b/libraries/render-utils/src/MeshPartPayload.cpp @@ -378,7 +378,8 @@ ShapeKey ModelMeshPartPayload::getShapeKey() const { bool isTranslucent = drawMaterialKey.isTransparent() || drawMaterialKey.isTransparentMap(); bool hasTangents = drawMaterialKey.isNormalMap() && !mesh.tangents.isEmpty(); - bool hasSpecular = drawMaterialKey.isGlossMap(); + //bool hasSpecular = drawMaterialKey.isGlossMap(); + bool hasSpecular = drawMaterialKey.isMetallicMap(); bool hasLightmap = drawMaterialKey.isLightmapMap(); bool isSkinned = _isSkinned; From 39a785297919ba58400ef97ba9870a3c216e77a4 Mon Sep 17 00:00:00 2001 From: samcake Date: Fri, 19 Feb 2016 18:43:07 -0800 Subject: [PATCH 08/24] MOving forward with PBR rendering, cleaning up the lighting shaders and the packi/unpack deferredBuffer shaders --- .../utilities/tools/render/framebuffer.qml | 16 ++++++- libraries/entities-renderer/src/polyvox.slf | 5 +- libraries/gpu/src/gpu/GLBackendShader.cpp | 5 +- .../src/model-networking/ModelCache.cpp | 13 +++++- libraries/model/src/model/Material.cpp | 2 +- libraries/model/src/model/Material.h | 39 ++++++++-------- .../render-utils/src/DebugDeferredBuffer.cpp | 46 ++++++++++++++++--- .../render-utils/src/DebugDeferredBuffer.h | 11 +++-- libraries/render-utils/src/DeferredBuffer.slh | 45 +++++++++++++----- .../render-utils/src/DeferredBufferWrite.slh | 17 ++++--- .../render-utils/src/DeferredGlobalLight.slh | 12 ++--- .../render-utils/src/DeferredLighting.slh | 16 ++++--- .../render-utils/src/MeshPartPayload.cpp | 16 ++++++- .../src/directional_ambient_light.slf | 3 +- .../src/directional_ambient_light_shadow.slf | 3 +- .../render-utils/src/directional_light.slf | 3 +- .../src/directional_light_shadow.slf | 3 +- .../src/directional_skybox_light.slf | 3 +- .../src/directional_skybox_light_shadow.slf | 3 +- libraries/render-utils/src/model.slf | 5 +- libraries/render-utils/src/model_emissive.slf | 3 +- libraries/render-utils/src/model_lightmap.slf | 3 +- .../src/model_lightmap_normal_map.slf | 3 +- .../model_lightmap_normal_specular_map.slf | 3 +- .../src/model_lightmap_specular_map.slf | 3 +- .../render-utils/src/model_normal_map.slf | 5 +- .../src/model_normal_specular_map.slf | 6 ++- .../render-utils/src/model_specular_map.slf | 6 ++- .../render-utils/src/model_translucent.slf | 6 ++- libraries/render-utils/src/overlay3D.slf | 10 ++-- .../src/overlay3D_translucent.slf | 10 ++-- libraries/render-utils/src/point_light.slf | 2 +- libraries/render-utils/src/sdf_text3D.slf | 20 ++++++-- libraries/render-utils/src/simple.slf | 4 +- .../render-utils/src/simple_textured.slf | 4 +- .../src/simple_textured_emisive.slf | 4 +- libraries/render-utils/src/spot_light.slf | 2 +- tests/gpu-test/src/unlit.slf | 2 +- 38 files changed, 248 insertions(+), 114 deletions(-) diff --git a/examples/utilities/tools/render/framebuffer.qml b/examples/utilities/tools/render/framebuffer.qml index 734c62ad19..20b8b384d8 100644 --- a/examples/utilities/tools/render/framebuffer.qml +++ b/examples/utilities/tools/render/framebuffer.qml @@ -26,8 +26,20 @@ Column { ExclusiveGroup { id: bufferGroup } Repeater { model: [ - "Off", "Diffuse", "Metallic", "Roughness", "Normal", "Depth", - "Lighting", "Shadow", "Pyramid Depth", "Ambient Occlusion", "Custom Shader" + "Off", + "Depth", + "Diffuse", + "Normal", + "Roughness", + "Metallic", + "Fresnel", + "Emissive", + "Lightmap", + "Lighting", + "Shadow", + "Pyramid Depth", + "Ambient Occlusion", + "Custom Shader" ] RadioButton { text: qsTr(modelData) diff --git a/libraries/entities-renderer/src/polyvox.slf b/libraries/entities-renderer/src/polyvox.slf index 2bdecbf9fe..38d9494a61 100644 --- a/libraries/entities-renderer/src/polyvox.slf +++ b/libraries/entities-renderer/src/polyvox.slf @@ -28,9 +28,6 @@ void main(void) { vec3 worldNormal = cross(dFdy(_worldPosition.xyz), dFdx(_worldPosition.xyz)); worldNormal = normalize(worldNormal); - vec3 specular = DEFAULT_SPECULAR; - float shininess = DEFAULT_SHININESS; - float inPositionX = (_worldPosition.x - 0.5) / voxelVolumeSize.x; float inPositionY = (_worldPosition.y - 0.5) / voxelVolumeSize.y; float inPositionZ = (_worldPosition.z - 0.5) / voxelVolumeSize.z; @@ -44,5 +41,5 @@ void main(void) { vec3 yzDiffuseScaled = yzDiffuse.rgb * abs(worldNormal.x); vec4 diffuse = vec4(xyDiffuseScaled + xzDiffuseScaled + yzDiffuseScaled, 1.0); - packDeferredFragment(_normal, 1.0, vec3(diffuse), specular, shininess); + packDeferredFragment(_normal, 1.0, vec3(diffuse), DEFAULT_ROUGHNESS, DEFAULT_METALLIC, DEFAULT_SPECULAR); } diff --git a/libraries/gpu/src/gpu/GLBackendShader.cpp b/libraries/gpu/src/gpu/GLBackendShader.cpp index 35ad7dbbd8..c27c5dd97d 100755 --- a/libraries/gpu/src/gpu/GLBackendShader.cpp +++ b/libraries/gpu/src/gpu/GLBackendShader.cpp @@ -162,8 +162,6 @@ GLBackend::GLShader* compileShader(const Shader& shader) { char* temp = new char[infoLength] ; glGetShaderInfoLog(glshader, infoLength, NULL, temp); - qCWarning(gpulogging) << "GLShader::compileShader - failed to compile the gl shader object:"; - qCWarning(gpulogging) << temp; /* filestream.open("debugshader.glsl.info.txt"); @@ -173,7 +171,10 @@ GLBackend::GLShader* compileShader(const Shader& shader) { } */ + qCWarning(gpulogging) << "GLShader::compileShader - failed to compile the gl shader object:"; qCWarning(gpulogging) << srcstr; + qCWarning(gpulogging) << "GLShader::compileShader - errors:"; + qCWarning(gpulogging) << temp; delete[] temp; glDeleteShader(glshader); diff --git a/libraries/model-networking/src/model-networking/ModelCache.cpp b/libraries/model-networking/src/model-networking/ModelCache.cpp index 3de21e5b0e..63cc113f5a 100644 --- a/libraries/model-networking/src/model-networking/ModelCache.cpp +++ b/libraries/model-networking/src/model-networking/ModelCache.cpp @@ -177,7 +177,7 @@ void NetworkGeometry::setTextureWithNameToURL(const QString& name, const QUrl& u auto glossMap = model::TextureMapPointer(new model::TextureMap()); glossMap->setTextureSource(material->specularTexture->_textureSource); - networkMaterial->setTextureMap(model::MaterialKey::GLOSS_MAP, glossMap); + networkMaterial->setTextureMap(model::MaterialKey::ROUGHNESS_MAP, glossMap); } else if (material->emissiveTextureName == name) { material->emissiveTexture = textureCache->getTexture(url); @@ -350,6 +350,15 @@ static NetworkMaterial* buildNetworkMaterial(const FBXMaterial& material, const material._material->setTextureMap(model::MaterialKey::METALLIC_MAP, specularMap); } + if (!material.metallicTexture.filename.isEmpty()) { + networkMaterial->specularTexture = textureCache->getTexture(textureBaseUrl.resolved(QUrl(material.metallicTexture.filename)), SPECULAR_TEXTURE, material.metallicTexture.content); + networkMaterial->specularTextureName = material.metallicTexture.name; + + auto metallicMap = model::TextureMapPointer(new model::TextureMap()); + metallicMap->setTextureSource(networkMaterial->specularTexture->_textureSource); + + material._material->setTextureMap(model::MaterialKey::METALLIC_MAP, metallicMap); + } if (!material.roughnessTexture.filename.isEmpty()) { networkMaterial->roughnessTexture = textureCache->getTexture(textureBaseUrl.resolved(QUrl(material.roughnessTexture.filename)), ROUGHNESS_TEXTURE, material.roughnessTexture.content); networkMaterial->roughnessTextureName = material.roughnessTexture.name; @@ -357,7 +366,7 @@ static NetworkMaterial* buildNetworkMaterial(const FBXMaterial& material, const auto roughnessMap = model::TextureMapPointer(new model::TextureMap()); roughnessMap->setTextureSource(networkMaterial->roughnessTexture->_textureSource); - material._material->setTextureMap(model::MaterialKey::GLOSS_MAP, roughnessMap); + material._material->setTextureMap(model::MaterialKey::ROUGHNESS_MAP, roughnessMap); } if (!material.emissiveTexture.filename.isEmpty()) { networkMaterial->emissiveTexture = textureCache->getTexture(textureBaseUrl.resolved(QUrl(material.emissiveTexture.filename)), EMISSIVE_TEXTURE, material.emissiveTexture.content); diff --git a/libraries/model/src/model/Material.cpp b/libraries/model/src/model/Material.cpp index 2d2bb38b62..770b110ad9 100755 --- a/libraries/model/src/model/Material.cpp +++ b/libraries/model/src/model/Material.cpp @@ -61,7 +61,7 @@ void Material::setAlbedo(const Color& albedo, bool isSRGB) { void Material::setRoughness(float roughness) { roughness = std::min(1.0f, std::max(roughness, 0.0f)); - _key.setGloss((roughness < 1.0f)); + _key.setGlossy((roughness < 1.0f)); _schemaBuffer.edit()._roughness = roughness; } diff --git a/libraries/model/src/model/Material.h b/libraries/model/src/model/Material.h index 88a8e27611..df58f60eb1 100755 --- a/libraries/model/src/model/Material.h +++ b/libraries/model/src/model/Material.h @@ -30,13 +30,13 @@ public: EMISSIVE_VAL_BIT = 0, ALBEDO_VAL_BIT, METALLIC_VAL_BIT, - GLOSS_VAL_BIT, + GLOSSY_VAL_BIT, TRANSPARENT_VAL_BIT, EMISSIVE_MAP_BIT, ALBEDO_MAP_BIT, METALLIC_MAP_BIT, - GLOSS_MAP_BIT, + ROUGHNESS_MAP_BIT, TRANSPARENT_MAP_BIT, NORMAL_MAP_BIT, LIGHTMAP_MAP_BIT, @@ -49,7 +49,7 @@ public: EMISSIVE_MAP = 0, ALBEDO_MAP, METALLIC_MAP, - GLOSS_MAP, + ROUGHNESS_MAP, TRANSPARENT_MAP, NORMAL_MAP, LIGHTMAP_MAP, @@ -73,13 +73,13 @@ public: Builder& withEmissive() { _flags.set(EMISSIVE_VAL_BIT); return (*this); } Builder& withAlbedo() { _flags.set(ALBEDO_VAL_BIT); return (*this); } Builder& withMetallic() { _flags.set(METALLIC_VAL_BIT); return (*this); } - Builder& withGloss() { _flags.set(GLOSS_VAL_BIT); return (*this); } + Builder& withGlossy() { _flags.set(GLOSSY_VAL_BIT); return (*this); } Builder& withTransparent() { _flags.set(TRANSPARENT_VAL_BIT); return (*this); } Builder& withEmissiveMap() { _flags.set(EMISSIVE_MAP_BIT); return (*this); } Builder& withAlbedoMap() { _flags.set(ALBEDO_MAP_BIT); return (*this); } Builder& withMetallicMap() { _flags.set(METALLIC_MAP_BIT); return (*this); } - Builder& withGlossMap() { _flags.set(GLOSS_MAP_BIT); return (*this); } + Builder& withRoughnessMap() { _flags.set(ROUGHNESS_MAP_BIT); return (*this); } Builder& withTransparentMap() { _flags.set(TRANSPARENT_MAP_BIT); return (*this); } Builder& withNormalMap() { _flags.set(NORMAL_MAP_BIT); return (*this); } @@ -107,11 +107,12 @@ public: void setMetallicMap(bool value) { _flags.set(METALLIC_MAP_BIT, value); } bool isMetallicMap() const { return _flags[METALLIC_MAP_BIT]; } - void setGloss(bool value) { _flags.set(GLOSS_VAL_BIT, value); } - bool isGloss() const { return _flags[GLOSS_VAL_BIT]; } + void setGlossy(bool value) { _flags.set(GLOSSY_VAL_BIT, value); } + bool isGlossy() const { return _flags[GLOSSY_VAL_BIT]; } + bool isRough() const { return !_flags[GLOSSY_VAL_BIT]; } - void setGlossMap(bool value) { _flags.set(GLOSS_MAP_BIT, value); } - bool isGlossMap() const { return _flags[GLOSS_MAP_BIT]; } + void setRoughnessMap(bool value) { _flags.set(ROUGHNESS_MAP_BIT, value); } + bool isRoughnessMap() const { return _flags[ROUGHNESS_MAP_BIT]; } void setTransparent(bool value) { _flags.set(TRANSPARENT_VAL_BIT, value); } bool isTransparent() const { return _flags[TRANSPARENT_VAL_BIT]; } @@ -166,11 +167,11 @@ public: Builder& withoutMetallicMap() { _value.reset(MaterialKey::METALLIC_MAP_BIT); _mask.set(MaterialKey::METALLIC_MAP_BIT); return (*this); } Builder& withMetallicMap() { _value.set(MaterialKey::METALLIC_MAP_BIT); _mask.set(MaterialKey::METALLIC_MAP_BIT); return (*this); } - Builder& withoutGloss() { _value.reset(MaterialKey::GLOSS_VAL_BIT); _mask.set(MaterialKey::GLOSS_VAL_BIT); return (*this); } - Builder& withGloss() { _value.set(MaterialKey::GLOSS_VAL_BIT); _mask.set(MaterialKey::GLOSS_VAL_BIT); return (*this); } + Builder& withoutGlossy() { _value.reset(MaterialKey::GLOSSY_VAL_BIT); _mask.set(MaterialKey::GLOSSY_VAL_BIT); return (*this); } + Builder& withGlossy() { _value.set(MaterialKey::GLOSSY_VAL_BIT); _mask.set(MaterialKey::GLOSSY_VAL_BIT); return (*this); } - Builder& withoutGlossMap() { _value.reset(MaterialKey::GLOSS_MAP_BIT); _mask.set(MaterialKey::GLOSS_MAP_BIT); return (*this); } - Builder& withGlossMap() { _value.set(MaterialKey::GLOSS_MAP_BIT); _mask.set(MaterialKey::GLOSS_MAP_BIT); return (*this); } + Builder& withoutRoughnessMap() { _value.reset(MaterialKey::ROUGHNESS_MAP_BIT); _mask.set(MaterialKey::ROUGHNESS_MAP_BIT); return (*this); } + Builder& withRoughnessMap() { _value.set(MaterialKey::ROUGHNESS_MAP_BIT); _mask.set(MaterialKey::ROUGHNESS_MAP_BIT); return (*this); } Builder& withoutTransparent() { _value.reset(MaterialKey::TRANSPARENT_VAL_BIT); _mask.set(MaterialKey::TRANSPARENT_VAL_BIT); return (*this); } Builder& withTransparent() { _value.set(MaterialKey::TRANSPARENT_VAL_BIT); _mask.set(MaterialKey::TRANSPARENT_VAL_BIT); return (*this); } @@ -241,14 +242,14 @@ public: // Schema to access the attribute values of the material class Schema { public: - glm::vec3 _emissive{ 0.0f }; - float _opacity{ 1.f }; + glm::vec3 _emissive{ 0.0f }; // No Emissive + float _opacity{ 1.f }; // Opacity = 1 => Not Transparent - glm::vec3 _albedo{ 0.5f }; - float _roughness{ 0.9f }; + glm::vec3 _albedo{ 0.5f }; // Grey albedo => isAlbedo + float _roughness{ 1.0f }; // Roughness = 1 => Not Glossy - glm::vec3 _fresnel{ 0.03f }; - float _metallic{ 0.0f }; + glm::vec3 _fresnel{ 0.03f }; // Fresnel value for a default non metallic + float _metallic{ 0.0f }; // Not Metallic glm::vec4 _spare0{ 0.0f }; diff --git a/libraries/render-utils/src/DebugDeferredBuffer.cpp b/libraries/render-utils/src/DebugDeferredBuffer.cpp index 0be3ce422d..1126023f41 100644 --- a/libraries/render-utils/src/DebugDeferredBuffer.cpp +++ b/libraries/render-utils/src/DebugDeferredBuffer.cpp @@ -54,25 +54,51 @@ enum Slot { static const std::string DEFAULT_ALBEDO_SHADER { "vec4 getFragmentColor() {" - " return vec4(pow(texture(albedoMap, uv).xyz, vec3(1.0 / 2.2)), 1.0);" + " DeferredFragment frag = unpackDeferredFragmentNoPosition(uv);" + " return vec4(pow(frag.diffuse, vec3(1.0 / 2.2)), 1.0);" " }" }; -static const std::string DEFAULT_SPECULAR_SHADER { +static const std::string DEFAULT_FRESNEL_SHADER{ "vec4 getFragmentColor() {" - " return vec4(texture(specularMap, uv).xyz, 1.0);" + " DeferredFragment frag = unpackDeferredFragmentNoPosition(uv);" + " return vec4(pow(frag.specular, vec3(1.0 / 2.2)), 1.0);" + " }" +}; + +static const std::string DEFAULT_METALLIC_SHADER { + "vec4 getFragmentColor() {" + " DeferredFragment frag = unpackDeferredFragmentNoPosition(uv);" + " return vec4(vec3(frag.metallic), 1.0);" " }" }; static const std::string DEFAULT_ROUGHNESS_SHADER { "vec4 getFragmentColor() {" - " return vec4(vec3(texture(specularMap, uv).a), 1.0);" + " DeferredFragment frag = unpackDeferredFragmentNoPosition(uv);" + " return vec4(vec3(frag.roughness), 1.0);" " }" }; static const std::string DEFAULT_NORMAL_SHADER { "vec4 getFragmentColor() {" - " return vec4(normalize(texture(normalMap, uv).xyz * 2.0 - vec3(1.0)), 1.0);" + " DeferredFragment frag = unpackDeferredFragmentNoPosition(uv);" + " return vec4(normalize(frag.normal), 1.0);" " }" }; + +static const std::string DEFAULT_EMISSIVE_SHADER{ + "vec4 getFragmentColor() {" + " DeferredFragment frag = unpackDeferredFragmentNoPosition(uv);" + " return (frag.mode != LIGHT_MAPPED ? vec4(pow(frag.emissive, vec3(1.0 / 2.2)), 1.0) : vec4(vec3(0.0), 1.0));" + " }" +}; + +static const std::string DEFAULT_LIGHTMAP_SHADER{ + "vec4 getFragmentColor() {" + " DeferredFragment frag = unpackDeferredFragmentNoPosition(uv);" + " return (frag.mode == LIGHT_MAPPED ? vec4(frag.emissive, 1.0) : vec4(vec3(0.0), 1.0));" + " }" +}; + static const std::string DEFAULT_DEPTH_SHADER { "vec4 getFragmentColor() {" " return vec4(vec3(texture(depthMap, uv).x), 1.0);" @@ -146,14 +172,20 @@ std::string DebugDeferredBuffer::getShaderSourceCode(Mode mode, std::string cust switch (mode) { case AlbedoMode: return DEFAULT_ALBEDO_SHADER; - case SpecularMode: - return DEFAULT_SPECULAR_SHADER; + case FresnelMode: + return DEFAULT_FRESNEL_SHADER; + case MetallicMode: + return DEFAULT_METALLIC_SHADER; case RoughnessMode: return DEFAULT_ROUGHNESS_SHADER; case NormalMode: return DEFAULT_NORMAL_SHADER; case DepthMode: return DEFAULT_DEPTH_SHADER; + case EmissiveMode: + return DEFAULT_EMISSIVE_SHADER; + case LightmapMode: + return DEFAULT_LIGHTMAP_SHADER; case LightingMode: return DEFAULT_LIGHTING_SHADER; case ShadowMode: diff --git a/libraries/render-utils/src/DebugDeferredBuffer.h b/libraries/render-utils/src/DebugDeferredBuffer.h index 221de9a68e..0d72eeec8a 100644 --- a/libraries/render-utils/src/DebugDeferredBuffer.h +++ b/libraries/render-utils/src/DebugDeferredBuffer.h @@ -47,11 +47,14 @@ protected: enum Mode : uint8_t { // Use Mode suffix to avoid collisions - AlbedoMode = 0, - SpecularMode, - RoughnessMode, + DepthMode = 0, + AlbedoMode, NormalMode, - DepthMode, + RoughnessMode, + MetallicMode, + FresnelMode, + EmissiveMode, + LightmapMode, LightingMode, ShadowMode, PyramidDepthMode, diff --git a/libraries/render-utils/src/DeferredBuffer.slh b/libraries/render-utils/src/DeferredBuffer.slh index 339b85ec1a..49cabee727 100755 --- a/libraries/render-utils/src/DeferredBuffer.slh +++ b/libraries/render-utils/src/DeferredBuffer.slh @@ -64,52 +64,73 @@ vec4 evalEyePositionFromZ(DeferredTransform deferredTransform, float depthVal, v } struct DeferredFragment { - float depthVal; vec4 normalVal; vec4 diffuseVal; vec4 specularVal; vec4 position; vec3 normal; + float metallic; vec3 diffuse; float obscurance; vec3 specular; - float gloss; + float roughness; + vec3 emissive; int mode; + float depthVal; }; const int LIGHT_MAPPED = 1; -DeferredFragment unpackDeferredFragment(DeferredTransform deferredTransform, vec2 texcoord) { - DeferredFragment frag; - frag.depthVal = texture(depthMap, texcoord).r; - frag.normalVal = texture(normalMap, texcoord); - frag.diffuseVal = texture(albedoMap, texcoord); - frag.specularVal = texture(specularMap, texcoord); - frag.obscurance = texture(obscuranceMap, texcoord).x; - +vec4 unpackDeferredPosition(DeferredTransform deferredTransform, float depthValue, vec2 texcoord) { if (getStereoMode(deferredTransform)) { if (texcoord.x > 0.5) { texcoord.x -= 0.5; } texcoord.x *= 2.0; } - frag.position = evalEyePositionFromZ(deferredTransform, frag.depthVal, texcoord); + return evalEyePositionFromZ(deferredTransform, depthValue, texcoord); +} + +DeferredFragment unpackDeferredFragmentNoPosition(vec2 texcoord) { + + DeferredFragment frag; + frag.depthVal = -1; + frag.normalVal = texture(normalMap, texcoord); + frag.diffuseVal = texture(albedoMap, texcoord); + frag.specularVal = texture(specularMap, texcoord); + frag.obscurance = texture(obscuranceMap, texcoord).x; // Unpack the normal from the map frag.normal = normalize(frag.normalVal.xyz * 2.0 - vec3(1.0)); frag.mode = 0; + frag.emissive = vec3(0.0); if ((frag.normalVal.a >= 0.45) && (frag.normalVal.a <= 0.55)) { frag.mode = LIGHT_MAPPED; + frag.emissive = frag.specularVal.xyz; } frag.diffuse = frag.diffuseVal.xyz; frag.specular = frag.specularVal.xyz; - frag.gloss = (frag.specularVal.w * 125)*(frag.specularVal.w * 125); + frag.metallic = length(frag.specularVal); + frag.roughness = frag.specularVal.w; + return frag; +} + +DeferredFragment unpackDeferredFragment(DeferredTransform deferredTransform, vec2 texcoord) { + + float depthValue = texture(depthMap, texcoord).r; + + DeferredFragment frag = unpackDeferredFragmentNoPosition(texcoord); + + frag.depthVal = depthValue; + frag.position = unpackDeferredPosition(deferredTransform, frag.depthVal, texcoord); return frag; } + + <@endif@> diff --git a/libraries/render-utils/src/DeferredBufferWrite.slh b/libraries/render-utils/src/DeferredBufferWrite.slh index a54ab4cb3a..d7244cbcad 100755 --- a/libraries/render-utils/src/DeferredBufferWrite.slh +++ b/libraries/render-utils/src/DeferredBufferWrite.slh @@ -40,37 +40,40 @@ float evalOpaqueFinalAlpha(float alpha, float mapAlpha) { return mix(alpha, 1.0 - alpha, step(mapAlpha, alphaThreshold)); } -const vec3 DEFAULT_SPECULAR = vec3(0.1); +const float DEFAULT_ROUGHNESS = 0.9; const float DEFAULT_SHININESS = 10; +const float DEFAULT_METALLIC = 0; +const vec3 DEFAULT_SPECULAR = vec3(0.1); -void packDeferredFragment(vec3 normal, float alpha, vec3 albedo, vec3 fresnel, float shininess) { + +void packDeferredFragment(vec3 normal, float alpha, vec3 albedo, float roughness, float metallic, vec3 fresnel) { if (alpha != 1.0) { discard; } _fragColor0 = vec4(albedo.rgb, 1.0); // Opaque _fragColor1 = vec4(bestFitNormal(normal), 1.0); - _fragColor2 = vec4(fresnel, shininess); + _fragColor2 = vec4(fresnel, roughness); } -void packDeferredFragmentLightmap(vec3 normal, float alpha, vec3 albedo, vec3 fresnel, float shininess, vec3 emissive) { +void packDeferredFragmentLightmap(vec3 normal, float alpha, vec3 albedo, float roughness, float metallic, vec3 fresnel, vec3 emissive) { if (alpha != 1.0) { discard; } _fragColor0 = vec4(albedo.rgb, 0.5); _fragColor1 = vec4(bestFitNormal(normal), 0.5); - _fragColor2 = vec4(emissive, shininess); + _fragColor2 = vec4(emissive, roughness); } -void packDeferredFragmentTranslucent(vec3 normal, float alpha, vec3 albedo, vec3 fresnel, float shininess) { +void packDeferredFragmentTranslucent(vec3 normal, float alpha, vec3 albedo, vec3 fresnel, float roughness) { if (alpha <= 0.0) { discard; } _fragColor0 = vec4(albedo.rgb, alpha); // _fragColor1 = vec4(normal, 0.0) * 0.5 + vec4(0.5, 0.5, 0.5, 1.0); - // _fragColor2 = vec4(fresnel, shininess); + // _fragColor2 = vec4(fresnel, roughness); } <@endif@> diff --git a/libraries/render-utils/src/DeferredGlobalLight.slh b/libraries/render-utils/src/DeferredGlobalLight.slh index aad06c9305..de4eb746ef 100755 --- a/libraries/render-utils/src/DeferredGlobalLight.slh +++ b/libraries/render-utils/src/DeferredGlobalLight.slh @@ -30,7 +30,7 @@ vec4 evalSkyboxLight(vec3 direction, float lod) { <@include model/Light.slh@> <@func declareEvalAmbientGlobalColor()@> -vec3 evalAmbientGlobalColor(mat4 invViewMat, float shadowAttenuation, float obscurance, vec3 position, vec3 normal, vec3 albedo, vec3 fresnel, float gloss) { +vec3 evalAmbientGlobalColor(mat4 invViewMat, float shadowAttenuation, float obscurance, vec3 position, vec3 normal, vec3 albedo, float metallic, vec3 fresnel, float roughness) { // Need the light now Light light = getLight(); @@ -41,7 +41,7 @@ vec3 evalAmbientGlobalColor(mat4 invViewMat, float shadowAttenuation, float obsc vec3 color = albedo * getLightColor(light) * obscurance * getLightAmbientIntensity(light); - vec4 shading = evalFragShading(fragNormal, -getLightDirection(light), fragEyeDir, fresnel, gloss); + vec4 shading = evalFragShading(fragNormal, -getLightDirection(light), fragEyeDir, metallic, fresnel, roughness); color += vec3(albedo * shading.w + shading.rgb) * min(shadowAttenuation, obscurance) * getLightColor(light) * getLightIntensity(light); @@ -52,7 +52,7 @@ vec3 evalAmbientGlobalColor(mat4 invViewMat, float shadowAttenuation, float obsc <@func declareEvalAmbientSphereGlobalColor()@> -vec3 evalAmbientSphereGlobalColor(mat4 invViewMat, float shadowAttenuation, float obscurance, vec3 position, vec3 normal, vec3 albedo, vec3 fresnel, float gloss) { +vec3 evalAmbientSphereGlobalColor(mat4 invViewMat, float shadowAttenuation, float obscurance, vec3 position, vec3 normal, vec3 albedo, float metallic, vec3 fresnel, float roughness) { // Need the light now Light light = getLight(); @@ -64,7 +64,7 @@ vec3 evalAmbientSphereGlobalColor(mat4 invViewMat, float shadowAttenuation, floa vec3 color = albedo * evalSphericalLight(getLightAmbientSphere(light), ambientNormal).xyz * obscurance * getLightAmbientIntensity(light); - vec4 shading = evalFragShading(fragNormal, -getLightDirection(light), fragEyeDir, fresnel, gloss); + vec4 shading = evalFragShading(fragNormal, -getLightDirection(light), fragEyeDir, metallic, fresnel, roughness); color += vec3(albedo * shading.w + shading.rgb) * min(shadowAttenuation, obscurance) * getLightColor(light) * getLightIntensity(light); @@ -76,7 +76,7 @@ vec3 evalAmbientSphereGlobalColor(mat4 invViewMat, float shadowAttenuation, floa <$declareSkyboxMap()$> -vec3 evalSkyboxGlobalColor(mat4 invViewMat, float shadowAttenuation, float obscurance, vec3 position, vec3 normal, vec3 albedo, vec3 fresnel, float gloss) { +vec3 evalSkyboxGlobalColor(mat4 invViewMat, float shadowAttenuation, float obscurance, vec3 position, vec3 normal, vec3 albedo, float metallic, vec3 fresnel, float roughness) { // Need the light now Light light = getLight(); @@ -87,7 +87,7 @@ vec3 evalSkyboxGlobalColor(mat4 invViewMat, float shadowAttenuation, float obscu vec3 color = albedo * evalSphericalLight(getLightAmbientSphere(light), fragNormal).xyz * obscurance * getLightAmbientIntensity(light); - vec4 shading = evalFragShading(fragNormal, -getLightDirection(light), fragEyeDir, fresnel, gloss); + vec4 shading = evalFragShading(fragNormal, -getLightDirection(light), fragEyeDir, metallic, fresnel, roughness); color += vec3(albedo * shading.w + shading.rgb) * min(shadowAttenuation, obscurance) * getLightColor(light) * getLightIntensity(light); diff --git a/libraries/render-utils/src/DeferredLighting.slh b/libraries/render-utils/src/DeferredLighting.slh index 01403a8180..032a273d6b 100755 --- a/libraries/render-utils/src/DeferredLighting.slh +++ b/libraries/render-utils/src/DeferredLighting.slh @@ -14,15 +14,17 @@ <@func declareEvalPBRShading()@> // Frag Shading returns the diffuse amount as W and the specular rgb as xyz -vec4 evalPBRShading(vec3 fragNormal, vec3 fragLightDir, vec3 fragEyeDir, vec3 specular, float gloss) { +vec4 evalPBRShading(vec3 fragNormal, vec3 fragLightDir, vec3 fragEyeDir, float metallic, vec3 specular, float roughness) { // Diffuse Lighting float diffuseDot = dot(fragNormal, fragLightDir); float facingLight = step(0.0, diffuseDot); float diffuse = diffuseDot * facingLight; - // Specular Lighting depends on the half vector and the gloss + // Specular Lighting depends on the half vector and the roughness vec3 halfDir = normalize(fragEyeDir + fragLightDir); + float gloss = (1.0 - roughness) * 128.0; + gloss *= gloss; float specularPower = pow(max(0.0, dot(halfDir, fragNormal)), gloss); specularPower *= (gloss * 0.125 + 0.25); @@ -38,15 +40,17 @@ vec4 evalPBRShading(vec3 fragNormal, vec3 fragLightDir, vec3 fragEyeDir, vec3 sp <@func declareEvalBlinnRShading()@> -vec4 evalBlinnShading(vec3 fragNormal, vec3 fragLightDir, vec3 fragEyeDir, vec3 specular, float gloss) { +vec4 evalBlinnShading(vec3 fragNormal, vec3 fragLightDir, vec3 fragEyeDir, vec3 specular, float roughness) { // Diffuse Lighting float diffuseDot = dot(fragNormal, fragLightDir); float facingLight = step(0.0, diffuseDot); float diffuse = diffuseDot * facingLight; - // Specular Lighting depends on the half vector and the gloss + // Specular Lighting depends on the half vector and the roughness vec3 halfDir = normalize(fragEyeDir + fragLightDir); + float gloss = (1.0 - roughness) * 128.0; + glos *= gloss; float specularPower = pow(facingLight * max(0.0, dot(halfDir, fragNormal)), gloss); vec3 reflect = specularPower * specular * diffuse; @@ -59,8 +63,8 @@ vec4 evalBlinnShading(vec3 fragNormal, vec3 fragLightDir, vec3 fragEyeDir, vec3 <$declareEvalPBRShading()$> // Return xyz the specular/reflection component and w the diffuse component -vec4 evalFragShading(vec3 fragNormal, vec3 fragLightDir, vec3 fragEyeDir, vec3 specular, float gloss) { - return evalPBRShading(fragNormal, fragLightDir, fragEyeDir, specular, gloss); +vec4 evalFragShading(vec3 fragNormal, vec3 fragLightDir, vec3 fragEyeDir, float metallic, vec3 specular, float roughness) { + return evalPBRShading(fragNormal, fragLightDir, fragEyeDir, metallic, specular, roughness); } <@endif@> diff --git a/libraries/render-utils/src/MeshPartPayload.cpp b/libraries/render-utils/src/MeshPartPayload.cpp index 9c6c8522fd..e15603cca5 100644 --- a/libraries/render-utils/src/MeshPartPayload.cpp +++ b/libraries/render-utils/src/MeshPartPayload.cpp @@ -106,7 +106,7 @@ ShapeKey MeshPartPayload::getShapeKey() const { if (drawMaterialKey.isNormalMap()) { builder.withTangents(); } - if (drawMaterialKey.isGlossMap()) { + if (drawMaterialKey.isMetallicMap()) { builder.withSpecular(); } if (drawMaterialKey.isLightmapMap()) { @@ -175,6 +175,20 @@ 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/directional_ambient_light.slf b/libraries/render-utils/src/directional_ambient_light.slf index 3d1b9db46c..57f9c7e791 100755 --- a/libraries/render-utils/src/directional_ambient_light.slf +++ b/libraries/render-utils/src/directional_ambient_light.slf @@ -44,8 +44,9 @@ void main(void) { frag.position.xyz, frag.normal, frag.diffuse, + frag.metallic, frag.specular, - frag.gloss); + frag.roughness); _fragColor = vec4(color, frag.normalVal.a); } } diff --git a/libraries/render-utils/src/directional_ambient_light_shadow.slf b/libraries/render-utils/src/directional_ambient_light_shadow.slf index ffe0e21851..a4d897044b 100644 --- a/libraries/render-utils/src/directional_ambient_light_shadow.slf +++ b/libraries/render-utils/src/directional_ambient_light_shadow.slf @@ -46,8 +46,9 @@ void main(void) { frag.position.xyz, frag.normal, frag.diffuse, + frag.metallic, frag.specular, - frag.gloss); + frag.roughness); _fragColor = vec4(color, frag.normalVal.a); } } diff --git a/libraries/render-utils/src/directional_light.slf b/libraries/render-utils/src/directional_light.slf index e07c57a905..eed3b94c91 100644 --- a/libraries/render-utils/src/directional_light.slf +++ b/libraries/render-utils/src/directional_light.slf @@ -45,8 +45,9 @@ void main(void) { frag.position.xyz, frag.normal, frag.diffuse, + frag.metallic, frag.specular, - frag.gloss); + frag.roughness); _fragColor = vec4(color, frag.normalVal.a); } } diff --git a/libraries/render-utils/src/directional_light_shadow.slf b/libraries/render-utils/src/directional_light_shadow.slf index 4aa041b847..494046bcb3 100644 --- a/libraries/render-utils/src/directional_light_shadow.slf +++ b/libraries/render-utils/src/directional_light_shadow.slf @@ -47,8 +47,9 @@ void main(void) { frag.position.xyz, frag.normal, frag.diffuse, + frag.metallic, frag.specular, - frag.gloss); + frag.roughness); _fragColor = vec4(color, frag.normalVal.a); } } diff --git a/libraries/render-utils/src/directional_skybox_light.slf b/libraries/render-utils/src/directional_skybox_light.slf index 78fdc4e234..605d1de963 100755 --- a/libraries/render-utils/src/directional_skybox_light.slf +++ b/libraries/render-utils/src/directional_skybox_light.slf @@ -45,8 +45,9 @@ void main(void) { frag.position.xyz, frag.normal, frag.diffuse, + frag.metallic, frag.specular, - frag.gloss); + frag.roughness); _fragColor = vec4(color, frag.normalVal.a); } diff --git a/libraries/render-utils/src/directional_skybox_light_shadow.slf b/libraries/render-utils/src/directional_skybox_light_shadow.slf index bbce15be68..75a48caacf 100644 --- a/libraries/render-utils/src/directional_skybox_light_shadow.slf +++ b/libraries/render-utils/src/directional_skybox_light_shadow.slf @@ -47,8 +47,9 @@ void main(void) { frag.position.xyz, frag.normal, frag.diffuse, + frag.metallic, frag.specular, - frag.gloss); + frag.roughness); _fragColor = vec4(color, frag.normalVal.a); } diff --git a/libraries/render-utils/src/model.slf b/libraries/render-utils/src/model.slf index 63f82a4f03..d09ae500ec 100755 --- a/libraries/render-utils/src/model.slf +++ b/libraries/render-utils/src/model.slf @@ -34,6 +34,7 @@ void main(void) { normalize(_normal.xyz), evalOpaqueFinalAlpha(getMaterialOpacity(mat), albedo.a), getMaterialAlbedo(mat) * albedo.rgb * _color, - getMaterialFresnel(mat), - getMaterialShininess(mat)); + getMaterialRoughness(mat), + getMaterialMetallic(mat), + getMaterialFresnel(mat)); } diff --git a/libraries/render-utils/src/model_emissive.slf b/libraries/render-utils/src/model_emissive.slf index b1b968f259..471e613eb4 100644 --- a/libraries/render-utils/src/model_emissive.slf +++ b/libraries/render-utils/src/model_emissive.slf @@ -32,7 +32,8 @@ void main(void) { normalize(_normal), texel.a, vec3(1.0), + getMaterialRoughness(mat), + getMaterialMetallic(mat), getMaterialFresnel(mat), - getMaterialShininess(mat), fragColor); } diff --git a/libraries/render-utils/src/model_lightmap.slf b/libraries/render-utils/src/model_lightmap.slf index 9287bcf821..a896f5b915 100755 --- a/libraries/render-utils/src/model_lightmap.slf +++ b/libraries/render-utils/src/model_lightmap.slf @@ -39,7 +39,8 @@ void main(void) { normalize(_normal), evalOpaqueFinalAlpha(getMaterialOpacity(mat), albedo.a), getMaterialAlbedo(mat) * albedo.rgb * _color, + getMaterialRoughness(mat), + getMaterialMetallic(mat), getMaterialFresnel(mat), - getMaterialShininess(mat), (vec3(emissiveParams.x) + emissiveParams.y * emissive.rgb)); } diff --git a/libraries/render-utils/src/model_lightmap_normal_map.slf b/libraries/render-utils/src/model_lightmap_normal_map.slf index 496f3883ea..6431dfb069 100755 --- a/libraries/render-utils/src/model_lightmap_normal_map.slf +++ b/libraries/render-utils/src/model_lightmap_normal_map.slf @@ -52,7 +52,8 @@ void main(void) { normalize(viewNormal.xyz), evalOpaqueFinalAlpha(getMaterialOpacity(mat), albedo.a), getMaterialAlbedo(mat) * albedo.rgb * _color, + getMaterialRoughness(mat), + getMaterialMetallic(mat), getMaterialFresnel(mat), - getMaterialShininess(mat), (vec3(emissiveParams.x) + emissiveParams.y * emissive.rgb)); } 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 bce14fbe94..66aacbb7f6 100755 --- a/libraries/render-utils/src/model_lightmap_normal_specular_map.slf +++ b/libraries/render-utils/src/model_lightmap_normal_specular_map.slf @@ -56,7 +56,8 @@ void main(void) { normalize(viewNormal.xyz), evalOpaqueFinalAlpha(getMaterialOpacity(mat), albedo.a), getMaterialAlbedo(mat) * albedo.rgb * _color, + getMaterialRoughness(mat), + getMaterialMetallic(mat), specular, // no use of getMaterialFresnel(mat) - getMaterialShininess(mat), (vec3(emissiveParams.x) + emissiveParams.y * emissive.rgb)); } diff --git a/libraries/render-utils/src/model_lightmap_specular_map.slf b/libraries/render-utils/src/model_lightmap_specular_map.slf index e7ff753d16..84fb47f73f 100755 --- a/libraries/render-utils/src/model_lightmap_specular_map.slf +++ b/libraries/render-utils/src/model_lightmap_specular_map.slf @@ -44,7 +44,8 @@ void main(void) { normalize(_normal), evalOpaqueFinalAlpha(getMaterialOpacity(mat), albedo.a), getMaterialAlbedo(mat) * albedo.rgb * _color, + getMaterialRoughness(mat), + getMaterialMetallic(mat), specular, // no use of getMaterialFresnel(mat) - getMaterialShininess(mat), (vec3(emissiveParams.x) + emissiveParams.y * emissive.rgb)); } diff --git a/libraries/render-utils/src/model_normal_map.slf b/libraries/render-utils/src/model_normal_map.slf index 58b1b7378e..204cfaec3f 100755 --- a/libraries/render-utils/src/model_normal_map.slf +++ b/libraries/render-utils/src/model_normal_map.slf @@ -45,6 +45,7 @@ void main(void) { normalize(viewNormal.xyz), evalOpaqueFinalAlpha(getMaterialOpacity(mat), albedo.a), getMaterialAlbedo(mat) * albedo.rgb * _color, - getMaterialFresnel(mat), - getMaterialShininess(mat)); + getMaterialRoughness(mat), + getMaterialMetallic(mat), + getMaterialFresnel(mat)); } diff --git a/libraries/render-utils/src/model_normal_specular_map.slf b/libraries/render-utils/src/model_normal_specular_map.slf index 0926ffa79a..ef39460ddd 100755 --- a/libraries/render-utils/src/model_normal_specular_map.slf +++ b/libraries/render-utils/src/model_normal_specular_map.slf @@ -50,6 +50,8 @@ void main(void) { normalize(viewNormal.xyz), evalOpaqueFinalAlpha(getMaterialOpacity(mat), albedo.a), getMaterialAlbedo(mat) * albedo.rgb * _color, - specular, //getMaterialFresnel(mat), - getMaterialShininess(mat)); + getMaterialRoughness(mat), + getMaterialMetallic(mat), + specular //getMaterialFresnel(mat) + ); } diff --git a/libraries/render-utils/src/model_specular_map.slf b/libraries/render-utils/src/model_specular_map.slf index 5ad0cada15..54d6a8a33f 100755 --- a/libraries/render-utils/src/model_specular_map.slf +++ b/libraries/render-utils/src/model_specular_map.slf @@ -39,6 +39,8 @@ void main(void) { normalize(_normal), evalOpaqueFinalAlpha(getMaterialOpacity(mat), albedo.a), getMaterialAlbedo(mat) * albedo.rgb * _color, - specular, //getMaterialFresnel(mat), - getMaterialShininess(mat)); + getMaterialRoughness(mat), + getMaterialMetallic(mat), + specular //getMaterialFresnel(mat) + ); } diff --git a/libraries/render-utils/src/model_translucent.slf b/libraries/render-utils/src/model_translucent.slf index 6cd3839191..3efa523434 100755 --- a/libraries/render-utils/src/model_translucent.slf +++ b/libraries/render-utils/src/model_translucent.slf @@ -37,8 +37,9 @@ void main(void) { vec3 fragPosition = _position.xyz; vec3 fragNormal = normalize(_normal); vec3 fragAlbedo = getMaterialAlbedo(mat) * albedo.rgb * _color; + float fragMetallic = getMaterialMetallic(mat); vec3 fragFresnel = getMaterialFresnel(mat); - float fragGloss = getMaterialShininess(mat) / 128; + float fragRoughness = getMaterialRoughness(mat); float fragOpacity = getMaterialOpacity(mat) * albedo.a * _alpha; TransformCamera cam = getTransformCamera(); @@ -50,7 +51,8 @@ void main(void) { fragPosition, fragNormal, fragAlbedo, + fragMetallic, fragFresnel, - fragGloss), + fragRoughness), fragOpacity); } diff --git a/libraries/render-utils/src/overlay3D.slf b/libraries/render-utils/src/overlay3D.slf index 8de7a23898..38199a7a82 100644 --- a/libraries/render-utils/src/overlay3D.slf +++ b/libraries/render-utils/src/overlay3D.slf @@ -17,7 +17,7 @@ <@include gpu/Transform.slh@> <$declareStandardCameraTransform()$> -vec4 evalGlobalColor(float shadowAttenuation, vec3 position, vec3 normal, vec3 albedo, vec3 specular, float gloss, float opacity) { +vec4 evalGlobalColor(float shadowAttenuation, vec3 position, vec3 normal, vec3 albedo, float metallic, vec3 specular, float roughness, float opacity) { // Need the light now Light light = getLight(); @@ -30,7 +30,7 @@ vec4 evalGlobalColor(float shadowAttenuation, vec3 position, vec3 normal, vec3 a vec3 color = opacity * albedo * getLightColor(light) * getLightAmbientIntensity(light); - vec4 shading = evalFragShading(fragNormal, -getLightDirection(light), fragEyeDir, specular, gloss); + vec4 shading = evalFragShading(fragNormal, -getLightDirection(light), fragEyeDir, metallic, specular, roughness); color += vec3(albedo * shading.w * opacity + shading.rgb) * shadowAttenuation * getLightColor(light) * getLightIntensity(light); @@ -53,8 +53,9 @@ void main(void) { vec3 fragPosition = _position.xyz; vec3 fragNormal = normalize(_normal); vec3 fragAlbedo = albedo.rgb * _color; + float fragMetallic = 0.0; vec3 fragSpecular = vec3(0.1); - float fragGloss = 10.0 / 128.0; + float fragRoughness = 0.9; float fragOpacity = albedo.a; if (fragOpacity <= 0.1) { @@ -65,8 +66,9 @@ void main(void) { fragPosition, fragNormal, fragAlbedo, + fragMetallic, fragSpecular, - fragGloss, + fragRoughness, fragOpacity); // Apply standard tone mapping diff --git a/libraries/render-utils/src/overlay3D_translucent.slf b/libraries/render-utils/src/overlay3D_translucent.slf index 8e6dbb9eff..f8c18abf20 100644 --- a/libraries/render-utils/src/overlay3D_translucent.slf +++ b/libraries/render-utils/src/overlay3D_translucent.slf @@ -18,7 +18,7 @@ <@include gpu/Transform.slh@> <$declareStandardCameraTransform()$> -vec4 evalGlobalColor(float shadowAttenuation, vec3 position, vec3 normal, vec3 albedo, vec3 specular, float gloss, float opacity) { +vec4 evalGlobalColor(float shadowAttenuation, vec3 position, vec3 normal, vec3 albedo, float metallic, vec3 specular, float roughness, float opacity) { // Need the light now Light light = getLight(); @@ -31,7 +31,7 @@ vec4 evalGlobalColor(float shadowAttenuation, vec3 position, vec3 normal, vec3 a vec3 color = opacity * albedo * getLightColor(light) * getLightAmbientIntensity(light); - vec4 shading = evalFragShading(fragNormal, -getLightDirection(light), fragEyeDir, specular, gloss); + vec4 shading = evalFragShading(fragNormal, -getLightDirection(light), fragEyeDir, metallic, specular, roughness); color += vec3(albedo * shading.w * opacity + shading.rgb) * shadowAttenuation * getLightColor(light) * getLightIntensity(light); @@ -54,16 +54,18 @@ void main(void) { vec3 fragPosition = _position.xyz; vec3 fragNormal = normalize(_normal); vec3 fragAlbedo = albedo.rgb * _color; + float fragMetallic = 0.0; vec3 fragSpecular = vec3(0.1); - float fragGloss = 10.0 / 128.0; + float fragRoughness = 0.9; float fragOpacity = albedo.a * _alpha; vec4 color = evalGlobalColor(1.0, fragPosition, fragNormal, fragAlbedo, + fragMetallic, fragSpecular, - fragGloss, + fragRoughness, fragOpacity); // Apply standard tone mapping diff --git a/libraries/render-utils/src/point_light.slf b/libraries/render-utils/src/point_light.slf index fcfb0b336e..298b3751b7 100644 --- a/libraries/render-utils/src/point_light.slf +++ b/libraries/render-utils/src/point_light.slf @@ -60,7 +60,7 @@ void main(void) { vec3 fragNormal = vec3(invViewMat * vec4(frag.normal, 0.0)); vec4 fragEyeVector = invViewMat * vec4(-frag.position.xyz, 0.0); vec3 fragEyeDir = normalize(fragEyeVector.xyz); - vec4 shading = evalFragShading(fragNormal, fragLightDir, fragEyeDir, frag.specular, frag.gloss); + vec4 shading = evalFragShading(fragNormal, fragLightDir, fragEyeDir, frag.metallic, frag.specular, frag.roughness); // Eval attenuation float radialAttenuation = evalLightAttenuation(light, fragLightDistance); diff --git a/libraries/render-utils/src/sdf_text3D.slf b/libraries/render-utils/src/sdf_text3D.slf index 69fec0042b..faa4d02bfa 100644 --- a/libraries/render-utils/src/sdf_text3D.slf +++ b/libraries/render-utils/src/sdf_text3D.slf @@ -10,6 +10,8 @@ // Distributed under the Apache License, Version 2.0. // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html +<@include DeferredBufferWrite.slh@> + uniform sampler2D Font; uniform bool Outline; uniform vec4 Color; @@ -17,12 +19,11 @@ uniform vec4 Color; // the interpolated normal in vec3 _normal; in vec2 _texCoord0; - +/* layout(location = 0) out vec4 _fragColor0; layout(location = 1) out vec4 _fragColor1; layout(location = 2) out vec4 _fragColor2; - -const float DEFAULT_SHININESS = 10; +*/ const float gamma = 2.2; const float smoothing = 256.0; @@ -53,7 +54,16 @@ void main() { } // final color - _fragColor0 = vec4(Color.rgb, Color.a * a); + /* _fragColor0 = vec4(Color.rgb, Color.a * a); _fragColor1 = vec4(normalize(_normal.xyz), 0.0) * 0.5 + vec4(0.5, 0.5, 0.5, 0.5); - _fragColor2 = vec4(Color.rgb, DEFAULT_SHININESS / 128.0); + _fragColor2 = vec4(Color.rgb, 10 / 128.0); + */ + packDeferredFragmentLightmap( + normalize(_normal), + Color.a * a, + Color.rgb, + DEFAULT_ROUGHNESS, + DEFAULT_METALLIC, + DEFAULT_SPECULAR, + Color.rgb); } \ No newline at end of file diff --git a/libraries/render-utils/src/simple.slf b/libraries/render-utils/src/simple.slf index b24e4f92ff..4b5cdfb5ce 100644 --- a/libraries/render-utils/src/simple.slf +++ b/libraries/render-utils/src/simple.slf @@ -51,9 +51,9 @@ void main(void) { if (emissiveAmount > 0.0) { packDeferredFragmentLightmap( - normal, 1.0, diffuse, specular, shininess, specular); + normal, 1.0, diffuse, max(0, 1.0 - shininess / 128.0), DEFAULT_METALLIC, specular, specular); } else { packDeferredFragment( - normal, 1.0, diffuse, specular, shininess); + normal, 1.0, diffuse, max(0, 1.0 - shininess / 128.0), DEFAULT_METALLIC, specular); } } diff --git a/libraries/render-utils/src/simple_textured.slf b/libraries/render-utils/src/simple_textured.slf index 6c99343a28..b2bc15dbad 100644 --- a/libraries/render-utils/src/simple_textured.slf +++ b/libraries/render-utils/src/simple_textured.slf @@ -31,5 +31,7 @@ void main(void) { normalize(_normal.xyz), texel.a, _color.rgb * texel.rgb, - DEFAULT_SPECULAR, DEFAULT_SHININESS); + DEFAULT_ROUGHNESS, + DEFAULT_METALLIC, + DEFAULT_SPECULAR); } \ No newline at end of file diff --git a/libraries/render-utils/src/simple_textured_emisive.slf b/libraries/render-utils/src/simple_textured_emisive.slf index 2119af17fb..92bdee8d02 100644 --- a/libraries/render-utils/src/simple_textured_emisive.slf +++ b/libraries/render-utils/src/simple_textured_emisive.slf @@ -29,6 +29,8 @@ void main(void) { normalize(_normal), texel.a, _color.rgb, - DEFAULT_SPECULAR, DEFAULT_SHININESS, + DEFAULT_ROUGHNESS, + DEFAULT_METALLIC, + DEFAULT_SPECULAR, texel.rgb); } \ No newline at end of file diff --git a/libraries/render-utils/src/spot_light.slf b/libraries/render-utils/src/spot_light.slf index 8170929636..a4077c0edb 100644 --- a/libraries/render-utils/src/spot_light.slf +++ b/libraries/render-utils/src/spot_light.slf @@ -66,7 +66,7 @@ void main(void) { vec3 fragNormal = vec3(invViewMat * vec4(frag.normal, 0.0)); vec4 fragEyeVector = invViewMat * vec4(-frag.position.xyz, 0.0); vec3 fragEyeDir = normalize(fragEyeVector.xyz); - vec4 shading = evalFragShading(fragNormal, fragLightDir, fragEyeDir, frag.specular, frag.gloss); + vec4 shading = evalFragShading(fragNormal, fragLightDir, fragEyeDir, frag.metallic, frag.specular, frag.roughness); // Eval attenuation float radialAttenuation = evalLightAttenuation(light, fragLightDistance); diff --git a/tests/gpu-test/src/unlit.slf b/tests/gpu-test/src/unlit.slf index 77d28aa7e9..d070662495 100644 --- a/tests/gpu-test/src/unlit.slf +++ b/tests/gpu-test/src/unlit.slf @@ -24,5 +24,5 @@ void main(void) { normalize(_normal.xyz), 1.0, _color.rgb, - DEFAULT_SPECULAR, DEFAULT_SHININESS); + DEFAULT_ROUGHNESS, DEFAULT_METALLIC, DEFAULT_SPECULAR); } From c7f43386e56f7b8bc9af87622dc9df2cf2b89267 Mon Sep 17 00:00:00 2001 From: samcake Date: Mon, 22 Feb 2016 09:23:16 -0800 Subject: [PATCH 09/24] Compressing the g-buffer better --- libraries/render-utils/src/DeferredBuffer.slh | 33 +++++++++++++--- .../render-utils/src/DeferredBufferWrite.slh | 11 +++--- .../render-utils/src/DeferredLighting.slh | 39 +++++++++++-------- 3 files changed, 56 insertions(+), 27 deletions(-) diff --git a/libraries/render-utils/src/DeferredBuffer.slh b/libraries/render-utils/src/DeferredBuffer.slh index 49cabee727..fe9b1a2a47 100755 --- a/libraries/render-utils/src/DeferredBuffer.slh +++ b/libraries/render-utils/src/DeferredBuffer.slh @@ -105,16 +105,39 @@ DeferredFragment unpackDeferredFragmentNoPosition(vec2 texcoord) { frag.mode = 0; frag.emissive = vec3(0.0); - if ((frag.normalVal.a >= 0.45) && (frag.normalVal.a <= 0.55)) { + if (frag.normalVal.a < 0.5) { + frag.mode = 0; + frag.roughness = 2.0 * frag.normalVal.a; + } else { frag.mode = LIGHT_MAPPED; + frag.roughness = 2.0 * frag.normalVal.a - 1.0; frag.emissive = frag.specularVal.xyz; } + + // if ((frag.normalVal.a >= 0.45) && (frag.normalVal.a <= 0.55)) { + // frag.mode = LIGHT_MAPPED; + // frag.emissive = frag.specularVal.xyz; + // } - frag.diffuse = frag.diffuseVal.xyz; - frag.specular = frag.specularVal.xyz; - frag.metallic = length(frag.specularVal); - frag.roughness = frag.specularVal.w; + frag.metallic = frag.diffuseVal.a; + if (frag.metallic > 0.5) { + frag.diffuse = vec3(0); + //frag.metallic = length(frag.specularVal); + frag.specular = frag.diffuseVal.xyz; + + } else { + frag.diffuse = frag.diffuseVal.xyz; + //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; + + + //frag.roughness = frag.specularVal.w; + return frag; } diff --git a/libraries/render-utils/src/DeferredBufferWrite.slh b/libraries/render-utils/src/DeferredBufferWrite.slh index d7244cbcad..21d579cb3a 100755 --- a/libraries/render-utils/src/DeferredBufferWrite.slh +++ b/libraries/render-utils/src/DeferredBufferWrite.slh @@ -50,9 +50,9 @@ void packDeferredFragment(vec3 normal, float alpha, vec3 albedo, float roughness if (alpha != 1.0) { discard; } - - _fragColor0 = vec4(albedo.rgb, 1.0); // Opaque - _fragColor1 = vec4(bestFitNormal(normal), 1.0); + vec3 baseColor = ((metallic < 0.5) ? albedo : fresnel); + _fragColor0 = vec4(baseColor, metallic); + _fragColor1 = vec4(bestFitNormal(normal), 0.5 * clamp(roughness, 0.0, 1.0)); _fragColor2 = vec4(fresnel, roughness); } @@ -61,8 +61,9 @@ void packDeferredFragmentLightmap(vec3 normal, float alpha, vec3 albedo, float r discard; } - _fragColor0 = vec4(albedo.rgb, 0.5); - _fragColor1 = vec4(bestFitNormal(normal), 0.5); + vec3 baseColor = ((metallic < 0.5) ? albedo : fresnel); + _fragColor0 = vec4(baseColor, 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 032a273d6b..eb821cccac 100755 --- a/libraries/render-utils/src/DeferredLighting.slh +++ b/libraries/render-utils/src/DeferredLighting.slh @@ -13,28 +13,33 @@ <@func declareEvalPBRShading()@> -// 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 specular, float roughness) { - // Diffuse Lighting - float diffuseDot = dot(fragNormal, fragLightDir); - float facingLight = step(0.0, diffuseDot); - float diffuse = diffuseDot * facingLight; - - // Specular Lighting depends on the half vector and the roughness - vec3 halfDir = normalize(fragEyeDir + fragLightDir); +vec3 fresnelSchlick(vec3 fresnelColor, vec3 lightDir, vec3 halfDir) { + return fresnelColor + (1.0f - fresnelColor) * pow(1.0f - clamp(dot(lightDir, halfDir), 0.0, 1.0), 5); +} + +float specularDistribution(float roughness, vec3 normal, vec3 halfDir) { float gloss = (1.0 - roughness) * 128.0; gloss *= gloss; - float specularPower = pow(max(0.0, dot(halfDir, fragNormal)), gloss); - specularPower *= (gloss * 0.125 + 0.25); + float power = pow(clamp(dot(halfDir, normal), 0.0, 1.0), gloss); + power *= (gloss * 0.125 + 0.25); + return power; +} - float shlickPower = (1.0 - dot(fragLightDir,halfDir)); - float shlickPower2 = shlickPower * shlickPower; - float shlickPower5 = shlickPower2 * shlickPower2 * shlickPower; - vec3 fresnel = specular * (1.0 - shlickPower5) + vec3(shlickPower5); - vec3 reflect = specularPower * fresnel * diffuse; +// 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 + vec3 halfDir = normalize(fragEyeDir + fragLightDir); + vec3 fresnelColor = fresnelSchlick(fresnel, fragLightDir,halfDir); + float power = specularDistribution(roughness, fragNormal, halfDir); + vec3 specular = power * fresnelColor * diffuse; - return vec4(reflect, diffuse * (1 - fresnel.x)); + return vec4(specular, diffuse * (1 - fresnelColor.x)); } <@endfunc@> From 00782b0e76a598f338447011fc1e8feadcf9d057 Mon Sep 17 00:00:00 2001 From: samcake Date: Mon, 22 Feb 2016 18:19:36 -0800 Subject: [PATCH 10/24] GOing home, this is in the middle of adding the Roughness texture and potentially cleaning up the different compinations of shaders regarding the material textures --- libraries/gpu/src/gpu/Format.cpp | 2 +- libraries/gpu/src/gpu/Format.h | 2 +- libraries/gpu/src/gpu/Texture.h | 54 +++++++++++++++++++ .../src/model-networking/ModelCache.cpp | 10 +++- .../src/model-networking/ModelCache.h | 7 +++ .../src/model-networking/TextureCache.cpp | 6 +-- .../src/model-networking/TextureCache.h | 10 ++-- libraries/model/src/model/Material.cpp | 6 +++ libraries/model/src/model/Material.h | 4 +- libraries/model/src/model/Material.slh | 1 + libraries/model/src/model/TextureMap.cpp | 31 ++++++++++- libraries/render-utils/src/model.slf | 7 +-- libraries/render-utils/src/model_lightmap.slf | 5 +- .../src/model_lightmap_normal_map.slf | 13 +++-- .../model_lightmap_normal_specular_map.slf | 16 +++--- .../src/model_lightmap_specular_map.slf | 6 ++- .../render-utils/src/model_normal_map.slf | 5 +- .../src/model_normal_specular_map.slf | 4 +- .../render-utils/src/model_specular_map.slf | 4 +- 19 files changed, 155 insertions(+), 38 deletions(-) diff --git a/libraries/gpu/src/gpu/Format.cpp b/libraries/gpu/src/gpu/Format.cpp index a66fc19458..7c603919fd 100644 --- a/libraries/gpu/src/gpu/Format.cpp +++ b/libraries/gpu/src/gpu/Format.cpp @@ -11,7 +11,7 @@ using namespace gpu; const Element Element::COLOR_RGBA_32{ VEC4, NUINT8, RGBA }; -const Element Element::COLOR_RGBA{ VEC4, FLOAT, RGBA }; +const Element Element::VEC4F_COLOR_RGBA{ VEC4, FLOAT, RGBA }; const Element Element::VEC2F_UV{ VEC2, FLOAT, UV }; const Element Element::VEC2F_XY{ VEC2, FLOAT, XY }; const Element Element::VEC3F_XYZ{ VEC3, FLOAT, XYZ }; diff --git a/libraries/gpu/src/gpu/Format.h b/libraries/gpu/src/gpu/Format.h index 54d40c3e12..625ca6cec3 100644 --- a/libraries/gpu/src/gpu/Format.h +++ b/libraries/gpu/src/gpu/Format.h @@ -247,7 +247,7 @@ public: } static const Element COLOR_RGBA_32; - static const Element COLOR_RGBA; + static const Element VEC4F_COLOR_RGBA; static const Element VEC2F_UV; static const Element VEC2F_XY; static const Element VEC3F_XYZ; diff --git a/libraries/gpu/src/gpu/Texture.h b/libraries/gpu/src/gpu/Texture.h index 378f49c2f4..e05dc84c25 100755 --- a/libraries/gpu/src/gpu/Texture.h +++ b/libraries/gpu/src/gpu/Texture.h @@ -14,6 +14,7 @@ #include "Resource.h" #include //min max and more +#include #include @@ -139,6 +140,53 @@ protected: class Texture : public Resource { public: + class Usage { + public: + enum FlagBit { + COLOR = 0, // Texture is a color map + NORMAL, // Texture is a normal map + ALPHA, // Texture has an alpha channel + ALPHA_MASK, // Texture alpha channel is a Mask 0/1 + + NUM_FLAGS, + }; + typedef std::bitset Flags; + + // The key is the Flags + Flags _flags; + + Usage() : _flags(0) {} + Usage(const Flags& flags) : _flags(flags) {} + + bool operator== (const Usage& rhs) const { return _flags == rhs._flags; } + bool operator!= (const Usage& rhs) const { return _flags != rhs._flags; } + + class Builder { + friend class Usage; + Flags _flags{ 0 }; + public: + Builder() {} + + Usage build() const { return Usage(_flags); } + + Builder& withColor() { _flags.set(COLOR); return (*this); } + Builder& withNormal() { _flags.set(NORMAL); return (*this); } + Builder& withAlpha() { _flags.set(ALPHA); return (*this); } + Builder& withAlphaMask() { _flags.set(ALPHA_MASK); return (*this); } + }; + Usage(const Builder& builder) : Usage(builder._flags) {} + + bool isColor() const { return _flags[COLOR]; } + bool isNormal() const { return _flags[NORMAL]; } + + bool isAlpha() const { return _flags[ALPHA]; } + bool isAlphaMask() const { return _flags[ALPHA_MASK]; } + + + bool operator==(const Usage& usage) { return (_flags == usage._flags); } + bool operator!=(const Usage& usage) { return (_flags != usage._flags); } + }; + class Pixels { public: Pixels() {} @@ -343,6 +391,10 @@ public: bool isDefined() const { return _defined; } + // Usage is a a set of flags providing Semantic about the usage of the Texture. + void setUsage(const Usage& usage) { _usage = usage; } + Usage getUsage() const { return _usage; } + // For Cube Texture, it's possible to generate the irradiance spherical harmonics and make them availalbe with the texture bool generateIrradiance(); const SHPointer& getIrradiance(uint16 slice = 0) const { return _irradiance; } @@ -380,6 +432,8 @@ protected: Type _type = TEX_1D; + Usage _usage; + SHPointer _irradiance; bool _autoGenerateMips = false; bool _isIrradianceValid = false; diff --git a/libraries/model-networking/src/model-networking/ModelCache.cpp b/libraries/model-networking/src/model-networking/ModelCache.cpp index 63cc113f5a..2bb71ba855 100644 --- a/libraries/model-networking/src/model-networking/ModelCache.cpp +++ b/libraries/model-networking/src/model-networking/ModelCache.cpp @@ -316,7 +316,7 @@ static NetworkMesh* buildNetworkMesh(const FBXMesh& mesh, const QUrl& textureBas return networkMesh; } -static NetworkMaterial* buildNetworkMaterial(const FBXMaterial& material, const QUrl& textureBaseUrl) { +static NetworkMaterial* buildNetworkMaterial(NetworkGeometry* geometry, const FBXMaterial& material, const QUrl& textureBaseUrl) { auto textureCache = DependencyManager::get(); NetworkMaterial* networkMaterial = new NetworkMaterial(); @@ -324,6 +324,9 @@ static NetworkMaterial* buildNetworkMaterial(const FBXMaterial& material, const if (!material.albedoTexture.filename.isEmpty()) { networkMaterial->albedoTexture = textureCache->getTexture(textureBaseUrl.resolved(QUrl(material.albedoTexture.filename)), DEFAULT_TEXTURE, material.albedoTexture.content); + QObject::connect(networkMaterial->albedoTexture.data(), &NetworkTexture::networkTextureCreated, + geometry, &NetworkGeometry::textureLoaded); + networkMaterial->albedoTextureName = material.albedoTexture.name; auto albedoMap = model::TextureMapPointer(new model::TextureMap()); @@ -407,7 +410,7 @@ void NetworkGeometry::modelParseSuccess(FBXGeometry* geometry) { QHash fbxMatIDToMatID; foreach(const FBXMaterial& material, _geometry->materials) { fbxMatIDToMatID[material.materialID] = _materials.size(); - _materials.emplace_back(buildNetworkMaterial(material, _textureBaseUrl)); + _materials.emplace_back(buildNetworkMaterial(this, material, _textureBaseUrl)); } @@ -453,3 +456,6 @@ const NetworkMaterial* NetworkGeometry::getShapeMaterial(int shapeID) { } } +void NetworkGeometry::textureLoaded(const QWeakPointer& networkTexture) { + numTextureLoaded++; +} diff --git a/libraries/model-networking/src/model-networking/ModelCache.h b/libraries/model-networking/src/model-networking/ModelCache.h index 1d43424813..c43a1e6d79 100644 --- a/libraries/model-networking/src/model-networking/ModelCache.h +++ b/libraries/model-networking/src/model-networking/ModelCache.h @@ -105,6 +105,9 @@ signals: // Fired when something went wrong. void onFailure(NetworkGeometry& networkGeometry, Error error); +public slots: + void textureLoaded(const QWeakPointer& networkTexture); + protected slots: void mappingRequestDone(const QByteArray& data); void mappingRequestError(QNetworkReply::NetworkError error); @@ -115,6 +118,7 @@ protected slots: void modelParseSuccess(FBXGeometry* geometry); void modelParseError(int error, QString str); + protected: void attemptRequestInternal(); void requestMapping(const QUrl& url); @@ -133,6 +137,7 @@ protected: QUrl _modelUrl; QVariantHash _mapping; QUrl _textureBaseUrl; + int numTextureLoaded = 0; Resource* _resource = nullptr; std::unique_ptr _geometry; // This should go away evenutally once we can put everything we need in the model::AssetPointer @@ -173,6 +178,7 @@ public: class NetworkMaterial { public: + model::MaterialPointer _material; QString albedoTextureName; QSharedPointer albedoTexture; @@ -186,6 +192,7 @@ public: QSharedPointer emissiveTexture; QString lightmapTextureName; QSharedPointer lightmapTexture; + }; diff --git a/libraries/model-networking/src/model-networking/TextureCache.cpp b/libraries/model-networking/src/model-networking/TextureCache.cpp index 2ec9ab4027..c10b82d235 100644 --- a/libraries/model-networking/src/model-networking/TextureCache.cpp +++ b/libraries/model-networking/src/model-networking/TextureCache.cpp @@ -339,10 +339,6 @@ void NetworkTexture::setImage(const QImage& image, void* voidTexture, int origin finishedLoading(true); - imageLoaded(image); -} - -void NetworkTexture::imageLoaded(const QImage& image) { - // nothing by default + emit networkTextureCreated(qWeakPointerCast (_self)); } diff --git a/libraries/model-networking/src/model-networking/TextureCache.h b/libraries/model-networking/src/model-networking/TextureCache.h index 1a0d22a8d3..2cfd225889 100644 --- a/libraries/model-networking/src/model-networking/TextureCache.h +++ b/libraries/model-networking/src/model-networking/TextureCache.h @@ -83,8 +83,6 @@ private: gpu::TexturePointer _blueTexture; gpu::TexturePointer _blackTexture; gpu::TexturePointer _normalFittingTexture; - - QHash > _dilatableNetworkTextures; }; /// A simple object wrapper for an OpenGL texture. @@ -114,7 +112,11 @@ public: int getHeight() const { return _height; } TextureLoaderFunc getTextureLoader() const; - + +signals: + void networkTextureCreated(const QWeakPointer& self); + + protected: virtual void downloadFinished(const QByteArray& data) override; @@ -123,8 +125,6 @@ protected: // FIXME: This void* should be a gpu::Texture* but i cannot get it to work for now, moving on... Q_INVOKABLE void setImage(const QImage& image, void* texture, int originalWidth, int originalHeight); - virtual void imageLoaded(const QImage& image); - private: TextureType _type; diff --git a/libraries/model/src/model/Material.cpp b/libraries/model/src/model/Material.cpp index 770b110ad9..522debad59 100755 --- a/libraries/model/src/model/Material.cpp +++ b/libraries/model/src/model/Material.cpp @@ -46,11 +46,13 @@ Material::~Material() { void Material::setEmissive(const Color& emissive, bool isSRGB) { _key.setEmissive(glm::any(glm::greaterThan(emissive, Color(0.0f)))); + _schemaBuffer.edit()._key = (uint32) _key._flags.to_ulong(); _schemaBuffer.edit()._emissive = (isSRGB ? ColorUtils::toLinearVec3(emissive) : emissive); } void Material::setOpacity(float opacity) { _key.setTransparent((opacity < 1.0f)); + _schemaBuffer.edit()._key = (uint32)_key._flags.to_ulong(); _schemaBuffer.edit()._opacity = opacity; } @@ -62,6 +64,7 @@ void Material::setAlbedo(const Color& albedo, bool isSRGB) { void Material::setRoughness(float roughness) { roughness = std::min(1.0f, std::max(roughness, 0.0f)); _key.setGlossy((roughness < 1.0f)); + _schemaBuffer.edit()._key = (uint32)_key._flags.to_ulong(); _schemaBuffer.edit()._roughness = roughness; } @@ -72,6 +75,7 @@ void Material::setFresnel(const Color& fresnel, bool isSRGB) { void Material::setMetallic(float metallic) { _key.setMetallic(metallic > 0.0f); + _schemaBuffer.edit()._key = (uint32)_key._flags.to_ulong(); _schemaBuffer.edit()._metallic = metallic; } @@ -79,9 +83,11 @@ void Material::setMetallic(float metallic) { void Material::setTextureMap(MapChannel channel, const TextureMapPointer& textureMap) { if (textureMap) { _key.setMapChannel(channel, (true)); + _schemaBuffer.edit()._key = (uint32)_key._flags.to_ulong(); _textureMaps[channel] = textureMap; } else { _key.setMapChannel(channel, (false)); + _schemaBuffer.edit()._key = (uint32)_key._flags.to_ulong(); _textureMaps.erase(channel); } } diff --git a/libraries/model/src/model/Material.h b/libraries/model/src/model/Material.h index df58f60eb1..a6b1cd3706 100755 --- a/libraries/model/src/model/Material.h +++ b/libraries/model/src/model/Material.h @@ -252,7 +252,9 @@ public: float _metallic{ 0.0f }; // Not Metallic - glm::vec4 _spare0{ 0.0f }; + glm::vec3 _spare0{ 0.0f }; + + uint32_t _key{ 0 }; // a copy of the materialKey // for alignment beauty, Material size == Mat4x4 diff --git a/libraries/model/src/model/Material.slh b/libraries/model/src/model/Material.slh index 90a7784042..02bd9568dc 100644 --- a/libraries/model/src/model/Material.slh +++ b/libraries/model/src/model/Material.slh @@ -37,4 +37,5 @@ float getMaterialMetallic(Material m) { return m._fresnelMetallic.a; } float getMaterialShininess(Material m) { return 1.0 - getMaterialRoughness(m); } + <@endif@> diff --git a/libraries/model/src/model/TextureMap.cpp b/libraries/model/src/model/TextureMap.cpp index 17d610e87f..b75f2d6acc 100755 --- a/libraries/model/src/model/TextureMap.cpp +++ b/libraries/model/src/model/TextureMap.cpp @@ -54,22 +54,38 @@ void TextureMap::setLightmapOffsetScale(float offset, float scale) { gpu::Texture* TextureUsage::create2DTextureFromImage(const QImage& srcImage, const std::string& srcImageName) { QImage image = srcImage; bool validAlpha = false; + bool alphaAsMask = true; + const uint8 OPAQUE_ALPHA = 255; + const uint8 TRANSLUCENT_ALPHA = 0; if (image.hasAlphaChannel()) { + std::map alphaHistogram; + if (image.format() != QImage::Format_ARGB32) { image = image.convertToFormat(QImage::Format_ARGB32); } - // Actual alpha channel? + // Actual alpha channel? create the histogram for (int y = 0; y < image.height(); ++y) { const QRgb* data = reinterpret_cast(image.constScanLine(y)); for (int x = 0; x < image.width(); ++x) { auto alpha = qAlpha(data[x]); - if (alpha != 255) { + alphaHistogram[alpha] ++; + if (alpha != OPAQUE_ALPHA) { validAlpha = true; break; } } } + + // If alpha was meaningfull refine + if (validAlpha && (alphaHistogram.size() > 1)) { + auto totalNumPixels = image.height() * image.width(); + auto numOpaques = alphaHistogram[OPAQUE_ALPHA]; + auto numTranslucents = alphaHistogram[TRANSLUCENT_ALPHA]; + auto numTransparents = totalNumPixels - numOpaques - numTranslucents; + + alphaAsMask = ((numTransparents / (double)totalNumPixels) < 0.05); + } } if (!validAlpha && image.format() != QImage::Format_RGB888) { @@ -89,10 +105,21 @@ gpu::Texture* TextureUsage::create2DTextureFromImage(const QImage& srcImage, con } theTexture = (gpu::Texture::create2D(formatGPU, image.width(), image.height(), gpu::Sampler(gpu::Sampler::FILTER_MIN_MAG_MIP_LINEAR))); + + auto usage = gpu::Texture::Usage::Builder().withColor(); + if (validAlpha) { + usage.withAlpha(); + if (alphaAsMask) { + usage.withAlphaMask(); + } + } + theTexture->setUsage(usage.build()); + theTexture->assignStoredMip(0, formatMip, image.byteCount(), image.constBits()); theTexture->autoGenerateMips(-1); // FIXME queue for transfer to GPU and block on completion + } return theTexture; diff --git a/libraries/render-utils/src/model.slf b/libraries/render-utils/src/model.slf index d09ae500ec..a1a75161b9 100755 --- a/libraries/render-utils/src/model.slf +++ b/libraries/render-utils/src/model.slf @@ -15,8 +15,8 @@ <@include model/Material.slh@> -// the diffuse texture uniform sampler2D albedoMap; +uniform sampler2D roughnessMap; in vec4 _position; in vec3 _normal; @@ -25,8 +25,9 @@ in vec2 _texCoord0; void main(void) { - // Fetch albedo map + // Fetch maps vec4 albedo = texture(albedoMap, _texCoord0); + float roughness = texture(roughnessMap, _texCoord0); Material mat = getMaterial(); @@ -34,7 +35,7 @@ void main(void) { normalize(_normal.xyz), evalOpaqueFinalAlpha(getMaterialOpacity(mat), albedo.a), getMaterialAlbedo(mat) * albedo.rgb * _color, - getMaterialRoughness(mat), + getMaterialRoughness(mat) * roughness, getMaterialMetallic(mat), getMaterialFresnel(mat)); } diff --git a/libraries/render-utils/src/model_lightmap.slf b/libraries/render-utils/src/model_lightmap.slf index a896f5b915..a3505ba779 100755 --- a/libraries/render-utils/src/model_lightmap.slf +++ b/libraries/render-utils/src/model_lightmap.slf @@ -18,6 +18,7 @@ // the albedo texture uniform sampler2D albedoMap; +uniform sampler2D roughnessMap; // the emissive map texture and parameters uniform sampler2D emissiveMap; @@ -30,7 +31,9 @@ in vec3 _normal; in vec3 _color; void main(void) { + // Fetch maps vec4 albedo = texture(albedoMap, _texCoord0); + float roughness = texture(roughnessMap, _texCoord0); vec4 emissive = texture(emissiveMap, _texCoord1); Material mat = getMaterial(); @@ -39,7 +42,7 @@ void main(void) { normalize(_normal), evalOpaqueFinalAlpha(getMaterialOpacity(mat), albedo.a), getMaterialAlbedo(mat) * albedo.rgb * _color, - getMaterialRoughness(mat), + getMaterialRoughness(mat) * roughness, getMaterialMetallic(mat), getMaterialFresnel(mat), (vec3(emissiveParams.x) + emissiveParams.y * emissive.rgb)); diff --git a/libraries/render-utils/src/model_lightmap_normal_map.slf b/libraries/render-utils/src/model_lightmap_normal_map.slf index 6431dfb069..223cec71fb 100755 --- a/libraries/render-utils/src/model_lightmap_normal_map.slf +++ b/libraries/render-utils/src/model_lightmap_normal_map.slf @@ -18,6 +18,7 @@ // the albedo texture uniform sampler2D albedoMap; +uniform sampler2D roughnessMap; // the normal map texture uniform sampler2D normalMap; @@ -34,17 +35,19 @@ in vec3 _tangent; in vec3 _color; void main(void) { + // Fetch maps + vec4 albedo = texture(albedoMap, _texCoord0); + float roughness = texture(roughness, _texCoord0); + vec4 emissive = texture(emissiveMap, _texCoord1); + vec3 localNormal = vec3(texture(normalMap, _texCoord0)) - vec3(0.5, 0.5, 0.5); + // compute the view normal from the various bits vec3 normalizedNormal = normalize(_normal); vec3 normalizedTangent = normalize(_tangent); vec3 normalizedBitangent = normalize(cross(normalizedNormal, normalizedTangent)); - vec3 localNormal = vec3(texture(normalMap, _texCoord0)) - vec3(0.5, 0.5, 0.5); vec4 viewNormal = vec4(normalizedTangent * localNormal.x + normalizedBitangent * localNormal.y + normalizedNormal * localNormal.z, 0.0); - - // set the diffuse, normal, specular data - vec4 albedo = texture(albedoMap, _texCoord0); - vec4 emissive = texture(emissiveMap, _texCoord1); + Material mat = getMaterial(); 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 66aacbb7f6..99799a4b25 100755 --- a/libraries/render-utils/src/model_lightmap_normal_specular_map.slf +++ b/libraries/render-utils/src/model_lightmap_normal_specular_map.slf @@ -18,6 +18,7 @@ // the albedo texture uniform sampler2D albedoMap; +uniform sampler2D roughnessMap; // the emissive map texture and parameters uniform sampler2D emissiveMap; @@ -37,18 +38,19 @@ in vec3 _tangent; in vec3 _color; void main(void) { + // Fetch maps + vec4 albedo = texture(albedoMap, _texCoord0); + float roughness = texture(roughnessMap, _texCoord0); + vec3 specular = texture(specularMap, _texCoord0).rgb; + vec4 emissive = texture(emissiveMap, _texCoord1); + vec3 localNormal = vec3(texture(normalMap, _texCoord0)) - vec3(0.5, 0.5, 0.5); + // compute the view normal from the various bits vec3 normalizedNormal = normalize(_normal); vec3 normalizedTangent = normalize(_tangent); vec3 normalizedBitangent = normalize(cross(normalizedNormal, normalizedTangent)); - vec3 localNormal = vec3(texture(normalMap, _texCoord0)) - vec3(0.5, 0.5, 0.5); vec4 viewNormal = vec4(normalizedTangent * localNormal.x + normalizedBitangent * localNormal.y + normalizedNormal * localNormal.z, 0.0); - - // set the albedo, normal, specular data - vec4 albedo = texture(albedoMap, _texCoord0); - vec3 specular = texture(specularMap, _texCoord0).rgb; - vec4 emissive = texture(emissiveMap, _texCoord1); Material mat = getMaterial(); @@ -56,7 +58,7 @@ void main(void) { normalize(viewNormal.xyz), evalOpaqueFinalAlpha(getMaterialOpacity(mat), albedo.a), getMaterialAlbedo(mat) * albedo.rgb * _color, - getMaterialRoughness(mat), + getMaterialRoughness(mat) * roughness, getMaterialMetallic(mat), specular, // no use of getMaterialFresnel(mat) (vec3(emissiveParams.x) + emissiveParams.y * emissive.rgb)); diff --git a/libraries/render-utils/src/model_lightmap_specular_map.slf b/libraries/render-utils/src/model_lightmap_specular_map.slf index 84fb47f73f..590dfe5727 100755 --- a/libraries/render-utils/src/model_lightmap_specular_map.slf +++ b/libraries/render-utils/src/model_lightmap_specular_map.slf @@ -18,6 +18,7 @@ // the albedo texture uniform sampler2D albedoMap; +uniform sampler2D roughnessMap; // the emissive map texture and parameters uniform sampler2D emissiveMap; @@ -33,8 +34,9 @@ in vec3 _normal; in vec3 _color; void main(void) { - // set the albedo, normal, specular data + // Fetch maps vec4 albedo = texture(albedoMap, _texCoord0); + float roughness = texture(roughnessMap, _texCoord0); vec3 specular = texture(specularMap, _texCoord0).rgb; vec4 emissive = texture(emissiveMap, _texCoord1); @@ -44,7 +46,7 @@ void main(void) { normalize(_normal), evalOpaqueFinalAlpha(getMaterialOpacity(mat), albedo.a), getMaterialAlbedo(mat) * albedo.rgb * _color, - getMaterialRoughness(mat), + getMaterialRoughness(mat) * roughness, getMaterialMetallic(mat), specular, // no use of getMaterialFresnel(mat) (vec3(emissiveParams.x) + emissiveParams.y * emissive.rgb)); diff --git a/libraries/render-utils/src/model_normal_map.slf b/libraries/render-utils/src/model_normal_map.slf index 204cfaec3f..8adde1d850 100755 --- a/libraries/render-utils/src/model_normal_map.slf +++ b/libraries/render-utils/src/model_normal_map.slf @@ -15,9 +15,11 @@ <@include DeferredBufferWrite.slh@> <@include model/Material.slh@> +<@include model/MaterialTextures.slh@> // the albedo texture uniform sampler2D albedoMap; +uniform sampler2D roughnessMap; // the normal map texture uniform sampler2D normalMap; @@ -38,6 +40,7 @@ void main(void) { normalizedBitangent * localNormal.y + normalizedNormal * localNormal.z, 0.0); vec4 albedo = texture(albedoMap, _texCoord0.st); + float roughness = texture(roughnessMap, _texCoord0); Material mat = getMaterial(); @@ -45,7 +48,7 @@ void main(void) { normalize(viewNormal.xyz), evalOpaqueFinalAlpha(getMaterialOpacity(mat), albedo.a), getMaterialAlbedo(mat) * albedo.rgb * _color, - getMaterialRoughness(mat), + getMaterialRoughness(mat) * roughness, getMaterialMetallic(mat), getMaterialFresnel(mat)); } diff --git a/libraries/render-utils/src/model_normal_specular_map.slf b/libraries/render-utils/src/model_normal_specular_map.slf index ef39460ddd..95a13198c0 100755 --- a/libraries/render-utils/src/model_normal_specular_map.slf +++ b/libraries/render-utils/src/model_normal_specular_map.slf @@ -18,6 +18,7 @@ // the albedo texture uniform sampler2D albedoMap; +uniform sampler2D roughnessMap; // the normal map texture uniform sampler2D normalMap; @@ -42,6 +43,7 @@ void main(void) { // set the albedo, normal, specular data vec4 albedo = texture(albedoMap, _texCoord0); + vec4 roughness = texture(roughnessMap, _texCoord0); vec3 specular = texture(specularMap, _texCoord0).rgb; Material mat = getMaterial(); @@ -50,7 +52,7 @@ void main(void) { normalize(viewNormal.xyz), evalOpaqueFinalAlpha(getMaterialOpacity(mat), albedo.a), getMaterialAlbedo(mat) * albedo.rgb * _color, - getMaterialRoughness(mat), + getMaterialRoughness(mat) * roughness, getMaterialMetallic(mat), specular //getMaterialFresnel(mat) ); diff --git a/libraries/render-utils/src/model_specular_map.slf b/libraries/render-utils/src/model_specular_map.slf index 54d6a8a33f..5821ab8154 100755 --- a/libraries/render-utils/src/model_specular_map.slf +++ b/libraries/render-utils/src/model_specular_map.slf @@ -18,6 +18,7 @@ // the albedo texture uniform sampler2D albedoMap; +uniform sampler2D roughnessMap; // the specular texture uniform sampler2D specularMap; @@ -31,6 +32,7 @@ in vec3 _color; void main(void) { // set the albedo, normal, specular data vec4 albedo = texture(albedoMap, _texCoord0); + float roughness = texture(roughnessMap, _texCoord0); vec3 specular = texture(specularMap, _texCoord0).rgb; Material mat = getMaterial(); @@ -39,7 +41,7 @@ void main(void) { normalize(_normal), evalOpaqueFinalAlpha(getMaterialOpacity(mat), albedo.a), getMaterialAlbedo(mat) * albedo.rgb * _color, - getMaterialRoughness(mat), + getMaterialRoughness(mat) * roughness, getMaterialMetallic(mat), specular //getMaterialFresnel(mat) ); From 9cb8bd0808bc0223302eea1347380e687e391ece Mon Sep 17 00:00:00 2001 From: samcake Date: Tue, 23 Feb 2016 02:41:26 -0800 Subject: [PATCH 11/24] Adding one more feature for NULL_VAR in scribe function parameters and unifying the MAterialTexture shader code --- .../render-utils/src/MaterialTextures.slh | 88 +++++++++++++++++++ libraries/render-utils/src/model.slf | 10 +-- libraries/render-utils/src/model_lightmap.slf | 17 ++-- .../src/model_lightmap_normal_map.slf | 32 ++----- .../model_lightmap_normal_specular_map.slf | 38 +++----- .../src/model_lightmap_specular_map.slf | 24 ++--- .../render-utils/src/model_normal_map.slf | 27 ++---- .../src/model_normal_specular_map.slf | 30 ++----- .../render-utils/src/model_specular_map.slf | 18 ++-- tools/scribe/src/TextTemplate.cpp | 12 ++- tools/scribe/src/TextTemplate.h | 2 + 11 files changed, 158 insertions(+), 140 deletions(-) create mode 100644 libraries/render-utils/src/MaterialTextures.slh diff --git a/libraries/render-utils/src/MaterialTextures.slh b/libraries/render-utils/src/MaterialTextures.slh new file mode 100644 index 0000000000..47eed14a82 --- /dev/null +++ b/libraries/render-utils/src/MaterialTextures.slh @@ -0,0 +1,88 @@ + +<@if not MODEL_MATERIAL_TEXTURES_SLH@> +<@def MODEL_MATERIAL_TEXTURES_SLH@> + +<@func declareMaterialTextures(withAlbedo, withRoughness, withNormal, withMetallic)@> + +<@if withAlbedo@> +uniform sampler2D albedoMap; +vec4 fetchAlbedoMap(vec2 uv) { + return texture(albedoMap, uv); +} +<@endif@> + + +uniform sampler2D roughnessMap; +float fetchRoughnessMap(vec2 uv) { + return texture(roughnessMap, uv).r; +} +<@endif@> +!> + +<@if withNormal@> +uniform sampler2D normalMap; +vec3 fetchNormalMap(vec2 uv) { + return texture(normalMap, uv).xyz; +} +<@endif@> + +<@if withMetallic@> +uniform sampler2D specularMap; +vec3 fetchMetallicMap(vec2 uv) { + return texture(specularMap, uv).rgb; +} +<@endif@> + +<@endfunc@> + + +<@func fetchMaterialTextures(texcoord0, albedo, roughness, normal, metallic)@> +<@if albedo@> + vec4 <$albedo$> = fetchAlbedoMap(<$texcoord0$>); +<@endif@> +<@if roughness@> +float <$roughness$> = 1.0; //fetchRoughnessMap(<$texcoord0$>); +<@endif@> +<@if normal@> + vec3 <$normal$> = fetchNormalMap(<$texcoord0$>); +<@endif@> +<@if metallic@> + vec3 <$metallic$> = fetchMetallicMap(<$texcoord0$>); +<@endif@> +<@endfunc@> + + +<@func declareMaterialLightmap()@> +uniform sampler2D emissiveMap; +uniform vec2 emissiveParams; +vec3 fetchLightmapMap(vec2 uv) { + return (vec3(emissiveParams.x) + emissiveParams.y * texture(emissiveMap, uv).rgb); +} +<@endfunc@> + +<@func fetchMaterialLightmap(texcoord1, lightmapVal)@> + vec3 <$lightmapVal$> = fetchLightmapMap(<$texcoord1$>); +<@endfunc@> + + +<@func tangentToViewSpace(fetchedNormal, interpolatedNormal, interpolatedTangent, normal)@> +{ + vec3 normalizedNormal = normalize(<$interpolatedNormal$>.xyz); + vec3 normalizedTangent = normalize(<$interpolatedTangent$>.xyz); + vec3 normalizedBitangent = normalize(cross(normalizedNormal, normalizedTangent)); + vec3 localNormal = normalize(<$fetchedNormal$> - vec3(0.5, 0.5, 0.5)); + <$normal$> = vec3(normalizedTangent * localNormal.x + normalizedBitangent * localNormal.y + normalizedNormal * localNormal.z); +} +<@endfunc@> + +<@endif@> \ No newline at end of file diff --git a/libraries/render-utils/src/model.slf b/libraries/render-utils/src/model.slf index a1a75161b9..b43694bbb7 100755 --- a/libraries/render-utils/src/model.slf +++ b/libraries/render-utils/src/model.slf @@ -15,8 +15,8 @@ <@include model/Material.slh@> -uniform sampler2D albedoMap; -uniform sampler2D roughnessMap; +<@include MaterialTextures.slh@> +<$declareMaterialTextures(ALBEDO, ROUGHNESS)$> in vec4 _position; in vec3 _normal; @@ -25,10 +25,8 @@ in vec2 _texCoord0; void main(void) { - // Fetch maps - vec4 albedo = texture(albedoMap, _texCoord0); - float roughness = texture(roughnessMap, _texCoord0); - + <$fetchMaterialTextures(_texCoord0, albedo, roughness)$> + Material mat = getMaterial(); packDeferredFragment( diff --git a/libraries/render-utils/src/model_lightmap.slf b/libraries/render-utils/src/model_lightmap.slf index a3505ba779..bfe809f472 100755 --- a/libraries/render-utils/src/model_lightmap.slf +++ b/libraries/render-utils/src/model_lightmap.slf @@ -16,13 +16,9 @@ <@include model/Material.slh@> -// the albedo texture -uniform sampler2D albedoMap; -uniform sampler2D roughnessMap; - -// the emissive map texture and parameters -uniform sampler2D emissiveMap; -uniform vec2 emissiveParams; +<@include MaterialTextures.slh@> +<$declareMaterialTextures(ALBEDO, ROUGHNESS)$> +<$declareMaterialLightmap()$> in vec4 _position; in vec2 _texCoord0; @@ -31,10 +27,9 @@ in vec3 _normal; in vec3 _color; void main(void) { - // Fetch maps - vec4 albedo = texture(albedoMap, _texCoord0); - float roughness = texture(roughnessMap, _texCoord0); - vec4 emissive = texture(emissiveMap, _texCoord1); + + <$fetchMaterialTextures(_texCoord0, albedo, roughness)$> + <$fetchMaterialLightmap(_texCoord1, emissive)$> Material mat = getMaterial(); diff --git a/libraries/render-utils/src/model_lightmap_normal_map.slf b/libraries/render-utils/src/model_lightmap_normal_map.slf index 223cec71fb..1d305541fa 100755 --- a/libraries/render-utils/src/model_lightmap_normal_map.slf +++ b/libraries/render-utils/src/model_lightmap_normal_map.slf @@ -16,16 +16,9 @@ <@include model/Material.slh@> -// the albedo texture -uniform sampler2D albedoMap; -uniform sampler2D roughnessMap; - -// the normal map texture -uniform sampler2D normalMap; - -// the emissive map texture and parameters -uniform sampler2D emissiveMap; -uniform vec2 emissiveParams; +<@include MaterialTextures.slh@> +<$declareMaterialTextures(ALBEDO, ROUGHNESS, NORMAL)$> +<$declareMaterialLightmap()$> in vec4 _position; in vec2 _texCoord0; @@ -35,22 +28,15 @@ in vec3 _tangent; in vec3 _color; void main(void) { - // Fetch maps - vec4 albedo = texture(albedoMap, _texCoord0); - float roughness = texture(roughness, _texCoord0); - vec4 emissive = texture(emissiveMap, _texCoord1); - vec3 localNormal = vec3(texture(normalMap, _texCoord0)) - vec3(0.5, 0.5, 0.5); - - // compute the view normal from the various bits - vec3 normalizedNormal = normalize(_normal); - vec3 normalizedTangent = normalize(_tangent); - vec3 normalizedBitangent = normalize(cross(normalizedNormal, normalizedTangent)); - vec4 viewNormal = vec4(normalizedTangent * localNormal.x + - normalizedBitangent * localNormal.y + normalizedNormal * localNormal.z, 0.0); + <$fetchMaterialTextures(_texCoord0, albedo, roughness, normalTexel)$> + <$fetchMaterialLightmap(_texCoord1, lightmapVal)$> Material mat = getMaterial(); + vec3 viewNormal; + <$tangentToViewSpace(normalTexel, _normal, _tangent, viewNormal)$> + packDeferredFragmentLightmap( normalize(viewNormal.xyz), evalOpaqueFinalAlpha(getMaterialOpacity(mat), albedo.a), @@ -58,5 +44,5 @@ void main(void) { getMaterialRoughness(mat), getMaterialMetallic(mat), getMaterialFresnel(mat), - (vec3(emissiveParams.x) + emissiveParams.y * emissive.rgb)); + lightmapVal); } 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 99799a4b25..513670fe89 100755 --- a/libraries/render-utils/src/model_lightmap_normal_specular_map.slf +++ b/libraries/render-utils/src/model_lightmap_normal_specular_map.slf @@ -16,19 +16,9 @@ <@include model/Material.slh@> -// the albedo texture -uniform sampler2D albedoMap; -uniform sampler2D roughnessMap; - -// the emissive map texture and parameters -uniform sampler2D emissiveMap; -uniform vec2 emissiveParams; - -// the normal map texture -uniform sampler2D normalMap; - -// the specular map texture -uniform sampler2D specularMap; +<@include MaterialTextures.slh@> +<$declareMaterialTextures(ALBEDO, ROUGHNESS, NORMAL, METALLIC)$> +<$declareMaterialLightmap()$> in vec4 _position; in vec2 _texCoord0; @@ -38,21 +28,13 @@ in vec3 _tangent; in vec3 _color; void main(void) { - // Fetch maps - vec4 albedo = texture(albedoMap, _texCoord0); - float roughness = texture(roughnessMap, _texCoord0); - vec3 specular = texture(specularMap, _texCoord0).rgb; - vec4 emissive = texture(emissiveMap, _texCoord1); - vec3 localNormal = vec3(texture(normalMap, _texCoord0)) - vec3(0.5, 0.5, 0.5); - - // compute the view normal from the various bits - vec3 normalizedNormal = normalize(_normal); - vec3 normalizedTangent = normalize(_tangent); - vec3 normalizedBitangent = normalize(cross(normalizedNormal, normalizedTangent)); - vec4 viewNormal = vec4(normalizedTangent * localNormal.x + - normalizedBitangent * localNormal.y + normalizedNormal * localNormal.z, 0.0); - + <$fetchMaterialTextures(_texCoord0, albedo, roughness, normalTexel, specular)$> + <$fetchMaterialLightmap(_texCoord1, lightmapVal)$> + Material mat = getMaterial(); + + vec3 viewNormal; + <$tangentToViewSpace(normalTexel, _normal, _tangent, viewNormal)$> packDeferredFragmentLightmap( normalize(viewNormal.xyz), @@ -61,5 +43,5 @@ void main(void) { getMaterialRoughness(mat) * roughness, getMaterialMetallic(mat), specular, // no use of getMaterialFresnel(mat) - (vec3(emissiveParams.x) + emissiveParams.y * emissive.rgb)); + lightmapVal); } diff --git a/libraries/render-utils/src/model_lightmap_specular_map.slf b/libraries/render-utils/src/model_lightmap_specular_map.slf index 590dfe5727..48db5a40dc 100755 --- a/libraries/render-utils/src/model_lightmap_specular_map.slf +++ b/libraries/render-utils/src/model_lightmap_specular_map.slf @@ -16,16 +16,9 @@ <@include model/Material.slh@> -// the albedo texture -uniform sampler2D albedoMap; -uniform sampler2D roughnessMap; - -// the emissive map texture and parameters -uniform sampler2D emissiveMap; -uniform vec2 emissiveParams; - -// the specular texture -uniform sampler2D specularMap; +<@include MaterialTextures.slh@> +<$declareMaterialTextures(ALBEDO, ROUGHNESS, _SCRIBE_NULL, METALLIC)$> +<$declareMaterialLightmap()$> in vec4 _position; in vec2 _texCoord0; @@ -34,12 +27,9 @@ in vec3 _normal; in vec3 _color; void main(void) { - // Fetch maps - vec4 albedo = texture(albedoMap, _texCoord0); - float roughness = texture(roughnessMap, _texCoord0); - vec3 specular = texture(specularMap, _texCoord0).rgb; - vec4 emissive = texture(emissiveMap, _texCoord1); - + <$fetchMaterialTextures(_texCoord0, albedo, roughness, _SCRIBE_NULL, specular)$> + <$fetchMaterialLightmap(_texCoord1, lightmapVal)$> + Material mat = getMaterial(); packDeferredFragmentLightmap( @@ -49,5 +39,5 @@ void main(void) { getMaterialRoughness(mat) * roughness, getMaterialMetallic(mat), specular, // no use of getMaterialFresnel(mat) - (vec3(emissiveParams.x) + emissiveParams.y * emissive.rgb)); + lightmapVal); } diff --git a/libraries/render-utils/src/model_normal_map.slf b/libraries/render-utils/src/model_normal_map.slf index 8adde1d850..13cb63d322 100755 --- a/libraries/render-utils/src/model_normal_map.slf +++ b/libraries/render-utils/src/model_normal_map.slf @@ -15,14 +15,9 @@ <@include DeferredBufferWrite.slh@> <@include model/Material.slh@> -<@include model/MaterialTextures.slh@> -// the albedo texture -uniform sampler2D albedoMap; -uniform sampler2D roughnessMap; - -// the normal map texture -uniform sampler2D normalMap; +<@include MaterialTextures.slh@> +<$declareMaterialTextures(ALBEDO, ROUGHNESS, NORMAL)$> in vec4 _position; in vec2 _texCoord0; @@ -31,21 +26,15 @@ in vec3 _tangent; in vec3 _color; void main(void) { - // compute the view normal from the various bits - vec3 normalizedNormal = normalize(_normal.xyz); - vec3 normalizedTangent = normalize(_tangent.xyz); - vec3 normalizedBitangent = normalize(cross(normalizedNormal, normalizedTangent)); - vec3 localNormal = normalize(vec3(texture(normalMap, _texCoord0.st)) - vec3(0.5, 0.5, 0.5)); - vec4 viewNormal = vec4(normalizedTangent * localNormal.x + - normalizedBitangent * localNormal.y + normalizedNormal * localNormal.z, 0.0); - - vec4 albedo = texture(albedoMap, _texCoord0.st); - float roughness = texture(roughnessMap, _texCoord0); - + <$fetchMaterialTextures(_texCoord0, albedo, roughness, normalTexel)$> + Material mat = getMaterial(); + vec3 viewNormal; + <$tangentToViewSpace(normalTexel, _normal, _tangent, viewNormal)$> + packDeferredFragment( - normalize(viewNormal.xyz), + viewNormal, evalOpaqueFinalAlpha(getMaterialOpacity(mat), albedo.a), getMaterialAlbedo(mat) * albedo.rgb * _color, getMaterialRoughness(mat) * roughness, diff --git a/libraries/render-utils/src/model_normal_specular_map.slf b/libraries/render-utils/src/model_normal_specular_map.slf index 95a13198c0..c4d5b22efe 100755 --- a/libraries/render-utils/src/model_normal_specular_map.slf +++ b/libraries/render-utils/src/model_normal_specular_map.slf @@ -16,15 +16,8 @@ <@include model/Material.slh@> -// the albedo texture -uniform sampler2D albedoMap; -uniform sampler2D roughnessMap; - -// the normal map texture -uniform sampler2D normalMap; - -// the specular map texture -uniform sampler2D specularMap; +<@include MaterialTextures.slh@> +<$declareMaterialTextures(ALBEDO, ROUGHNESS, NORMAL, METALLIC)$> in vec4 _position; in vec2 _texCoord0; @@ -33,27 +26,20 @@ in vec3 _tangent; in vec3 _color; void main(void) { - // compute the view normal from the various bits - vec3 normalizedNormal = normalize(_normal); - vec3 normalizedTangent = normalize(_tangent); - vec3 normalizedBitangent = normalize(cross(normalizedNormal, normalizedTangent)); - vec3 localNormal = normalize(vec3(texture(normalMap, _texCoord0)) - vec3(0.5, 0.5, 0.5)); - vec4 viewNormal = vec4(normalizedTangent * localNormal.x + - normalizedBitangent * localNormal.y + normalizedNormal * localNormal.z, 0.0); - // set the albedo, normal, specular data - vec4 albedo = texture(albedoMap, _texCoord0); - vec4 roughness = texture(roughnessMap, _texCoord0); - vec3 specular = texture(specularMap, _texCoord0).rgb; - + <$fetchMaterialTextures(_texCoord0, albedo, roughness, normalTexel, specular)$> + Material mat = getMaterial(); + vec3 viewNormal; + <$tangentToViewSpace(normalTexel, _normal, _tangent, viewNormal)$> + packDeferredFragment( normalize(viewNormal.xyz), evalOpaqueFinalAlpha(getMaterialOpacity(mat), albedo.a), getMaterialAlbedo(mat) * albedo.rgb * _color, getMaterialRoughness(mat) * roughness, getMaterialMetallic(mat), - specular //getMaterialFresnel(mat) + 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 5821ab8154..99f0364520 100755 --- a/libraries/render-utils/src/model_specular_map.slf +++ b/libraries/render-utils/src/model_specular_map.slf @@ -16,12 +16,8 @@ <@include model/Material.slh@> -// the albedo texture -uniform sampler2D albedoMap; -uniform sampler2D roughnessMap; - -// the specular texture -uniform sampler2D specularMap; +<@include MaterialTextures.slh@> +<$declareMaterialTextures(ALBEDO, ROUGHNESS, _SCRIBE_NULL, METALLIC)$> in vec4 _position; in vec2 _texCoord0; @@ -30,11 +26,9 @@ in vec3 _color; void main(void) { - // set the albedo, normal, specular data - vec4 albedo = texture(albedoMap, _texCoord0); - float roughness = texture(roughnessMap, _texCoord0); - vec3 specular = texture(specularMap, _texCoord0).rgb; - + + <$fetchMaterialTextures(_texCoord0, albedo, roughness, _SCRIBE_NULL, specular)$> + Material mat = getMaterial(); packDeferredFragment( @@ -43,6 +37,6 @@ void main(void) { getMaterialAlbedo(mat) * albedo.rgb * _color, getMaterialRoughness(mat) * roughness, getMaterialMetallic(mat), - specular //getMaterialFresnel(mat) + vec3(specular) //getMaterialFresnel(mat) ); } diff --git a/tools/scribe/src/TextTemplate.cpp b/tools/scribe/src/TextTemplate.cpp index 3ccb0baea0..741ddb6846 100755 --- a/tools/scribe/src/TextTemplate.cpp +++ b/tools/scribe/src/TextTemplate.cpp @@ -18,6 +18,8 @@ typedef TextTemplate::Block::Pointer BlockPointer; typedef TextTemplate::Config::Pointer ConfigPointer; typedef TextTemplate::Pointer TextTemplatePointer; +const std::string TextTemplate::Tag::NULL_VAR = "_SCRIBE_NULL"; + //----------------------------------------------------------------------------- TextTemplate::Config::Config() : _includes(), @@ -370,7 +372,11 @@ bool TextTemplate::convertExpressionToFuncArguments(String& src, std::vector< St token += c; } else if (c == ',') { if (!token.empty()) { - arguments.push_back(token); + if (token == Tag::NULL_VAR) { + arguments.push_back(Tag::NULL_VAR); + } else { + arguments.push_back(token); + } nbTokens++; } token.clear(); @@ -750,7 +756,9 @@ int TextTemplate::evalBlockGeneration(std::ostream& dst, const BlockPointer& blo paramCache.push_back((*it).second); (*it).second = val; } else { - vars.insert(Vars::value_type(funcBlock->command.arguments[i], val)); + if (val != Tag::NULL_VAR) { + vars.insert(Vars::value_type(funcBlock->command.arguments[i], val)); + } paramCache.push_back(""); } } diff --git a/tools/scribe/src/TextTemplate.h b/tools/scribe/src/TextTemplate.h index a6fd04da5c..44edc23c12 100755 --- a/tools/scribe/src/TextTemplate.h +++ b/tools/scribe/src/TextTemplate.h @@ -42,6 +42,8 @@ public: static const char VAR = '$'; static const char COM = '@'; static const char REM = '!'; + + static const std::string NULL_VAR; }; class Command { From 2f5800a4ccc268507c4436265eea62de766d687d Mon Sep 17 00:00:00 2001 From: samcake Date: Tue, 23 Feb 2016 18:31:38 -0800 Subject: [PATCH 12/24] 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; From a0d7ce145e90a689ebb26b527d4ec9bcac9bc403 Mon Sep 17 00:00:00 2001 From: samcake Date: Wed, 24 Feb 2016 17:30:29 -0800 Subject: [PATCH 13/24] Adding support for emissive and occlusion maps, on to pr land --- .../utilities/tools/render/framebuffer.qml | 3 +- libraries/fbx/src/FBXReader_Material.cpp | 8 +++ .../src/model-networking/ModelCache.cpp | 9 ++++ .../src/model-networking/ModelCache.h | 2 + .../src/model-networking/TextureCache.h | 4 +- libraries/model/src/model/Material.cpp | 1 + libraries/model/src/model/Material.h | 9 ++++ libraries/model/src/model/Material.slh | 3 +- .../render-utils/src/DebugDeferredBuffer.cpp | 9 ++++ .../render-utils/src/DebugDeferredBuffer.h | 1 + libraries/render-utils/src/DeferredBuffer.slh | 27 +--------- .../render-utils/src/DeferredBufferWrite.slh | 10 ++-- .../render-utils/src/DeferredGlobalLight.slh | 17 ++++--- .../render-utils/src/MaterialTextures.slh | 51 ++++++++++++++++--- .../render-utils/src/MeshPartPayload.cpp | 36 ++++++++++--- .../src/directional_ambient_light.slf | 2 +- .../src/directional_ambient_light_shadow.slf | 2 +- .../render-utils/src/directional_light.slf | 2 +- .../src/directional_light_shadow.slf | 2 +- .../src/directional_skybox_light.slf | 2 +- .../src/directional_skybox_light_shadow.slf | 2 +- libraries/render-utils/src/model.slf | 25 ++++++--- libraries/render-utils/src/model_lightmap.slf | 6 +-- .../src/model_lightmap_normal_map.slf | 7 ++- .../model_lightmap_normal_specular_map.slf | 8 +-- .../src/model_lightmap_specular_map.slf | 10 ++-- .../render-utils/src/model_normal_map.slf | 27 +++++++--- .../src/model_normal_specular_map.slf | 22 ++++---- .../render-utils/src/model_specular_map.slf | 26 ++++++---- .../render-utils/src/model_translucent.slf | 4 +- libraries/render-utils/src/simple.slf | 2 +- .../render-utils/src/simple_textured.slf | 5 +- libraries/render/src/render/ShapePipeline.cpp | 6 ++- libraries/render/src/render/ShapePipeline.h | 6 ++- tests/gpu-test/src/unlit.slf | 2 +- 35 files changed, 241 insertions(+), 117 deletions(-) diff --git a/examples/utilities/tools/render/framebuffer.qml b/examples/utilities/tools/render/framebuffer.qml index 20b8b384d8..e31f986bc1 100644 --- a/examples/utilities/tools/render/framebuffer.qml +++ b/examples/utilities/tools/render/framebuffer.qml @@ -28,12 +28,13 @@ Column { model: [ "Off", "Depth", - "Diffuse", + "Albedo", "Normal", "Roughness", "Metallic", "Fresnel", "Emissive", + "Occlusion", "Lightmap", "Lighting", "Shadow", diff --git a/libraries/fbx/src/FBXReader_Material.cpp b/libraries/fbx/src/FBXReader_Material.cpp index 2604d83423..188c50304d 100644 --- a/libraries/fbx/src/FBXReader_Material.cpp +++ b/libraries/fbx/src/FBXReader_Material.cpp @@ -149,6 +149,14 @@ void FBXReader::consolidateFBXMaterials() { material.emissiveTexture = emissiveTexture; } + FBXTexture occlusionTexture; + QString occlusionTextureID = occlusionTextures.value(material.materialID); + if (!occlusionTextureID.isNull()) { + occlusionTexture = getTexture(occlusionTextureID); + detectDifferentUVs |= (occlusionTexture.texcoordSet != 0) || (!emissiveTexture.transform.isIdentity()); + material.occlusionTexture = occlusionTexture; + } + glm::vec2 lightmapParams(0.f, 1.f); lightmapParams.x = _lightmapOffset; lightmapParams.y = _lightmapLevel; diff --git a/libraries/model-networking/src/model-networking/ModelCache.cpp b/libraries/model-networking/src/model-networking/ModelCache.cpp index 2bb71ba855..7d59bb5028 100644 --- a/libraries/model-networking/src/model-networking/ModelCache.cpp +++ b/libraries/model-networking/src/model-networking/ModelCache.cpp @@ -363,6 +363,7 @@ static NetworkMaterial* buildNetworkMaterial(NetworkGeometry* geometry, const FB material._material->setTextureMap(model::MaterialKey::METALLIC_MAP, metallicMap); } if (!material.roughnessTexture.filename.isEmpty()) { + material.roughnessTexture.isGlossmap; networkMaterial->roughnessTexture = textureCache->getTexture(textureBaseUrl.resolved(QUrl(material.roughnessTexture.filename)), ROUGHNESS_TEXTURE, material.roughnessTexture.content); networkMaterial->roughnessTextureName = material.roughnessTexture.name; @@ -392,7 +393,15 @@ static NetworkMaterial* buildNetworkMaterial(NetworkGeometry* geometry, const FB material._material->setTextureMap(model::MaterialKey::LIGHTMAP_MAP, lightmapMap); } + if (!material.occlusionTexture.filename.isEmpty()) { + networkMaterial->occlusionTexture = textureCache->getTexture(textureBaseUrl.resolved(QUrl(material.occlusionTexture.filename)), OCCLUSION_TEXTURE, material.occlusionTexture.content); + networkMaterial->occlusionTextureName = material.occlusionTexture.name; + auto occlusionMap = model::TextureMapPointer(new model::TextureMap()); + occlusionMap->setTextureSource(networkMaterial->occlusionTexture->_textureSource); + + material._material->setTextureMap(model::MaterialKey::OCCLUSION_MAP, occlusionMap); + } return networkMaterial; } diff --git a/libraries/model-networking/src/model-networking/ModelCache.h b/libraries/model-networking/src/model-networking/ModelCache.h index c43a1e6d79..b517cc67ca 100644 --- a/libraries/model-networking/src/model-networking/ModelCache.h +++ b/libraries/model-networking/src/model-networking/ModelCache.h @@ -190,6 +190,8 @@ public: QSharedPointer roughnessTexture; QString emissiveTextureName; QSharedPointer emissiveTexture; + QString occlusionTextureName; + QSharedPointer occlusionTexture; QString lightmapTextureName; QSharedPointer lightmapTexture; diff --git a/libraries/model-networking/src/model-networking/TextureCache.h b/libraries/model-networking/src/model-networking/TextureCache.h index 2cfd225889..e9c232c4b6 100644 --- a/libraries/model-networking/src/model-networking/TextureCache.h +++ b/libraries/model-networking/src/model-networking/TextureCache.h @@ -29,7 +29,9 @@ class NetworkTexture; typedef QSharedPointer NetworkTexturePointer; -enum TextureType { DEFAULT_TEXTURE, NORMAL_TEXTURE, BUMP_TEXTURE, SPECULAR_TEXTURE, ROUGHNESS_TEXTURE, EMISSIVE_TEXTURE, CUBE_TEXTURE, LIGHTMAP_TEXTURE, CUSTOM_TEXTURE }; +enum TextureType { DEFAULT_TEXTURE, NORMAL_TEXTURE, BUMP_TEXTURE, SPECULAR_TEXTURE, ROUGHNESS_TEXTURE, EMISSIVE_TEXTURE, + CUBE_TEXTURE, OCCLUSION_TEXTURE, LIGHTMAP_TEXTURE, CUSTOM_TEXTURE +}; /// Stores cached textures, including render-to-texture targets. class TextureCache : public ResourceCache, public Dependency { diff --git a/libraries/model/src/model/Material.cpp b/libraries/model/src/model/Material.cpp index 522debad59..10fa15edd5 100755 --- a/libraries/model/src/model/Material.cpp +++ b/libraries/model/src/model/Material.cpp @@ -58,6 +58,7 @@ void Material::setOpacity(float opacity) { void Material::setAlbedo(const Color& albedo, bool isSRGB) { _key.setAlbedo(glm::any(glm::greaterThan(albedo, Color(0.0f)))); + _schemaBuffer.edit()._key = (uint32)_key._flags.to_ulong(); _schemaBuffer.edit()._albedo = (isSRGB ? ColorUtils::toLinearVec3(albedo) : albedo); } diff --git a/libraries/model/src/model/Material.h b/libraries/model/src/model/Material.h index a6b1cd3706..cdc3e4dc39 100755 --- a/libraries/model/src/model/Material.h +++ b/libraries/model/src/model/Material.h @@ -39,6 +39,7 @@ public: ROUGHNESS_MAP_BIT, TRANSPARENT_MAP_BIT, NORMAL_MAP_BIT, + OCCLUSION_MAP_BIT, LIGHTMAP_MAP_BIT, NUM_FLAGS, @@ -52,6 +53,7 @@ public: ROUGHNESS_MAP, TRANSPARENT_MAP, NORMAL_MAP, + OCCLUSION_MAP, LIGHTMAP_MAP, NUM_MAP_CHANNELS, @@ -83,6 +85,7 @@ public: Builder& withTransparentMap() { _flags.set(TRANSPARENT_MAP_BIT); return (*this); } Builder& withNormalMap() { _flags.set(NORMAL_MAP_BIT); return (*this); } + Builder& withOcclusionMap() { _flags.set(OCCLUSION_MAP_BIT); return (*this); } Builder& withLightmapMap() { _flags.set(LIGHTMAP_MAP_BIT); return (*this); } // Convenient standard keys that we will keep on using all over the place @@ -124,6 +127,9 @@ public: void setNormalMap(bool value) { _flags.set(NORMAL_MAP_BIT, value); } bool isNormalMap() const { return _flags[NORMAL_MAP_BIT]; } + void setOcclusionMap(bool value) { _flags.set(OCCLUSION_MAP_BIT, value); } + bool isOcclusionMap() const { return _flags[OCCLUSION_MAP_BIT]; } + void setLightmapMap(bool value) { _flags.set(LIGHTMAP_MAP_BIT, value); } bool isLightmapMap() const { return _flags[LIGHTMAP_MAP_BIT]; } @@ -182,6 +188,9 @@ public: Builder& withoutNormalMap() { _value.reset(MaterialKey::NORMAL_MAP_BIT); _mask.set(MaterialKey::NORMAL_MAP_BIT); return (*this); } Builder& withNormalMap() { _value.set(MaterialKey::NORMAL_MAP_BIT); _mask.set(MaterialKey::NORMAL_MAP_BIT); return (*this); } + Builder& withoutOcclusionMap() { _value.reset(MaterialKey::OCCLUSION_MAP_BIT); _mask.set(MaterialKey::OCCLUSION_MAP_BIT); return (*this); } + Builder& withOcclusionMap() { _value.set(MaterialKey::OCCLUSION_MAP_BIT); _mask.set(MaterialKey::OCCLUSION_MAP_BIT); return (*this); } + Builder& withoutLightmapMap() { _value.reset(MaterialKey::LIGHTMAP_MAP_BIT); _mask.set(MaterialKey::LIGHTMAP_MAP_BIT); return (*this); } Builder& withLightmapMap() { _value.set(MaterialKey::LIGHTMAP_MAP_BIT); _mask.set(MaterialKey::LIGHTMAP_MAP_BIT); return (*this); } diff --git a/libraries/model/src/model/Material.slh b/libraries/model/src/model/Material.slh index ac5d750bdd..d75f3df5e0 100644 --- a/libraries/model/src/model/Material.slh +++ b/libraries/model/src/model/Material.slh @@ -52,8 +52,9 @@ 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 OCCLUSION_MAP_BIT = 0x00000800; -const int LIGHTMAP_MAP_BIT = 0x00000800; +const int LIGHTMAP_MAP_BIT = 0x00001000; diff --git a/libraries/render-utils/src/DebugDeferredBuffer.cpp b/libraries/render-utils/src/DebugDeferredBuffer.cpp index 1126023f41..2360b6310f 100644 --- a/libraries/render-utils/src/DebugDeferredBuffer.cpp +++ b/libraries/render-utils/src/DebugDeferredBuffer.cpp @@ -85,6 +85,13 @@ static const std::string DEFAULT_NORMAL_SHADER { " }" }; +static const std::string DEFAULT_OCCLUSION_SHADER{ + "vec4 getFragmentColor() {" + " DeferredFragment frag = unpackDeferredFragmentNoPosition(uv);" + " return vec4(vec3(frag.obscurance), 1.0);" + " }" +}; + static const std::string DEFAULT_EMISSIVE_SHADER{ "vec4 getFragmentColor() {" " DeferredFragment frag = unpackDeferredFragmentNoPosition(uv);" @@ -184,6 +191,8 @@ std::string DebugDeferredBuffer::getShaderSourceCode(Mode mode, std::string cust return DEFAULT_DEPTH_SHADER; case EmissiveMode: return DEFAULT_EMISSIVE_SHADER; + case OcclusionMode: + return DEFAULT_OCCLUSION_SHADER; case LightmapMode: return DEFAULT_LIGHTMAP_SHADER; case LightingMode: diff --git a/libraries/render-utils/src/DebugDeferredBuffer.h b/libraries/render-utils/src/DebugDeferredBuffer.h index 0d72eeec8a..f4eacea5b6 100644 --- a/libraries/render-utils/src/DebugDeferredBuffer.h +++ b/libraries/render-utils/src/DebugDeferredBuffer.h @@ -54,6 +54,7 @@ protected: MetallicMode, FresnelMode, EmissiveMode, + OcclusionMode, LightmapMode, LightingMode, ShadowMode, diff --git a/libraries/render-utils/src/DeferredBuffer.slh b/libraries/render-utils/src/DeferredBuffer.slh index f35fc12d1f..bfed92018a 100755 --- a/libraries/render-utils/src/DeferredBuffer.slh +++ b/libraries/render-utils/src/DeferredBuffer.slh @@ -104,43 +104,20 @@ DeferredFragment unpackDeferredFragmentNoPosition(vec2 texcoord) { frag.normal = normalize(frag.normalVal.xyz * 2.0 - vec3(1.0)); frag.mode = 0; - frag.emissive = vec3(0.0); + frag.emissive = frag.specularVal.xyz; if (frag.normalVal.a < 0.5) { frag.mode = 0; frag.roughness = 2.0 * frag.normalVal.a; } else { frag.mode = LIGHT_MAPPED; frag.roughness = 2.0 * frag.normalVal.a - 1.0; - frag.emissive = frag.specularVal.xyz; } - - // if ((frag.normalVal.a >= 0.45) && (frag.normalVal.a <= 0.55)) { - // frag.mode = LIGHT_MAPPED; - // frag.emissive = frag.specularVal.xyz; - // } frag.metallic = frag.diffuseVal.a; 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; - - } else { - frag.diffuse = frag.diffuseVal.xyz; - //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; - - - //frag.roughness = frag.specularVal.w; + frag.obscurance = min(frag.specularVal.w, frag.obscurance); - return frag; } diff --git a/libraries/render-utils/src/DeferredBufferWrite.slh b/libraries/render-utils/src/DeferredBufferWrite.slh index a92ce1d26a..2a2c2883d3 100755 --- a/libraries/render-utils/src/DeferredBufferWrite.slh +++ b/libraries/render-utils/src/DeferredBufferWrite.slh @@ -44,25 +44,27 @@ const float DEFAULT_ROUGHNESS = 0.9; const float DEFAULT_SHININESS = 10; const float DEFAULT_METALLIC = 0; const vec3 DEFAULT_SPECULAR = vec3(0.1); +const vec3 DEFAULT_EMISSIVE = vec3(0.0); +const float DEFAULT_OCCLUSION = 1.0; -void packDeferredFragment(vec3 normal, float alpha, vec3 albedo, float roughness, float metallic, vec3 fresnel) { +void packDeferredFragment(vec3 normal, float alpha, vec3 albedo, float roughness, float metallic, vec3 emissive, float occlusion) { if (alpha != 1.0) { discard; } - // 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); + _fragColor2 = vec4(emissive, occlusion); } + void packDeferredFragmentLightmap(vec3 normal, float alpha, vec3 albedo, float roughness, float metallic, vec3 fresnel, vec3 emissive) { if (alpha != 1.0) { discard; } _fragColor0 = vec4(albedo, metallic); _fragColor1 = vec4(bestFitNormal(normal), 0.5 + 0.5 * clamp(roughness, 0.0, 1.0)); - _fragColor2 = vec4(emissive, roughness); + _fragColor2 = vec4(emissive, 1.0); } void packDeferredFragmentTranslucent(vec3 normal, float alpha, vec3 albedo, vec3 fresnel, float roughness) { diff --git a/libraries/render-utils/src/DeferredGlobalLight.slh b/libraries/render-utils/src/DeferredGlobalLight.slh index de4eb746ef..a3fb1de3ec 100755 --- a/libraries/render-utils/src/DeferredGlobalLight.slh +++ b/libraries/render-utils/src/DeferredGlobalLight.slh @@ -30,7 +30,7 @@ vec4 evalSkyboxLight(vec3 direction, float lod) { <@include model/Light.slh@> <@func declareEvalAmbientGlobalColor()@> -vec3 evalAmbientGlobalColor(mat4 invViewMat, float shadowAttenuation, float obscurance, vec3 position, vec3 normal, vec3 albedo, float metallic, vec3 fresnel, float roughness) { +vec3 evalAmbientGlobalColor(mat4 invViewMat, float shadowAttenuation, float obscurance, vec3 position, vec3 normal, vec3 albedo, float metallic, vec3 emissive, float roughness) { // Need the light now Light light = getLight(); @@ -41,9 +41,11 @@ vec3 evalAmbientGlobalColor(mat4 invViewMat, float shadowAttenuation, float obsc vec3 color = albedo * getLightColor(light) * obscurance * getLightAmbientIntensity(light); - vec4 shading = evalFragShading(fragNormal, -getLightDirection(light), fragEyeDir, metallic, fresnel, roughness); + + vec4 shading = evalFragShading(fragNormal, -getLightDirection(light), fragEyeDir, metallic, vec3(metallic), roughness); color += vec3(albedo * shading.w + shading.rgb) * min(shadowAttenuation, obscurance) * getLightColor(light) * getLightIntensity(light); + color += emissive; return color; } @@ -52,7 +54,7 @@ vec3 evalAmbientGlobalColor(mat4 invViewMat, float shadowAttenuation, float obsc <@func declareEvalAmbientSphereGlobalColor()@> -vec3 evalAmbientSphereGlobalColor(mat4 invViewMat, float shadowAttenuation, float obscurance, vec3 position, vec3 normal, vec3 albedo, float metallic, vec3 fresnel, float roughness) { +vec3 evalAmbientSphereGlobalColor(mat4 invViewMat, float shadowAttenuation, float obscurance, vec3 position, vec3 normal, vec3 albedo, float metallic, vec3 emissive, float roughness) { // Need the light now Light light = getLight(); @@ -64,10 +66,10 @@ vec3 evalAmbientSphereGlobalColor(mat4 invViewMat, float shadowAttenuation, floa vec3 color = albedo * evalSphericalLight(getLightAmbientSphere(light), ambientNormal).xyz * obscurance * getLightAmbientIntensity(light); - vec4 shading = evalFragShading(fragNormal, -getLightDirection(light), fragEyeDir, metallic, fresnel, roughness); + vec4 shading = evalFragShading(fragNormal, -getLightDirection(light), fragEyeDir, metallic, vec3(metallic), roughness); color += vec3(albedo * shading.w + shading.rgb) * min(shadowAttenuation, obscurance) * getLightColor(light) * getLightIntensity(light); - + color += emissive; return color; } <@endfunc@> @@ -76,7 +78,7 @@ vec3 evalAmbientSphereGlobalColor(mat4 invViewMat, float shadowAttenuation, floa <$declareSkyboxMap()$> -vec3 evalSkyboxGlobalColor(mat4 invViewMat, float shadowAttenuation, float obscurance, vec3 position, vec3 normal, vec3 albedo, float metallic, vec3 fresnel, float roughness) { +vec3 evalSkyboxGlobalColor(mat4 invViewMat, float shadowAttenuation, float obscurance, vec3 position, vec3 normal, vec3 albedo, float metallic, vec3 emissive, float roughness) { // Need the light now Light light = getLight(); @@ -87,9 +89,10 @@ vec3 evalSkyboxGlobalColor(mat4 invViewMat, float shadowAttenuation, float obscu vec3 color = albedo * evalSphericalLight(getLightAmbientSphere(light), fragNormal).xyz * obscurance * getLightAmbientIntensity(light); - vec4 shading = evalFragShading(fragNormal, -getLightDirection(light), fragEyeDir, metallic, fresnel, roughness); + vec4 shading = evalFragShading(fragNormal, -getLightDirection(light), fragEyeDir, metallic, vec3(metallic), roughness); color += vec3(albedo * shading.w + shading.rgb) * min(shadowAttenuation, obscurance) * getLightColor(light) * getLightIntensity(light); + color += emissive; return color; } diff --git a/libraries/render-utils/src/MaterialTextures.slh b/libraries/render-utils/src/MaterialTextures.slh index ab58b7cff5..d0f2563d43 100644 --- a/libraries/render-utils/src/MaterialTextures.slh +++ b/libraries/render-utils/src/MaterialTextures.slh @@ -11,7 +11,7 @@ <@if not MODEL_MATERIAL_TEXTURES_SLH@> <@def MODEL_MATERIAL_TEXTURES_SLH@> -<@func declareMaterialTextures(withAlbedo, withRoughness, withNormal, withMetallic)@> +<@func declareMaterialTextures(withAlbedo, withRoughness, withNormal, withMetallic, withEmissive, withOcclusion)@> <@if withAlbedo@> uniform sampler2D albedoMap; @@ -41,21 +41,40 @@ float fetchMetallicMap(vec2 uv) { } <@endif@> +<@if withEmissive@> +uniform sampler2D emissiveMap; +vec3 fetchEmissiveMap(vec2 uv) { + return texture(emissiveMap, uv).rgb; +} +<@endif@> + +<@if withOcclusion@> +uniform sampler2D occlusionMap; +float fetchOcclusionMap(vec2 uv) { + return texture(occlusionMap, uv).r; +} +<@endif@> <@endfunc@> -<@func fetchMaterialTextures(texcoord0, albedo, roughness, normal, metallic)@> +<@func fetchMaterialTextures(matKey, texcoord0, albedo, roughness, normal, metallic, emissive, occlusion)@> <@if albedo@> - vec4 <$albedo$> = fetchAlbedoMap(<$texcoord0$>); + vec4 <$albedo$> = (((<$matKey$> & ALBEDO_MAP_BIT) != 0) ? fetchAlbedoMap(<$texcoord0$>) : vec4(1.0)); <@endif@> <@if roughness@> - float <$roughness$> = fetchRoughnessMap(<$texcoord0$>); + float <$roughness$> = (((<$matKey$> & ROUGHNESS_MAP_BIT) != 0) ? fetchRoughnessMap(<$texcoord0$>) : 1.0); <@endif@> <@if normal@> - vec3 <$normal$> = fetchNormalMap(<$texcoord0$>); + vec3 <$normal$> = (((<$matKey$> & NORMAL_MAP_BIT) != 0) ? fetchNormalMap(<$texcoord0$>) : vec3(0.0, 1.0, 0.0)); <@endif@> <@if metallic@> - float <$metallic$> = fetchMetallicMap(<$texcoord0$>); + float <$metallic$> = (((<$matKey$> & METALLIC_MAP_BIT) != 0) ? fetchMetallicMap(<$texcoord0$>) : 0.0); +<@endif@> +<@if emissive@> + vec3 <$emissive$> = (((<$matKey$> & EMISSIVE_MAP_BIT) != 0) ? fetchEmissiveMap(<$texcoord0$>) : vec3(0.0)); +<@endif@> +<@if occlusion@> + float <$occlusion$> = (((<$matKey$> & OCCLUSION_MAP_BIT) != 0) ? fetchOcclusionMap(<$texcoord0$>) : 1.0); <@endif@> <@endfunc@> @@ -83,6 +102,15 @@ vec3 fetchLightmapMap(vec2 uv) { } <@endfunc@> +<@func evalMaterialAlbedo(fetchedAlbedo, materialAlbedo, matKey, albedo)@> +{ + <$albedo$>.xyz = (((<$matKey$> & ALBEDO_VAL_BIT) != 0) ? <$materialAlbedo$> : vec3(1.0)); + + if (((<$matKey$> & ALBEDO_MAP_BIT) != 0)) { + <$albedo$>.xyz *= <$fetchedAlbedo$>.xyz; + } +} +<@endfunc@> <@func evalMaterialRoughness(fetchedRoughness, materialRoughness, matKey, roughness)@> { @@ -96,5 +124,16 @@ vec3 fetchLightmapMap(vec2 uv) { } <@endfunc@> +<@func evalMaterialEmissive(fetchedEmissive, materialEmissive, matKey, emissive)@> +{ + <$emissive$> = (((<$matKey$> & EMISSIVE_MAP_BIT) != 0) ? <$fetchedEmissive$> : <$materialEmissive$>); +} +<@endfunc@> + +<@func evalMaterialOcclusion(fetchedOcclusion, matKey, occlusion)@> +{ + <$occlusion$> = <$fetchedOcclusion$>; +} +<@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 ab2a969637..8bba58eae2 100644 --- a/libraries/render-utils/src/MeshPartPayload.cpp +++ b/libraries/render-utils/src/MeshPartPayload.cpp @@ -193,22 +193,36 @@ void MeshPartPayload::bindMaterial(gpu::Batch& batch, const ShapePipeline::Locat if (materialKey.isMetallicMap()) { auto specularMap = textureMaps[model::MaterialKey::METALLIC_MAP]; if (specularMap && specularMap->isDefined()) { - batch.setResourceTexture(ShapePipeline::Slot::SPECULAR_MAP, specularMap->getTextureView()); + batch.setResourceTexture(ShapePipeline::Slot::METALLIC_MAP, specularMap->getTextureView()); // texcoord are assumed to be the same has albedo } else { - batch.setResourceTexture(ShapePipeline::Slot::SPECULAR_MAP, textureCache->getBlackTexture()); + batch.setResourceTexture(ShapePipeline::Slot::METALLIC_MAP, textureCache->getBlackTexture()); } } else { - batch.setResourceTexture(ShapePipeline::Slot::SPECULAR_MAP, nullptr); + batch.setResourceTexture(ShapePipeline::Slot::METALLIC_MAP, nullptr); } - // TODO: For now lightmaop is piped into the emissive map unit, we need to fix that and support for real emissive too + // Occlusion map + if (materialKey.isOcclusionMap()) { + auto specularMap = textureMaps[model::MaterialKey::OCCLUSION_MAP]; + if (specularMap && specularMap->isDefined()) { + batch.setResourceTexture(ShapePipeline::Slot::OCCLUSION_MAP, specularMap->getTextureView()); + + // texcoord are assumed to be the same has albedo + } else { + batch.setResourceTexture(ShapePipeline::Slot::OCCLUSION_MAP, textureCache->getWhiteTexture()); + } + } else { + batch.setResourceTexture(ShapePipeline::Slot::OCCLUSION_MAP, nullptr); + } + + // Emissive / Lightmap if (materialKey.isLightmapMap()) { auto lightmapMap = textureMaps[model::MaterialKey::LIGHTMAP_MAP]; if (lightmapMap && lightmapMap->isDefined()) { - batch.setResourceTexture(ShapePipeline::Slot::LIGHTMAP_MAP, lightmapMap->getTextureView()); + batch.setResourceTexture(ShapePipeline::Slot::EMISSIVE_LIGHTMAP_MAP, lightmapMap->getTextureView()); auto lightmapOffsetScale = lightmapMap->getLightmapOffsetScale(); batch._glUniform2f(locations->emissiveParams, lightmapOffsetScale.x, lightmapOffsetScale.y); @@ -217,10 +231,18 @@ void MeshPartPayload::bindMaterial(gpu::Batch& batch, const ShapePipeline::Locat lightmapMap->getTextureTransform().getMatrix(texcoordTransform[1]); } } else { - batch.setResourceTexture(ShapePipeline::Slot::LIGHTMAP_MAP, textureCache->getGrayTexture()); + batch.setResourceTexture(ShapePipeline::Slot::EMISSIVE_LIGHTMAP_MAP, textureCache->getGrayTexture()); + } + } else if (materialKey.isEmissiveMap()) { + auto emissiveMap = textureMaps[model::MaterialKey::EMISSIVE_MAP]; + + if (emissiveMap && emissiveMap->isDefined()) { + batch.setResourceTexture(ShapePipeline::Slot::EMISSIVE_LIGHTMAP_MAP, emissiveMap->getTextureView()); + } else { + batch.setResourceTexture(ShapePipeline::Slot::EMISSIVE_LIGHTMAP_MAP, textureCache->getBlackTexture()); } } else { - batch.setResourceTexture(ShapePipeline::Slot::LIGHTMAP_MAP, nullptr); + batch.setResourceTexture(ShapePipeline::Slot::EMISSIVE_LIGHTMAP_MAP, nullptr); } // Texcoord transforms ? diff --git a/libraries/render-utils/src/directional_ambient_light.slf b/libraries/render-utils/src/directional_ambient_light.slf index 57f9c7e791..e1ec0d3ef0 100755 --- a/libraries/render-utils/src/directional_ambient_light.slf +++ b/libraries/render-utils/src/directional_ambient_light.slf @@ -45,7 +45,7 @@ void main(void) { frag.normal, frag.diffuse, frag.metallic, - frag.specular, + frag.emissive, frag.roughness); _fragColor = vec4(color, frag.normalVal.a); } diff --git a/libraries/render-utils/src/directional_ambient_light_shadow.slf b/libraries/render-utils/src/directional_ambient_light_shadow.slf index a4d897044b..963a10d579 100644 --- a/libraries/render-utils/src/directional_ambient_light_shadow.slf +++ b/libraries/render-utils/src/directional_ambient_light_shadow.slf @@ -47,7 +47,7 @@ void main(void) { frag.normal, frag.diffuse, frag.metallic, - frag.specular, + frag.emissive, frag.roughness); _fragColor = vec4(color, frag.normalVal.a); } diff --git a/libraries/render-utils/src/directional_light.slf b/libraries/render-utils/src/directional_light.slf index eed3b94c91..1a739f9389 100644 --- a/libraries/render-utils/src/directional_light.slf +++ b/libraries/render-utils/src/directional_light.slf @@ -46,7 +46,7 @@ void main(void) { frag.normal, frag.diffuse, frag.metallic, - frag.specular, + frag.emissive, frag.roughness); _fragColor = vec4(color, frag.normalVal.a); } diff --git a/libraries/render-utils/src/directional_light_shadow.slf b/libraries/render-utils/src/directional_light_shadow.slf index 494046bcb3..3b9b4d22c6 100644 --- a/libraries/render-utils/src/directional_light_shadow.slf +++ b/libraries/render-utils/src/directional_light_shadow.slf @@ -48,7 +48,7 @@ void main(void) { frag.normal, frag.diffuse, frag.metallic, - frag.specular, + frag.emissive, frag.roughness); _fragColor = vec4(color, frag.normalVal.a); } diff --git a/libraries/render-utils/src/directional_skybox_light.slf b/libraries/render-utils/src/directional_skybox_light.slf index 605d1de963..9e24a5f585 100755 --- a/libraries/render-utils/src/directional_skybox_light.slf +++ b/libraries/render-utils/src/directional_skybox_light.slf @@ -46,7 +46,7 @@ void main(void) { frag.normal, frag.diffuse, frag.metallic, - frag.specular, + frag.emissive, frag.roughness); _fragColor = vec4(color, frag.normalVal.a); diff --git a/libraries/render-utils/src/directional_skybox_light_shadow.slf b/libraries/render-utils/src/directional_skybox_light_shadow.slf index 75a48caacf..c3008b5509 100644 --- a/libraries/render-utils/src/directional_skybox_light_shadow.slf +++ b/libraries/render-utils/src/directional_skybox_light_shadow.slf @@ -48,7 +48,7 @@ void main(void) { frag.normal, frag.diffuse, frag.metallic, - frag.specular, + frag.emissive, frag.roughness); _fragColor = vec4(color, frag.normalVal.a); diff --git a/libraries/render-utils/src/model.slf b/libraries/render-utils/src/model.slf index b43694bbb7..c9ca6d9eb7 100755 --- a/libraries/render-utils/src/model.slf +++ b/libraries/render-utils/src/model.slf @@ -16,7 +16,7 @@ <@include model/Material.slh@> <@include MaterialTextures.slh@> -<$declareMaterialTextures(ALBEDO, ROUGHNESS)$> +<$declareMaterialTextures(ALBEDO, ROUGHNESS, _SCRIBE_NULL, _SCRIBE_NULL, EMISSIVE, OCCLUSION)$> in vec4 _position; in vec3 _normal; @@ -25,15 +25,26 @@ in vec2 _texCoord0; void main(void) { - <$fetchMaterialTextures(_texCoord0, albedo, roughness)$> - Material mat = getMaterial(); + int matKey = getMaterialKey(mat); + <$fetchMaterialTextures(matKey, _texCoord0, albedoTex, roughnessTex, _SCRIBE_NULL, _SCRIBE_NULL, emissiveTex, occlusionTex)$> + + vec3 albedo = getMaterialAlbedo(mat); + <$evalMaterialAlbedo(albedoTex, albedo, matKey, albedo)$>; + albedo *= _color; + + float roughness = getMaterialRoughness(mat); + <$evalMaterialRoughness(roughnessTex, roughness, matKey, roughness)$>; + + vec3 emissive = getMaterialEmissive(mat); + <$evalMaterialEmissive(emissiveTex, emissive, matKey, emissive)$>; packDeferredFragment( normalize(_normal.xyz), - evalOpaqueFinalAlpha(getMaterialOpacity(mat), albedo.a), - getMaterialAlbedo(mat) * albedo.rgb * _color, - getMaterialRoughness(mat) * roughness, + evalOpaqueFinalAlpha(getMaterialOpacity(mat), albedoTex.a), + albedo, + roughness, getMaterialMetallic(mat), - getMaterialFresnel(mat)); + emissive, + occlusionTex); } diff --git a/libraries/render-utils/src/model_lightmap.slf b/libraries/render-utils/src/model_lightmap.slf index bfe809f472..46d81d39e1 100755 --- a/libraries/render-utils/src/model_lightmap.slf +++ b/libraries/render-utils/src/model_lightmap.slf @@ -27,11 +27,11 @@ in vec3 _normal; in vec3 _color; void main(void) { - - <$fetchMaterialTextures(_texCoord0, albedo, roughness)$> + Material mat = getMaterial(); + int matKey = getMaterialKey(mat); + <$fetchMaterialTextures(matKey, _texCoord0, albedo, roughness)$> <$fetchMaterialLightmap(_texCoord1, emissive)$> - Material mat = getMaterial(); packDeferredFragmentLightmap( normalize(_normal), diff --git a/libraries/render-utils/src/model_lightmap_normal_map.slf b/libraries/render-utils/src/model_lightmap_normal_map.slf index 1d305541fa..9ccc6e5352 100755 --- a/libraries/render-utils/src/model_lightmap_normal_map.slf +++ b/libraries/render-utils/src/model_lightmap_normal_map.slf @@ -28,11 +28,10 @@ in vec3 _tangent; in vec3 _color; void main(void) { - - <$fetchMaterialTextures(_texCoord0, albedo, roughness, normalTexel)$> - <$fetchMaterialLightmap(_texCoord1, lightmapVal)$> - Material mat = getMaterial(); + int matKey = getMaterialKey(mat); + <$fetchMaterialTextures(matKey, _texCoord0, albedo, roughness, normalTexel)$> + <$fetchMaterialLightmap(_texCoord1, lightmapVal)$> vec3 viewNormal; <$tangentToViewSpace(normalTexel, _normal, _tangent, viewNormal)$> 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 93bb3a793e..71909a789f 100755 --- a/libraries/render-utils/src/model_lightmap_normal_specular_map.slf +++ b/libraries/render-utils/src/model_lightmap_normal_specular_map.slf @@ -28,10 +28,10 @@ in vec3 _tangent; in vec3 _color; void main(void) { - <$fetchMaterialTextures(_texCoord0, albedo, roughness, normalTexel, metalllicTex)$> - <$fetchMaterialLightmap(_texCoord1, lightmapVal)$> - Material mat = getMaterial(); + int matKey = getMaterialKey(mat); + <$fetchMaterialTextures(matKey, _texCoord0, albedo, roughness, normalTexel, metallicTex)$> + <$fetchMaterialLightmap(_texCoord1, lightmapVal)$> vec3 viewNormal; <$tangentToViewSpace(normalTexel, _normal, _tangent, viewNormal)$> @@ -41,7 +41,7 @@ void main(void) { evalOpaqueFinalAlpha(getMaterialOpacity(mat), albedo.a), getMaterialAlbedo(mat) * albedo.rgb * _color, getMaterialRoughness(mat) * roughness, - getMaterialMetallic(mat) * metalllicTex, + getMaterialMetallic(mat) * metallicTex, /*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 23d3a8e2b3..5eefefdc29 100755 --- a/libraries/render-utils/src/model_lightmap_specular_map.slf +++ b/libraries/render-utils/src/model_lightmap_specular_map.slf @@ -27,17 +27,17 @@ in vec3 _normal; in vec3 _color; void main(void) { - <$fetchMaterialTextures(_texCoord0, albedo, roughness, _SCRIBE_NULL, specular)$> - <$fetchMaterialLightmap(_texCoord1, lightmapVal)$> - Material mat = getMaterial(); + int matKey = getMaterialKey(mat); + <$fetchMaterialTextures(matKey, _texCoord0, albedo, roughness, _SCRIBE_NULL, metallicTex)$> + <$fetchMaterialLightmap(_texCoord1, lightmapVal)$> packDeferredFragmentLightmap( normalize(_normal), evalOpaqueFinalAlpha(getMaterialOpacity(mat), albedo.a), getMaterialAlbedo(mat) * albedo.rgb * _color, getMaterialRoughness(mat) * roughness, - getMaterialMetallic(mat), - /*specular, // no use of */getMaterialFresnel(mat), + getMaterialMetallic(mat) * metallicTex, + /*metallicTex, // no use of */getMaterialFresnel(mat), lightmapVal); } diff --git a/libraries/render-utils/src/model_normal_map.slf b/libraries/render-utils/src/model_normal_map.slf index 13cb63d322..a584427005 100755 --- a/libraries/render-utils/src/model_normal_map.slf +++ b/libraries/render-utils/src/model_normal_map.slf @@ -17,7 +17,7 @@ <@include model/Material.slh@> <@include MaterialTextures.slh@> -<$declareMaterialTextures(ALBEDO, ROUGHNESS, NORMAL)$> +<$declareMaterialTextures(ALBEDO, ROUGHNESS, NORMAL, _SCRIBE_NULL, EMISSIVE, OCCLUSION)$> in vec4 _position; in vec2 _texCoord0; @@ -26,18 +26,29 @@ in vec3 _tangent; in vec3 _color; void main(void) { - <$fetchMaterialTextures(_texCoord0, albedo, roughness, normalTexel)$> - Material mat = getMaterial(); + int matKey = getMaterialKey(mat); + <$fetchMaterialTextures(matKey, _texCoord0, albedoTex, roughnessTex, normalTex, _SCRIBE_NULL, emissiveTex, occlusionTex)$> + + vec3 albedo = getMaterialAlbedo(mat); + <$evalMaterialAlbedo(albedoTex, albedo, matKey, albedo)$>; + albedo *= _color; + + float roughness = getMaterialRoughness(mat); + <$evalMaterialRoughness(roughnessTex, roughness, matKey, roughness)$>; + + vec3 emissive = getMaterialEmissive(mat); + <$evalMaterialEmissive(emissiveTex, emissive, matKey, emissive)$>; vec3 viewNormal; - <$tangentToViewSpace(normalTexel, _normal, _tangent, viewNormal)$> + <$tangentToViewSpace(normalTex, _normal, _tangent, viewNormal)$> packDeferredFragment( viewNormal, - evalOpaqueFinalAlpha(getMaterialOpacity(mat), albedo.a), - getMaterialAlbedo(mat) * albedo.rgb * _color, - getMaterialRoughness(mat) * roughness, + evalOpaqueFinalAlpha(getMaterialOpacity(mat), albedoTex.a), + albedo, + roughness, getMaterialMetallic(mat), - getMaterialFresnel(mat)); + emissive, + occlusionTex); } diff --git a/libraries/render-utils/src/model_normal_specular_map.slf b/libraries/render-utils/src/model_normal_specular_map.slf index 66fe0cd67e..cf461db7ef 100755 --- a/libraries/render-utils/src/model_normal_specular_map.slf +++ b/libraries/render-utils/src/model_normal_specular_map.slf @@ -17,7 +17,7 @@ <@include model/Material.slh@> <@include MaterialTextures.slh@> -<$declareMaterialTextures(ALBEDO, ROUGHNESS, NORMAL, METALLIC)$> +<$declareMaterialTextures(ALBEDO, ROUGHNESS, NORMAL, METALLIC, EMISSIVE, OCCLUSION)$> in vec4 _position; in vec2 _texCoord0; @@ -26,18 +26,22 @@ in vec3 _tangent; in vec3 _color; void main(void) { - - <$fetchMaterialTextures(_texCoord0, albedo, roughnessTex, normalTexel, metallicTex)$> - Material mat = getMaterial(); int matKey = getMaterialKey(mat); + <$fetchMaterialTextures(matKey, _texCoord0, albedoTex, roughnessTex, normalTex, metallicTex, emissiveTex, occlusionTex)$> + + vec3 albedo = getMaterialAlbedo(mat); + <$evalMaterialAlbedo(albedoTex, albedo, matKey, albedo)$>; + albedo *= _color; float roughness = getMaterialRoughness(mat); <$evalMaterialRoughness(roughnessTex, roughness, matKey, roughness)$>; + vec3 emissive = getMaterialEmissive(mat); + <$evalMaterialEmissive(emissiveTex, emissive, matKey, emissive)$>; vec3 viewNormal; - <$tangentToViewSpace(normalTexel, _normal, _tangent, viewNormal)$> + <$tangentToViewSpace(normalTex, _normal, _tangent, viewNormal)$> float metallic = getMaterialMetallic(mat); <$evalMaterialMetallic(metallicTex, metallic, matKey, metallic)$>; @@ -45,10 +49,10 @@ void main(void) { packDeferredFragment( normalize(viewNormal.xyz), - evalOpaqueFinalAlpha(getMaterialOpacity(mat), albedo.a), - getMaterialAlbedo(mat) * albedo.rgb * _color, + evalOpaqueFinalAlpha(getMaterialOpacity(mat), albedoTex.a), + albedo, roughness, metallic, - /*vec3(specular) //*/getMaterialFresnel(mat) - ); + emissive, + occlusionTex); } diff --git a/libraries/render-utils/src/model_specular_map.slf b/libraries/render-utils/src/model_specular_map.slf index cfdf023ae3..32e5823430 100755 --- a/libraries/render-utils/src/model_specular_map.slf +++ b/libraries/render-utils/src/model_specular_map.slf @@ -17,7 +17,7 @@ <@include model/Material.slh@> <@include MaterialTextures.slh@> -<$declareMaterialTextures(ALBEDO, ROUGHNESS, _SCRIBE_NULL, METALLIC)$> +<$declareMaterialTextures(ALBEDO, ROUGHNESS, _SCRIBE_NULL, METALLIC, EMISSIVE, OCCLUSION)$> in vec4 _position; in vec2 _texCoord0; @@ -26,21 +26,29 @@ in vec3 _color; void main(void) { - - <$fetchMaterialTextures(_texCoord0, albedo, roughness, _SCRIBE_NULL, metallicTex)$> - Material mat = getMaterial(); int matKey = getMaterialKey(mat); + <$fetchMaterialTextures(matKey, _texCoord0, albedoTex, roughnessTex, _SCRIBE_NULL, metallicTex, emissiveTex, occlusionTex)$> + + vec3 albedo = getMaterialAlbedo(mat); + <$evalMaterialAlbedo(albedoTex, albedo, matKey, albedo)$>; + albedo *= _color; + + float roughness = getMaterialRoughness(mat); + <$evalMaterialRoughness(roughnessTex, roughness, matKey, roughness)$>; + + vec3 emissive = getMaterialEmissive(mat); + <$evalMaterialEmissive(emissiveTex, emissive, matKey, emissive)$>; 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, + evalOpaqueFinalAlpha(getMaterialOpacity(mat), albedoTex.a), + albedo, + roughness, metallic, - /*vec3(specular) //*/ getMaterialFresnel(mat) - ); + emissive, + occlusionTex); } diff --git a/libraries/render-utils/src/model_translucent.slf b/libraries/render-utils/src/model_translucent.slf index 3efa523434..086f9fe168 100755 --- a/libraries/render-utils/src/model_translucent.slf +++ b/libraries/render-utils/src/model_translucent.slf @@ -38,7 +38,7 @@ void main(void) { vec3 fragNormal = normalize(_normal); vec3 fragAlbedo = getMaterialAlbedo(mat) * albedo.rgb * _color; float fragMetallic = getMaterialMetallic(mat); - vec3 fragFresnel = getMaterialFresnel(mat); + vec3 fragEmissive = getMaterialEmissive(mat); float fragRoughness = getMaterialRoughness(mat); float fragOpacity = getMaterialOpacity(mat) * albedo.a * _alpha; @@ -52,7 +52,7 @@ void main(void) { fragNormal, fragAlbedo, fragMetallic, - fragFresnel, + fragEmissive, fragRoughness), fragOpacity); } diff --git a/libraries/render-utils/src/simple.slf b/libraries/render-utils/src/simple.slf index 4b5cdfb5ce..5ad9dc174f 100644 --- a/libraries/render-utils/src/simple.slf +++ b/libraries/render-utils/src/simple.slf @@ -54,6 +54,6 @@ void main(void) { normal, 1.0, diffuse, max(0, 1.0 - shininess / 128.0), DEFAULT_METALLIC, specular, specular); } else { packDeferredFragment( - normal, 1.0, diffuse, max(0, 1.0 - shininess / 128.0), DEFAULT_METALLIC, specular); + normal, 1.0, diffuse, max(0, 1.0 - shininess / 128.0), length(specular), DEFAULT_EMISSIVE, DEFAULT_OCCLUSION); } } diff --git a/libraries/render-utils/src/simple_textured.slf b/libraries/render-utils/src/simple_textured.slf index b2bc15dbad..0832b22214 100644 --- a/libraries/render-utils/src/simple_textured.slf +++ b/libraries/render-utils/src/simple_textured.slf @@ -2,7 +2,7 @@ <$VERSION_HEADER$> // Generated on <$_SCRIBE_DATE$> // -// simple.frag +// simple_textured.slf // fragment shader // // Created by Clément Brisset on 5/29/15. @@ -33,5 +33,6 @@ void main(void) { _color.rgb * texel.rgb, DEFAULT_ROUGHNESS, DEFAULT_METALLIC, - DEFAULT_SPECULAR); + DEFAULT_EMISSIVE, + DEFAULT_OCCLUSION); } \ No newline at end of file diff --git a/libraries/render/src/render/ShapePipeline.cpp b/libraries/render/src/render/ShapePipeline.cpp index 68c0b94766..f20b5144d3 100644 --- a/libraries/render/src/render/ShapePipeline.cpp +++ b/libraries/render/src/render/ShapePipeline.cpp @@ -56,8 +56,9 @@ void ShapePlumber::addPipeline(const Filter& filter, const gpu::ShaderPointer& p 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)); + slotBindings.insert(gpu::Shader::Binding(std::string("specularMap"), Slot::METALLIC_MAP)); + slotBindings.insert(gpu::Shader::Binding(std::string("emissiveMap"), Slot::EMISSIVE_LIGHTMAP_MAP)); + slotBindings.insert(gpu::Shader::Binding(std::string("occlusionMap"), Slot::OCCLUSION_MAP)); slotBindings.insert(gpu::Shader::Binding(std::string("lightBuffer"), Slot::LIGHT_BUFFER)); slotBindings.insert(gpu::Shader::Binding(std::string("normalFittingMap"), Slot::NORMAL_FITTING_MAP)); @@ -72,6 +73,7 @@ void ShapePlumber::addPipeline(const Filter& filter, const gpu::ShaderPointer& p locations->normalTextureUnit = program->getTextures().findLocation("normalMap"); locations->specularTextureUnit = program->getTextures().findLocation("specularMap"); locations->emissiveTextureUnit = program->getTextures().findLocation("emissiveMap"); + locations->occlusionTextureUnit = program->getTextures().findLocation("occlusionMap"); locations->skinClusterBufferUnit = program->getBuffers().findLocation("skinClusterBuffer"); locations->materialBufferUnit = program->getBuffers().findLocation("materialBuffer"); locations->lightBufferUnit = program->getBuffers().findLocation("lightBuffer"); diff --git a/libraries/render/src/render/ShapePipeline.h b/libraries/render/src/render/ShapePipeline.h index b4fb622b39..963a3c76d4 100644 --- a/libraries/render/src/render/ShapePipeline.h +++ b/libraries/render/src/render/ShapePipeline.h @@ -197,9 +197,10 @@ public: static const int MATERIAL_GPU = 3; static const int ALBEDO_MAP = 0; static const int NORMAL_MAP = 1; - static const int SPECULAR_MAP = 2; - static const int LIGHTMAP_MAP = 3; + static const int METALLIC_MAP = 2; + static const int EMISSIVE_LIGHTMAP_MAP = 3; static const int ROUGHNESS_MAP = 4; + static const int OCCLUSION_MAP = 5; static const int LIGHT_BUFFER = 4; static const int NORMAL_FITTING_MAP = 10; @@ -213,6 +214,7 @@ public: int roughnessTextureUnit; int specularTextureUnit; int emissiveTextureUnit; + int occlusionTextureUnit; int emissiveParams; int normalFittingMapUnit; int skinClusterBufferUnit; diff --git a/tests/gpu-test/src/unlit.slf b/tests/gpu-test/src/unlit.slf index d070662495..f88fcb510b 100644 --- a/tests/gpu-test/src/unlit.slf +++ b/tests/gpu-test/src/unlit.slf @@ -24,5 +24,5 @@ void main(void) { normalize(_normal.xyz), 1.0, _color.rgb, - DEFAULT_ROUGHNESS, DEFAULT_METALLIC, DEFAULT_SPECULAR); + DEFAULT_ROUGHNESS, DEFAULT_METALLIC, DEFAULT_EMISSIVE, DEFAULT_OCCLUSION); } From 287bfeddda0c8c81cab2267bda680dd446c20c61 Mon Sep 17 00:00:00 2001 From: samcake Date: Wed, 24 Feb 2016 18:31:08 -0800 Subject: [PATCH 14/24] Fixing the way the Obscurance is used with the ssao map --- libraries/render-utils/src/DebugDeferredBuffer.cpp | 13 ++++++++++--- libraries/render-utils/src/DeferredLightingEffect.h | 1 + 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/libraries/render-utils/src/DebugDeferredBuffer.cpp b/libraries/render-utils/src/DebugDeferredBuffer.cpp index 2360b6310f..c4c7d8c9c4 100644 --- a/libraries/render-utils/src/DebugDeferredBuffer.cpp +++ b/libraries/render-utils/src/DebugDeferredBuffer.cpp @@ -20,6 +20,7 @@ #include "GeometryCache.h" #include "FramebufferCache.h" +#include "TextureCache.h" #include "DeferredLightingEffect.h" #include "debug_deferred_buffer_vert.h" @@ -138,7 +139,7 @@ static const std::string DEFAULT_PYRAMID_DEPTH_SHADER { static const std::string DEFAULT_AMBIENT_OCCLUSION_SHADER{ "vec4 getFragmentColor() {" - " return vec4(vec3(texture(occlusionMap, uv).x), 1.0);" + " return vec4(vec3(texture(obscuranceMap, uv).x), 1.0);" // When drawing color " return vec4(vec3(texture(occlusionMap, uv).xyz), 1.0);" // when drawing normal " return vec4(normalize(texture(occlusionMap, uv).xyz * 2.0 - vec3(1.0)), 1.0);" " }" @@ -251,10 +252,10 @@ const gpu::PipelinePointer& DebugDeferredBuffer::getPipeline(Mode mode, std::str slotBindings.insert(gpu::Shader::Binding("normalMap", Normal)); slotBindings.insert(gpu::Shader::Binding("specularMap", Specular)); slotBindings.insert(gpu::Shader::Binding("depthMap", Depth)); + slotBindings.insert(gpu::Shader::Binding("obscuranceMap", AmbientOcclusion)); slotBindings.insert(gpu::Shader::Binding("lightingMap", Lighting)); slotBindings.insert(gpu::Shader::Binding("shadowMap", Shadow)); slotBindings.insert(gpu::Shader::Binding("pyramidMap", Pyramid)); - slotBindings.insert(gpu::Shader::Binding("occlusionMap", AmbientOcclusion)); slotBindings.insert(gpu::Shader::Binding("occlusionBlurredMap", AmbientOcclusionBlurred)); gpu::Shader::makeProgram(*program, slotBindings); @@ -288,6 +289,7 @@ void DebugDeferredBuffer::run(const SceneContextPointer& sceneContext, const Ren gpu::doInBatch(args->_context, [&](gpu::Batch& batch) { const auto geometryBuffer = DependencyManager::get(); const auto framebufferCache = DependencyManager::get(); + const auto textureCache = DependencyManager::get(); const auto& lightStage = DependencyManager::get()->getLightStage(); glm::mat4 projMat; @@ -310,7 +312,12 @@ void DebugDeferredBuffer::run(const SceneContextPointer& sceneContext, const Ren batch.setResourceTexture(Lighting, framebufferCache->getLightingTexture()); batch.setResourceTexture(Shadow, lightStage.lights[0]->shadow.framebuffer->getDepthStencilBuffer()); batch.setResourceTexture(Pyramid, framebufferCache->getDepthPyramidTexture()); - batch.setResourceTexture(AmbientOcclusion, framebufferCache->getOcclusionTexture()); + if (DependencyManager::get()->isAmbientOcclusionEnabled()) { + batch.setResourceTexture(AmbientOcclusion, framebufferCache->getOcclusionTexture()); + } else { + // need to assign the white texture if ao is off + batch.setResourceTexture(AmbientOcclusion, textureCache->getWhiteTexture()); + } batch.setResourceTexture(AmbientOcclusionBlurred, framebufferCache->getOcclusionBlurredTexture()); const glm::vec4 color(1.0f, 1.0f, 1.0f, 1.0f); diff --git a/libraries/render-utils/src/DeferredLightingEffect.h b/libraries/render-utils/src/DeferredLightingEffect.h index 660e44ea1e..ad52cdce96 100644 --- a/libraries/render-utils/src/DeferredLightingEffect.h +++ b/libraries/render-utils/src/DeferredLightingEffect.h @@ -56,6 +56,7 @@ public: const LightStage& getLightStage() { return _lightStage; } void setShadowMapEnabled(bool enable) { _shadowMapEnabled = enable; }; void setAmbientOcclusionEnabled(bool enable) { _ambientOcclusionEnabled = enable; } + bool isAmbientOcclusionEnabled() const { return _ambientOcclusionEnabled; } private: DeferredLightingEffect() = default; From d64b9bb6d95713311fe2b4245336185964728268 Mon Sep 17 00:00:00 2001 From: samcake Date: Fri, 26 Feb 2016 09:18:40 -0800 Subject: [PATCH 15/24] Drafting the support for gloss map --- .../src/model-networking/ModelCache.cpp | 4 ++- .../src/model-networking/TextureCache.cpp | 4 +++ .../src/model-networking/TextureCache.h | 14 +++++++-- libraries/model/src/model/TextureMap.cpp | 30 +++++++++++++++++++ libraries/model/src/model/TextureMap.h | 1 + 5 files changed, 50 insertions(+), 3 deletions(-) diff --git a/libraries/model-networking/src/model-networking/ModelCache.cpp b/libraries/model-networking/src/model-networking/ModelCache.cpp index 7dea008e1d..d944b0c686 100644 --- a/libraries/model-networking/src/model-networking/ModelCache.cpp +++ b/libraries/model-networking/src/model-networking/ModelCache.cpp @@ -364,7 +364,9 @@ static NetworkMaterial* buildNetworkMaterial(NetworkGeometry* geometry, const FB } if (!material.roughnessTexture.filename.isEmpty()) { // FIXME: COnvert from gloss to roughness if material.roughnessTexture.isGlossmap; - networkMaterial->roughnessTexture = textureCache->getTexture(textureBaseUrl.resolved(QUrl(material.roughnessTexture.filename)), ROUGHNESS_TEXTURE, material.roughnessTexture.content); + networkMaterial->roughnessTexture = textureCache->getTexture(textureBaseUrl.resolved(QUrl(material.roughnessTexture.filename)), + (material.roughnessTexture.isGlossmap ? GLOSS_TEXTURE : ROUGHNESS_TEXTURE), + material.roughnessTexture.content); networkMaterial->roughnessTextureName = material.roughnessTexture.name; auto roughnessMap = model::TextureMapPointer(new model::TextureMap()); diff --git a/libraries/model-networking/src/model-networking/TextureCache.cpp b/libraries/model-networking/src/model-networking/TextureCache.cpp index 1b8c73bb80..d49ff91abf 100644 --- a/libraries/model-networking/src/model-networking/TextureCache.cpp +++ b/libraries/model-networking/src/model-networking/TextureCache.cpp @@ -216,6 +216,10 @@ NetworkTexture::TextureLoaderFunc NetworkTexture::getTextureLoader() const { return TextureLoaderFunc(model::TextureUsage::createRoughnessTextureFromImage); break; } + case GLOSS_TEXTURE: { + return TextureLoaderFunc(model::TextureUsage::createRoughnessTextureFromGlossImage); + break; + } case SPECULAR_TEXTURE: { return TextureLoaderFunc(model::TextureUsage::createMetallicTextureFromImage); break; diff --git a/libraries/model-networking/src/model-networking/TextureCache.h b/libraries/model-networking/src/model-networking/TextureCache.h index e9c232c4b6..ded90e7780 100644 --- a/libraries/model-networking/src/model-networking/TextureCache.h +++ b/libraries/model-networking/src/model-networking/TextureCache.h @@ -29,8 +29,18 @@ class NetworkTexture; typedef QSharedPointer NetworkTexturePointer; -enum TextureType { DEFAULT_TEXTURE, NORMAL_TEXTURE, BUMP_TEXTURE, SPECULAR_TEXTURE, ROUGHNESS_TEXTURE, EMISSIVE_TEXTURE, - CUBE_TEXTURE, OCCLUSION_TEXTURE, LIGHTMAP_TEXTURE, CUSTOM_TEXTURE +enum TextureType { + DEFAULT_TEXTURE, + NORMAL_TEXTURE, + BUMP_TEXTURE, + SPECULAR_TEXTURE, + ROUGHNESS_TEXTURE, + GLOSS_TEXTURE, + EMISSIVE_TEXTURE, + CUBE_TEXTURE, + OCCLUSION_TEXTURE, + LIGHTMAP_TEXTURE, + CUSTOM_TEXTURE }; /// Stores cached textures, including render-to-texture targets. diff --git a/libraries/model/src/model/TextureMap.cpp b/libraries/model/src/model/TextureMap.cpp index b2504c4085..41be048440 100755 --- a/libraries/model/src/model/TextureMap.cpp +++ b/libraries/model/src/model/TextureMap.cpp @@ -280,6 +280,36 @@ gpu::Texture* TextureUsage::createRoughnessTextureFromImage(const QImage& srcIma return theTexture; } +gpu::Texture* TextureUsage::createRoughnessTextureFromGlossImage(const QImage& srcImage, const std::string& srcImageName) { + QImage image = srcImage; + if (!image.hasAlphaChannel()) { + if (image.format() != QImage::Format_RGB888) { + image = image.convertToFormat(QImage::Format_RGB888); + } + } else { + if (image.format() != QImage::Format_ARGB32) { + image = image.convertToFormat(QImage::Format_ARGB32); + } + } + + image = image.convertToFormat(QImage::Format_Grayscale8); + + gpu::Texture* theTexture = nullptr; + if ((image.width() > 0) && (image.height() > 0)) { + + gpu::Element formatGPU = gpu::Element(gpu::SCALAR, gpu::NUINT8, gpu::RGB); + gpu::Element formatMip = gpu::Element(gpu::SCALAR, gpu::NUINT8, gpu::RGB); + + theTexture = (gpu::Texture::create2D(formatGPU, image.width(), image.height(), gpu::Sampler(gpu::Sampler::FILTER_MIN_MAG_MIP_LINEAR))); + theTexture->assignStoredMip(0, formatMip, image.byteCount(), image.constBits()); + theTexture->autoGenerateMips(-1); + + // FIXME queue for transfer to GPU and block on completion + } + + return theTexture; +} + gpu::Texture* TextureUsage::createMetallicTextureFromImage(const QImage& srcImage, const std::string& srcImageName) { QImage image = srcImage; if (!image.hasAlphaChannel()) { diff --git a/libraries/model/src/model/TextureMap.h b/libraries/model/src/model/TextureMap.h index 886bc9d3b3..228adb25e6 100755 --- a/libraries/model/src/model/TextureMap.h +++ b/libraries/model/src/model/TextureMap.h @@ -35,6 +35,7 @@ public: static gpu::Texture* createNormalTextureFromNormalImage(const QImage& image, const std::string& srcImageName); static gpu::Texture* createNormalTextureFromBumpImage(const QImage& image, const std::string& srcImageName); static gpu::Texture* createRoughnessTextureFromImage(const QImage& image, const std::string& srcImageName); + static gpu::Texture* createRoughnessTextureFromGlossImage(const QImage& image, const std::string& srcImageName); static gpu::Texture* createMetallicTextureFromImage(const QImage& image, const std::string& srcImageName); static gpu::Texture* createCubeTextureFromImage(const QImage& image, const std::string& srcImageName); static gpu::Texture* createLightmapTextureFromImage(const QImage& image, const std::string& srcImageName); From 540114971392a7d0f8df11e00ae31c3044e9f261 Mon Sep 17 00:00:00 2001 From: samcake Date: Fri, 26 Feb 2016 12:10:13 -0800 Subject: [PATCH 16/24] Cleaning the tlist of textures in the NetworkTexture and in FBXMaterial --- libraries/fbx/src/FBXReader.h | 21 +-- libraries/fbx/src/FBXReader_Material.cpp | 15 ++- libraries/gpu/src/gpu/GLBackendTexture.cpp | 2 +- .../src/model-networking/ModelCache.cpp | 120 ++++++++++++------ .../src/model-networking/ModelCache.h | 8 +- .../src/model-networking/TextureCache.h | 1 + libraries/model/src/model/TextureMap.cpp | 3 + 7 files changed, 106 insertions(+), 64 deletions(-) diff --git a/libraries/fbx/src/FBXReader.h b/libraries/fbx/src/FBXReader.h index 5371d5a41d..3fefd837f3 100644 --- a/libraries/fbx/src/FBXReader.h +++ b/libraries/fbx/src/FBXReader.h @@ -111,7 +111,6 @@ public: QString texcoordSetName; bool isBumpmap{ false }; - bool isGlossmap{ true }; bool isNull() const { return name.isEmpty() && filename.isEmpty() && content.isEmpty(); } }; @@ -151,21 +150,13 @@ public: float roughness{ 1.0f }; float emissiveIntensity{ 1.0f }; - bool useNormalMap{ false }; - bool useAlbedoMap{ false }; - bool useOpacityMap{ false }; - bool useRoughnessMap{ false }; - bool useSpecularMap{ false }; - bool useMetallicMap{ false }; - bool useEmissiveMap{ false }; - bool useOcclusionMap{ false }; - QString materialID; model::MaterialPointer _material; FBXTexture normalTexture; FBXTexture albedoTexture; FBXTexture opacityTexture; + FBXTexture glossTexture; FBXTexture roughnessTexture; FBXTexture specularTexture; FBXTexture metallicTexture; @@ -176,6 +167,16 @@ public: bool isPBSMaterial{ false }; + // THe use XXXMap are not really used to drive which map are going or not, debug only + bool useNormalMap{ false }; + bool useAlbedoMap{ false }; + bool useOpacityMap{ false }; + bool useRoughnessMap{ false }; + bool useSpecularMap{ false }; + bool useMetallicMap{ false }; + bool useEmissiveMap{ false }; + bool useOcclusionMap{ false }; + bool needTangentSpace() const; }; diff --git a/libraries/fbx/src/FBXReader_Material.cpp b/libraries/fbx/src/FBXReader_Material.cpp index cc6452c620..d87eac405f 100644 --- a/libraries/fbx/src/FBXReader_Material.cpp +++ b/libraries/fbx/src/FBXReader_Material.cpp @@ -128,19 +128,20 @@ void FBXReader::consolidateFBXMaterials() { FBXTexture roughnessTexture; QString roughnessTextureID = roughnessTextures.value(material.materialID); - QString shininessTextureID = shininessTextures.value(material.materialID); if (!roughnessTextureID.isNull()) { roughnessTexture = getTexture(roughnessTextureID); - roughnessTexture.isGlossmap = false; - material.roughnessTexture = roughnessTexture; - detectDifferentUVs |= (roughnessTexture.texcoordSet != 0) || (!roughnessTexture.transform.isIdentity()); - } else if (!shininessTextureID.isNull()) { - roughnessTexture = getTexture(roughnessTextureID); - roughnessTexture.isGlossmap = true; material.roughnessTexture = roughnessTexture; detectDifferentUVs |= (roughnessTexture.texcoordSet != 0) || (!roughnessTexture.transform.isIdentity()); } + FBXTexture shininessTexture; + QString shininessTextureID = shininessTextures.value(material.materialID); + if (!shininessTextureID.isNull()) { + shininessTexture = getTexture(shininessTextureID); + material.glossTexture = shininessTexture; + detectDifferentUVs |= (shininessTexture.texcoordSet != 0) || (!shininessTexture.transform.isIdentity()); + } + FBXTexture emissiveTexture; QString emissiveTextureID = emissiveTextures.value(material.materialID); if (!emissiveTextureID.isNull()) { diff --git a/libraries/gpu/src/gpu/GLBackendTexture.cpp b/libraries/gpu/src/gpu/GLBackendTexture.cpp index 8efb4736c1..efcafc9d60 100755 --- a/libraries/gpu/src/gpu/GLBackendTexture.cpp +++ b/libraries/gpu/src/gpu/GLBackendTexture.cpp @@ -199,7 +199,7 @@ public: } case gpu::NUINT8: { if ((dstFormat.getSemantic() == gpu::SRGB || dstFormat.getSemantic() == gpu::SRGBA)) { - texel.internalFormat = GL_SLUMINANCE8; + texel.internalFormat = GL_SLUMINANCE; } else { texel.internalFormat = GL_R8; } diff --git a/libraries/model-networking/src/model-networking/ModelCache.cpp b/libraries/model-networking/src/model-networking/ModelCache.cpp index d944b0c686..f795472137 100644 --- a/libraries/model-networking/src/model-networking/ModelCache.cpp +++ b/libraries/model-networking/src/model-networking/ModelCache.cpp @@ -138,7 +138,9 @@ bool NetworkGeometry::isLoadedWithTextures() const { for (auto&& material : _materials) { if ((material->albedoTexture && !material->albedoTexture->isLoaded()) || (material->normalTexture && !material->normalTexture->isLoaded()) || - (material->specularTexture && !material->specularTexture->isLoaded()) || + (material->roughnessTexture && !material->roughnessTexture->isLoaded()) || + (material->metallicTexture && !material->metallicTexture->isLoaded()) || + (material->occlusionTexture && !material->occlusionTexture->isLoaded()) || (material->emissiveTexture && !material->emissiveTexture->isLoaded()) || (material->lightmapTexture && !material->lightmapTexture->isLoaded())) { return false; @@ -171,22 +173,31 @@ void NetworkGeometry::setTextureWithNameToURL(const QString& name, const QUrl& u normalMap->setTextureSource(material->normalTexture->_textureSource); networkMaterial->setTextureMap(model::MaterialKey::NORMAL_MAP, normalMap); - } else if (material->specularTextureName == name) { - material->specularTexture = textureCache->getTexture(url); + } else if (material->roughnessTextureName == name) { + // FIXME: If passing a gloss map instead of a roughmap how to say that ? looking for gloss in the name ? + material->roughnessTexture = textureCache->getTexture(url, ROUGHNESS_TEXTURE); + + auto roughnessMap = model::TextureMapPointer(new model::TextureMap()); + roughnessMap->setTextureSource(material->roughnessTexture->_textureSource); + + networkMaterial->setTextureMap(model::MaterialKey::ROUGHNESS_MAP, roughnessMap); + } else if (material->metallicTextureName == name) { + // FIXME: If passing a specular map instead of a metallic how to say that ? looking for wtf in the name ? + material->metallicTexture = textureCache->getTexture(url, METALLIC_TEXTURE); auto glossMap = model::TextureMapPointer(new model::TextureMap()); - glossMap->setTextureSource(material->specularTexture->_textureSource); + glossMap->setTextureSource(material->metallicTexture->_textureSource); - networkMaterial->setTextureMap(model::MaterialKey::ROUGHNESS_MAP, glossMap); + networkMaterial->setTextureMap(model::MaterialKey::METALLIC_MAP, glossMap); } else if (material->emissiveTextureName == name) { - material->emissiveTexture = textureCache->getTexture(url); + material->emissiveTexture = textureCache->getTexture(url, EMISSIVE_TEXTURE); auto emissiveMap = model::TextureMapPointer(new model::TextureMap()); emissiveMap->setTextureSource(material->emissiveTexture->_textureSource); networkMaterial->setTextureMap(model::MaterialKey::EMISSIVE_MAP, emissiveMap); } else if (material->lightmapTextureName == name) { - material->emissiveTexture = textureCache->getTexture(url); + material->emissiveTexture = textureCache->getTexture(url, LIGHTMAP_TEXTURE); auto lightmapMap = model::TextureMapPointer(new model::TextureMap()); lightmapMap->setTextureSource(material->emissiveTexture->_textureSource); @@ -208,6 +219,11 @@ void NetworkGeometry::setTextureWithNameToURL(const QString& name, const QUrl& u QStringList NetworkGeometry::getTextureNames() const { QStringList result; for (auto&& material : _materials) { + if (!material->emissiveTextureName.isEmpty() && material->emissiveTexture) { + QString textureURL = material->emissiveTexture->getURL().toString(); + result << material->emissiveTextureName + ":\"" + textureURL + "\""; + } + if (!material->albedoTextureName.isEmpty() && material->albedoTexture) { QString textureURL = material->albedoTexture->getURL().toString(); result << material->albedoTextureName + ":\"" + textureURL + "\""; @@ -218,15 +234,21 @@ QStringList NetworkGeometry::getTextureNames() const { result << material->normalTextureName + ":\"" + textureURL + "\""; } - if (!material->specularTextureName.isEmpty() && material->specularTexture) { - QString textureURL = material->specularTexture->getURL().toString(); - result << material->specularTextureName + ":\"" + textureURL + "\""; + if (!material->roughnessTextureName.isEmpty() && material->roughnessTexture) { + QString textureURL = material->roughnessTexture->getURL().toString(); + result << material->roughnessTextureName + ":\"" + textureURL + "\""; } - if (!material->emissiveTextureName.isEmpty() && material->emissiveTexture) { - QString textureURL = material->emissiveTexture->getURL().toString(); - result << material->emissiveTextureName + ":\"" + textureURL + "\""; + if (!material->metallicTextureName.isEmpty() && material->metallicTexture) { + QString textureURL = material->metallicTexture->getURL().toString(); + result << material->metallicTextureName + ":\"" + textureURL + "\""; } + + if (!material->occlusionTextureName.isEmpty() && material->occlusionTexture) { + QString textureURL = material->occlusionTexture->getURL().toString(); + result << material->occlusionTextureName + ":\"" + textureURL + "\""; + } + if (!material->lightmapTextureName.isEmpty() && material->lightmapTexture) { QString textureURL = material->lightmapTexture->getURL().toString(); result << material->lightmapTextureName + ":\"" + textureURL + "\""; @@ -335,6 +357,7 @@ static NetworkMaterial* buildNetworkMaterial(NetworkGeometry* geometry, const FB material._material->setTextureMap(model::MaterialKey::ALBEDO_MAP, albedoMap); } + if (!material.normalTexture.filename.isEmpty()) { networkMaterial->normalTexture = textureCache->getTexture(textureBaseUrl.resolved(QUrl(material.normalTexture.filename)), (material.normalTexture.isBumpmap ? BUMP_TEXTURE : NORMAL_TEXTURE), material.normalTexture.content); networkMaterial->normalTextureName = material.normalTexture.name; @@ -344,29 +367,18 @@ static NetworkMaterial* buildNetworkMaterial(NetworkGeometry* geometry, const FB material._material->setTextureMap(model::MaterialKey::NORMAL_MAP, normalMap); } - if (!material.specularTexture.filename.isEmpty()) { - networkMaterial->specularTexture = textureCache->getTexture(textureBaseUrl.resolved(QUrl(material.specularTexture.filename)), SPECULAR_TEXTURE, material.specularTexture.content); - networkMaterial->specularTextureName = material.specularTexture.name; - auto specularMap = model::TextureMapPointer(new model::TextureMap()); - specularMap->setTextureSource(networkMaterial->specularTexture->_textureSource); - - material._material->setTextureMap(model::MaterialKey::METALLIC_MAP, specularMap); - } - if (!material.metallicTexture.filename.isEmpty()) { - networkMaterial->specularTexture = textureCache->getTexture(textureBaseUrl.resolved(QUrl(material.metallicTexture.filename)), SPECULAR_TEXTURE, material.metallicTexture.content); - networkMaterial->specularTextureName = material.metallicTexture.name; - - auto metallicMap = model::TextureMapPointer(new model::TextureMap()); - metallicMap->setTextureSource(networkMaterial->specularTexture->_textureSource); - - material._material->setTextureMap(model::MaterialKey::METALLIC_MAP, metallicMap); - } + // Roughness first or gloss maybe if (!material.roughnessTexture.filename.isEmpty()) { - // FIXME: COnvert from gloss to roughness if material.roughnessTexture.isGlossmap; - networkMaterial->roughnessTexture = textureCache->getTexture(textureBaseUrl.resolved(QUrl(material.roughnessTexture.filename)), - (material.roughnessTexture.isGlossmap ? GLOSS_TEXTURE : ROUGHNESS_TEXTURE), - material.roughnessTexture.content); + networkMaterial->roughnessTexture = textureCache->getTexture(textureBaseUrl.resolved(QUrl(material.roughnessTexture.filename)), ROUGHNESS_TEXTURE, material.roughnessTexture.content); + networkMaterial->roughnessTextureName = material.roughnessTexture.name; + + auto roughnessMap = model::TextureMapPointer(new model::TextureMap()); + roughnessMap->setTextureSource(networkMaterial->roughnessTexture->_textureSource); + + material._material->setTextureMap(model::MaterialKey::ROUGHNESS_MAP, roughnessMap); + } else if (!material.glossTexture.filename.isEmpty()) { + networkMaterial->roughnessTexture = textureCache->getTexture(textureBaseUrl.resolved(QUrl(material.glossTexture.filename)), GLOSS_TEXTURE, material.glossTexture.content); networkMaterial->roughnessTextureName = material.roughnessTexture.name; auto roughnessMap = model::TextureMapPointer(new model::TextureMap()); @@ -374,6 +386,37 @@ static NetworkMaterial* buildNetworkMaterial(NetworkGeometry* geometry, const FB material._material->setTextureMap(model::MaterialKey::ROUGHNESS_MAP, roughnessMap); } + + // Metallic first or specular maybe + + if (!material.metallicTexture.filename.isEmpty()) { + networkMaterial->metallicTexture = textureCache->getTexture(textureBaseUrl.resolved(QUrl(material.metallicTexture.filename)), METALLIC_TEXTURE, material.metallicTexture.content); + networkMaterial->metallicTextureName = material.metallicTexture.name; + + auto metallicMap = model::TextureMapPointer(new model::TextureMap()); + metallicMap->setTextureSource(networkMaterial->metallicTexture->_textureSource); + + material._material->setTextureMap(model::MaterialKey::METALLIC_MAP, metallicMap); + } else if (!material.specularTexture.filename.isEmpty()) { + networkMaterial->metallicTexture = textureCache->getTexture(textureBaseUrl.resolved(QUrl(material.specularTexture.filename)), SPECULAR_TEXTURE, material.specularTexture.content); + networkMaterial->metallicTextureName = material.specularTexture.name; + + auto metallicMap = model::TextureMapPointer(new model::TextureMap()); + metallicMap->setTextureSource(networkMaterial->metallicTexture->_textureSource); + + material._material->setTextureMap(model::MaterialKey::METALLIC_MAP, metallicMap); + } + + if (!material.occlusionTexture.filename.isEmpty()) { + networkMaterial->occlusionTexture = textureCache->getTexture(textureBaseUrl.resolved(QUrl(material.occlusionTexture.filename)), OCCLUSION_TEXTURE, material.occlusionTexture.content); + networkMaterial->occlusionTextureName = material.occlusionTexture.name; + + auto occlusionMap = model::TextureMapPointer(new model::TextureMap()); + occlusionMap->setTextureSource(networkMaterial->occlusionTexture->_textureSource); + + material._material->setTextureMap(model::MaterialKey::OCCLUSION_MAP, occlusionMap); + } + if (!material.emissiveTexture.filename.isEmpty()) { networkMaterial->emissiveTexture = textureCache->getTexture(textureBaseUrl.resolved(QUrl(material.emissiveTexture.filename)), EMISSIVE_TEXTURE, material.emissiveTexture.content); networkMaterial->emissiveTextureName = material.emissiveTexture.name; @@ -383,6 +426,7 @@ static NetworkMaterial* buildNetworkMaterial(NetworkGeometry* geometry, const FB material._material->setTextureMap(model::MaterialKey::EMISSIVE_MAP, emissiveMap); } + if (!material.lightmapTexture.filename.isEmpty()) { networkMaterial->lightmapTexture = textureCache->getTexture(textureBaseUrl.resolved(QUrl(material.lightmapTexture.filename)), LIGHTMAP_TEXTURE, material.lightmapTexture.content); networkMaterial->lightmapTextureName = material.lightmapTexture.name; @@ -395,15 +439,7 @@ static NetworkMaterial* buildNetworkMaterial(NetworkGeometry* geometry, const FB material._material->setTextureMap(model::MaterialKey::LIGHTMAP_MAP, lightmapMap); } - if (!material.occlusionTexture.filename.isEmpty()) { - networkMaterial->occlusionTexture = textureCache->getTexture(textureBaseUrl.resolved(QUrl(material.occlusionTexture.filename)), OCCLUSION_TEXTURE, material.occlusionTexture.content); - networkMaterial->occlusionTextureName = material.occlusionTexture.name; - auto occlusionMap = model::TextureMapPointer(new model::TextureMap()); - occlusionMap->setTextureSource(networkMaterial->occlusionTexture->_textureSource); - - material._material->setTextureMap(model::MaterialKey::OCCLUSION_MAP, occlusionMap); - } return networkMaterial; } diff --git a/libraries/model-networking/src/model-networking/ModelCache.h b/libraries/model-networking/src/model-networking/ModelCache.h index b517cc67ca..60f185f691 100644 --- a/libraries/model-networking/src/model-networking/ModelCache.h +++ b/libraries/model-networking/src/model-networking/ModelCache.h @@ -180,16 +180,16 @@ class NetworkMaterial { public: model::MaterialPointer _material; + QString emissiveTextureName; + QSharedPointer emissiveTexture; QString albedoTextureName; QSharedPointer albedoTexture; QString normalTextureName; QSharedPointer normalTexture; - QString specularTextureName; - QSharedPointer specularTexture; QString roughnessTextureName; QSharedPointer roughnessTexture; - QString emissiveTextureName; - QSharedPointer emissiveTexture; + QString metallicTextureName; + QSharedPointer metallicTexture; QString occlusionTextureName; QSharedPointer occlusionTexture; QString lightmapTextureName; diff --git a/libraries/model-networking/src/model-networking/TextureCache.h b/libraries/model-networking/src/model-networking/TextureCache.h index ded90e7780..af82d2a1ad 100644 --- a/libraries/model-networking/src/model-networking/TextureCache.h +++ b/libraries/model-networking/src/model-networking/TextureCache.h @@ -34,6 +34,7 @@ enum TextureType { NORMAL_TEXTURE, BUMP_TEXTURE, SPECULAR_TEXTURE, + METALLIC_TEXTURE = SPECULAR_TEXTURE, // for now spec and metallic texture are the same, converted to grey ROUGHNESS_TEXTURE, GLOSS_TEXTURE, EMISSIVE_TEXTURE, diff --git a/libraries/model/src/model/TextureMap.cpp b/libraries/model/src/model/TextureMap.cpp index 41be048440..c775443425 100755 --- a/libraries/model/src/model/TextureMap.cpp +++ b/libraries/model/src/model/TextureMap.cpp @@ -291,6 +291,9 @@ gpu::Texture* TextureUsage::createRoughnessTextureFromGlossImage(const QImage& s image = image.convertToFormat(QImage::Format_ARGB32); } } + + // Gloss turned into Rough + image.invertPixels(QImage::InvertRgba); image = image.convertToFormat(QImage::Format_Grayscale8); From fdffd8b6e070434ec32851697f0d897c08f9ec74 Mon Sep 17 00:00:00 2001 From: Zach Pomerantz Date: Thu, 25 Feb 2016 14:13:57 -0800 Subject: [PATCH 17/24] Track skybox mipmap levels in DeferredTransform --- .../render-utils/src/DeferredLightingEffect.cpp | 14 +++++++++++--- .../render-utils/src/DeferredLightingEffect.h | 6 +++--- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/libraries/render-utils/src/DeferredLightingEffect.cpp b/libraries/render-utils/src/DeferredLightingEffect.cpp index e2b5721bd9..736b34d779 100644 --- a/libraries/render-utils/src/DeferredLightingEffect.cpp +++ b/libraries/render-utils/src/DeferredLightingEffect.cpp @@ -94,7 +94,7 @@ void DeferredLightingEffect::init() { lp->setColor(glm::vec3(1.0f)); lp->setIntensity(1.0f); lp->setType(model::Light::SUN); - lp->setAmbientSpherePreset(gpu::SphericalHarmonics::Preset(_ambientLightMode % gpu::SphericalHarmonics::NUM_PRESET)); + lp->setAmbientSpherePreset(gpu::SphericalHarmonics::Preset::OLD_TOWN_SQUARE); } void DeferredLightingEffect::addPointLight(const glm::vec3& position, float radius, const glm::vec3& color, @@ -321,7 +321,7 @@ void DeferredLightingEffect::render(const render::RenderContextPointer& renderCo if (_skyboxTexture) { program = _directionalSkyboxLightShadow; locations = _directionalSkyboxLightShadowLocations; - } else if (_ambientLightMode > -1) { + } else { program = _directionalAmbientSphereLightShadow; locations = _directionalAmbientSphereLightShadowLocations; } @@ -329,7 +329,7 @@ void DeferredLightingEffect::render(const render::RenderContextPointer& renderCo if (_skyboxTexture) { program = _directionalSkyboxLight; locations = _directionalSkyboxLightLocations; - } else if (_ambientLightMode > -1) { + } else { program = _directionalAmbientSphereLight; locations = _directionalAmbientSphereLightLocations; } @@ -562,6 +562,14 @@ static void loadLightProgram(const char* vertSource, const char* fragSource, boo void DeferredLightingEffect::setGlobalLight(const model::LightPointer& light, const gpu::TexturePointer& skyboxTexture) { _allocatedLights.front() = light; _skyboxTexture = skyboxTexture; + + // Update the available mipmap levels + if (_skyboxTexture) { + float dim = glm::max(_skyboxTexture->getHeight(), _skyboxTexture->getWidth(), _skyboxTexture->getDepth()); + auto skyboxMipmapLevels = 1 + glm::floor(glm::log2(dim)); + _deferredTransformBuffer[0].edit().skyboxMipmapLevels = skyboxMipmapLevels; + _deferredTransformBuffer[1].edit().skyboxMipmapLevels = skyboxMipmapLevels; + } } model::MeshPointer DeferredLightingEffect::getSpotLightMesh() { diff --git a/libraries/render-utils/src/DeferredLightingEffect.h b/libraries/render-utils/src/DeferredLightingEffect.h index 7fc6d99a95..49a3825fca 100644 --- a/libraries/render-utils/src/DeferredLightingEffect.h +++ b/libraries/render-utils/src/DeferredLightingEffect.h @@ -96,7 +96,6 @@ private: std::vector _pointLights; std::vector _spotLights; - int _ambientLightMode = 0; gpu::TexturePointer _skyboxTexture; // Class describing the uniform buffer with all the parameters common to the deferred shaders @@ -104,8 +103,9 @@ private: public: glm::mat4 projection; glm::mat4 viewInverse; - float stereoSide{ 0.f }; - float spareA, spareB, spareC; + float stereoSide { 0.f }; + float skyboxMipmapLevels { 1.0f }; + float spareA, spareB; DeferredTransform() {} }; From 1fbcaea4b4d6b77ab65d5031952400aa9f50fafb Mon Sep 17 00:00:00 2001 From: Zach Pomerantz Date: Thu, 25 Feb 2016 14:16:06 -0800 Subject: [PATCH 18/24] Fix evalSkyboxGlobalColor --- libraries/render-utils/src/DeferredBuffer.slh | 9 +- .../render-utils/src/DeferredGlobalLight.slh | 107 +++++++----------- .../src/directional_skybox_light.slf | 3 +- .../src/directional_skybox_light_shadow.slf | 3 +- 4 files changed, 49 insertions(+), 73 deletions(-) diff --git a/libraries/render-utils/src/DeferredBuffer.slh b/libraries/render-utils/src/DeferredBuffer.slh index bfed92018a..699926a87c 100755 --- a/libraries/render-utils/src/DeferredBuffer.slh +++ b/libraries/render-utils/src/DeferredBuffer.slh @@ -34,8 +34,9 @@ uniform sampler2D lightingMap; struct DeferredTransform { mat4 projection; mat4 viewInverse; - - vec4 stereoSide_spareABC; + float stereoSide; + float skyboxMipmapLevels; + vec2 _spareAB; }; layout(std140) uniform deferredTransformBuffer { @@ -46,10 +47,10 @@ DeferredTransform getDeferredTransform() { } bool getStereoMode(DeferredTransform deferredTransform) { - return (deferredTransform.stereoSide_spareABC.x != 0.0); + return (deferredTransform.stereoSide != 0.0); } float getStereoSide(DeferredTransform deferredTransform) { - return (deferredTransform.stereoSide_spareABC.x); + return (deferredTransform.stereoSide); } vec4 evalEyePositionFromZ(DeferredTransform deferredTransform, float depthVal, vec2 texcoord) { diff --git a/libraries/render-utils/src/DeferredGlobalLight.slh b/libraries/render-utils/src/DeferredGlobalLight.slh index a3fb1de3ec..3764b19f7c 100755 --- a/libraries/render-utils/src/DeferredGlobalLight.slh +++ b/libraries/render-utils/src/DeferredGlobalLight.slh @@ -11,88 +11,61 @@ <@if not DEFERRED_GLOBAL_LIGHT_SLH@> <@def DEFERRED_GLOBAL_LIGHT_SLH@> +<@include model/Light.slh@> <@include DeferredLighting.slh@> <@func declareSkyboxMap()@> - +// declareSkyboxMap uniform samplerCube skyboxMap; vec4 evalSkyboxLight(vec3 direction, float lod) { - // FIXME - //vec4 skytexel = textureLod(skyboxMap, direction, lod * textureQueryLevels(skyboxMap)); - vec4 skytexel = texture(skyboxMap, direction); - return skytexel; + // textureQueryLevels is not available until #430, so we require explicit lod + // float mipmapLevel = lod * textureQueryLevels(skyboxMap); + return textureLod(skyboxMap, direction, lod); } - <@endfunc@> -// Everything about light -<@include model/Light.slh@> +<@func prepareGlobalLight()@> + // prepareGlobalLight + + // Transform directions to worldspace + vec3 fragNormal = vec3(invViewMat * vec4(normal, 0.0)); + vec3 fragEyeVector = vec3(invViewMat * vec4(-position, 0.0)); + vec3 fragEyeDir = normalize(fragEyeVector); + + // Get light + Light light = getLight(); + + vec4 shading = evalFragShading(fragNormal, -getLightDirection(light), fragEyeDir, metallic, vec3(metallic), roughness); + color = vec3(albedo * shading.w + shading.rgb) * min(shadowAttenuation, obscurance) * getLightColor(light) * getLightIntensity(light); + color += emissive; +<@endfunc@> <@func declareEvalAmbientGlobalColor()@> vec3 evalAmbientGlobalColor(mat4 invViewMat, float shadowAttenuation, float obscurance, vec3 position, vec3 normal, vec3 albedo, float metallic, vec3 emissive, float roughness) { - - // Need the light now - Light light = getLight(); - - vec3 fragNormal = vec3(invViewMat * vec4(normal, 0.0)); - vec4 fragEyeVector = invViewMat * vec4(-position, 0.0); - vec3 fragEyeDir = normalize(fragEyeVector.xyz); - - vec3 color = albedo * getLightColor(light) * obscurance * getLightAmbientIntensity(light); - - - vec4 shading = evalFragShading(fragNormal, -getLightDirection(light), fragEyeDir, metallic, vec3(metallic), roughness); - - color += vec3(albedo * shading.w + shading.rgb) * min(shadowAttenuation, obscurance) * getLightColor(light) * getLightIntensity(light); - color += emissive; - + <$prepareGlobalLight()$> + color += albedo * getLightColor(light) * obscurance * getLightAmbientIntensity(light); return color; } <@endfunc@> <@func declareEvalAmbientSphereGlobalColor()@> - - vec3 evalAmbientSphereGlobalColor(mat4 invViewMat, float shadowAttenuation, float obscurance, vec3 position, vec3 normal, vec3 albedo, float metallic, vec3 emissive, float roughness) { - // Need the light now - Light light = getLight(); - - vec3 fragNormal = normalize(vec3(invViewMat * vec4(normal, 0.0))); - vec4 fragEyeVector = invViewMat * vec4(-position, 0.0); - vec3 fragEyeDir = normalize(fragEyeVector.xyz); - - vec3 ambientNormal = fragNormal.xyz; - - vec3 color = albedo * evalSphericalLight(getLightAmbientSphere(light), ambientNormal).xyz * obscurance * getLightAmbientIntensity(light); - - vec4 shading = evalFragShading(fragNormal, -getLightDirection(light), fragEyeDir, metallic, vec3(metallic), roughness); - - color += vec3(albedo * shading.w + shading.rgb) * min(shadowAttenuation, obscurance) * getLightColor(light) * getLightIntensity(light); - color += emissive; + <$prepareGlobalLight()$> + color += albedo * evalSphericalLight(getLightAmbientSphere(light), fragNormal).xyz * obscurance * getLightAmbientIntensity(light); return color; } <@endfunc@> <@func declareEvalSkyboxGlobalColor()@> - <$declareSkyboxMap()$> - vec3 evalSkyboxGlobalColor(mat4 invViewMat, float shadowAttenuation, float obscurance, vec3 position, vec3 normal, vec3 albedo, float metallic, vec3 emissive, float roughness) { - // Need the light now - Light light = getLight(); - - vec3 fragNormal = normalize(vec3(invViewMat * vec4(normal, 0.0))); - vec4 fragEyeVector = invViewMat * vec4(-position, 0.0); - vec3 fragEyeDir = normalize(fragEyeVector.xyz); - + <$prepareGlobalLight()$> - vec3 color = albedo * evalSphericalLight(getLightAmbientSphere(light), fragNormal).xyz * obscurance * getLightAmbientIntensity(light); - - vec4 shading = evalFragShading(fragNormal, -getLightDirection(light), fragEyeDir, metallic, vec3(metallic), roughness); - - color += vec3(albedo * shading.w + shading.rgb) * min(shadowAttenuation, obscurance) * getLightColor(light) * getLightIntensity(light); - color += emissive; + vec3 direction = -reflect(fragEyeDir, fragNormal); + float lod = min(1.0 + floor((1.0 - gloss) * levels), levels); + vec4 skyboxLight = evalSkyboxLight(direction, lod); + color += albedo * skyboxLight.rgb * skyboxLight.a * obscurance * getLightAmbientIntensity(light); return color; } @@ -100,26 +73,26 @@ vec3 evalSkyboxGlobalColor(mat4 invViewMat, float shadowAttenuation, float obscu <@func declareEvalLightmappedColor()@> vec3 evalLightmappedColor(mat4 invViewMat, float shadowAttenuation, float obscurance, vec3 normal, vec3 albedo, vec3 lightmap) { - +vec3 evalLightmappedColor(mat4 invViewMat, float shadowAttenuation, float obscurance, vec3 normal, vec3 diffuse, vec3 lightmap) { Light light = getLight(); - vec3 fragNormal = vec3(invViewMat * vec4(normal, 0.0)); - float diffuseDot = dot(fragNormal, -getLightDirection(light)); - - // need to catch normals perpendicular to the projection plane hence the magic number for the threshold - // it should be just 0, but we have innacurracy so we need to overshoot + // Catch normals perpendicular to the projection plane, hence the magic number for the threshold + // It should be just 0, but we have inaccuracy so we overshoot const float PERPENDICULAR_THRESHOLD = -0.005; + vec3 fragNormal = vec3(invViewMat * vec4(normal, 0.0)); // transform to worldspace + float diffuseDot = dot(fragNormal, -getLightDirection(light)); float facingLight = step(PERPENDICULAR_THRESHOLD, diffuseDot); - //float facingLight = step(PERPENDICULAR_THRESHOLD, diffuseDot); - // evaluate the shadow test but only relevant for light facing fragments + + // Reevaluate the shadow attenuation for light facing fragments float lightAttenuation = (1 - facingLight) + facingLight * shadowAttenuation; - // diffuse light is the lightmap dimmed by shadow + + // Diffuse light is the lightmap dimmed by shadow vec3 diffuseLight = lightAttenuation * lightmap; - // ambient is a tiny percentage of the lightmap and only when in the shadow + // Ambient light is the lightmap when in shadow vec3 ambientLight = (1 - lightAttenuation) * lightmap * getLightAmbientIntensity(light); - return obscurance * albedo * (ambientLight + diffuseLight); + return obscurance * albedo * (diffuseLight + ambientLight); } <@endfunc@> diff --git a/libraries/render-utils/src/directional_skybox_light.slf b/libraries/render-utils/src/directional_skybox_light.slf index 9e24a5f585..5ec6cc5da3 100755 --- a/libraries/render-utils/src/directional_skybox_light.slf +++ b/libraries/render-utils/src/directional_skybox_light.slf @@ -47,7 +47,8 @@ void main(void) { frag.diffuse, frag.metallic, frag.emissive, - frag.roughness); + frag.roughness, + deferredTransform.skyboxMipmapLevels); _fragColor = vec4(color, frag.normalVal.a); } diff --git a/libraries/render-utils/src/directional_skybox_light_shadow.slf b/libraries/render-utils/src/directional_skybox_light_shadow.slf index c3008b5509..b3f273b39e 100644 --- a/libraries/render-utils/src/directional_skybox_light_shadow.slf +++ b/libraries/render-utils/src/directional_skybox_light_shadow.slf @@ -49,7 +49,8 @@ void main(void) { frag.diffuse, frag.metallic, frag.emissive, - frag.roughness); + frag.roughness, + deferredTransform.skyboxMipmapLevels); _fragColor = vec4(color, frag.normalVal.a); } From cfcde5032ccb9a9f99a98ddf65bad3e9f55643d7 Mon Sep 17 00:00:00 2001 From: samcake Date: Fri, 26 Feb 2016 16:32:29 -0800 Subject: [PATCH 19/24] Post merge with Zach work for environment map --- libraries/render-utils/src/DeferredLightingEffect.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/libraries/render-utils/src/DeferredLightingEffect.cpp b/libraries/render-utils/src/DeferredLightingEffect.cpp index 7c5c9d1a06..3ffdb2d47b 100644 --- a/libraries/render-utils/src/DeferredLightingEffect.cpp +++ b/libraries/render-utils/src/DeferredLightingEffect.cpp @@ -323,7 +323,7 @@ void DeferredLightingEffect::render(const render::RenderContextPointer& renderCo /* if (_skyboxTexture) { program = _directionalSkyboxLightShadow; locations = _directionalSkyboxLightShadowLocations; - } else {*/ + } else*/ { program = _directionalAmbientSphereLightShadow; locations = _directionalAmbientSphereLightShadowLocations; } @@ -331,7 +331,7 @@ void DeferredLightingEffect::render(const render::RenderContextPointer& renderCo /* if (_skyboxTexture) { program = _directionalSkyboxLight; locations = _directionalSkyboxLightLocations; - } else {*/ + } else*/ { program = _directionalAmbientSphereLight; locations = _directionalAmbientSphereLightLocations; } From 630c61e61da2009ff1460dc0d286dee18f1cca73 Mon Sep 17 00:00:00 2001 From: samcake Date: Mon, 29 Feb 2016 18:54:43 -0800 Subject: [PATCH 20/24] Reflection is working correctly with the PBR property, ship t --- .../src/EntityTreeRenderer.cpp | 2 ++ libraries/model/src/model/Light.cpp | 13 +++++++++-- libraries/model/src/model/Light.h | 7 +++++- libraries/model/src/model/Material.h | 2 +- libraries/model/src/model/Stage.cpp | 3 +++ libraries/model/src/model/Stage.h | 1 + libraries/model/src/model/TextureMap.cpp | 4 ++-- libraries/render-utils/src/DeferredBuffer.slh | 8 ++++++- .../render-utils/src/DeferredGlobalLight.slh | 23 ++++++++++++++----- .../render-utils/src/DeferredLighting.slh | 2 +- .../src/DeferredLightingEffect.cpp | 20 +++++++++------- .../render-utils/src/MaterialTextures.slh | 4 ++-- .../src/SceneScriptingInterface.cpp | 5 ++++ .../src/SceneScriptingInterface.h | 1 + 14 files changed, 71 insertions(+), 24 deletions(-) diff --git a/libraries/entities-renderer/src/EntityTreeRenderer.cpp b/libraries/entities-renderer/src/EntityTreeRenderer.cpp index bc1f614f79..ccf5036fa1 100644 --- a/libraries/entities-renderer/src/EntityTreeRenderer.cpp +++ b/libraries/entities-renderer/src/EntityTreeRenderer.cpp @@ -325,6 +325,7 @@ void EntityTreeRenderer::applyZonePropertiesToScene(std::shared_ptrgetGPUTexture()->getIrradiance()) { sceneKeyLight->setAmbientSphere(_ambientTexture->getGPUTexture()->getIrradiance()); + sceneKeyLight->setAmbientMap(_ambientTexture->getGPUTexture()); isAmbientTextureSet = true; } } else { @@ -360,6 +361,7 @@ void EntityTreeRenderer::applyZonePropertiesToScene(std::shared_ptrgetIrradiance()) { sceneKeyLight->setAmbientSphere(texture->getIrradiance()); + sceneKeyLight->setAmbientMap(texture); isAmbientTextureSet = true; } } else { diff --git a/libraries/model/src/model/Light.cpp b/libraries/model/src/model/Light.cpp index 92f53ad4c6..1497ceba86 100755 --- a/libraries/model/src/model/Light.cpp +++ b/libraries/model/src/model/Light.cpp @@ -141,6 +141,15 @@ void Light::setAmbientSpherePreset(gpu::SphericalHarmonics::Preset preset) { editSchema()._ambientSphere.assignPreset(preset); } -void Light::setAmbientMapNumMips(int numMips) { - editSchema()._ambientMapNumMips = numMips; +void Light::setAmbientMap(gpu::TexturePointer ambientMap) { + _ambientMap = ambientMap; + if (ambientMap) { + setAmbientMapNumMips(_ambientMap->evalNumMips()); + } else { + setAmbientMapNumMips(0); + } +} + +void Light::setAmbientMapNumMips(int numMips) { + editSchema()._ambientMapNumMips = (float)numMips; } diff --git a/libraries/model/src/model/Light.h b/libraries/model/src/model/Light.h index 1ec47f7eea..1db73f4093 100755 --- a/libraries/model/src/model/Light.h +++ b/libraries/model/src/model/Light.h @@ -108,6 +108,9 @@ public: const gpu::SphericalHarmonics& getAmbientSphere() const { return getSchema()._ambientSphere; } void setAmbientSpherePreset(gpu::SphericalHarmonics::Preset preset); + void setAmbientMap(gpu::TexturePointer ambientMap); + gpu::TexturePointer getAmbientMap() const { return _ambientMap; } + void setAmbientMapNumMips(int numMips); int getAmbientMapNumMips() const { return getSchema()._ambientMapNumMips; } @@ -123,7 +126,7 @@ public: Vec4 _spot{0.0f, 0.0f, 0.0f, 0.0f}; Vec4 _shadow{0.0f}; - int _ambientMapNumMips{ 0 }; + float _ambientMapNumMips{ 0 }; Vec3 _control{ 0.0f, 0.0f, 0.0f }; gpu::SphericalHarmonics _ambientSphere; @@ -137,6 +140,8 @@ protected: UniformBufferView _schemaBuffer; Transform _transform; + gpu::TexturePointer _ambientMap; + const Schema& getSchema() const { return _schemaBuffer.get(); } Schema& editSchema() { return _schemaBuffer.edit(); } diff --git a/libraries/model/src/model/Material.h b/libraries/model/src/model/Material.h index cdc3e4dc39..81a55eea8a 100755 --- a/libraries/model/src/model/Material.h +++ b/libraries/model/src/model/Material.h @@ -277,7 +277,7 @@ public: const TextureMaps& getTextureMaps() const { return _textureMaps; } // conversion from legacy material properties to PBR equivalent - static float shininessToRoughness(float shininess) { return 1.0f - shininess / 128.0f; } + static float shininessToRoughness(float shininess) { return 1.0f - shininess / 100.0f; } protected: diff --git a/libraries/model/src/model/Stage.cpp b/libraries/model/src/model/Stage.cpp index c976e5c396..23503ef6fb 100644 --- a/libraries/model/src/model/Stage.cpp +++ b/libraries/model/src/model/Stage.cpp @@ -212,6 +212,9 @@ void SunSkyStage::setSunAmbientSphere(const gpu::SHPointer& sphere) { _sunLight->setAmbientSpherePreset(DEFAULT_AMBIENT_SPHERE); } } +void SunSkyStage::setSunAmbientMap(const gpu::TexturePointer& map) { + _sunLight->setAmbientMap(map); +} void SunSkyStage::setSunDirection(const Vec3& direction) { if (!isSunModelEnabled()) { diff --git a/libraries/model/src/model/Stage.h b/libraries/model/src/model/Stage.h index 31772d5e48..d9fa81b16f 100644 --- a/libraries/model/src/model/Stage.h +++ b/libraries/model/src/model/Stage.h @@ -150,6 +150,7 @@ public: void setSunAmbientIntensity(float intensity) { _sunLight->setAmbientIntensity(intensity); } float getSunAmbientIntensity() const { return getSunLight()->getAmbientIntensity(); } void setSunAmbientSphere(const gpu::SHPointer& sphere); + void setSunAmbientMap(const gpu::TexturePointer& map); // The sun direction is expressed in the world space void setSunDirection(const Vec3& direction); diff --git a/libraries/model/src/model/TextureMap.cpp b/libraries/model/src/model/TextureMap.cpp index c775443425..d0416ec3b5 100755 --- a/libraries/model/src/model/TextureMap.cpp +++ b/libraries/model/src/model/TextureMap.cpp @@ -593,8 +593,8 @@ gpu::Texture* TextureUsage::createCubeTextureFromImage(const QImage& srcImage, c theTexture->assignStoredMipFace(0, formatMip, face.byteCount(), face.constBits(), f); f++; } - - // GEnerate irradiance while we are at it + + // Generate irradiance while we are at it theTexture->generateIrradiance(); } } diff --git a/libraries/render-utils/src/DeferredBuffer.slh b/libraries/render-utils/src/DeferredBuffer.slh index cc5780e425..5a3c941ce3 100755 --- a/libraries/render-utils/src/DeferredBuffer.slh +++ b/libraries/render-utils/src/DeferredBuffer.slh @@ -115,7 +115,13 @@ DeferredFragment unpackDeferredFragmentNoPosition(vec2 texcoord) { frag.metallic = frag.diffuseVal.a; frag.diffuse = frag.diffuseVal.xyz; - frag.specular = vec3(frag.metallic); + if (frag.metallic <= 0.5) { + frag.metallic = 0.0; + frag.specular = vec3(0.03); // Default Di-electric fresnel value + } else { + frag.specular = vec3(frag.diffuseVal.xyz); + frag.metallic = 1.0; + } frag.obscurance = min(frag.specularVal.w, frag.obscurance); return frag; diff --git a/libraries/render-utils/src/DeferredGlobalLight.slh b/libraries/render-utils/src/DeferredGlobalLight.slh index f0231dbb9e..0f92f8df2c 100755 --- a/libraries/render-utils/src/DeferredGlobalLight.slh +++ b/libraries/render-utils/src/DeferredGlobalLight.slh @@ -35,8 +35,12 @@ vec4 evalSkyboxLight(vec3 direction, float lod) { // Get light Light light = getLight(); - - vec4 shading = evalFragShading(fragNormal, -getLightDirection(light), fragEyeDir, metallic, vec3(metallic), roughness); + vec3 fresnel = vec3(0.03); // Default Di-electric fresnel value + if (metallic > 0.5) { + fresnel = albedo; + metallic = 1.0; + } + vec4 shading = evalFragShading(fragNormal, -getLightDirection(light), fragEyeDir, metallic, fresnel, roughness); vec3 color = vec3(albedo * shading.w + shading.rgb) * min(shadowAttenuation, obscurance) * getLightColor(light) * getLightIntensity(light); color += emissive; <@endfunc@> @@ -52,21 +56,28 @@ vec3 evalAmbientGlobalColor(mat4 invViewMat, float shadowAttenuation, float obsc <@func declareEvalAmbientSphereGlobalColor()@> vec3 evalAmbientSphereGlobalColor(mat4 invViewMat, float shadowAttenuation, float obscurance, vec3 position, vec3 normal, vec3 albedo, float metallic, vec3 emissive, float roughness) { <$prepareGlobalLight()$> - color += albedo * evalSphericalLight(getLightAmbientSphere(light), fragNormal).xyz * obscurance * getLightAmbientIntensity(light); + color += (1 - (metallic * 0.5)) * albedo * evalSphericalLight(getLightAmbientSphere(light), fragNormal).xyz * obscurance * getLightAmbientIntensity(light); return color; } <@endfunc@> <@func declareEvalSkyboxGlobalColor()@> <$declareSkyboxMap()$> + +vec3 fresnelSchlickAmbient(vec3 fresnelColor, vec3 lightDir, vec3 halfDir, float gloss) { + return fresnelColor + (max(vec3(gloss), fresnelColor) - fresnelColor) * pow(1.0f - clamp(dot(lightDir, halfDir), 0.0, 1.0), 5); +} + vec3 evalSkyboxGlobalColor(mat4 invViewMat, float shadowAttenuation, float obscurance, vec3 position, vec3 normal, vec3 albedo, float metallic, vec3 emissive, float roughness) { <$prepareGlobalLight()$> - + color += (1 - metallic) * albedo * evalSphericalLight(getLightAmbientSphere(light), fragNormal).xyz * obscurance * getLightAmbientIntensity(light); + vec3 direction = -reflect(fragEyeDir, fragNormal); float levels = getLightAmbientMapNumMips(light); - float lod = min(1.0 + floor((roughness) * levels), levels); + float lod = min(floor((roughness) * levels), levels); vec4 skyboxLight = evalSkyboxLight(direction, lod); - color += albedo * skyboxLight.rgb * skyboxLight.a * obscurance * getLightAmbientIntensity(light); + vec3 ambientFresnel = fresnelSchlickAmbient(fresnel, fragEyeDir, fragNormal, 1 - roughness); + color += ambientFresnel * skyboxLight.rgb * obscurance * getLightAmbientIntensity(light); return color; } diff --git a/libraries/render-utils/src/DeferredLighting.slh b/libraries/render-utils/src/DeferredLighting.slh index ca3989256a..d02f9e3345 100755 --- a/libraries/render-utils/src/DeferredLighting.slh +++ b/libraries/render-utils/src/DeferredLighting.slh @@ -37,7 +37,7 @@ vec4 evalPBRShading(vec3 fragNormal, vec3 fragLightDir, vec3 fragEyeDir, float m float power = specularDistribution(roughness, fragNormal, halfDir); vec3 specular = power * fresnelColor * diffuse; - return vec4(specular, diffuse * (1 - fresnelColor.x)); + return vec4(specular, (1.0 - metallic) * diffuse * (1 - fresnelColor.x)); } <@endfunc@> diff --git a/libraries/render-utils/src/DeferredLightingEffect.cpp b/libraries/render-utils/src/DeferredLightingEffect.cpp index 3ffdb2d47b..a05c8c4cc7 100644 --- a/libraries/render-utils/src/DeferredLightingEffect.cpp +++ b/libraries/render-utils/src/DeferredLightingEffect.cpp @@ -320,18 +320,19 @@ void DeferredLightingEffect::render(const render::RenderContextPointer& renderCo // Setup the global directional pass pipeline { if (_shadowMapEnabled) { - /* if (_skyboxTexture) { + //if (_skyboxTexture) { + if (_skyboxTexture) { program = _directionalSkyboxLightShadow; locations = _directionalSkyboxLightShadowLocations; - } else*/ { + } else { program = _directionalAmbientSphereLightShadow; locations = _directionalAmbientSphereLightShadowLocations; } } else { - /* if (_skyboxTexture) { + if (_skyboxTexture) { program = _directionalSkyboxLight; locations = _directionalSkyboxLightLocations; - } else*/ { + } else { program = _directionalAmbientSphereLight; locations = _directionalAmbientSphereLightLocations; } @@ -501,8 +502,9 @@ void DeferredLightingEffect::setupKeyLightBatch(gpu::Batch& batch, int lightBuff batch.setUniformBuffer(lightBufferUnit, globalLight->getSchemaBuffer()); } - if (_skyboxTexture && (skyboxCubemapUnit >= 0)) { - batch.setResourceTexture(skyboxCubemapUnit, _skyboxTexture); + // if (_skyboxTexture && (skyboxCubemapUnit >= 0)) { + if (globalLight->getAmbientMap() && (skyboxCubemapUnit >= 0)) { + batch.setResourceTexture(skyboxCubemapUnit, globalLight->getAmbientMap()); } } @@ -569,10 +571,12 @@ void DeferredLightingEffect::setGlobalLight(const model::LightPointer& light, co globalLight->setAmbientIntensity(light->getAmbientIntensity()); globalLight->setAmbientSphere(light->getAmbientSphere()); - _skyboxTexture = skyboxTexture; + // _skyboxTexture = skyboxTexture; + _skyboxTexture = (light->getAmbientMap() ? light->getAmbientMap() : _skyboxTexture); // Update the available mipmap levels - globalLight->setAmbientMapNumMips((_skyboxTexture ? _skyboxTexture->evalNumMips() : 0)); + globalLight->setAmbientMap((light->getAmbientMap() ? light->getAmbientMap() : _skyboxTexture)); + // globalLight->setAmbientMapNumMips((_skyboxTexture ? _skyboxTexture->evalNumMips() : 0)); } model::MeshPointer DeferredLightingEffect::getSpotLightMesh() { diff --git a/libraries/render-utils/src/MaterialTextures.slh b/libraries/render-utils/src/MaterialTextures.slh index 5372419465..fb253e3f6d 100644 --- a/libraries/render-utils/src/MaterialTextures.slh +++ b/libraries/render-utils/src/MaterialTextures.slh @@ -23,7 +23,7 @@ vec4 fetchAlbedoMap(vec2 uv) { <@if withRoughness@> uniform sampler2D roughnessMap; float fetchRoughnessMap(vec2 uv) { - return pow(texture(roughnessMap, uv).r, 2.2); + return (texture(roughnessMap, uv).r); } <@endif@> @@ -37,7 +37,7 @@ vec3 fetchNormalMap(vec2 uv) { <@if withMetallic@> uniform sampler2D specularMap; float fetchMetallicMap(vec2 uv) { - return pow(texture(specularMap, uv).r, 2.2); + return (texture(specularMap, uv).r); } <@endif@> diff --git a/libraries/script-engine/src/SceneScriptingInterface.cpp b/libraries/script-engine/src/SceneScriptingInterface.cpp index 079cfff2c7..3883b948df 100644 --- a/libraries/script-engine/src/SceneScriptingInterface.cpp +++ b/libraries/script-engine/src/SceneScriptingInterface.cpp @@ -81,6 +81,11 @@ void SceneScripting::KeyLight::setAmbientSphere(const gpu::SHPointer& sphere) { _skyStage->setSunAmbientSphere(sphere); } +void SceneScripting::KeyLight::setAmbientMap(const gpu::TexturePointer& map) { + _skyStage->setSunAmbientMap(map); +} + + glm::vec3 SceneScripting::KeyLight::getDirection() const { return _skyStage->getSunDirection(); } diff --git a/libraries/script-engine/src/SceneScriptingInterface.h b/libraries/script-engine/src/SceneScriptingInterface.h index 12681b1887..e8ea2e0217 100644 --- a/libraries/script-engine/src/SceneScriptingInterface.h +++ b/libraries/script-engine/src/SceneScriptingInterface.h @@ -84,6 +84,7 @@ namespace SceneScripting { // AmbientTexture is unscriptable - it must be set through the zone entity void setAmbientSphere(const gpu::SHPointer& sphere); void resetAmbientSphere() { setAmbientSphere(nullptr); } + void setAmbientMap(const gpu::TexturePointer& map); protected: model::SunSkyStagePointer _skyStage; From 89920370dbd74f22355a1121c09ac73f8f16f0f2 Mon Sep 17 00:00:00 2001 From: samcake Date: Tue, 1 Mar 2016 10:53:08 -0800 Subject: [PATCH 21/24] A bunch of typo fixes after review --- examples/utilities/tools/render/debugFramebuffer.js | 4 ++-- libraries/model/src/model/Light.cpp | 2 +- libraries/model/src/model/Light.h | 6 +++--- libraries/model/src/model/Material.h | 2 +- libraries/render-utils/src/DeferredGlobalLight.slh | 2 +- libraries/render-utils/src/DeferredLightingEffect.cpp | 7 ++----- libraries/render-utils/src/MeshPartPayload.cpp | 1 - 7 files changed, 10 insertions(+), 14 deletions(-) diff --git a/examples/utilities/tools/render/debugFramebuffer.js b/examples/utilities/tools/render/debugFramebuffer.js index 4aad8f9488..e764cf52d8 100644 --- a/examples/utilities/tools/render/debugFramebuffer.js +++ b/examples/utilities/tools/render/debugFramebuffer.js @@ -1,5 +1,5 @@ // -// debug.js +// ddebugFramBuffer.js // examples/utilities/tools/render // // Sam Gateau created on 2/18/2016. @@ -10,8 +10,8 @@ // var DDB = Render.RenderDeferredTask.DebugDeferredBuffer; -DDB.enabled = true; oldConfig = DDB.toJSON(); +DDB.enabled = true; // Set up the qml ui diff --git a/libraries/model/src/model/Light.cpp b/libraries/model/src/model/Light.cpp index 1497ceba86..1762b0a9fb 100755 --- a/libraries/model/src/model/Light.cpp +++ b/libraries/model/src/model/Light.cpp @@ -150,6 +150,6 @@ void Light::setAmbientMap(gpu::TexturePointer ambientMap) { } } -void Light::setAmbientMapNumMips(int numMips) { +void Light::setAmbientMapNumMips(uint16_t numMips) { editSchema()._ambientMapNumMips = (float)numMips; } diff --git a/libraries/model/src/model/Light.h b/libraries/model/src/model/Light.h index 1db73f4093..3b320e08fd 100755 --- a/libraries/model/src/model/Light.h +++ b/libraries/model/src/model/Light.h @@ -111,8 +111,8 @@ public: void setAmbientMap(gpu::TexturePointer ambientMap); gpu::TexturePointer getAmbientMap() const { return _ambientMap; } - void setAmbientMapNumMips(int numMips); - int getAmbientMapNumMips() const { return getSchema()._ambientMapNumMips; } + void setAmbientMapNumMips(uint16_t numMips); + uint16_t getAmbientMapNumMips() const { return (uint16_t) getSchema()._ambientMapNumMips; } // Schema to access the attribute values of the light class Schema { @@ -126,7 +126,7 @@ public: Vec4 _spot{0.0f, 0.0f, 0.0f, 0.0f}; Vec4 _shadow{0.0f}; - float _ambientMapNumMips{ 0 }; + float _ambientMapNumMips{ 0.0f }; Vec3 _control{ 0.0f, 0.0f, 0.0f }; gpu::SphericalHarmonics _ambientSphere; diff --git a/libraries/model/src/model/Material.h b/libraries/model/src/model/Material.h index 81a55eea8a..b22f8a3f42 100755 --- a/libraries/model/src/model/Material.h +++ b/libraries/model/src/model/Material.h @@ -252,7 +252,7 @@ public: class Schema { public: glm::vec3 _emissive{ 0.0f }; // No Emissive - float _opacity{ 1.f }; // Opacity = 1 => Not Transparent + float _opacity{ 1.0f }; // Opacity = 1 => Not Transparent glm::vec3 _albedo{ 0.5f }; // Grey albedo => isAlbedo float _roughness{ 1.0f }; // Roughness = 1 => Not Glossy diff --git a/libraries/render-utils/src/DeferredGlobalLight.slh b/libraries/render-utils/src/DeferredGlobalLight.slh index 0f92f8df2c..341b9da9e8 100755 --- a/libraries/render-utils/src/DeferredGlobalLight.slh +++ b/libraries/render-utils/src/DeferredGlobalLight.slh @@ -65,7 +65,7 @@ vec3 evalAmbientSphereGlobalColor(mat4 invViewMat, float shadowAttenuation, floa <$declareSkyboxMap()$> vec3 fresnelSchlickAmbient(vec3 fresnelColor, vec3 lightDir, vec3 halfDir, float gloss) { - return fresnelColor + (max(vec3(gloss), fresnelColor) - fresnelColor) * pow(1.0f - clamp(dot(lightDir, halfDir), 0.0, 1.0), 5); + return fresnelColor + (max(vec3(gloss), fresnelColor) - fresnelColor) * pow(1.0 - clamp(dot(lightDir, halfDir), 0.0, 1.0), 5); } vec3 evalSkyboxGlobalColor(mat4 invViewMat, float shadowAttenuation, float obscurance, vec3 position, vec3 normal, vec3 albedo, float metallic, vec3 emissive, float roughness) { diff --git a/libraries/render-utils/src/DeferredLightingEffect.cpp b/libraries/render-utils/src/DeferredLightingEffect.cpp index a05c8c4cc7..faa11584dd 100644 --- a/libraries/render-utils/src/DeferredLightingEffect.cpp +++ b/libraries/render-utils/src/DeferredLightingEffect.cpp @@ -92,7 +92,7 @@ void DeferredLightingEffect::init() { // Add the global light to the light stage (for later shadow rendering) _lightStage.addLight(lp); - lp->setDirection(-glm::vec3(1.0f, 1.0f, 1.0f)); + lp->setDirection(glm::vec3(-1.0f)); lp->setColor(glm::vec3(1.0f)); lp->setIntensity(1.0f); lp->setType(model::Light::SUN); @@ -502,7 +502,6 @@ void DeferredLightingEffect::setupKeyLightBatch(gpu::Batch& batch, int lightBuff batch.setUniformBuffer(lightBufferUnit, globalLight->getSchemaBuffer()); } - // if (_skyboxTexture && (skyboxCubemapUnit >= 0)) { if (globalLight->getAmbientMap() && (skyboxCubemapUnit >= 0)) { batch.setResourceTexture(skyboxCubemapUnit, globalLight->getAmbientMap()); } @@ -571,12 +570,10 @@ void DeferredLightingEffect::setGlobalLight(const model::LightPointer& light, co globalLight->setAmbientIntensity(light->getAmbientIntensity()); globalLight->setAmbientSphere(light->getAmbientSphere()); - // _skyboxTexture = skyboxTexture; _skyboxTexture = (light->getAmbientMap() ? light->getAmbientMap() : _skyboxTexture); // Update the available mipmap levels - globalLight->setAmbientMap((light->getAmbientMap() ? light->getAmbientMap() : _skyboxTexture)); - // globalLight->setAmbientMapNumMips((_skyboxTexture ? _skyboxTexture->evalNumMips() : 0)); + globalLight->setAmbientMap(_skyboxTexture); } model::MeshPointer DeferredLightingEffect::getSpotLightMesh() { diff --git a/libraries/render-utils/src/MeshPartPayload.cpp b/libraries/render-utils/src/MeshPartPayload.cpp index bcddc72fa4..166b14333f 100644 --- a/libraries/render-utils/src/MeshPartPayload.cpp +++ b/libraries/render-utils/src/MeshPartPayload.cpp @@ -414,7 +414,6 @@ ShapeKey ModelMeshPartPayload::getShapeKey() const { bool isTranslucent = drawMaterialKey.isTransparent() || drawMaterialKey.isTransparentMap(); bool hasTangents = drawMaterialKey.isNormalMap() && !mesh.tangents.isEmpty(); - //bool hasSpecular = drawMaterialKey.isGlossMap(); bool hasSpecular = drawMaterialKey.isMetallicMap(); bool hasLightmap = drawMaterialKey.isLightmapMap(); From 9fb318570bcb0bcfd2e8392ad1a7c0f649774fd3 Mon Sep 17 00:00:00 2001 From: samcake Date: Tue, 1 Mar 2016 12:24:44 -0800 Subject: [PATCH 22/24] Ajusting the pure Ambient SH lighting pass and more review issues --- libraries/fbx/src/FBXReader.cpp | 6 ++-- .../render-utils/src/DeferredGlobalLight.slh | 29 +++++++++++++++---- .../render-utils/src/DeferredLighting.slh | 2 +- .../render-utils/src/MaterialTextures.slh | 4 +-- libraries/render/src/render/ShapePipeline.cpp | 4 +-- libraries/render/src/render/ShapePipeline.h | 2 +- 6 files changed, 32 insertions(+), 15 deletions(-) diff --git a/libraries/fbx/src/FBXReader.cpp b/libraries/fbx/src/FBXReader.cpp index 1778e06894..500f856450 100644 --- a/libraries/fbx/src/FBXReader.cpp +++ b/libraries/fbx/src/FBXReader.cpp @@ -882,7 +882,7 @@ FBXGeometry* FBXReader::extractFBXGeometry(const QVariantHash& mapping, const QS propertyName = "P"; index = 4; } - if (properties && !material.isPBSMaterial) { + if (!material.isPBSMaterial && properties) { foreach (const FBXNode& property, subobject.children) { if (property.name == propertyName) { if (property.properties.at(0) == "DiffuseColor") { @@ -917,7 +917,7 @@ FBXGeometry* FBXReader::extractFBXGeometry(const QVariantHash& mapping, const QS #endif } } - } else if (properties && material.isPBSMaterial) { + } else if (material.isPBSMaterial && properties) { std::vector unknowns; foreach(const FBXNode& property, subobject.children) { if (property.name == propertyName) { @@ -1095,7 +1095,7 @@ FBXGeometry* FBXReader::extractFBXGeometry(const QVariantHash& mapping, const QS } else if (type.contains("emissive")) { emissiveTextures.insert(getID(connection.properties, 2), getID(connection.properties, 1)); } else if (type.contains("tex_emissive_map")) { - roughnessTextures.insert(getID(connection.properties, 2), getID(connection.properties, 1)); + emissiveTextures.insert(getID(connection.properties, 2), getID(connection.properties, 1)); } else if (type.contains("ambient")) { ambientTextures.insert(getID(connection.properties, 2), getID(connection.properties, 1)); } else if (type.contains("tex_ao_map")) { diff --git a/libraries/render-utils/src/DeferredGlobalLight.slh b/libraries/render-utils/src/DeferredGlobalLight.slh index 341b9da9e8..71bc2dc6d0 100755 --- a/libraries/render-utils/src/DeferredGlobalLight.slh +++ b/libraries/render-utils/src/DeferredGlobalLight.slh @@ -45,6 +45,12 @@ vec4 evalSkyboxLight(vec3 direction, float lod) { color += emissive; <@endfunc@> +<@func declareAmbientFresnel()@> +vec3 fresnelSchlickAmbient(vec3 fresnelColor, vec3 lightDir, vec3 halfDir, float gloss) { + return fresnelColor + (max(vec3(gloss), fresnelColor) - fresnelColor) * pow(1.0 - clamp(dot(lightDir, halfDir), 0.0, 1.0), 5); +} +<@endfunc@> + <@func declareEvalAmbientGlobalColor()@> vec3 evalAmbientGlobalColor(mat4 invViewMat, float shadowAttenuation, float obscurance, vec3 position, vec3 normal, vec3 albedo, float metallic, vec3 emissive, float roughness) { <$prepareGlobalLight()$> @@ -54,24 +60,35 @@ vec3 evalAmbientGlobalColor(mat4 invViewMat, float shadowAttenuation, float obsc <@endfunc@> <@func declareEvalAmbientSphereGlobalColor()@> +<$declareAmbientFresnel()$> + vec3 evalAmbientSphereGlobalColor(mat4 invViewMat, float shadowAttenuation, float obscurance, vec3 position, vec3 normal, vec3 albedo, float metallic, vec3 emissive, float roughness) { <$prepareGlobalLight()$> - color += (1 - (metallic * 0.5)) * albedo * evalSphericalLight(getLightAmbientSphere(light), fragNormal).xyz * obscurance * getLightAmbientIntensity(light); + + // Diffuse from ambient + color += (1 - metallic) * albedo * evalSphericalLight(getLightAmbientSphere(light), fragNormal).xyz * obscurance * getLightAmbientIntensity(light); + + // Specular highlight from ambient + vec3 direction = -reflect(fragEyeDir, fragNormal); + vec3 skyboxLight = evalSphericalLight(getLightAmbientSphere(light), direction).xyz; + vec3 ambientFresnel = fresnelSchlickAmbient(fresnel, fragEyeDir, fragNormal, 1 - roughness); + color += ambientFresnel * skyboxLight.rgb * obscurance * getLightAmbientIntensity(light); + return color; } <@endfunc@> <@func declareEvalSkyboxGlobalColor()@> <$declareSkyboxMap()$> - -vec3 fresnelSchlickAmbient(vec3 fresnelColor, vec3 lightDir, vec3 halfDir, float gloss) { - return fresnelColor + (max(vec3(gloss), fresnelColor) - fresnelColor) * pow(1.0 - clamp(dot(lightDir, halfDir), 0.0, 1.0), 5); -} +<$declareAmbientFresnel()$> vec3 evalSkyboxGlobalColor(mat4 invViewMat, float shadowAttenuation, float obscurance, vec3 position, vec3 normal, vec3 albedo, float metallic, vec3 emissive, float roughness) { <$prepareGlobalLight()$> + + // Diffuse from ambient color += (1 - metallic) * albedo * evalSphericalLight(getLightAmbientSphere(light), fragNormal).xyz * obscurance * getLightAmbientIntensity(light); - + + // Specular highlight from ambient vec3 direction = -reflect(fragEyeDir, fragNormal); float levels = getLightAmbientMapNumMips(light); float lod = min(floor((roughness) * levels), levels); diff --git a/libraries/render-utils/src/DeferredLighting.slh b/libraries/render-utils/src/DeferredLighting.slh index d02f9e3345..b33371b10d 100755 --- a/libraries/render-utils/src/DeferredLighting.slh +++ b/libraries/render-utils/src/DeferredLighting.slh @@ -15,7 +15,7 @@ <@func declareEvalPBRShading()@> vec3 fresnelSchlick(vec3 fresnelColor, vec3 lightDir, vec3 halfDir) { - return fresnelColor + (1.0f - fresnelColor) * pow(1.0f - clamp(dot(lightDir, halfDir), 0.0, 1.0), 5); + return fresnelColor + (1.0 - fresnelColor) * pow(1.0 - clamp(dot(lightDir, halfDir), 0.0, 1.0), 5); } float specularDistribution(float roughness, vec3 normal, vec3 halfDir) { diff --git a/libraries/render-utils/src/MaterialTextures.slh b/libraries/render-utils/src/MaterialTextures.slh index fb253e3f6d..8904ae34b2 100644 --- a/libraries/render-utils/src/MaterialTextures.slh +++ b/libraries/render-utils/src/MaterialTextures.slh @@ -35,9 +35,9 @@ vec3 fetchNormalMap(vec2 uv) { <@endif@> <@if withMetallic@> -uniform sampler2D specularMap; +uniform sampler2D metallicMap; float fetchMetallicMap(vec2 uv) { - return (texture(specularMap, uv).r); + return (texture(metallicMap, uv).r); } <@endif@> diff --git a/libraries/render/src/render/ShapePipeline.cpp b/libraries/render/src/render/ShapePipeline.cpp index f20b5144d3..6974a7e385 100644 --- a/libraries/render/src/render/ShapePipeline.cpp +++ b/libraries/render/src/render/ShapePipeline.cpp @@ -56,7 +56,7 @@ void ShapePlumber::addPipeline(const Filter& filter, const gpu::ShaderPointer& p 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::METALLIC_MAP)); + slotBindings.insert(gpu::Shader::Binding(std::string("metallicMap"), Slot::METALLIC_MAP)); slotBindings.insert(gpu::Shader::Binding(std::string("emissiveMap"), Slot::EMISSIVE_LIGHTMAP_MAP)); slotBindings.insert(gpu::Shader::Binding(std::string("occlusionMap"), Slot::OCCLUSION_MAP)); slotBindings.insert(gpu::Shader::Binding(std::string("lightBuffer"), Slot::LIGHT_BUFFER)); @@ -71,7 +71,7 @@ void ShapePlumber::addPipeline(const Filter& filter, const gpu::ShaderPointer& p 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->metallicTextureUnit = program->getTextures().findLocation("metallicMap"); locations->emissiveTextureUnit = program->getTextures().findLocation("emissiveMap"); locations->occlusionTextureUnit = program->getTextures().findLocation("occlusionMap"); locations->skinClusterBufferUnit = program->getBuffers().findLocation("skinClusterBuffer"); diff --git a/libraries/render/src/render/ShapePipeline.h b/libraries/render/src/render/ShapePipeline.h index 963a3c76d4..0f795aadde 100644 --- a/libraries/render/src/render/ShapePipeline.h +++ b/libraries/render/src/render/ShapePipeline.h @@ -212,7 +212,7 @@ public: int albedoTextureUnit; int normalTextureUnit; int roughnessTextureUnit; - int specularTextureUnit; + int metallicTextureUnit; int emissiveTextureUnit; int occlusionTextureUnit; int emissiveParams; From 5c64115b2642f34041e3224688c805cc8046ccda Mon Sep 17 00:00:00 2001 From: Zach Pomerantz Date: Tue, 1 Mar 2016 14:44:34 -0800 Subject: [PATCH 23/24] Use keyLight in DLE instead of _skyboxTexture --- interface/src/Application.cpp | 2 +- .../src/EntityTreeRenderer.cpp | 2 ++ .../src/DeferredLightingEffect.cpp | 24 ++++++++----------- .../render-utils/src/DeferredLightingEffect.h | 4 +--- 4 files changed, 14 insertions(+), 18 deletions(-) diff --git a/interface/src/Application.cpp b/interface/src/Application.cpp index 21377fa945..82e44fa907 100644 --- a/interface/src/Application.cpp +++ b/interface/src/Application.cpp @@ -3818,7 +3818,7 @@ void Application::displaySide(RenderArgs* renderArgs, Camera& theCamera, bool se // Setup the current Zone Entity lighting { auto stage = DependencyManager::get()->getSkyStage(); - DependencyManager::get()->setGlobalLight(stage->getSunLight(), stage->getSkybox()->getCubemap()); + DependencyManager::get()->setGlobalLight(stage->getSunLight()); } { diff --git a/libraries/entities-renderer/src/EntityTreeRenderer.cpp b/libraries/entities-renderer/src/EntityTreeRenderer.cpp index ccf5036fa1..85cbbbd328 100644 --- a/libraries/entities-renderer/src/EntityTreeRenderer.cpp +++ b/libraries/entities-renderer/src/EntityTreeRenderer.cpp @@ -273,6 +273,7 @@ void EntityTreeRenderer::applyZonePropertiesToScene(std::shared_ptrresetAmbientSphere(); + sceneKeyLight->setAmbientMap(nullptr); sceneKeyLight->setColor(_previousKeyLightColor); sceneKeyLight->setIntensity(_previousKeyLightIntensity); sceneKeyLight->setAmbientIntensity(_previousKeyLightAmbientIntensity); @@ -383,6 +384,7 @@ void EntityTreeRenderer::applyZonePropertiesToScene(std::shared_ptrresetAmbientSphere(); + sceneKeyLight->setAmbientMap(nullptr); } } diff --git a/libraries/render-utils/src/DeferredLightingEffect.cpp b/libraries/render-utils/src/DeferredLightingEffect.cpp index faa11584dd..82bd0e73d0 100644 --- a/libraries/render-utils/src/DeferredLightingEffect.cpp +++ b/libraries/render-utils/src/DeferredLightingEffect.cpp @@ -316,12 +316,12 @@ void DeferredLightingEffect::render(const render::RenderContextPointer& renderCo { auto& program = _shadowMapEnabled ? _directionalLightShadow : _directionalLight; LightLocationsPtr locations = _shadowMapEnabled ? _directionalLightShadowLocations : _directionalLightLocations; + const auto& keyLight = _allocatedLights[_globalLights.front()]; // Setup the global directional pass pipeline { if (_shadowMapEnabled) { - //if (_skyboxTexture) { - if (_skyboxTexture) { + if (keyLight->getAmbientMap()) { program = _directionalSkyboxLightShadow; locations = _directionalSkyboxLightShadowLocations; } else { @@ -329,7 +329,7 @@ void DeferredLightingEffect::render(const render::RenderContextPointer& renderCo locations = _directionalAmbientSphereLightShadowLocations; } } else { - if (_skyboxTexture) { + if (keyLight->getAmbientMap()) { program = _directionalSkyboxLight; locations = _directionalSkyboxLightLocations; } else { @@ -357,7 +357,7 @@ void DeferredLightingEffect::render(const render::RenderContextPointer& renderCo geometryCache->renderQuad(batch, topLeft, bottomRight, texCoordTopLeft, texCoordBottomRight, color); } - if (_skyboxTexture) { + if (keyLight->getAmbientMap()) { batch.setResourceTexture(SKYBOX_MAP_UNIT, nullptr); } } @@ -496,14 +496,14 @@ void DeferredLightingEffect::render(const render::RenderContextPointer& renderCo void DeferredLightingEffect::setupKeyLightBatch(gpu::Batch& batch, int lightBufferUnit, int skyboxCubemapUnit) { PerformanceTimer perfTimer("DLE->setupBatch()"); - auto globalLight = _allocatedLights[_globalLights.front()]; + auto keyLight = _allocatedLights[_globalLights.front()]; if (lightBufferUnit >= 0) { - batch.setUniformBuffer(lightBufferUnit, globalLight->getSchemaBuffer()); + batch.setUniformBuffer(lightBufferUnit, keyLight->getSchemaBuffer()); } - if (globalLight->getAmbientMap() && (skyboxCubemapUnit >= 0)) { - batch.setResourceTexture(skyboxCubemapUnit, globalLight->getAmbientMap()); + if (keyLight->getAmbientMap() && (skyboxCubemapUnit >= 0)) { + batch.setResourceTexture(skyboxCubemapUnit, keyLight->getAmbientMap()); } } @@ -562,18 +562,14 @@ static void loadLightProgram(const char* vertSource, const char* fragSource, boo } -void DeferredLightingEffect::setGlobalLight(const model::LightPointer& light, const gpu::TexturePointer& skyboxTexture) { +void DeferredLightingEffect::setGlobalLight(const model::LightPointer& light) { auto globalLight = _allocatedLights.front(); globalLight->setDirection(light->getDirection()); globalLight->setColor(light->getColor()); globalLight->setIntensity(light->getIntensity()); globalLight->setAmbientIntensity(light->getAmbientIntensity()); globalLight->setAmbientSphere(light->getAmbientSphere()); - - _skyboxTexture = (light->getAmbientMap() ? light->getAmbientMap() : _skyboxTexture); - - // Update the available mipmap levels - globalLight->setAmbientMap(_skyboxTexture); + globalLight->setAmbientMap(light->getAmbientMap()); } model::MeshPointer DeferredLightingEffect::getSpotLightMesh() { diff --git a/libraries/render-utils/src/DeferredLightingEffect.h b/libraries/render-utils/src/DeferredLightingEffect.h index 4ee3d9a565..63d8f4d175 100644 --- a/libraries/render-utils/src/DeferredLightingEffect.h +++ b/libraries/render-utils/src/DeferredLightingEffect.h @@ -49,7 +49,7 @@ public: void setupKeyLightBatch(gpu::Batch& batch, int lightBufferUnit, int skyboxCubemapUnit); // update global lighting - void setGlobalLight(const model::LightPointer& light, const gpu::TexturePointer& skyboxTexture); + void setGlobalLight(const model::LightPointer& light); const LightStage& getLightStage() { return _lightStage; } void setShadowMapEnabled(bool enable) { _shadowMapEnabled = enable; }; @@ -96,8 +96,6 @@ private: std::vector _pointLights; std::vector _spotLights; - gpu::TexturePointer _skyboxTexture; - // Class describing the uniform buffer with all the parameters common to the deferred shaders class DeferredTransform { public: From aed2ad72d13556b7ecffcbcbd43260e8f7581f23 Mon Sep 17 00:00:00 2001 From: samcake Date: Tue, 1 Mar 2016 17:09:57 -0800 Subject: [PATCH 24/24] Using another formula for the specularDistribution giving much better results --- libraries/render-utils/src/DeferredLighting.slh | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/libraries/render-utils/src/DeferredLighting.slh b/libraries/render-utils/src/DeferredLighting.slh index 95c77ef2f0..2e06deb0af 100755 --- a/libraries/render-utils/src/DeferredLighting.slh +++ b/libraries/render-utils/src/DeferredLighting.slh @@ -20,19 +20,19 @@ vec3 fresnelSchlick(vec3 fresnelColor, vec3 lightDir, vec3 halfDir) { float specularDistribution(float roughness, vec3 normal, vec3 halfDir) { float ndoth = clamp(dot(halfDir, normal), 0.0, 1.0); - float gloss2 = pow(roughness, 4); + float gloss2 = pow(0.001 + roughness, 4); float denom = (ndoth * ndoth*(gloss2 - 1) + 1); float power = gloss2 / (3.14159 * denom * denom); return power; } -/* + // 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