14 #ifndef hifi_EntityTreeRenderer_h
15 #define hifi_EntityTreeRenderer_h
19 #include <QtCore/QSet>
20 #include <QtCore/QStack>
21 #include <QtGui/QMouseEvent>
22 #include <QtCore/QSharedPointer>
24 #include <AudioInjectorManager.h>
25 #include <EntityScriptingInterface.h>
26 #include <EntityTree.h>
27 #include <PointerEvent.h>
28 #include <ScriptCache.h>
29 #include <TextureCache.h>
30 #include <OctreeProcessor.h>
31 #include <render/Forward.h>
32 #include <workload/Space.h>
42 using ScriptEnginePointer = std::shared_ptr<ScriptEngine>;
43 using ScriptManagerPointer = std::shared_ptr<ScriptManager>;
45 namespace render {
namespace entities {
47 using EntityRendererPointer = std::shared_ptr<EntityRenderer>;
48 using EntityRendererWeakPointer = std::weak_ptr<EntityRenderer>;
52 using EntityRenderer = render::entities::EntityRenderer;
53 using EntityRendererPointer = render::entities::EntityRendererPointer;
54 using EntityRendererWeakPointer = render::entities::EntityRendererWeakPointer;
56 using ModelPointer = std::shared_ptr<Model>;
57 using ModelWeakPointer = std::weak_ptr<Model>;
59 using CalculateEntityLoadingPriority = std::function<float(
const EntityItem& item)>;
62 class EntityTreeRenderer :
public OctreeProcessor,
public Dependency {
65 static void setEntitiesShouldFadeFunction(std::function<
bool()> func) { _entitiesShouldFadeFunction = func; }
66 static std::function<bool()> getEntitiesShouldFadeFunction() {
return _entitiesShouldFadeFunction; }
70 virtual ~EntityTreeRenderer();
72 QSharedPointer<EntityTreeRenderer> getSharedFromThis() {
73 return qSharedPointerCast<EntityTreeRenderer>(sharedFromThis());
76 virtual char getMyNodeType()
const override {
return NodeType::EntityServer; }
77 virtual PacketType getMyQueryMessageType()
const override {
return PacketType::EntityQuery; }
78 virtual PacketType getExpectedPacketType()
const override {
return PacketType::EntityData; }
81 static CalculateEntityLoadingPriority getEntityLoadingPriorityOperator() {
return _calculateEntityLoadingPriorityFunc; }
82 static float getEntityLoadingPriority(
const EntityItem& item) {
return _calculateEntityLoadingPriorityFunc(item); }
83 static void setEntityLoadingPriorityFunction(CalculateEntityLoadingPriority fn) { _calculateEntityLoadingPriorityFunc = fn; }
85 void setMouseRayPickID(
unsigned int rayPickID) { _mouseRayPickID = rayPickID; }
86 unsigned int getMouseRayPickID() {
return _mouseRayPickID; }
87 void setMouseRayPickResultOperator(std::function<RayToEntityIntersectionResult(
unsigned int)> getPrevRayPickResultOperator) { _getPrevRayPickResultOperator = getPrevRayPickResultOperator; }
88 void setSetPrecisionPickingOperator(std::function<
void(
unsigned int,
bool)> setPrecisionPickingOperator) { _setPrecisionPickingOperator = setPrecisionPickingOperator; }
92 void update(
bool simulate);
94 EntityTreePointer getTree() {
return std::static_pointer_cast<EntityTree>(_tree); }
96 void processEraseMessage(ReceivedMessage& message,
const SharedNodePointer& sourceNode);
98 virtual void init()
override;
101 virtual void clearDomainAndNonOwnedEntities()
override;
102 virtual void clear()
override;
105 void reloadEntityScripts();
107 void fadeOutRenderable(
const EntityRendererPointer& renderable);
110 QUuid mousePressEvent(QMouseEvent* event);
111 void mouseReleaseEvent(QMouseEvent* event);
112 void mouseDoublePressEvent(QMouseEvent* event);
113 void mouseMoveEvent(QMouseEvent* event);
117 void connectSignalsToSlots(EntityScriptingInterface* entityScriptingInterface);
120 QList<EntityItemID>& getEntitiesLastInScene() {
return _entityIDsLastInScene; }
122 std::pair<bool, bool> getZoneInteractionProperties();
126 bool wantsHandControllerPointerEvents(
const EntityItemID&
id)
const;
127 void setProxyWindow(
const EntityItemID&
id, QWindow* proxyWindow);
128 void setCollisionSound(
const EntityItemID&
id,
const SharedSoundPointer& sound);
134 workload::SpacePointer getWorkloadSpace()
const {
return _space; }
138 static void setAddMaterialToEntityOperator(std::function<
bool(
const QUuid&, graphics::MaterialLayer,
const std::string&)> addMaterialToEntityOperator) { _addMaterialToEntityOperator = addMaterialToEntityOperator; }
139 static void setRemoveMaterialFromEntityOperator(std::function<
bool(
const QUuid&, graphics::MaterialPointer,
const std::string&)> removeMaterialFromEntityOperator) { _removeMaterialFromEntityOperator = removeMaterialFromEntityOperator; }
140 static bool addMaterialToEntity(
const QUuid& entityID, graphics::MaterialLayer material,
const std::string& parentMaterialName);
141 static bool removeMaterialFromEntity(
const QUuid& entityID, graphics::MaterialPointer material,
const std::string& parentMaterialName);
143 static void setAddMaterialToAvatarOperator(std::function<
bool(
const QUuid&, graphics::MaterialLayer,
const std::string&)> addMaterialToAvatarOperator) { _addMaterialToAvatarOperator = addMaterialToAvatarOperator; }
144 static void setRemoveMaterialFromAvatarOperator(std::function<
bool(
const QUuid&, graphics::MaterialPointer,
const std::string&)> removeMaterialFromAvatarOperator) { _removeMaterialFromAvatarOperator = removeMaterialFromAvatarOperator; }
145 static bool addMaterialToAvatar(
const QUuid& avatarID, graphics::MaterialLayer material,
const std::string& parentMaterialName);
146 static bool removeMaterialFromAvatar(
const QUuid& avatarID, graphics::MaterialPointer material,
const std::string& parentMaterialName);
148 size_t getPrevNumEntityUpdates()
const {
return _prevNumEntityUpdates; }
149 size_t getPrevTotalNeededEntityUpdates()
const {
return _prevTotalNeededEntityUpdates; }
159 void entityScriptChanging(
const EntityItemID& entityID,
const bool reload);
161 void updateEntityRenderStatus(
bool shouldRenderEntities);
165 void setDisplayModelBounds(
bool value) { _displayModelBounds = value; }
166 void setPrecisionPicking(
bool value) { _setPrecisionPickingOperator(_mouseRayPickID, value); }
167 EntityRendererPointer renderableForEntityId(
const EntityItemID&
id)
const;
168 render::ItemID renderableIdForEntityId(
const EntityItemID&
id)
const;
170 void handleSpaceUpdate(std::pair<int32_t, glm::vec4> proxyUpdate);
173 virtual OctreePointer createTree()
override {
174 EntityTreePointer newTree = std::make_shared<EntityTree>(
true);
175 newTree->createRootElement();
180 void addPendingEntities(
const render::ScenePointer& scene, render::Transaction& transaction);
181 void updateChangedEntities(
const render::ScenePointer& scene, render::Transaction& transaction);
182 EntityRendererPointer renderableForEntity(
const EntityItemPointer& entity)
const {
return renderableForEntityId(entity->getID()); }
183 render::ItemID renderableIdForEntity(
const EntityItemPointer& entity)
const {
return renderableIdForEntityId(entity->getID()); }
185 void resetPersistentEntitiesScriptEngine();
186 void resetNonPersistentEntitiesScriptEngine();
187 void setupEntityScriptEngineSignals(
const ScriptManagerPointer& scriptManager);
189 void findBestZoneAndMaybeContainingEntities(QSet<EntityItemID>& entitiesContainingAvatar);
191 bool applyLayeredZones();
192 void stopDomainAndNonOwnedEntities();
194 void checkAndCallPreload(
const EntityItemID& entityID,
bool reload =
false,
bool unloadFirst =
false);
199 ScriptValueList createEntityArgs(
const EntityItemID& entityID);
200 void checkEnterLeaveEntities();
201 void leaveDomainAndNonOwnedEntities();
202 void leaveAllEntities();
203 void forceRecheckEntities();
205 glm::vec3 _avatarPosition { 0.0f };
206 bool _forceRecheckEntities {
true };
207 QSet<EntityItemID> _currentEntitiesInside;
210 ScriptManagerPointer _nonPersistentEntitiesScriptManager;
211 ScriptManagerPointer _persistentEntitiesScriptManager;
213 void playEntityCollisionSound(
const EntityItemPointer& entity,
const Collision& collision);
215 bool _lastPointerEventValid;
219 bool _displayModelBounds;
221 bool _shuttingDown {
false };
223 QMultiMap<QUrl, EntityItemID> _waitingOnPreload;
225 unsigned int _mouseRayPickID;
226 std::function<RayToEntityIntersectionResult(
unsigned int)> _getPrevRayPickResultOperator;
227 std::function<void(
unsigned int,
bool)> _setPrecisionPickingOperator;
231 LayeredZone(std::shared_ptr<ZoneEntityItem> zone) : zone(zone), id(zone->getID()), volume(zone->getVolumeEstimate()) {}
234 bool operator<(
const LayeredZone& r)
const {
return volume < r.volume || (volume == r.volume &&
id < r.id); }
235 bool operator==(
const LayeredZone& r)
const {
return zone.lock() && zone.lock() == r.zone.lock(); }
236 bool operator!=(
const LayeredZone& r)
const {
return !(*
this == r); }
237 bool operator<=(
const LayeredZone& r)
const {
return (*
this < r) || (*
this == r); }
239 std::weak_ptr<ZoneEntityItem> zone;
244 class LayeredZones :
public std::vector<LayeredZone> {
246 bool clearDomainAndNonOwnedZones();
248 void sort() { std::sort(begin(), end(), std::less<LayeredZone>()); }
249 bool equals(
const LayeredZones& other)
const;
250 bool update(std::shared_ptr<ZoneEntityItem> zone,
const glm::vec3& position, EntityTreeRenderer* entityTreeRenderer);
252 void appendRenderIDs(render::ItemIDs& list, EntityTreeRenderer* entityTreeRenderer)
const;
253 std::pair<bool, bool> getZoneInteractionProperties()
const;
256 LayeredZones _layeredZones;
257 uint64_t _lastZoneCheck { 0 };
258 const uint64_t ZONE_CHECK_INTERVAL = USECS_PER_MSEC * 100;
259 const float ZONE_CHECK_DISTANCE = 0.001f;
261 float _avgRenderableUpdateCost { 0.0f };
263 ReadWriteLockable _changedEntitiesGuard;
264 std::unordered_set<EntityItemID> _changedEntities;
265 size_t _prevNumEntityUpdates { 0 };
266 size_t _prevTotalNeededEntityUpdates { 0 };
268 std::unordered_set<EntityRendererPointer> _renderablesToUpdate;
269 std::unordered_map<EntityItemID, EntityRendererPointer> _entitiesInScene;
270 std::unordered_map<EntityItemID, EntityItemWeakPointer> _entitiesToAdd;
273 QList<EntityItemID> _entityIDsLastInScene;
275 static int _entitiesScriptEngineCount;
276 static CalculateEntityLoadingPriority _calculateEntityLoadingPriorityFunc;
277 static std::function<bool()> _entitiesShouldFadeFunction;
279 mutable std::mutex _spaceLock;
280 workload::SpacePointer _space{
new workload::Space() };
281 workload::Transaction::Updates _spaceUpdates;
283 static std::function<bool(
const QUuid&, graphics::MaterialLayer,
const std::string&)> _addMaterialToEntityOperator;
284 static std::function<bool(
const QUuid&, graphics::MaterialPointer,
const std::string&)> _removeMaterialFromEntityOperator;
285 static std::function<bool(
const QUuid&, graphics::MaterialLayer,
const std::string&)> _addMaterialToAvatarOperator;
286 static std::function<bool(
const QUuid&, graphics::MaterialPointer,
const std::string&)> _removeMaterialFromAvatarOperator;
Interface provided by Application to other objects that need access to scripting services of the appl...
Definition: AbstractScriptingServicesInterface.h:26
Interface provided by Application to other objects that need access to the current view state details...
Definition: AbstractViewStateInterface.h:31
Utility for processing, packing, queueing and sending of outbound edit voxel messages.
Definition: EntityEditPacketSender.h:25
Definition: EntityItem.h:82
Abstract ID for editing model items. Used in EntityItem JS API.
Definition: EntityItemID.h:28
A generic 3D model displaying geometry loaded from a URL.
Definition: Model.h:84
Represents a 2D or 3D pointer to the scripting engine. Exposed as PointerEvent
Definition: PointerEvent.h:30
Provides an engine-independent interface for a scripting engine.
Definition: ScriptEngine.h:93
Manages a single scripting engine.
Definition: ScriptManager.h:281