Support glow effect in 3D line overlays

This commit is contained in:
Brad Davis 2016-07-06 12:37:36 -07:00
parent 790f74da1d
commit 8ca3630cfa
7 changed files with 545 additions and 244 deletions

View file

@ -55,12 +55,14 @@ void Line3DOverlay::render(RenderArgs* args) {
batch->setModelTransform(_transform);
auto geometryCache = DependencyManager::get<GeometryCache>();
geometryCache->bindSimpleProgram(*batch, false, false, true, true);
if (getIsDashedLine()) {
// TODO: add support for color to renderDashedLine()
geometryCache->bindSimpleProgram(*batch, false, false, true, true);
geometryCache->renderDashedLine(*batch, _start, _end, colorv4, _geometryCacheID);
} else if (_glow > 0.0f) {
geometryCache->renderGlowLine(*batch, _start, _end, colorv4, _glow, _glowWidth, _geometryCacheID);
} else {
geometryCache->bindSimpleProgram(*batch, false, false, true, true);
geometryCache->renderLine(*batch, _start, _end, colorv4, _geometryCacheID);
}
}
@ -68,7 +70,7 @@ void Line3DOverlay::render(RenderArgs* args) {
const render::ShapeKey Line3DOverlay::getShapeKey() {
auto builder = render::ShapeKey::Builder().withOwnPipeline();
if (getAlpha() != 1.0f) {
if (getAlpha() != 1.0f || _glow > 0.0f) {
builder.withTranslucent();
}
return builder.build();
@ -94,6 +96,19 @@ void Line3DOverlay::setProperties(const QVariantMap& properties) {
if (end.isValid()) {
setEnd(vec3FromVariant(end));
}
auto glow = properties["glow"];
if (glow.isValid()) {
setGlow(glow.toFloat());
if (_glow > 0.0f) {
_alpha = 0.5f;
}
}
auto glowWidth = properties["glow"];
if (glowWidth.isValid()) {
setGlow(glowWidth.toFloat());
}
}
QVariant Line3DOverlay::getProperty(const QString& property) {

View file

@ -30,10 +30,14 @@ public:
// getters
const glm::vec3& getStart() const { return _start; }
const glm::vec3& getEnd() const { return _end; }
const float& getGlow() const { return _glow; }
const float& getGlowWidth() const { return _glowWidth; }
// setters
void setStart(const glm::vec3& start) { _start = start; }
void setEnd(const glm::vec3& end) { _end = end; }
void setGlow(const float& glow) { _glow = glow; }
void setGlowWidth(const float& glowWidth) { _glowWidth = glowWidth; }
void setProperties(const QVariantMap& properties) override;
QVariant getProperty(const QString& property) override;
@ -43,6 +47,8 @@ public:
protected:
glm::vec3 _start;
glm::vec3 _end;
float _glow { 0.0 };
float _glowWidth { 0.0 };
int _geometryCacheID;
};

File diff suppressed because it is too large Load diff

View file

@ -259,6 +259,9 @@ public:
void renderLine(gpu::Batch& batch, const glm::vec3& p1, const glm::vec3& p2,
const glm::vec4& color1, const glm::vec4& color2, int id = UNKNOWN_ID);
void renderGlowLine(gpu::Batch& batch, const glm::vec3& p1, const glm::vec3& p2,
const glm::vec4& color, float glowIntensity = 1.0f, float glowWidth = 0.05f, int id = UNKNOWN_ID);
void renderDashedLine(gpu::Batch& batch, const glm::vec3& start, const glm::vec3& end, const glm::vec4& color,
int id = UNKNOWN_ID)
{ renderDashedLine(batch, start, end, color, 0.05f, 0.025f, id); }
@ -403,6 +406,7 @@ private:
gpu::ShaderPointer _unlitShader;
static render::ShapePipelinePointer _simplePipeline;
static render::ShapePipelinePointer _simpleWirePipeline;
gpu::PipelinePointer _glowLinePipeline;
QHash<SimpleProgramKey, gpu::PipelinePointer> _simplePrograms;
};

View file

@ -0,0 +1,32 @@
<@include gpu/Config.slh@>
<$VERSION_HEADER$>
// Generated on <$_SCRIBE_DATE$>
//
// Created by Bradley Austin Davis on 2016/07/05
// Copyright 2013-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
//
layout(location = 0) in vec4 inColor;
layout(location = 1) in vec3 inLineDistance;
out vec4 _fragColor;
void main(void) {
vec2 d = inLineDistance.xy;
d.y = abs(d.y);
d.x = abs(d.x);
if (d.x > 1.0) {
d.x = (d.x - 1.0) / 0.02;
} else {
d.x = 0.0;
}
float alpha = 1.0 - length(d);
if (alpha < 0.01) {
discard;
}
alpha = pow(alpha, 10.0);
_fragColor = vec4(inColor.rgb, alpha);
}

View file

@ -0,0 +1,127 @@
<@include gpu/Config.slh@>
<$VERSION_HEADER$>
// Generated on <$_SCRIBE_DATE$>
//
// Created by Bradley Austin Davis on 2016/07/05
// Copyright 2013-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
//
#extension GL_EXT_geometry_shader4 : enable
layout(location = 0) in vec4 inColor[];
layout(location = 0) out vec4 outColor;
layout(location = 1) out vec3 outLineDistance;
layout(lines) in;
layout(triangle_strip, max_vertices = 24) out;
struct TransformCamera {
mat4 _view;
mat4 _viewInverse;
mat4 _projectionViewUntranslated;
mat4 _projection;
mat4 _projectionInverse;
vec4 _viewport;
};
layout(std140) uniform transformCameraBuffer {
TransformCamera _camera;
};
TransformCamera getTransformCamera() {
return _camera;
}
vec3 getEyeWorldPos() {
return _camera._viewInverse[3].xyz;
}
vec3 ndcToEyeSpace(in vec4 v) {
TransformCamera cam = getTransformCamera();
vec4 u = cam._projectionInverse * v;
return u.xyz / u.w;
}
vec2 toScreenSpace(in vec4 v)
{
TransformCamera cam = getTransformCamera();
vec4 u = cam._projection * cam._view * v;
return u.xy / u.w;
}
vec3[2] getOrthogonals(in vec3 n, float scale) {
float yDot = abs(dot(n, vec3(0, 1, 0)));
vec3 result[2];
if (yDot < 0.9) {
result[0] = normalize(cross(n, vec3(0, 1, 0)));
} else {
result[0] = normalize(cross(n, vec3(1, 0, 0)));
}
// The cross of result[0] and n is orthogonal to both, which are orthogonal to each other
result[1] = cross(result[0], n);
result[0] *= scale;
result[1] *= scale;
return result;
}
vec2 orthogonal(vec2 v) {
vec2 result = v.yx;
result.y *= -1.0;
return result;
}
void main() {
vec2 endpoints[2];
vec3 eyeSpace[2];
TransformCamera cam = getTransformCamera();
for (int i = 0; i < 2; ++i) {
eyeSpace[i] = ndcToEyeSpace(gl_PositionIn[i]);
endpoints[i] = gl_PositionIn[i].xy / gl_PositionIn[i].w;
}
vec2 lineNormal = normalize(endpoints[1] - endpoints[0]);
vec2 lineOrthogonal = orthogonal(lineNormal);
lineNormal *= 0.02;
lineOrthogonal *= 0.02;
gl_Position = gl_PositionIn[0];
gl_Position.xy -= lineNormal;
gl_Position.xy -= lineOrthogonal;
outColor = inColor[0];
outLineDistance = vec3(-1.02, -1, gl_Position.z);
EmitVertex();
gl_Position = gl_PositionIn[0];
gl_Position.xy -= lineNormal;
gl_Position.xy += lineOrthogonal;
outColor = inColor[0];
outLineDistance = vec3(-1.02, 1, gl_Position.z);
EmitVertex();
gl_Position = gl_PositionIn[1];
gl_Position.xy += lineNormal;
gl_Position.xy -= lineOrthogonal;
outColor = inColor[1];
outLineDistance = vec3(1.02, -1, gl_Position.z);
EmitVertex();
gl_Position = gl_PositionIn[1];
gl_Position.xy += lineNormal;
gl_Position.xy += lineOrthogonal;
outColor = inColor[1];
outLineDistance = vec3(1.02, 1, gl_Position.z);
EmitVertex();
EndPrimitive();
}

View file

@ -0,0 +1,26 @@
<@include gpu/Config.slh@>
<$VERSION_HEADER$>
// Generated on <$_SCRIBE_DATE$>
//
// Created by Bradley Austin Davis on 2016/07/05
// Copyright 2013-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
//
<@include gpu/Inputs.slh@>
<@include gpu/Color.slh@>
<@include gpu/Transform.slh@>
<$declareStandardTransform()$>
layout(location = 0) out vec4 _color;
void main(void) {
_color = inColor;
// standard transform
TransformCamera cam = getTransformCamera();
TransformObject obj = getTransformObject();
<$transformModelToClipPos(cam, obj, inPosition, gl_Position)$>
}