/
MaterialPreview.cpp
131 lines (100 loc) · 3.23 KB
/
MaterialPreview.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#include "MaterialPreview.h"
#include "ibrush.h"
#include "ientity.h"
#include "ieclass.h"
#include "wxutil/dialog/MessageBox.h"
namespace ui
{
namespace
{
const char* const FUNC_STATIC_CLASS = "func_static";
inline scene::INodePtr createCubicBrush(const scene::INodePtr& parent,
const Vector3& origin = Vector3(0, 0, 0),
const std::string& material = "_default")
{
auto brushNode = GlobalBrushCreator().createBrush();
parent->addChildNode(brushNode);
auto& brush = *Node_getIBrush(brushNode);
auto translation = Matrix4::getTranslation(origin);
brush.addFace(Plane3(+1, 0, 0, 64).transform(translation));
brush.addFace(Plane3(-1, 0, 0, 64).transform(translation));
brush.addFace(Plane3(0, +1, 0, 64).transform(translation));
brush.addFace(Plane3(0, -1, 0, 64).transform(translation));
brush.addFace(Plane3(0, 0, +1, 64).transform(translation));
brush.addFace(Plane3(0, 0, -1, 64).transform(translation));
brush.setShader(material);
brush.evaluateBRep();
return brushNode;
}
}
MaterialPreview::MaterialPreview(wxWindow* parent) :
RenderPreview(parent, true),
_sceneIsReady(false),
_defaultCamDistanceFactor(2.8f)
{}
const MaterialPtr& MaterialPreview::getMaterial()
{
return _material;
}
void MaterialPreview::setMaterial(const MaterialPtr& material)
{
_material = material;
_sceneIsReady = false;
if (_brush)
{
auto& brush = *Node_getIBrush(_brush);
for (auto i = 0; i < brush.getNumFaces(); ++i)
{
brush.getFace(i).setShader(_material ? _material->getName() : "");
brush.getFace(i).fitTexture(1, 1);
}
}
queueDraw();
}
bool MaterialPreview::onPreRender()
{
if (!_sceneIsReady)
{
prepareScene();
}
return RenderPreview::onPreRender();
}
void MaterialPreview::prepareScene()
{
_sceneIsReady = true;
if (!_brush) return;
// Reset the default view, facing down to the model from diagonally above the bounding box
double distance = _brush->localAABB().getRadius() * _defaultCamDistanceFactor;
setViewOrigin(Vector3(1, 1, 1) * distance);
setViewAngles(Vector3(34, 135, 0));
}
bool MaterialPreview::canDrawGrid()
{
return false;
}
void MaterialPreview::setupSceneGraph()
{
RenderPreview::setupSceneGraph();
try
{
_rootNode = std::make_shared<scene::BasicRootNode>();
_entity = GlobalEntityModule().createEntity(
GlobalEntityClassManager().findClass(FUNC_STATIC_CLASS));
_rootNode->addChildNode(_entity);
// Set up a brush
_brush = createCubicBrush(_entity);
getScene()->setRoot(_rootNode);
// Set up the light
_light = GlobalEntityModule().createEntity(
GlobalEntityClassManager().findClass("light"));
Node_getEntity(_light)->setKeyValue("light_radius", "600 600 600");
Node_getEntity(_light)->setKeyValue("origin", "0 0 300");
_rootNode->addChildNode(_light);
}
catch (std::runtime_error&)
{
wxutil::Messagebox::ShowError(fmt::format(_("Unable to setup the preview,\n"
"could not find the entity class {0}"), FUNC_STATIC_CLASS));
}
}
}