!XT (BREAK-16) (Sandbox) Remove double-newlines at the end of files.
[CRYENGINE.git] / Code / Sandbox / Plugins / CryDesigner / UVMappingEditor / UnwrappingTools / CubeUnwrappingTool.cpp
blobd1e2e4ca36d294a1d24fd8e955268f6d9f76cd19
1 // Copyright 2001-2018 Crytek GmbH / Crytek Group. All rights reserved.
3 #include "StdAfx.h"
4 #include "CubeUnwrappingTool.h"
5 #include "Util/ElementSet.h"
6 #include "Core/UVIslandManager.h"
7 #include "../UVMappingEditor.h"
8 #include "../UVUndo.h"
9 #include "Util/UVUnwrapUtil.h"
11 namespace Designer {
12 namespace UVMapping
14 void CubeUnwrappingTool::Enter()
16 if (!GetUVEditor()->GetModel())
17 return;
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)
24 return;
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())
37 continue;
39 float fBiggestCos = -1.5f;
40 int axisIndex = -1;
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)
49 fBiggestCos = fCos;
50 axisIndex = i;
54 DESIGNER_ASSERT(axisIndex != -1);
55 if (axisIndex == -1)
56 continue;
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")