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 {