Polishing the scope shaders

This commit is contained in:
Sam Cake 2017-05-18 01:06:59 -07:00
parent 966afa1e4a
commit 5edcc38eba
7 changed files with 122 additions and 101 deletions

View file

@ -25,6 +25,8 @@ typedef glm::vec3 Color;
class Skybox { class Skybox {
public: public:
typedef gpu::BufferView UniformBufferView;
Skybox(); Skybox();
Skybox& operator= (const Skybox& skybox); Skybox& operator= (const Skybox& skybox);
virtual ~Skybox() {}; virtual ~Skybox() {};
@ -43,6 +45,8 @@ public:
static void render(gpu::Batch& batch, const ViewFrustum& frustum, const Skybox& skybox); static void render(gpu::Batch& batch, const ViewFrustum& frustum, const Skybox& skybox);
const UniformBufferView& getSchemaBuffer() const { return _schemaBuffer; }
protected: protected:
static const int SKYBOX_SKYMAP_SLOT { 0 }; static const int SKYBOX_SKYMAP_SLOT { 0 };
static const int SKYBOX_CONSTANTS_SLOT { 0 }; static const int SKYBOX_CONSTANTS_SLOT { 0 };

View file

@ -109,6 +109,7 @@ const gpu::PipelinePointer& DebugZoneLighting::getBackgroundPipeline() {
gpu::Shader::BindingSet slotBindings; gpu::Shader::BindingSet slotBindings;
slotBindings.insert(gpu::Shader::Binding(std::string("deferredFrameTransformBuffer"), ZONE_DEFERRED_TRANSFORM_BUFFER)); slotBindings.insert(gpu::Shader::Binding(std::string("deferredFrameTransformBuffer"), ZONE_DEFERRED_TRANSFORM_BUFFER));
slotBindings.insert(gpu::Shader::Binding(std::string("skyboxMap"), ZONE_SKYBOX_MAP)); slotBindings.insert(gpu::Shader::Binding(std::string("skyboxMap"), ZONE_SKYBOX_MAP));
slotBindings.insert(gpu::Shader::Binding(std::string("skyboxBuffer"), ZONE_SKYBOX_BUFFER));
gpu::Shader::makeProgram(*program, slotBindings); gpu::Shader::makeProgram(*program, slotBindings);
@ -188,6 +189,7 @@ void DebugZoneLighting::run(const render::RenderContextPointer& context, const I
batch.setModelTransform(model); batch.setModelTransform(model);
if (skybox) { if (skybox) {
batch.setResourceTexture(ZONE_SKYBOX_MAP, skybox->getCubemap()); batch.setResourceTexture(ZONE_SKYBOX_MAP, skybox->getCubemap());
batch.setUniformBuffer(ZONE_SKYBOX_BUFFER, skybox->getSchemaBuffer());
} }
batch.draw(gpu::TRIANGLE_STRIP, 4); batch.draw(gpu::TRIANGLE_STRIP, 4);

View file

@ -68,6 +68,7 @@ protected:
ZONE_KEYLIGHT_BUFFER, ZONE_KEYLIGHT_BUFFER,
ZONE_AMBIENT_BUFFER, ZONE_AMBIENT_BUFFER,
ZONE_AMBIENT_MAP, ZONE_AMBIENT_MAP,
ZONE_SKYBOX_BUFFER,
ZONE_SKYBOX_MAP, ZONE_SKYBOX_MAP,
}; };

View file

@ -0,0 +1,37 @@
// Generated on <$_SCRIBE_DATE$>
//
// Created by Sam Gateau on 5/17/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 DeferredTransform.slh@>
<$declareDeferredFrameTransform()$>
<@func evalGlobeWidget()@>
const float SCOPE_RADIUS = 1.0;
const float SCOPE_RADIUS2 = SCOPE_RADIUS * SCOPE_RADIUS;
const float EDGE_HALFWIDTH = 0.025;
const float EDGE_HALFWIDTH2 = EDGE_HALFWIDTH * EDGE_HALFWIDTH;
const float OUT_RADIUS = SCOPE_RADIUS + EDGE_HALFWIDTH;
vec2 sphereUV = (varTexCoord0.xy * 2.0 - vec2(1.0)) * OUT_RADIUS;
float sphereR2 = dot(sphereUV.xy, sphereUV.xy);
if (sphereR2 > OUT_RADIUS * OUT_RADIUS) {
discard;
}
float sphereR = sqrt(sphereR2);
float edgeFalloff = (SCOPE_RADIUS - sphereR) / (EDGE_HALFWIDTH);
float edgeFalloff2 = min(1.0, edgeFalloff * edgeFalloff);
vec4 base = vec4(0.0, 0.0, 0.0, 1.0 - edgeFalloff2);
if (sphereR2 > SCOPE_RADIUS2) {
_fragColor = base;
return;
}
<@endfunc@>

View file

@ -8,9 +8,7 @@
// Distributed under the Apache License, Version 2.0. // Distributed under the Apache License, Version 2.0.
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
// //
<@include zone_draw.slh@>
<@include DeferredTransform.slh@>
<$declareDeferredFrameTransform()$>
<@include model/Light.slh@> <@include model/Light.slh@>
@ -26,20 +24,8 @@ out vec4 _fragColor;
void main(void) { void main(void) {
const float INNER_RADIUS = 1.0; <$evalGlobeWidget()$>
const float INNER_RADIUS2 = INNER_RADIUS * INNER_RADIUS;
const float OUTER_RADIUS = 1.05;
const float OUTER_RADIUS2 = OUTER_RADIUS * OUTER_RADIUS;
vec2 sphereUV = (varTexCoord0.xy * 2.0 - vec2(1.0)) * OUTER_RADIUS;
float sphereR2 = dot(sphereUV.xy, sphereUV.xy);
if (sphereR2 > OUTER_RADIUS * OUTER_RADIUS) {
discard;
}
if (sphereR2 > INNER_RADIUS2) {
float falloff = (sphereR2 - OUTER_RADIUS2) / (OUTER_RADIUS2 - INNER_RADIUS2);
_fragColor = vec4(0.0, 0.0, 0.0, falloff * falloff);
return;
}
vec3 spherePos = normalize(vec3(sphereUV, sqrt(1.0 - sphereR2))); vec3 spherePos = normalize(vec3(sphereUV, sqrt(1.0 - sphereR2)));
@ -58,10 +44,11 @@ void main(void) {
// vec3 ambient = sphericalHarmonics_evalSphericalLight(getLightAmbientSphere(lightAmbient), fragNormal).xyz; // vec3 ambient = sphericalHarmonics_evalSphericalLight(getLightAmbientSphere(lightAmbient), fragNormal).xyz;
// _fragColor = vec4( 0.5 * (fragNormal + vec3(1.0)), 1.0); // _fragColor = vec4( 0.5 * (fragNormal + vec3(1.0)), 1.0);
vec3 ambient = (sphereUV.x > 0 ? ambientMap : ambientSH); vec3 color = (sphereUV.x > 0 ? ambientMap : ambientSH);
color = color * 1.0 - base.w + base.xyz * base.w;
const float INV_GAMMA_22 = 1.0 / 2.2; const float INV_GAMMA_22 = 1.0 / 2.2;
_fragColor = vec4(pow(ambient, vec3(INV_GAMMA_22)), 1.0); _fragColor = vec4(pow(color, vec3(INV_GAMMA_22)), 1.0);
} }

View file

@ -8,9 +8,7 @@
// Distributed under the Apache License, Version 2.0. // Distributed under the Apache License, Version 2.0.
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
// //
<@include zone_draw.slh@>
<@include DeferredTransform.slh@>
<$declareDeferredFrameTransform()$>
<@include model/Light.slh@> <@include model/Light.slh@>
@ -25,33 +23,31 @@ out vec4 _fragColor;
void main(void) { void main(void) {
const float INNER_RADIUS = 1.0; <$evalGlobeWidget()$>
const float INNER_RADIUS2 = INNER_RADIUS * INNER_RADIUS;
const float OUTER_RADIUS = 1.05;
const float OUTER_RADIUS2 = OUTER_RADIUS * OUTER_RADIUS;
vec2 sphereUV = (varTexCoord0.xy * 2.0 - vec2(1.0)) * OUTER_RADIUS;
float sphereR2 = dot(sphereUV.xy, sphereUV.xy);
if (sphereR2 > OUTER_RADIUS * OUTER_RADIUS) {
discard;
}
if (sphereR2 > INNER_RADIUS2) {
float falloff = (sphereR2 - OUTER_RADIUS2) / (OUTER_RADIUS2 - INNER_RADIUS2);
_fragColor = vec4(0.0, 0.0, 0.0, falloff * falloff);
return;
}
vec3 spherePos = normalize(vec3(sphereUV, sqrt(1.0 - sphereR2)));
vec3 fragNormal = vec3(getViewInverse() * vec4(spherePos, 0.0));
Light light = getLight(); Light light = getLight();
vec3 lightDirection = getLightDirection(light); vec3 lightDirection = getLightDirection(light);
vec3 lightIrradiance = getLightIrradiance(light); vec3 lightIrradiance = getLightIrradiance(light);
vec3 color = vec3(0.0);
vec3 color = lightIrradiance; const float INOUT_RATIO = 0.4;
const float SUN_THRESHOLD = 0.99;
vec3 outSpherePos = normalize(vec3(sphereUV, -sqrt(1.0 - sphereR2)));
vec3 outNormal = vec3(getViewInverse() * vec4(outSpherePos, 0.0));
float val = step(SUN_THRESHOLD, dot(-lightDirection, outNormal));
color = lightIrradiance * vec3(val);
if (sphereR2 < INOUT_RATIO * INOUT_RATIO * SCOPE_RADIUS2) {
vec2 inSphereUV = sphereUV / INOUT_RATIO;
vec3 inSpherePos = normalize(vec3(inSphereUV, sqrt(1.0 - dot(inSphereUV.xy, inSphereUV.xy))));
vec3 inNormal = vec3(getViewInverse() * vec4(inSpherePos, 0.0));
color += lightIrradiance * vec3(dot(-lightDirection, inNormal));
}
color = color * 1.0 - base.w + base.xyz * base.w;
const float INV_GAMMA_22 = 1.0 / 2.2; const float INV_GAMMA_22 = 1.0 / 2.2;
_fragColor = vec4(pow(color, vec3(INV_GAMMA_22)), 1.0); _fragColor = vec4(pow(color, vec3(INV_GAMMA_22)), 1.0);
} }

View file

@ -8,47 +8,41 @@
// Distributed under the Apache License, Version 2.0. // Distributed under the Apache License, Version 2.0.
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
// //
<@include zone_draw.slh@>
<@include DeferredTransform.slh@>
<$declareDeferredFrameTransform()$>
// declareSkyboxMap
uniform samplerCube skyboxMap; uniform samplerCube skyboxMap;
vec4 evalSkyboxLight(vec3 direction, float lod) { struct Skybox {
// textureQueryLevels is not available until #430, so we require explicit lod vec4 color;
// float mipmapLevel = lod * textureQueryLevels(skyboxMap); };
return textureLod(skyboxMap, direction, lod);
} uniform skyboxBuffer {
Skybox skybox;
};
in vec2 varTexCoord0; in vec2 varTexCoord0;
out vec4 _fragColor; out vec4 _fragColor;
void main(void) { void main(void) {
<$evalGlobeWidget()$>
const float INNER_RADIUS = 1.0;
const float INNER_RADIUS2 = INNER_RADIUS * INNER_RADIUS;
const float OUTER_RADIUS = 1.05;
const float OUTER_RADIUS2 = OUTER_RADIUS * OUTER_RADIUS;
vec2 sphereUV = (varTexCoord0.xy * 2.0 - vec2(1.0)) * OUTER_RADIUS;
float sphereR2 = dot(sphereUV.xy, sphereUV.xy);
if (sphereR2 > OUTER_RADIUS * OUTER_RADIUS) {
discard;
}
if (sphereR2 > INNER_RADIUS2) {
float falloff = (sphereR2 - OUTER_RADIUS2) / (OUTER_RADIUS2 - INNER_RADIUS2);
_fragColor = vec4(0.0, 0.0, 0.0, falloff * falloff);
return;
}
vec3 spherePos = normalize(vec3(sphereUV, -sqrt(1.0 - sphereR2))); vec3 spherePos = normalize(vec3(sphereUV, -sqrt(1.0 - sphereR2)));
vec3 fragNormal = vec3(getViewInverse() * vec4(spherePos, 0.0)); vec3 direction = vec3(getViewInverse() * vec4(spherePos, 0.0));
float lod = 0; vec3 color = skybox.color.rgb;
vec3 ambient = evalSkyboxLight(fragNormal, lod).xyz;
// blend is only set if there is a cubemap
if (skybox.color.a > 0.0) {
color = texture(skyboxMap, direction).rgb;
if (skybox.color.a < 1.0) {
color *= skybox.color.rgb;
}
}
color = color * 1.0 - base.w + base.xyz * base.w;
const float INV_GAMMA_22 = 1.0 / 2.2; const float INV_GAMMA_22 = 1.0 / 2.2;
_fragColor = vec4(pow(ambient, vec3(INV_GAMMA_22)), 1.0); _fragColor = vec4(pow(color, vec3(INV_GAMMA_22)), 1.0);
} }