Skip to content

Commit

Permalink
feat(gizmos): make transform gizmo have constant size
Browse files Browse the repository at this point in the history
  • Loading branch information
DiogoMendonc-a committed Mar 24, 2024
1 parent ce5b291 commit 38fb934
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 24 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Force plugins to be added only once (#422, **@RiscadoA**).
- Return direct references to resources instead of guards on World (#922, **@RiscadoA**).
- Stop using old serialization to load Settings (#1100, **@RiscadoA**).
- Make transform gizmo screen size indpendent from entity's distance to camera (#1017, **@DiogoMendonc-a**)

### Changed

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ namespace tesseratos
///
/// ## Settings
/// - `"transformGizmo.useLocalAxis"` - whether to use local instead of global axis (default: `true`).
/// - `"transformGizmo.distanceToCamera"` - distance at which the gizmo is drawn, the larger the value the smaller
/// the gizmo will be (default: `10`).

/// @brief Plugin entry function.
/// @param cubos @b CUBOS. main class
Expand Down
51 changes: 27 additions & 24 deletions tools/tesseratos/src/tesseratos/transform_gizmo/plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,8 @@ static void checkRotation(Rotation& rotation, glm::vec3 globalPosition, glm::vec

static void drawPositionGizmo(Query<const Camera&, LocalToWorld&> cameraQuery,
Query<Position&, LocalToWorld&> positionQuery, Gizmos& gizmos, const Input& input,
const Window& window, Entity cameraEntity, Entity selectedEntity, bool useLocalAxis)
const Window& window, Entity cameraEntity, Entity selectedEntity, bool useLocalAxis,
double distance)
{
auto [camera, cameraLtw] = *cameraQuery.at(cameraEntity);

Expand Down Expand Up @@ -188,9 +189,11 @@ static void drawPositionGizmo(Query<const Camera&, LocalToWorld&> cameraQuery,
yzColor = {1, 1, 1};
}

auto drawPosition =
cameraPosition + glm::normalize(localToWorld.worldPosition() - cameraPosition) * (float)distance;

gizmos.color(xyColor);
glm::mat4 transformXY =
glm::translate(glm::mat4(1.0F), localToWorld.worldPosition() + (rightVector + upVector) * 0.125F);
glm::mat4 transformXY = glm::translate(glm::mat4(1.0F), drawPosition + (rightVector + upVector) * 0.125F);
if (useLocalAxis)
{
transformXY = transformXY * glm::toMat4(localToWorld.worldRotation());
Expand All @@ -199,8 +202,7 @@ static void drawPositionGizmo(Query<const Camera&, LocalToWorld&> cameraQuery,

gizmos.drawBox("transform_gizmo.position.xy", transformXY);
gizmos.color(xzColor);
glm::mat4 transformXZ =
glm::translate(glm::mat4(1.0F), localToWorld.worldPosition() + (rightVector + forwardVector) * 0.125F);
glm::mat4 transformXZ = glm::translate(glm::mat4(1.0F), drawPosition + (rightVector + forwardVector) * 0.125F);
if (useLocalAxis)
{
transformXZ = transformXZ * glm::toMat4(localToWorld.worldRotation());
Expand All @@ -209,8 +211,7 @@ static void drawPositionGizmo(Query<const Camera&, LocalToWorld&> cameraQuery,

gizmos.drawBox("transform_gizmo.position.xz", transformXZ);
gizmos.color(yzColor);
glm::mat4 transformYZ =
glm::translate(glm::mat4(1.0F), localToWorld.worldPosition() + (upVector + forwardVector) * 0.125F);
glm::mat4 transformYZ = glm::translate(glm::mat4(1.0F), drawPosition + (upVector + forwardVector) * 0.125F);
if (useLocalAxis)
{
transformYZ = transformYZ * glm::toMat4(localToWorld.worldRotation());
Expand All @@ -219,23 +220,21 @@ static void drawPositionGizmo(Query<const Camera&, LocalToWorld&> cameraQuery,
gizmos.drawBox("transform_gizmo.position.yz", transformYZ);

gizmos.color({0.7F, 0.7F, 0.7F});
gizmos.drawBox("", localToWorld.worldPosition() + glm::vec3{0.03F, 0.03F, 0.03F},
localToWorld.worldPosition() - glm::vec3{0.03F, 0.03F, 0.03F});
gizmos.drawBox("", drawPosition + glm::vec3{0.03F, 0.03F, 0.03F}, drawPosition - glm::vec3{0.03F, 0.03F, 0.03F});

gizmos.color(xColor);
gizmos.drawArrow("transform_gizmo.position.x", localToWorld.worldPosition() + rightVector * 0.03F, rightVector,
0.03F, 0.07F, 0.7F);
gizmos.drawArrow("transform_gizmo.position.x", drawPosition + rightVector * 0.03F, rightVector, 0.03F, 0.07F, 0.7F);
gizmos.color(yColor);
gizmos.drawArrow("transform_gizmo.position.y", localToWorld.worldPosition() + upVector * 0.03F, upVector, 0.03F,
0.07F, 0.7F);
gizmos.drawArrow("transform_gizmo.position.y", drawPosition + upVector * 0.03F, upVector, 0.03F, 0.07F, 0.7F);
gizmos.color(zColor);
gizmos.drawArrow("transform_gizmo.position.z", localToWorld.worldPosition() + forwardVector * 0.03F, forwardVector,
0.03F, 0.07F, 0.7F);
gizmos.drawArrow("transform_gizmo.position.z", drawPosition + forwardVector * 0.03F, forwardVector, 0.03F, 0.07F,
0.7F);
}

static void drawRotationGizmo(Query<const Camera&, LocalToWorld&> cameraQuery,
Query<Rotation&, LocalToWorld&> positionQuery, Gizmos& gizmos, const Input& input,
const Window& window, Entity cameraEntity, Entity selectedEntity, bool useLocalAxis)
const Window& window, Entity cameraEntity, Entity selectedEntity, bool useLocalAxis,
double distance)
{
auto [camera, cameraLtw] = *cameraQuery.at(cameraEntity);

Expand Down Expand Up @@ -282,15 +281,18 @@ static void drawRotationGizmo(Query<const Camera&, LocalToWorld&> cameraQuery,
zColor = {1, 1, 0};
}

auto drawPosition =
cameraPosition + glm::normalize(localToWorld.worldPosition() - cameraPosition) * (float)distance;

gizmos.color(xColor);
gizmos.drawRing("transform_gizmo.rotation.x", localToWorld.worldPosition() + (rightVector * 0.1F),
localToWorld.worldPosition() - (rightVector * 0.01F), 1.4F, 1.25F);
gizmos.drawRing("transform_gizmo.rotation.x", drawPosition + (rightVector * 0.1F),
drawPosition - (rightVector * 0.01F), 1.4F, 1.25F);
gizmos.color(yColor);
gizmos.drawRing("transform_gizmo.rotation.y", localToWorld.worldPosition() + (upVector * 0.1F),
localToWorld.worldPosition() - (upVector * 0.01F), 1.4F, 1.25F);
gizmos.drawRing("transform_gizmo.rotation.y", drawPosition + (upVector * 0.1F), drawPosition - (upVector * 0.01F),
1.4F, 1.25F);
gizmos.color(zColor);
gizmos.drawRing("transform_gizmo.rotation.z", localToWorld.worldPosition() + (forwardVector * 0.1F),
localToWorld.worldPosition() - (forwardVector * 0.01F), 1.4F, 1.25F);
gizmos.drawRing("transform_gizmo.rotation.z", drawPosition + (forwardVector * 0.1F),
drawPosition - (forwardVector * 0.01F), 1.4F, 1.25F);
}

void tesseratos::transformGizmoPlugin(Cubos& cubos)
Expand Down Expand Up @@ -327,10 +329,11 @@ void tesseratos::transformGizmoPlugin(Cubos& cubos)
}

bool useLocal = settings.getBool("transformGizmo.useLocalAxis", true);
double distance = settings.getDouble("transformGizmo.distanceToCamera", 10.0F);

drawPositionGizmo(cameraQuery, positionQuery, gizmos, input, window, activeCameras.entities[0],
entitySelector.selection, useLocal);
entitySelector.selection, useLocal, distance);
drawRotationGizmo(cameraQuery, rotationQuery, gizmos, input, window, activeCameras.entities[0],
entitySelector.selection, useLocal);
entitySelector.selection, useLocal, distance);
});
}

0 comments on commit 38fb934

Please sign in to comment.