-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathExplodeEffect.hpp
More file actions
199 lines (172 loc) · 6.46 KB
/
ExplodeEffect.hpp
File metadata and controls
199 lines (172 loc) · 6.46 KB
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#include <OpenMfx/Sdk/Cpp/Plugin/MfxEffect>
#include "utils.hpp"
#include <cmath>
/**
* Copy each face (potentially several times) and translate it along its normal
*/
class ExplodeEffect : public MfxEffect {
public:
const char* GetName() override {
return "Explode";
}
protected:
OfxStatus Describe(OfxMeshEffectHandle) override {
AddInput(kOfxMeshMainInput)
.RequestCornerAttribute(
"normal",
3, MfxAttributeType::Float,
MfxAttributeSemantic::Normal,
false /* not mandatory */
);
AddInput(kOfxMeshMainOutput);
// Number of duplicates of each faces
AddParam("iterations", 3)
.Label("Iterations")
.Range(0, 20);
// Translation added at each copy of a face
AddParam("offset", 0.5)
.Label("Offset")
.Range(-10.0, 10.0);
// Scale applied to each copy of a face
AddParam("inset", 0.9)
.Label("Inset Factor");
return kOfxStatOK;
}
OfxStatus Cook(OfxMeshEffectHandle) override {
MfxMesh input_mesh = GetInput(kOfxMeshMainInput).GetMesh();
MfxMeshProps input_props;
input_mesh.FetchProperties(input_props);
int iterations = GetParam<int>("iterations").GetValue();
float offset = static_cast<float>(GetParam<double>("offset").GetValue());
float inset = static_cast<float>(GetParam<double>("inset").GetValue());
bool use_corner_normals = true;
MfxAttributeProps input_normal;
if (input_mesh.HasCornerAttribute("normal")) {
input_mesh.GetCornerAttribute("normal").FetchProperties(input_normal);
if (input_normal.componentCount < 3 || input_normal.type != MfxAttributeType::Float) {
use_corner_normals = false; // incompatible type, so we deactivate this feature
}
} else {
use_corner_normals = false; // no normals available, so we will compute them from faces
}
MfxMesh output_mesh = GetInput(kOfxMeshMainOutput).GetMesh();
output_mesh.Allocate(
input_props.pointCount + input_props.cornerCount * iterations,
input_props.cornerCount * (iterations + 1),
input_props.faceCount * (iterations + 1),
input_props.noLooseEdge,
input_props.constantFaceSize);
MfxAttributeProps input_positions, input_corner_points, input_face_size;
input_mesh.GetPointAttribute(kOfxMeshAttribPointPosition).FetchProperties(input_positions);
input_mesh.GetCornerAttribute(kOfxMeshAttribCornerPoint).FetchProperties(input_corner_points);
if (input_props.constantFaceSize == -1) {
input_mesh.GetFaceAttribute(kOfxMeshAttribFaceSize).FetchProperties(input_face_size);
}
MfxAttributeProps output_positions, output_corner_points, output_face_counts;
output_mesh.GetPointAttribute(kOfxMeshAttribPointPosition).FetchProperties(output_positions);
output_mesh.GetCornerAttribute(kOfxMeshAttribCornerPoint).FetchProperties(output_corner_points);
output_mesh.GetFaceAttribute(kOfxMeshAttribFaceSize).FetchProperties(output_face_counts);
// First copy the original points
for (int i = 0; i < input_props.pointCount; ++i) {
float* ipos = input_positions.at<float>(i);
float* opos = output_positions.at<float>(i);
opos[0] = ipos[0];
opos[1] = ipos[1];
opos[2] = ipos[2];
}
// Then add points at corners
float totalInset = 1.0;
for (int k = 0; k < iterations; ++k) {
totalInset *= inset;
int opointIndexOffset = input_props.pointCount + input_props.cornerCount * k;
int i = 0;
for (int f = 0; f < input_props.faceCount; ++f) {
int cornerCount = (
input_props.constantFaceSize >= 0
? input_props.constantFaceSize
: *input_face_size.at<int>(f)
);
// Compute barycenter and normal vector
double3 faceNormal = double3{ 0.0, 0.0, 1.0 };
double3 barycenter = double3{ 0.0, 0.0, 0.0 };
double3 p0{};
double3 p1{};
for (int j = 0; j < cornerCount; ++j) {
int* ipointIndex = input_corner_points.at<int>(i + j);
float* ipos = input_positions.at<float>(*ipointIndex);
barycenter[0] += ipos[0];
barycenter[1] += ipos[1];
barycenter[2] += ipos[2];
if (j == 0) {
p0[0] = ipos[0]; p0[1] = ipos[1]; p0[2] = ipos[2];
}
else if (j == 1) {
p1[0] = ipos[0]; p1[1] = ipos[1]; p1[2] = ipos[2];
}
else if (j == 2) {
double3 d0 = { p1[0] - p0[0], p1[1] - p0[1], p1[2] - p0[2] };
double3 d1 = { ipos[0] - p0[0], ipos[1] - p0[1], ipos[2] - p0[2] };
faceNormal[0] = d0[1] * d1[2] - d0[2] * d1[1];
faceNormal[1] = d0[2] * d1[0] - d0[0] * d1[2];
faceNormal[2] = d0[0] * d1[1] - d0[1] * d1[0];
double length = std::sqrt(faceNormal[0] * faceNormal[0] + faceNormal[1] * faceNormal[1] + faceNormal[2] * faceNormal[2]);
faceNormal[0] /= length;
faceNormal[1] /= length;
faceNormal[2] /= length;
}
}
barycenter[0] /= cornerCount;
barycenter[1] /= cornerCount;
barycenter[2] /= cornerCount;
// iterate over the same corners, this time to create output points
for (int j = 0; j < cornerCount; ++j, ++i) {
int* ipointIndex = input_corner_points.at<int>(i);
float* ipos = input_positions.at<float>(*ipointIndex);
double3 normal = faceNormal;
if (use_corner_normals) {
float* cornerNormal = input_normal.at<float>(i);
normal = double3{ cornerNormal[0], cornerNormal[1], cornerNormal[2] };
}
float* opos = output_positions.at<float>(opointIndexOffset + i);
for (int c = 0; c < 3; ++c) {
opos[c] = static_cast<float>(
(ipos[c] - barycenter[c]) * totalInset
+ barycenter[c]
+ offset * (k + 1) * normal[c]
);
}
}
}
}
for (int i = 0; i < input_props.cornerCount; ++i) {
int* ipointIndex = input_corner_points.at<int>(i);
int* opointIndex = output_corner_points.at<int>(i);
opointIndex[0] = ipointIndex[0];
}
for (int k = 0; k < iterations; ++k) {
int pointIndexOffset = input_props.pointCount + input_props.cornerCount * k;
int ocornerIndexOffset = (k + 1) * input_props.cornerCount;
for (int i = 0; i < input_props.cornerCount; ++i) {
int* opointIndex = output_corner_points.at<int>(ocornerIndexOffset + i);
opointIndex[0] = pointIndexOffset + i;
}
}
if (input_props.constantFaceSize < 0) {
for (int k = 0; k < iterations + 1; ++k) {
int ofaceIndexOffset = k * input_props.faceCount;
for (int i = 0; i < input_props.faceCount; ++i) {
int icount = (
input_props.constantFaceSize >= 0
? input_props.constantFaceSize
: *input_face_size.at<int>(i)
);
int* ocount = output_face_counts.at<int>(ofaceIndexOffset + i);
ocount[0] = icount;
}
}
}
output_mesh.Release();
input_mesh.Release();
return kOfxStatOK;
}
};