From 16aa150f104fe202b162c0e189dfe3e8a29187ef Mon Sep 17 00:00:00 2001 From: Emma Broman Date: Fri, 17 Jan 2025 13:18:45 +0100 Subject: [PATCH] Polish `RenderableCartesianAxes` - documentation, examples, and make fading work correctly (#3478) * Make `RenderableCartesianAxes` fade and fix opacity setting * Update existing documentation of cartesian axes * Add example with custom colros and parent node * Apply suggestions from code review Co-authored-by: Alexander Bock * Include earth asset instead of using string identifer --------- Co-authored-by: Alexander Bock --- .../cartesianaxes.asset | 4 +-- .../cartesianaxes_customcolor.asset | 32 +++++++++++++++++++ .../cartesianaxes_parent.asset | 31 ++++++++++++++++++ .../rendering/renderablecartesianaxes.cpp | 8 +++-- modules/base/shaders/axes_fs.glsl | 4 +-- 5 files changed, 73 insertions(+), 6 deletions(-) create mode 100644 data/assets/examples/renderable/renderablecartesianaxes/cartesianaxes_customcolor.asset create mode 100644 data/assets/examples/renderable/renderablecartesianaxes/cartesianaxes_parent.asset diff --git a/data/assets/examples/renderable/renderablecartesianaxes/cartesianaxes.asset b/data/assets/examples/renderable/renderablecartesianaxes/cartesianaxes.asset index 49c29d5b6e..14f9f23159 100644 --- a/data/assets/examples/renderable/renderablecartesianaxes/cartesianaxes.asset +++ b/data/assets/examples/renderable/renderablecartesianaxes/cartesianaxes.asset @@ -1,6 +1,6 @@ -- Basic --- This asset creates a SceneGraphNode that only displays coordinate axes. The --- parent is not set which defaults to placing the axes at the center the Sun. +-- This example creates a SceneGraphNode that only displays coordinate axes. The +-- parent is not set which defaults to placing the axes at the center of the Sun. local Node = { Identifier = "RenderableCartesianAxes_Example", diff --git a/data/assets/examples/renderable/renderablecartesianaxes/cartesianaxes_customcolor.asset b/data/assets/examples/renderable/renderablecartesianaxes/cartesianaxes_customcolor.asset new file mode 100644 index 0000000000..a8dc00d6d5 --- /dev/null +++ b/data/assets/examples/renderable/renderablecartesianaxes/cartesianaxes_customcolor.asset @@ -0,0 +1,32 @@ +-- Custom Colors +-- This example creates a set of cartesian coordinate axes with specified colors, instead +-- of using the default which is red (X), green (Y), and blue (Z). The parent is not set +-- which defaults to placing the axes at the center of the Solar System. + +local Node = { + Identifier = "RenderableCartesianAxes_Example_CustomColors", + Transform = { + Scale = { + Type = "StaticScale", + Scale = 30000000 + } + }, + Renderable = { + Type = "RenderableCartesianAxes", + XColor = { 0.0, 1.0, 1.0 }, -- Cyan + YColor = { 1.0, 0.0, 1.0 }, -- Magenta + ZColor = { 1.0, 1.0, 0.0 } -- Yellow + }, + GUI = { + Name = "RenderableCartesianAxes - Custom Colors", + Path = "/Examples" + } +} + +asset.onInitialize(function() + openspace.addSceneGraphNode(Node) +end) + +asset.onDeinitialize(function() + openspace.removeSceneGraphNode(Node) +end) diff --git a/data/assets/examples/renderable/renderablecartesianaxes/cartesianaxes_parent.asset b/data/assets/examples/renderable/renderablecartesianaxes/cartesianaxes_parent.asset new file mode 100644 index 0000000000..4dc7eb0dfd --- /dev/null +++ b/data/assets/examples/renderable/renderablecartesianaxes/cartesianaxes_parent.asset @@ -0,0 +1,31 @@ +-- With Parent +-- This example creates a SceneGraphNode that displays coordinate axes of the given parent +-- node, in this case Earth. + +local earth = asset.require("scene/solarsystem/planets/earth/earth") + +local Node = { + Identifier = "RenderableCartesianAxes_Example_Parent", + Parent = earth.Earth.Identifier, + Transform = { + Scale = { + Type = "StaticScale", + Scale = 30000000 + } + }, + Renderable = { + Type = "RenderableCartesianAxes" + }, + GUI = { + Name = "RenderableCartesianAxes - With Parent", + Path = "/Examples" + } +} + +asset.onInitialize(function() + openspace.addSceneGraphNode(Node) +end) + +asset.onDeinitialize(function() + openspace.removeSceneGraphNode(Node) +end) diff --git a/modules/base/rendering/renderablecartesianaxes.cpp b/modules/base/rendering/renderablecartesianaxes.cpp index d685229f6a..9281f4fddd 100644 --- a/modules/base/rendering/renderablecartesianaxes.cpp +++ b/modules/base/rendering/renderablecartesianaxes.cpp @@ -65,9 +65,9 @@ namespace { // // To add the axes, create a scene graph node with the RenderableCartesianAxes // renderable and add it as a child to the other scene graph node, i.e. specify the - // other node as the Parent of the node with this renderable. Also, the axes have to + // other node as the `Parent` of the node with this renderable. Also, the axes have to // be scaled to match the parent object for the axes to be visible in the scene, for - // example using a StaticScale. + // example using a [StaticScale](#base_scale_static). struct [[codegen::Dictionary(RenderableCartesianAxes)]] Parameters { // [[codegen::verbatim(XColorInfo.description)]] std::optional xColor [[codegen::color()]]; @@ -95,6 +95,9 @@ RenderableCartesianAxes::RenderableCartesianAxes(const ghoul::Dictionary& dictio , _zColor(ZColorInfo, glm::vec3(0.f, 0.f, 1.f), glm::vec3(0.f), glm::vec3(1.f)) { const Parameters p = codegen::bake(dictionary); + + addProperty(Fadeable::_opacity); + _xColor = p.xColor.value_or(_xColor); _xColor.setViewOption(properties::Property::ViewOptions::Color); addProperty(_xColor); @@ -195,6 +198,7 @@ void RenderableCartesianAxes::render(const RenderData& data, RendererTasks&) { _program->setUniform("xColor", _xColor); _program->setUniform("yColor", _yColor); _program->setUniform("zColor", _zColor); + _program->setUniform("opacity", opacity()); // Changes GL state: glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); diff --git a/modules/base/shaders/axes_fs.glsl b/modules/base/shaders/axes_fs.glsl index 8448d4549f..df84bd633c 100644 --- a/modules/base/shaders/axes_fs.glsl +++ b/modules/base/shaders/axes_fs.glsl @@ -31,7 +31,7 @@ in vec3 vs_positionModelSpace; uniform vec3 xColor; uniform vec3 yColor; uniform vec3 zColor; - +uniform float opacity; Fragment getFragment() { Fragment frag; @@ -44,7 +44,7 @@ Fragment getFragment() { frag.color.rgb = colorComponents.x * xColor + colorComponents.y * yColor + colorComponents.z * zColor; - frag.color.a = 1.0; + frag.color.a = opacity; frag.depth = vs_screenSpaceDepth; frag.gPosition = vs_positionViewSpace;