22 #include <unordered_map>
23 #include <unordered_set>
27 #include <DependencyManager.h>
28 #include <GeometryUtil.h>
29 #include <gpu/Batch.h>
30 #include <render/Forward.h>
31 #include <render/Scene.h>
32 #include <graphics-scripting/Forward.h>
33 #include <Transform.h>
34 #include <SpatiallyNestable.h>
35 #include <TriangleSet.h>
36 #include <DualQuaternion.h>
38 #include "RenderHifi.h"
39 #include "GeometryCache.h"
40 #include "TextureCache.h"
42 #include "PrimitiveMode.h"
43 #include "BillboardMode.h"
56 typedef unsigned int ItemID;
58 class MeshPartPayload;
59 class ModelMeshPartPayload;
60 class ModelRenderLocations;
62 inline uint qHash(
const std::shared_ptr<MeshPartPayload>& a, uint seed) {
63 return qHash(a.get(), seed);
67 using ModelPointer = std::shared_ptr<Model>;
68 using ModelWeakPointer = std::weak_ptr<Model>;
70 struct SortedTriangleSet {
71 SortedTriangleSet(
float distance, TriangleSet* triangleSet,
int partIndex,
int shapeID,
int subMeshIndex) :
72 distance(distance), triangleSet(triangleSet), partIndex(partIndex), shapeID(shapeID), subMeshIndex(subMeshIndex) {}
75 TriangleSet* triangleSet;
81 using BlendShapeOperator = std::function<void(
int,
const QVector<BlendshapeOffset>&,
const QVector<int>&,
const render::ItemIDs&)>;
84 class Model :
public QObject,
public std::enable_shared_from_this<Model>,
public scriptable::ModelProvider {
89 typedef RenderArgs::RenderMode RenderMode;
93 Model(QObject* parent =
nullptr, SpatiallyNestable* spatiallyNestableOverride =
nullptr, uint64_t created = 0);
96 inline ModelPointer getThisPointer()
const {
97 return std::static_pointer_cast<Model>(std::const_pointer_cast<Model>(shared_from_this()));
102 Q_INVOKABLE
virtual void setURL(
const QUrl& url);
103 const QUrl& getURL()
const {
return _url; }
106 void setVisibleInScene(
bool isVisible,
const render::ScenePointer& scene =
nullptr);
107 bool isVisible()
const;
109 render::hifi::Tag getTagMask()
const;
110 void setTagMask(uint8_t mask,
const render::ScenePointer& scene =
nullptr);
112 bool isGroupCulled()
const;
113 void setGroupCulled(
bool isGroupCulled,
const render::ScenePointer& scene =
nullptr);
115 bool canCastShadow()
const;
116 void setCanCastShadow(
bool canCastShadow,
const render::ScenePointer& scene =
nullptr);
118 void setHifiRenderLayer(render::hifi::Layer layer,
const render::ScenePointer& scene =
nullptr);
120 bool isCauterized()
const {
return _cauterized; }
121 void setCauterized(
bool value,
const render::ScenePointer& scene =
nullptr);
123 void setPrimitiveMode(PrimitiveMode primitiveMode,
const render::ScenePointer& scene =
nullptr);
124 PrimitiveMode getPrimitiveMode()
const {
return _primitiveMode; }
126 void setBillboardMode(BillboardMode billboardMode,
const render::ScenePointer& scene =
nullptr);
127 BillboardMode getBillboardMode()
const {
return _billboardMode; }
129 void setCullWithParent(
bool value,
const render::ScenePointer& scene =
nullptr);
131 void setRenderWithZones(
const QVector<QUuid>& renderWithZones,
const render::ScenePointer& scene =
nullptr);
132 const QVector<QUuid>& getRenderWithZones()
const {
return _renderWithZones; }
135 const render::ItemKey getRenderItemKeyGlobalFlags()
const;
137 bool needsFixupInScene()
const;
139 bool needsReload()
const {
return _needsReload; }
140 bool addToScene(
const render::ScenePointer& scene,
141 render::Transaction& transaction) {
142 auto getters = render::Item::Status::Getters(0);
143 return addToScene(scene, transaction, getters);
145 bool addToScene(
const render::ScenePointer& scene,
146 render::Transaction& transaction,
147 BlendShapeOperator modelBlendshapeOperator) {
148 auto getters = render::Item::Status::Getters(0);
149 return addToScene(scene, transaction, getters, modelBlendshapeOperator);
151 bool addToScene(
const render::ScenePointer& scene,
152 render::Transaction& transaction,
153 render::Item::Status::Getters& statusGetters,
154 BlendShapeOperator modelBlendshapeOperator =
nullptr);
155 void removeFromScene(
const render::ScenePointer& scene, render::Transaction& transaction);
156 bool isRenderable()
const;
158 void updateRenderItemsKey(
const render::ScenePointer& scene);
160 virtual void updateRenderItems();
161 void setRenderItemsNeedUpdate();
162 bool getRenderItemsNeedUpdate() {
return _renderItemsNeedUpdate; }
163 AABox getRenderableMeshBound()
const;
164 const render::ItemIDs& fetchRenderItemIDs()
const;
166 bool maybeStartBlender();
168 bool isLoaded()
const {
return (
bool)_renderGeometry && _renderGeometry->isHFMModelLoaded(); }
169 bool isAddedToScene()
const {
return _addedToScene; }
173 void setSnapModelToRegistrationPoint(
bool snapModelToRegistrationPoint,
const glm::vec3& registrationPoint);
177 virtual void simulate(
float deltaTime,
bool fullUpdate =
true);
178 virtual void updateClusterMatrices();
179 virtual void updateBlendshapes();
182 const Geometry::Pointer&
getGeometry()
const {
return _renderGeometry; }
184 const QVariantMap getTextures()
const { assert(isLoaded());
return _renderGeometry->getTextures(); }
185 Q_INVOKABLE
virtual void setTextures(
const QVariantMap& textures);
190 const MaterialMapping& getMaterialMapping()
const { assert(isLoaded());
return _renderGeometry->getMaterialMapping(); }
192 bool didVisualGeometryRequestFail()
const {
return _visualGeometryRequestFailed; }
194 glm::mat4 getWorldToHFMMatrix()
const;
196 QStringList getJointNames()
const;
199 void setJointState(
int index,
bool valid,
const glm::quat& rotation,
const glm::vec3& translation,
float priority);
200 void setJointRotation(
int index,
bool valid,
const glm::quat& rotation,
float priority);
201 void setJointTranslation(
int index,
bool valid,
const glm::vec3& translation,
float priority);
203 bool findRayIntersectionAgainstSubMeshes(
const glm::vec3& origin,
const glm::vec3& direction,
const glm::vec3& viewFrustumPos,
204 float& distance, BoxFace& face, glm::vec3& surfaceNormal,
205 QVariantMap& extraInfo,
bool pickAgainstTriangles =
false,
bool allowBackface =
false);
206 bool findParabolaIntersectionAgainstSubMeshes(
const glm::vec3& origin,
const glm::vec3& velocity,
const glm::vec3& acceleration,
207 const glm::vec3& viewFrustumPos,
float& parabolicDistance, BoxFace& face, glm::vec3& surfaceNormal,
208 QVariantMap& extraInfo,
bool pickAgainstTriangles =
false,
bool allowBackface =
false);
210 void setOffset(
const glm::vec3& offset);
211 const glm::vec3& getOffset()
const {
return _offset; }
212 glm::vec3 getOriginalOffset()
const;
214 void setScaleToFit(
bool scaleToFit,
float largestDimension = 0.0f,
bool forceRescale =
false);
215 void setScaleToFit(
bool scaleToFit,
const glm::vec3& dimensions,
bool forceRescale =
false);
216 bool getScaleToFit()
const {
return _scaleToFit; }
219 setSnapModelToRegistrationPoint(snapModelToCenter, glm::vec3(0.5f,0.5f,0.5f));
221 bool getSnapModelToCenter() {
227 bool getJointPositionInWorldFrame(
int jointIndex, glm::vec3& position)
const;
228 bool getJointRotationInWorldFrame(
int jointIndex, glm::quat& rotation)
const;
234 bool getJointTranslation(
int jointIndex, glm::vec3& translation)
const;
237 bool getAbsoluteJointRotationInRigFrame(
int jointIndex, glm::quat& rotationOut)
const;
238 bool getAbsoluteJointTranslationInRigFrame(
int jointIndex, glm::vec3& translationOut)
const;
240 bool getRelativeDefaultJointRotation(
int jointIndex, glm::quat& rotationOut)
const;
241 bool getRelativeDefaultJointTranslation(
int jointIndex, glm::vec3& translationOut)
const;
255 void setTranslation(
const glm::vec3& translation);
256 void setRotation(
const glm::quat& rotation);
257 void overrideModelTransformAndOffset(
const Transform& transform,
const glm::vec3& offset);
258 bool isOverridingModelTransformAndOffset() {
return _overrideModelTransform; };
259 void stopTransformAndOffsetOverride() { _overrideModelTransform =
false; };
260 void setTransformNoUpdateRenderItems(
const Transform& transform);
262 const glm::vec3& getTranslation()
const {
return _translation; }
263 const glm::quat& getRotation()
const {
return _rotation; }
264 const glm::vec3& getOverrideTranslation()
const {
return _overrideTranslation; }
265 const glm::quat& getOverrideRotation()
const {
return _overrideRotation; }
267 glm::vec3 getNaturalDimensions()
const;
269 Transform getTransform()
const;
271 void setScale(
const glm::vec3& scale);
272 const glm::vec3& getScale()
const {
return _scale; }
279 float getBlendshapeCoefficient(
int index)
const {
280 return ((index < 0) && (index >= _blendshapeCoefficients.size())) ? 0.0f : _blendshapeCoefficients.at(index);
283 Rig& getRig() {
return _rig; }
284 const Rig& getRig()
const {
return _rig; }
289 virtual bool updateGeometry();
291 void setLoadingPriority(
float priority) { _loadingPriority = priority; }
293 size_t getRenderInfoVertexCount()
const {
return _renderInfoVertexCount; }
294 size_t getRenderInfoTextureSize();
295 int getRenderInfoTextureCount();
296 int getRenderInfoDrawCalls()
const {
return _renderInfoDrawCalls; }
297 bool getRenderInfoHasTransparent()
const {
return _renderInfoHasTransparent; }
299 class TransformDualQuaternion {
301 TransformDualQuaternion() {}
302 TransformDualQuaternion(
const glm::mat4& m) {
304 _scale.x = p.scale().x;
305 _scale.y = p.scale().y;
306 _scale.z = p.scale().z;
308 _dq = DualQuaternion(p.rot(), p.trans());
310 TransformDualQuaternion(
const glm::vec3& scale,
const glm::quat& rot,
const glm::vec3& trans) {
315 _dq = DualQuaternion(rot, trans);
317 TransformDualQuaternion(
const Transform& transform) {
318 _scale = glm::vec4(transform.getScale(), 0.0f);
320 _dq = DualQuaternion(transform.getRotation(), transform.getTranslation());
322 glm::vec3 getScale()
const {
return glm::vec3(_scale); }
323 glm::quat getRotation()
const {
return _dq.getRotation(); }
324 glm::vec3 getTranslation()
const {
return _dq.getTranslation(); }
325 glm::mat4 getMatrix()
const {
return createMatFromScaleQuatAndPos(getScale(), getRotation(), getTranslation()); };
327 void setCauterizationParameters(
float cauterizationAmount,
const glm::vec3& cauterizedPosition) {
328 _scale.w = cauterizationAmount;
329 _cauterizedPosition = glm::vec4(cauterizedPosition, 1.0f);
332 glm::vec4 _scale { 1.0f, 1.0f, 1.0f, 0.0f };
334 glm::vec4 _cauterizedPosition { 0.0f, 0.0f, 0.0f, 1.0f };
339 std::vector<TransformDualQuaternion> clusterDualQuaternions;
340 std::vector<glm::mat4> clusterMatrices;
343 const MeshState& getMeshState(
int index) {
return _meshStates.at(index); }
345 const QMap<render::ItemID, render::PayloadPointer>& getRenderItems()
const {
return _modelMeshRenderItemsMap; }
346 BlendShapeOperator getModelBlendshapeOperator()
const {
return _modelBlendshapeOperator; }
348 void renderDebugMeshBoxes(gpu::Batch& batch,
bool forward);
350 int getResourceDownloadAttempts() {
return _renderWatcher.getResourceDownloadAttempts(); }
351 int getResourceDownloadAttemptsRemaining() {
return _renderWatcher.getResourceDownloadAttemptsRemaining(); }
353 Q_INVOKABLE MeshProxyList getMeshes()
const;
354 virtual scriptable::ScriptableModelBase getScriptableModel()
override;
355 virtual bool replaceScriptableModelMeshPart(scriptable::ScriptableModelBasePointer model,
int meshIndex,
int partIndex)
override;
358 void snapToRegistrationPoint();
359 bool getUseDualQuaternionSkinning()
const {
return _useDualQuaternionSkinning; }
360 void setUseDualQuaternionSkinning(
bool value);
362 void addMaterial(graphics::MaterialLayer material,
const std::string& parentMaterialName);
363 void removeMaterial(graphics::MaterialPointer material,
const std::string& parentMaterialName);
365 void setBlendshapeCoefficients(
const QVector<float>& coefficients) { _blendshapeCoefficients = coefficients; }
368 void loadURLFinished(
bool success);
371 void setURLFinished(
bool success);
372 void setCollisionModelURLFinished(
bool success);
373 void requestRenderUpdate();
379 std::unordered_map<unsigned int, quint16> _priorityMap;
380 std::unordered_map<unsigned int, std::vector<graphics::MaterialLayer>> _materialMapping;
381 std::mutex _materialMappingMutex;
382 void applyMaterialMapping();
384 const QVector<float>& getBlendshapeCoefficients()
const {
return _blendshapeCoefficients; }
394 Geometry::Pointer _renderGeometry;
396 GeometryResourceWatcher _renderWatcher;
398 SpatiallyNestable* _spatiallyNestableOverride;
400 glm::vec3 _translation;
402 glm::vec3 _scale { 1.0f };
404 glm::vec3 _overrideTranslation;
405 glm::quat _overrideRotation;
412 static float FAKE_DIMENSION_PLACEHOLDER;
423 std::vector<MeshState> _meshStates;
425 virtual void initJointStates();
427 void setScaleInternal(
const glm::vec3& scale);
429 virtual void updateRig(
float deltaTime, glm::mat4 parentTransform);
433 _triangleSetsValid =
false;
437 virtual void onInvalidate() {};
439 virtual void deleteGeometry();
443 BlendShapeOperator _modelBlendshapeOperator {
nullptr };
444 QVector<float> _blendshapeCoefficients;
445 QVector<float> _blendedBlendshapeCoefficients;
446 int _blendNumber { 0 };
448 mutable QRecursiveMutex _mutex;
450 bool _overrideModelTransform {
false };
451 bool _triangleSetsValid {
false };
452 void calculateTriangleSets(
const HFMModel& hfmModel);
453 std::vector<std::vector<TriangleSet>> _modelSpaceMeshTriangleSets;
455 virtual void createRenderItemSet();
457 PrimitiveMode _primitiveMode { PrimitiveMode::SOLID };
458 BillboardMode _billboardMode { BillboardMode::NONE };
459 bool _useDualQuaternionSkinning {
false };
462 int _debugMeshBoxesID = GeometryCache::UNKNOWN_ID;
466 QVector<std::shared_ptr<ModelMeshPartPayload>> _modelMeshRenderItems;
467 QMap<render::ItemID, render::PayloadPointer> _modelMeshRenderItemsMap;
468 render::ItemIDs _modelMeshRenderItemIDs;
469 using ShapeInfo =
struct {
int meshIndex; };
470 std::vector<ShapeInfo> _modelMeshRenderItemShapes;
471 std::vector<std::string> _modelMeshMaterialNames;
473 bool _addedToScene {
false };
474 bool _needsFixupInScene {
true };
475 bool _needsReload {
true };
476 bool _needsUpdateClusterMatrices {
true };
477 QVariantMap _pendingTextures { };
479 friend class ModelMeshPartPayload;
482 bool _visualGeometryRequestFailed {
false };
484 bool _renderItemsNeedUpdate {
false };
486 size_t _renderInfoVertexCount { 0 };
487 int _renderInfoTextureCount { 0 };
488 size_t _renderInfoTextureSize { 0 };
489 bool _hasCalculatedTextureInfo {
false };
490 int _renderInfoDrawCalls { 0 };
491 int _renderInfoHasTransparent {
false };
502 render::ItemKey _renderItemKeyGlobalFlags;
503 bool _cauterized {
false };
504 bool _cullWithParent {
false };
505 QVector<QUuid> _renderWithZones;
507 bool shouldInvalidatePayloadShapeKey(
int meshIndex);
512 float _loadingPriority { 0.0f };
514 void calculateTextureInfo();
516 std::set<unsigned int> getMeshIDsFromMaterialID(QString parentMaterialName);
519 Q_DECLARE_METATYPE(ModelPointer)
520 Q_DECLARE_METATYPE(Geometry::WeakPointer)
521 Q_DECLARE_METATYPE(BlendshapeOffset)
523 class ModelBlender :
public QObject,
public Dependency {
531 void noteRequiresBlend(ModelPointer model);
533 bool shouldComputeBlendshapes() {
return _computeBlendshapes; }
536 void setBlendedVertices(ModelPointer model,
int blendNumber, QVector<BlendshapeOffset> blendshapeOffsets, QVector<int> blendedMeshSizes);
537 void setComputeBlendshapes(
bool computeBlendshapes) { _computeBlendshapes = computeBlendshapes; }
540 using Mutex = std::mutex;
541 using Lock = std::unique_lock<Mutex>;
544 virtual ~ModelBlender();
546 std::queue<ModelWeakPointer> _modelsRequiringBlendsQueue;
547 std::set<ModelWeakPointer, std::owner_less<ModelWeakPointer>> _modelsRequiringBlendsSet;
548 int _pendingBlenders;
551 bool _computeBlendshapes {
true };
Interface provided by Application to other objects that need access to the current view state details...
Definition: AbstractViewStateInterface.h:31
A generic 3D model displaying geometry loaded from a URL.
Definition: Model.h:84
const HFMModel & getHFMModel() const
Provided as a convenience, will crash if !isLoaded()
Definition: Model.h:189
bool _snappedToRegistrationPoint
is the model's offset automatically adjusted to a registration point in model space
Definition: Model.h:419
glm::vec3 _scaleToFitDimensions
If you set scaleToFit, we will calculate scale based on MeshExtents.
Definition: Model.h:415
bool _forceOffset
the point in model space our center is snapped to
Definition: Model.h:421
Extents getBindExtents() const
Returns the extents of the model in its bind pose.
Definition: Model.cpp:1208
Extents getUnscaledMeshExtents() const
Returns the unscaled extents of the model's mesh.
Definition: Model.cpp:1227
bool getJointPosition(int jointIndex, glm::vec3 &position) const
Definition: Model.cpp:1322
void clearJointState(int index)
Clear the joint states.
Definition: Model.cpp:1243
glm::vec3 getScaleToFitDimensions() const
is model scaled to fit
Definition: Model.cpp:1402
bool getIsScaledToFit() const
enables/disables scale to fit behavior, the model will be automatically scaled to the specified large...
Definition: Model.h:275
const Geometry::Pointer & getGeometry() const
Returns a reference to the shared geometry.
Definition: Model.h:182
bool _snapModelToRegistrationPoint
have we scaled to fit
Definition: Model.h:418
Extents getMeshExtents() const
Returns the extents of the model's mesh.
Definition: Model.cpp:1222
int getBlendshapeCoefficientsNum() const
the dimensions model is scaled to, including inferred y/z
Definition: Model.h:278
void setJointState(int index, bool valid, const glm::quat &rotation, const glm::vec3 &translation, float priority)
Sets the joint state at the specified index.
Definition: Model.cpp:1247
glm::vec3 _registrationPoint
are we currently snapped to a registration point
Definition: Model.h:420
void setSnapModelToCenter(bool snapModelToCenter)
is scale to fit enabled
Definition: Model.h:218
bool getJointRotation(int jointIndex, glm::quat &rotation) const
Definition: Model.cpp:1330
int getJointStateCount() const
Returns the number of joint states in the model.
Definition: Model.h:226
void invalidCalculatedMeshBoxes()
Allow sub classes to force invalidating the bboxes.
Definition: Model.h:432
virtual Q_INVOKABLE void setURL(const QUrl &url)
Sets the URL of the model to render.
Definition: Model.cpp:1273
bool _scaledToFit
this is the dimensions that scale to fit will use
Definition: Model.h:416
int getParentJointIndex(int jointIndex) const
Returns the index of the parent of the indexed joint, or -1 if not found.
Definition: Model.cpp:1259
The runtime model format.
Definition: HFM.h:302