1 // Copyright 2001-2018 Crytek GmbH / Crytek Group. All rights reserved.
4 #include "CubeUnwrappingTool.h"
5 #include "Util/ElementSet.h"
6 #include "Core/UVIslandManager.h"
7 #include "../UVMappingEditor.h"
9 #include "Util/UVUnwrapUtil.h"
14 void CubeUnwrappingTool::Enter()
16 if (!GetUVEditor()->GetModel())
19 Vec3 axises
[] = { Vec3(1, 0, 0), Vec3(0, 1, 0), Vec3(0, 0, 1), Vec3(-1, 0, 0), Vec3(0, -1, 0), Vec3(0, 0, -1) };
20 constexpr int numberOfAxises
= CRY_ARRAY_COUNT(axises
);
21 std::vector
<PolygonPtr
> polygonSets
[numberOfAxises
];
22 ElementSet
* pSelected
= GetUVEditor()->GetSelectedElements();
23 if (pSelected
== NULL
)
25 UVIslandManager
* pUVIslandMgr
= GetUVEditor()->GetUVIslandMgr();
27 CUndo
undo("UV Mapping : Cube Projection");
28 CUndo::Record(new UVIslandUndo
);
29 CUndo::Record(new UVProjectionUndo
);
31 AssignMatIDToSelectedPolygons(GetUVEditor()->GetSubMatID());
33 for (int k
= 0, iCount(pSelected
->GetCount()); k
< iCount
; ++k
)
35 const Element
& element
= pSelected
->Get(k
);
36 if (!element
.IsPolygon())
39 float fBiggestCos
= -1.5f
;
42 for (int i
= 0; i
< numberOfAxises
; ++i
)
44 const Vec3
& axis
= axises
[i
];
45 float fCos
= axis
.Dot(ToVec3(element
.m_pPolygon
->GetPlane().Normal()));
47 if (fCos
> fBiggestCos
)
54 DESIGNER_ASSERT(axisIndex
!= -1);
58 UnwrapUtil::ApplyPlanarProjection(BrushPlane(ToVec3(axises
[axisIndex
]), 0), element
.m_pPolygon
);
59 polygonSets
[axisIndex
].push_back(element
.m_pPolygon
);
62 std::vector
<UVIslandPtr
> UVIslands
;
63 for (int i
= 0; i
< numberOfAxises
; ++i
)
65 UVIslandPtr pUVIsland
= new UVIsland
;
66 for (int k
= 0, iCount(polygonSets
[i
].size()); k
< iCount
; ++k
)
67 pUVIsland
->AddPolygon(polygonSets
[i
][k
]);
68 UVIslands
.push_back(pUVIsland
);
71 UnwrapUtil::PackUVIslands(pUVIslandMgr
, UVIslands
, ePlaneAxis_Average
);
73 GetUVEditor()->SelectUVIslands(UVIslands
);
74 GetUVEditor()->UpdateGizmoPos();
75 GetUVEditor()->GetUVIslandMgr()->ConvertIsolatedAreasToIslands();
76 GetUVEditor()->SetTool(eUVMappingTool_Island
);
77 GetUVEditor()->ApplyPostProcess();
83 REGISTER_UVMAPPING_TOOL_AND_COMMAND(eUVMappingTool_Cube
, eUVMappingToolGroup_Unwrapping
, "Cube", CubeUnwrappingTool
,
84 cube_unwrmapping
, "runs cube unwrapping tool", "uvmapping.cube_unwrapping")