Skip to content

Commit

Permalink
#80 Fix the decal projection matrix
Browse files Browse the repository at this point in the history
  • Loading branch information
ducphamhong committed May 17, 2022
1 parent 1e8fb40 commit 80b1562
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 32 deletions.
52 changes: 21 additions & 31 deletions Projects/Skylicht/Collision/Source/Decal/CDecalsRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,35 +132,29 @@ namespace Skylicht
position + decal->Dimension * 0.5f
);

// Calculate uv rotation
core::quaternion quatDirection;
quatDirection.rotationFromTo(irr::core::vector3df(0, 1, 0), decal->Normal);

core::vector3df rotation;
quatDirection.toEuler(rotation);
rotation.Y += (decal->TextureRotation * core::DEGTORAD);

// Create uv rotation matrix
core::matrix4 rotationMatrix;
rotationMatrix.setRotationRadians(rotation);
rotationMatrix.setRotationCenter(core::vector3df(0.5f, 0.5f, 0.5f), core::vector3df(0, 0, 0));

// Query tris collision
core::array<core::triangle3df*> triangles;
core::array<CCollisionNode*> nodes;
decal->Collision->getTriangles(box, triangles, nodes);
u32 triangleCount = triangles.size();

// Scale to 0.0f - 1.0f (UV space)
core::vector3df scale = core::vector3df(1, 1, 1) / box.getExtent();
core::vector3df uvScale = core::vector3df(1, 1, 1) / decal->Dimension;
core::vector3df uvOffset(0.5f, 0.0f, 0.5f);

// UV Rotation matrix
core::quaternion r1;
r1.rotationFromTo(core::vector3df(0.0f, 1.0f, 0.0f), decal->Normal);

// Create scale matrix
irr::core::matrix4 scaleMatrix;
scaleMatrix.setScale(scale);
core::quaternion r2;
r2.fromAngleAxis(-decal->TextureRotation * core::DEGTORAD, core::vector3df(0.0f, 1.0f, 0.0f));

irr::core::matrix4 m;
m.setTranslation(-(box.MinEdge * scale));
m *= scaleMatrix;
core::quaternion q = r2 * r1;

core::matrix4 uvMatrix = q.getMatrix();
uvMatrix.setTranslation(position);

uvMatrix.makeInverse();

// Clip all triangles and fill vertex and indices
u32 vertexIndex = 0;
Expand All @@ -179,33 +173,29 @@ namespace Skylicht
core::vector3df triangleNormal = triangle.getNormal();
triangleNormal.normalize();

// Scale & Translate positions
m.transformVect(uvTriangle.pointA);
m.transformVect(uvTriangle.pointB);
m.transformVect(uvTriangle.pointC);

// Rotate positions
rotationMatrix.transformVect(uvTriangle.pointA);
rotationMatrix.transformVect(uvTriangle.pointB);
rotationMatrix.transformVect(uvTriangle.pointC);
uvMatrix.transformVect(uvTriangle.pointA);
uvMatrix.transformVect(uvTriangle.pointB);
uvMatrix.transformVect(uvTriangle.pointC);


// Fill vertices and indices
{
video::SColor color(255, 255, 255, 255);

for (u32 p = 0; p < 3; p++)
{
core::vector3df uvPos = uvTriangle.pointA;
core::vector3df uvPos = uvTriangle.pointA * uvScale + uvOffset;
core::vector3df pos = triangle.pointA;

if (p == 1)
{
uvPos = uvTriangle.pointB;
uvPos = uvTriangle.pointB * uvScale + uvOffset;
pos = triangle.pointB;
}
else if (p == 2)
{
uvPos = uvTriangle.pointC;
uvPos = uvTriangle.pointC * uvScale + uvOffset;
pos = triangle.pointC;
}

Expand Down
2 changes: 1 addition & 1 deletion Samples/Collision/Source/CViewDemo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ void CViewDemo::onUpdate()
{
m_decals->addDecal(
intersection,
core::vector3df(m_decalSizeX, m_decalSizeX, m_decalSizeX),
core::vector3df(m_decalSizeX, m_decalSizeY, m_decalSizeZ),
normal,
m_decalRotation,
m_decalLifeTime,
Expand Down

0 comments on commit 80b1562

Please sign in to comment.