From cce99ef8c45576db84cb71efe154fe91c9ad3409 Mon Sep 17 00:00:00 2001 From: Olivier Prat Date: Mon, 5 Jun 2017 16:56:57 +0200 Subject: [PATCH] Added translucent shaders --- .../render-utils/src/RenderPipelines.cpp | 47 ++++++++++ .../src/model_translucent_fade.slf | 87 +++++++++++++++++++ .../src/model_translucent_unlit_fade.slf | 46 ++++++++++ .../src/simple_transparent_textured_fade.slf | 68 +++++++++++++++ ...simple_transparent_textured_unlit_fade.slf | 41 +++++++++ 5 files changed, 289 insertions(+) create mode 100644 libraries/render-utils/src/model_translucent_fade.slf create mode 100644 libraries/render-utils/src/model_translucent_unlit_fade.slf create mode 100644 libraries/render-utils/src/simple_transparent_textured_fade.slf create mode 100644 libraries/render-utils/src/simple_transparent_textured_unlit_fade.slf diff --git a/libraries/render-utils/src/RenderPipelines.cpp b/libraries/render-utils/src/RenderPipelines.cpp index 3a9464395a..2cdad86156 100644 --- a/libraries/render-utils/src/RenderPipelines.cpp +++ b/libraries/render-utils/src/RenderPipelines.cpp @@ -44,6 +44,8 @@ #include "simple_fade_vert.h" #include "simple_textured_fade_frag.h" #include "simple_textured_unlit_fade_frag.h" +#include "simple_transparent_textured_fade_frag.h" +#include "simple_transparent_textured_unlit_fade_frag.h" #include "model_frag.h" #include "model_unlit_frag.h" @@ -79,6 +81,8 @@ #include "model_lightmap_normal_map_fade_frag.h" #include "model_lightmap_normal_specular_map_fade_frag.h" #include "model_lightmap_specular_map_fade_frag.h" +#include "model_translucent_fade_frag.h" +#include "model_translucent_unlit_fade_frag.h" #include "overlay3D_vert.h" #include "overlay3D_frag.h" @@ -216,8 +220,12 @@ void initDeferredPipelines(render::ShapePlumber& plumber) { auto modelSpecularMapFadePixel = gpu::Shader::createPixel(std::string(model_specular_map_fade_frag)); auto modelNormalSpecularMapFadePixel = gpu::Shader::createPixel(std::string(model_normal_specular_map_fade_frag)); auto modelShadowFadePixel = gpu::Shader::createPixel(std::string(model_shadow_fade_frag)); + auto modelTranslucentFadePixel = gpu::Shader::createPixel(std::string(model_translucent_fade_frag)); + auto modelTranslucentUnlitFadePixel = gpu::Shader::createPixel(std::string(model_translucent_unlit_fade_frag)); auto simpleFadePixel = gpu::Shader::createPixel(std::string(simple_textured_fade_frag)); auto simpleUnlitFadePixel = gpu::Shader::createPixel(std::string(simple_textured_unlit_fade_frag)); + auto simpleTranslucentFadePixel = gpu::Shader::createPixel(std::string(simple_transparent_textured_fade_frag)); + auto simpleTranslucentUnlitFadePixel = gpu::Shader::createPixel(std::string(simple_transparent_textured_unlit_fade_frag)); using Key = render::ShapeKey; auto addPipeline = std::bind(&addPlumberPipeline, std::ref(plumber), _1, _2, _3); @@ -293,6 +301,32 @@ void initDeferredPipelines(render::ShapePlumber& plumber) { // FIXME: Ignore lightmap for translucents meshpart Key::Builder().withMaterial().withTranslucent().withLightmap(), modelVertex, modelTranslucentPixel); + // Same thing but with Fade on + addPipeline( + Key::Builder().withMaterial().withTranslucent().withFade(), + modelFadeVertex, modelTranslucentFadePixel); + addPipeline( + Key::Builder().withTranslucent().withFade(), + simpleFadeVertex, simpleTranslucentFadePixel); + addPipeline( + Key::Builder().withMaterial().withTranslucent().withUnlit().withFade(), + modelFadeVertex, modelTranslucentUnlitFadePixel); + addPipeline( + Key::Builder().withTranslucent().withUnlit().withFade(), + simpleFadeVertex, simpleTranslucentUnlitFadePixel); + addPipeline( + Key::Builder().withMaterial().withTranslucent().withTangents().withFade(), + modelNormalMapFadeVertex, modelTranslucentFadePixel); + addPipeline( + Key::Builder().withMaterial().withTranslucent().withSpecular().withFade(), + modelFadeVertex, modelTranslucentFadePixel); + addPipeline( + Key::Builder().withMaterial().withTranslucent().withTangents().withSpecular().withFade(), + modelNormalMapFadeVertex, modelTranslucentFadePixel); + addPipeline( + // FIXME: Ignore lightmap for translucents meshpart + Key::Builder().withMaterial().withTranslucent().withLightmap().withFade(), + modelFadeVertex, modelTranslucentFadePixel); // Lightmapped addPipeline( @@ -361,6 +395,19 @@ void initDeferredPipelines(render::ShapePlumber& plumber) { addPipeline( Key::Builder().withMaterial().withSkinned().withTranslucent().withTangents().withSpecular(), skinModelNormalMapVertex, modelTranslucentPixel); + // Same thing but with Fade on + addPipeline( + Key::Builder().withMaterial().withSkinned().withTranslucent().withFade(), + skinModelFadeVertex, modelTranslucentFadePixel); + addPipeline( + Key::Builder().withMaterial().withSkinned().withTranslucent().withTangents().withFade(), + skinModelNormalMapFadeVertex, modelTranslucentFadePixel); + addPipeline( + Key::Builder().withMaterial().withSkinned().withTranslucent().withSpecular().withFade(), + skinModelFadeVertex, modelTranslucentFadePixel); + addPipeline( + Key::Builder().withMaterial().withSkinned().withTranslucent().withTangents().withSpecular().withFade(), + skinModelNormalMapFadeVertex, modelTranslucentFadePixel); // Depth-only addPipeline( diff --git a/libraries/render-utils/src/model_translucent_fade.slf b/libraries/render-utils/src/model_translucent_fade.slf new file mode 100644 index 0000000000..3bbfdabc7f --- /dev/null +++ b/libraries/render-utils/src/model_translucent_fade.slf @@ -0,0 +1,87 @@ +<@include gpu/Config.slh@> +<$VERSION_HEADER$> +// Generated on <$_SCRIBE_DATE$> +// +// model_translucent_fade.frag +// fragment shader +// +// Created by Olivier Prat on 06/05/17. +// Copyright 2017 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 +// + +<@include model/Material.slh@> + +<@include DeferredGlobalLight.slh@> + +<$declareEvalGlobalLightingAlphaBlended()$> + +<@include gpu/Transform.slh@> +<$declareStandardCameraTransform()$> + +<@include MaterialTextures.slh@> +<$declareMaterialTextures(ALBEDO, ROUGHNESS, _SCRIBE_NULL, _SCRIBE_NULL, EMISSIVE, OCCLUSION)$> + +<@include Fade.slh@> +<$declareFadeFragment()$> + +in vec2 _texCoord0; +in vec2 _texCoord1; +in vec4 _position; +in vec3 _normal; +in vec3 _color; +in float _alpha; +in vec4 _worldFadePosition; + +out vec4 _fragColor; + +void main(void) { + applyFade(_worldFadePosition.xyz); + + Material mat = getMaterial(); + int matKey = getMaterialKey(mat); + <$fetchMaterialTexturesCoord0(matKey, _texCoord0, albedoTex, roughnessTex, _SCRIBE_NULL, _SCRIBE_NULL, emissiveTex)$> + <$fetchMaterialTexturesCoord1(matKey, _texCoord1, occlusionTex)$> + + float opacity = getMaterialOpacity(mat) * _alpha; + <$evalMaterialOpacity(albedoTex.a, opacity, matKey, opacity)$>; + + vec3 albedo = getMaterialAlbedo(mat); + <$evalMaterialAlbedo(albedoTex, albedo, matKey, albedo)$>; + albedo *= _color; + + float roughness = getMaterialRoughness(mat); + <$evalMaterialRoughness(roughnessTex, roughness, matKey, roughness)$>; + + float metallic = getMaterialMetallic(mat); + vec3 fresnel = vec3(0.03); // Default Di-electric fresnel value + if (metallic <= 0.5) { + metallic = 0.0; + } else { + fresnel = albedo; + metallic = 1.0; + } + + vec3 emissive = getMaterialEmissive(mat); + <$evalMaterialEmissive(emissiveTex, emissive, matKey, emissive)$>; + + vec3 fragPosition = _position.xyz; + vec3 fragNormal = normalize(_normal); + + TransformCamera cam = getTransformCamera(); + + _fragColor = vec4(evalGlobalLightingAlphaBlended( + cam._viewInverse, + 1.0, + occlusionTex, + fragPosition, + fragNormal, + albedo, + fresnel, + metallic, + emissive, + roughness, opacity), + opacity); +} diff --git a/libraries/render-utils/src/model_translucent_unlit_fade.slf b/libraries/render-utils/src/model_translucent_unlit_fade.slf new file mode 100644 index 0000000000..dd885f66d7 --- /dev/null +++ b/libraries/render-utils/src/model_translucent_unlit_fade.slf @@ -0,0 +1,46 @@ +<@include gpu/Config.slh@> +<$VERSION_HEADER$> +// Generated on <$_SCRIBE_DATE$> +// +// model_translucent_unlit_fade.frag +// fragment shader +// +// Created by Olivier Prat on 06/05/17. +// Copyright 2017 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 +// + +<@include model/Material.slh@> + +<@include MaterialTextures.slh@> +<$declareMaterialTextures(ALBEDO, ROUGHNESS, _SCRIBE_NULL, _SCRIBE_NULL, EMISSIVE, OCCLUSION)$> +<@include LightingModel.slh@> + +<@include Fade.slh@> +<$declareFadeFragment()$> + +in vec2 _texCoord0; +in vec3 _color; +in float _alpha; +in vec4 _worldFadePosition; + +out vec4 _fragColor; + +void main(void) { + applyFade(_worldFadePosition.xyz); + + Material mat = getMaterial(); + int matKey = getMaterialKey(mat); + <$fetchMaterialTexturesCoord0(matKey, _texCoord0, albedoTex)$> + + float opacity = getMaterialOpacity(mat) * _alpha; + <$evalMaterialOpacity(albedoTex.a, opacity, matKey, opacity)$>; + + vec3 albedo = getMaterialAlbedo(mat); + <$evalMaterialAlbedo(albedoTex, albedo, matKey, albedo)$>; + albedo *= _color; + + _fragColor = vec4(albedo * isUnlitEnabled(), opacity); +} diff --git a/libraries/render-utils/src/simple_transparent_textured_fade.slf b/libraries/render-utils/src/simple_transparent_textured_fade.slf new file mode 100644 index 0000000000..84cdffeeec --- /dev/null +++ b/libraries/render-utils/src/simple_transparent_textured_fade.slf @@ -0,0 +1,68 @@ +<@include gpu/Config.slh@> +<$VERSION_HEADER$> +// Generated on <$_SCRIBE_DATE$> +// +// simple_transparent_textured_fade.slf +// fragment shader +// +// Created by Olivier Prat on 06/05/17. +// Copyright 2017 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 +// + +<@include gpu/Color.slh@> + +<@include DeferredBufferWrite.slh@> +<@include DeferredGlobalLight.slh@> +<$declareEvalGlobalLightingAlphaBlended()$> + +<@include gpu/Transform.slh@> +<$declareStandardCameraTransform()$> + +<@include Fade.slh@> +<$declareFadeFragment()$> + +// the albedo texture +uniform sampler2D originalTexture; + +// the interpolated normal +in vec4 _position; +in vec3 _normal; +in vec4 _color; +in vec2 _texCoord0; +in vec4 _worldFadePosition; + +void main(void) { + applyFade(_worldFadePosition.xyz); + + vec4 texel = texture(originalTexture, _texCoord0.st); + float opacity = _color.a; + if (_color.a <= 0.0) { + texel = colorToLinearRGBA(texel); + opacity = -_color.a; + } + opacity *= texel.a; + vec3 albedo = _color.rgb * texel.rgb; + + vec3 fragPosition = _position.xyz; + vec3 fragNormal = normalize(_normal); + + TransformCamera cam = getTransformCamera(); + + _fragColor0 = vec4(evalGlobalLightingAlphaBlended( + cam._viewInverse, + 1.0, + 1.0, + fragPosition, + fragNormal, + albedo, + DEFAULT_FRESNEL, + 0.0, + vec3(0.0f), + DEFAULT_ROUGHNESS, + opacity), + opacity); + +} \ No newline at end of file diff --git a/libraries/render-utils/src/simple_transparent_textured_unlit_fade.slf b/libraries/render-utils/src/simple_transparent_textured_unlit_fade.slf new file mode 100644 index 0000000000..688e1de422 --- /dev/null +++ b/libraries/render-utils/src/simple_transparent_textured_unlit_fade.slf @@ -0,0 +1,41 @@ +<@include gpu/Config.slh@> +<$VERSION_HEADER$> +// Generated on <$_SCRIBE_DATE$> +// +// simple_transparent_textured_unlit_fade.slf +// fragment shader +// +// Created by Olivier Prat on 06/05/17. +// Copyright 2017 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 +// + +<@include gpu/Color.slh@> + +<@include Fade.slh@> +<$declareFadeFragment()$> + +// the albedo texture +uniform sampler2D originalTexture; + +// the interpolated normal +in vec3 _normal; +in vec4 _color; +in vec2 _texCoord0; +in vec4 _worldFadePosition; + +layout(location = 0) out vec4 _fragColor0; + +void main(void) { + applyFade(_worldFadePosition.xyz); + + vec4 texel = texture(originalTexture, _texCoord0.st); + float colorAlpha = _color.a; + if (_color.a <= 0.0) { + texel = colorToLinearRGBA(texel); + colorAlpha = -_color.a; + } + _fragColor0 = vec4(_color.rgb * texel.rgb, colorAlpha * texel.a); +} \ No newline at end of file