Skip to content

Instantly share code, notes, and snippets.

@victorholt
Created May 31, 2016 23:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save victorholt/2c7757dd6fba7504b5018053bbe145ee to your computer and use it in GitHub Desktop.
Save victorholt/2c7757dd6fba7504b5018053bbe145ee to your computer and use it in GitHub Desktop.
CurvedText3D.cpp
#include "../PreCompiledHeaders.h"
#include "CurvedText3D.h"
using namespace CorFramework;
namespace Urho3D
{
extern const char* horizontalAlignments[];
extern const char* verticalAlignments[];
extern const char* textEffects[];
extern const char* faceCameraModeNames[];
extern const char* GEOMETRY_CATEGORY;
static const float TEXT_SCALING = 1.0f / 128.0f;
static const float DEFAULT_EFFECT_DEPTH_BIAS = 0.1f;
CurvedText3D::CurvedText3D(Context* context) :
Text3D(context)
{
useLineTool_ = true;
}
CurvedText3D::~CurvedText3D()
{
}
void CurvedText3D::RegisterObject(Context* context)
{
context->RegisterFactory<CurvedText3D>(GEOMETRY_CATEGORY);
URHO3D_ACCESSOR_ATTRIBUTE("Font Point Size", GetFontPointSize, SetFontPointSize, float, 0.0f, AM_DEFAULT);
URHO3D_ACCESSOR_ATTRIBUTE("Font Max Point Size", GetMaxFontPointSize, SetMaxFontPointSize, float, 0.0f, AM_DEFAULT);
URHO3D_ACCESSOR_ATTRIBUTE("Font Min Point Size", GetMinFontPointSize, SetMinFontPointSize, float, 0.0f, AM_DEFAULT);
URHO3D_COPY_BASE_ATTRIBUTES(Text3D);
}
/// Apply attribute changes that can not be applied immediately.
void CurvedText3D::ApplyAttributes()
{
Text3D::ApplyAttributes();
}
/// Visualize the component as debug geometry.
void CurvedText3D::DrawDebugGeometry(DebugRenderer* debug, bool depthTest)
{
Text3D::DrawDebugGeometry(debug, depthTest);
}
/// Apply the curve to the text given the start/end location.
void CurvedText3D::ApplyCurve(Vector3 curveStart, Vector3 curveEnd, float curveAmount, float maxCurveHeight)
{
//curveStart *= TEXT_SCALING;
//curveEnd *= TEXT_SCALING;
//maxCurveHeight *= TEXT_SCALING;
CorApplication* app = GetSubsystem<CorApplication>();
Camera* camera = app->GetMainCamera()->GetComponent<Camera>();
// Clear our text draw data.
uiBatches_.Clear();
uiVertexData_.Clear();
text_.GetBatches(uiBatches_, uiVertexData_, IntRect::ZERO);
// Use the world position/scale to modify the position correctly.
// Otherwise you will end up with positions that are WAY off.
Vector3 worldPosition = node_->GetWorldPosition();
Vector3 worldScale = node_->GetWorldScale();
worldPosition *= TEXT_SCALING;
worldScale *= TEXT_SCALING;
// Grab the number of characters.
unsigned numChars = GetNumChars();
// Index to the current character we're working with.
unsigned charIndex = 0;
float tWidth = text_.GetWidth();
float tHeight = text_.GetHeight();
Vector3 bbMin = boundingBox_.min_;
Vector3 bbMax = boundingBox_.max_;
float maxHeight = tHeight;
// Clear our bounding box.
boundingBox_.Clear();
// Create our line helper.
if (useLineTool_) {
lineTool_ = new LineTool(GetContext());
}
// Go through each character and modify the position.
for (unsigned i = 0; i < uiVertexData_.Size(); i += (UI_VERTEX_SIZE * 6)) {
// Find the curve position.
float interval = charIndex != numChars - 1 ? charIndex / static_cast<float>(numChars) : 1.0f;
Vector3 curvePos = MathUtil::Vec3(MathUtil::SampleParabola(glm::vec3(curveStart.x_, 0.0f, curveStart.z_), glm::vec3(curveEnd.x_, 0.0f, curveEnd.z_), maxCurveHeight, interval));
if (useLineTool_) {
lineTool_->AddVertex(curvePos);
}
BoundingBox curveBox = boundingBox_.Transformed(Matrix3x4(curvePos,
customWorldTransform_.Rotation(), customWorldTransform_.Scale()));
Vector3 trans = Matrix3x4(curvePos * TEXT_SCALING,
customWorldTransform_.Rotation(), customWorldTransform_.Scale()).Translation();
//curvePos.x_ = curvePos.x_ * TEXT_SCALING;
//curvePos.z_ = curvePos.z_ * TEXT_SCALING;
GetSubsystem<Log>()->WriteRaw(fmt::format("\nCurvePosX: {0}, CurvePosZ: {1}\n", trans.x_, trans.z_).c_str());
//curvePos *= TEXT_SCALING;
//curvePos += worldPosition;
// BottomLeft point
Vector3& v1 = *(reinterpret_cast<Vector3*>(&uiVertexData_[i]));
// BottomRight point
Vector3& v2 = *(reinterpret_cast<Vector3*>(&uiVertexData_[i + (UI_VERTEX_SIZE * 1)]));
// TopLeft point
Vector3& v3 = *(reinterpret_cast<Vector3*>(&uiVertexData_[i + (UI_VERTEX_SIZE * 2)]));
// BottomRight point
Vector3& v4 = *(reinterpret_cast<Vector3*>(&uiVertexData_[i + (UI_VERTEX_SIZE * 3)]));
// TopRight point
Vector3& v5 = *(reinterpret_cast<Vector3*>(&uiVertexData_[i + (UI_VERTEX_SIZE * 4)]));
// TopLeft point
Vector3& v6 = *(reinterpret_cast<Vector3*>(&uiVertexData_[i + (UI_VERTEX_SIZE * 5)]));
float fontSpacing = v2.x_ - v1.x_;
/*GetSubsystem<Log>()->WriteRaw(
fmt::format("\nx1: ({0},{1},{2})\nx2: ({3},{4},{5})\nx3: ({6},{7},{8})\nx4: ({9},{10},{11})\nx5: ({12},{13},{14})\nx6: ({15},{16},{17})\n\n\n",
v1.x_, v1.y_, v1.z_,
v2.x_, v2.y_, v2.z_,
v3.x_, v3.y_, v3.z_,
v4.x_, v4.y_, v4.z_,
v5.x_, v5.y_, v5.z_,
v6.x_, v6.y_, v6.z_
).c_str()
);*/
// Update the positions.
v1.y_ += trans.z_;
v2.y_ += trans.z_;
v3.y_ += trans.z_;
v4.y_ += trans.z_;
v5.y_ += trans.z_;
v6.y_ += trans.z_;
// Modify the scaling
v1 *= TEXT_SCALING;
v2 *= TEXT_SCALING;
v3 *= TEXT_SCALING;
v4 *= TEXT_SCALING;
v5 *= TEXT_SCALING;
v6 *= TEXT_SCALING;
// Flip the character.
v1.y_ = -v1.y_;
v2.y_ = -v2.y_;
v3.y_ = -v3.y_;
v4.y_ = -v4.y_;
v5.y_ = -v5.y_;
v6.y_ = -v6.y_;
// Update our bounding box.
boundingBox_.Merge(v1);
boundingBox_.Merge(v2);
boundingBox_.Merge(v3);
boundingBox_.Merge(v4);
boundingBox_.Merge(v5);
boundingBox_.Merge(v6);
// Update the character index.
charIndex++;
}
// Render the line tool.
if (useLineTool_) {
lineTool_->LineColor = Color::RED;
lineTool_->BuildMesh("CurveText3D");
}
// Mark our text geometry as dirty.
textDirty_ = false;
geometryDirty_ = true;
//MarkTextDirty();
//UpdateTextBatches();
UpdateTextMaterials();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment