16 this->varDensity()->setRange(1.0f, 10000.0f);
19 this->varLocation()->attach(callback);
20 this->varScale()->attach(callback);
21 this->varRotation()->attach(callback);
23 auto EnvelopeRender = std::make_shared<GLSurfaceVisualModule>();
24 EnvelopeRender->setColor(
Color(0.8f, 0.8f, 0.8f));
25 this->stateEnvelope()->promoteOuput()->connect(EnvelopeRender->inTriangleSet());
27 EnvelopeRender->setVisible(
false);
30 auto texMeshRender = std::make_shared<GLPhotorealisticInstanceRender>();
31 this->stateTextureMesh()->connect(texMeshRender->inTextureMesh());
32 this->stateInstanceTransform()->connect(texMeshRender->inTransform());
36 auto evenlopeLoader = std::make_shared<FCallBackFunc>(
38 std::string name = this->varEnvelopeName()->getValue().string();
43 auto envelope = this->stateEnvelope()->getDataPtr();
47 envelope->scale(this->varScale()->getValue());
48 envelope->rotate(this->varRotation()->getValue() *
M_PI / 180);
49 envelope->translate(this->varLocation()->getValue());
55 evenlopeLoader->update();
57 this->varEnvelopeName()->attach(evenlopeLoader);
59 auto textureMeshLoader = std::make_shared<FCallBackFunc>(
61 std::string filepath = this->varTextureMeshName()->getValue().string();
62 std::shared_ptr<TextureMesh> texMesh = this->stateTextureMesh()->getDataPtr();
67 this->varTextureMeshName()->attach(textureMeshLoader);
69 this->varDensity()->setValue(150.0f);
70 this->varBarycenterOffset()->setValue(
Vec3f(0.0f, 0.0f, -0.5f));
92 if (this->stateEnvelope()->isEmpty()) this->stateEnvelope()->allocate();
93 if (this->stateTextureMesh()->isEmpty()) this->stateTextureMesh()->allocate();
95 std::string envFileName = getAssetPath() +
"obj/boat_boundary.obj";
96 if (this->varEnvelopeName()->getValue() != envFileName) {
97 this->varEnvelopeName()->setValue(
FilePath(envFileName));
100 std::string texMeshName = getAssetPath() +
"gltf/SailBoat/SailBoat.gltf";
101 if (this->varTextureMeshName()->getValue() != texMeshName) {
102 this->varTextureMeshName()->setValue(
FilePath(texMeshName));
107 auto texMesh = this->stateTextureMesh()->constDataPtr();
119 Coord scale = this->varScale()->getValue();
124 Real lx = hi.x - lo.x;
125 Real ly = hi.y - lo.y;
126 Real lz = hi.z - lo.z;
128 Real rho = this->varDensity()->getData();
129 Real mass = rho * lx * ly * lz;
132 Matrix inertia = 1.0f / 12.0f * mass
133 *
Mat3f(ly * ly + lz * lz, 0, 0,
134 0, lx * lx + lz * lz, 0,
135 0, 0, lx * lx + ly * ly);
138 Coord location = this->varLocation()->getValue();
139 Coord rot = this->varRotation()->getValue();
142 auto offset = this->varBarycenterOffset()->getValue();
144 this->stateMass()->setValue(mass);
146 this->stateBarycenter()->setValue(location +
mShapeCenter +
quat.rotate(offset));
147 this->stateVelocity()->setValue(
Vec3f(0));
148 this->stateAngularVelocity()->setValue(
Vec3f(0));
149 this->stateInertia()->setValue(inertia);
150 this->stateQuaternion()->setValue(
quat);
151 this->stateInitialInertia()->setValue(inertia);
162 auto center = this->stateCenter()->getValue();
163 auto quat = this->stateQuaternion()->getValue();
164 auto scale = this->varScale()->getValue();
166 auto offset = this->varBarycenterOffset()->getValue();
168 this->stateBarycenter()->setValue(center +
quat.rotate(offset));
170 auto buoy = this->stateEnvelope()->getDataPtr();
176 auto texMesh = this->stateTextureMesh()->getDataPtr();
179 uint N = texMesh->shapes().size();
182 tms.assign(this->stateInstanceTransform()->constData());
184 for (
uint i = 0; i < tms.size(); i++)
187 for (
uint j = 0; j < list.size(); j++)
189 list[j].translation() = center +
quat.rotate(texMesh->shapes()[i]->boundingTransform.translation() * scale) -
mShapeCenter;
190 list[j].rotation() =
quat.toMatrix3x3();
191 list[j].scale() = scale;
196 auto instantanceTransform = this->stateInstanceTransform()->getDataPtr();
197 instantanceTransform->assign(tms);
208 Coord location = this->varLocation()->getValue();
209 Coord rot = this->varRotation()->getValue();
210 Coord scale = this->varScale()->getValue();
214 auto envelope = this->stateEnvelope()->getDataPtr();
216 envelope->scale(scale);
217 envelope->rotate(
quat);
218 envelope->translate(location);
220 if (this->stateTextureMesh()->isEmpty())
223 auto texMesh = this->stateTextureMesh()->constDataPtr();
225 uint N = texMesh->shapes().size();
230 for (
uint i = 0; i <
N; i++)
232 Transform3f t = texMesh->shapes()[i]->boundingTransform;
234 tms[i].insert(
Transform3f(t.translation() * scale + location,
quat.toMatrix3x3(), t.scale() * scale));
237 if (this->stateInstanceTransform()->isEmpty())
239 this->stateInstanceTransform()->allocate();
242 auto instantanceTransform = this->stateInstanceTransform()->getDataPtr();
243 instantanceTransform->assign(tms);
void updateStates() override