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>
33 #include <FadeProperties.h>
43 using ScriptEnginePointer = std::shared_ptr<ScriptEngine>;
44 using ScriptManagerPointer = std::shared_ptr<ScriptManager>;
46 namespace render {
namespace entities {
48 using EntityRendererPointer = std::shared_ptr<EntityRenderer>;
49 using EntityRendererWeakPointer = std::weak_ptr<EntityRenderer>;
53 using EntityRenderer = render::entities::EntityRenderer;
54 using EntityRendererPointer = render::entities::EntityRendererPointer;
55 using EntityRendererWeakPointer = render::entities::EntityRendererWeakPointer;
57 using ModelPointer = std::shared_ptr<Model>;
58 using ModelWeakPointer = std::weak_ptr<Model>;
60 using CalculateEntityLoadingPriority = std::function<float(
const EntityItem& item)>;
63 class EntityTreeRenderer :
public OctreeProcessor,
public Dependency {
68 virtual ~EntityTreeRenderer();
70 QSharedPointer<EntityTreeRenderer> getSharedFromThis() {
71 return qSharedPointerCast<EntityTreeRenderer>(sharedFromThis());
74 virtual char getMyNodeType()
const override {
return NodeType::EntityServer; }
75 virtual PacketType getMyQueryMessageType()
const override {
return PacketType::EntityQuery; }
76 virtual PacketType getExpectedPacketType()
const override {
return PacketType::EntityData; }
79 static CalculateEntityLoadingPriority getEntityLoadingPriorityOperator() {
return _calculateEntityLoadingPriorityFunc; }
80 static float getEntityLoadingPriority(
const EntityItem& item) {
return _calculateEntityLoadingPriorityFunc(item); }
81 static void setEntityLoadingPriorityFunction(CalculateEntityLoadingPriority fn) { _calculateEntityLoadingPriorityFunc = fn; }
83 void setMouseRayPickID(
unsigned int rayPickID) { _mouseRayPickID = rayPickID; }
84 unsigned int getMouseRayPickID() {
return _mouseRayPickID; }
85 void setMouseRayPickResultOperator(std::function<RayToEntityIntersectionResult(
unsigned int)> getPrevRayPickResultOperator) { _getPrevRayPickResultOperator = getPrevRayPickResultOperator; }
86 void setSetPrecisionPickingOperator(std::function<
void(
unsigned int,
bool)> setPrecisionPickingOperator) { _setPrecisionPickingOperator = setPrecisionPickingOperator; }
90 void update(
bool simulate);
92 EntityTreePointer getTree() {
return std::static_pointer_cast<EntityTree>(_tree); }
94 void processEraseMessage(ReceivedMessage& message,
const SharedNodePointer& sourceNode);
96 virtual void init()
override;
99 virtual void clearDomainAndNonOwnedEntities()
override;
100 virtual void clear()
override;
103 void reloadEntityScripts();
105 void fadeOutRenderable(
const EntityRendererPointer& renderable);
106 FadeProperties getLayeredZoneFadeProperties(
const TransitionType type)
const {
return _layeredZones.getFadeProperties(type); }
109 QUuid mousePressEvent(QMouseEvent* event);
110 void mouseReleaseEvent(QMouseEvent* event);
111 void mouseDoublePressEvent(QMouseEvent* event);
112 void mouseMoveEvent(QMouseEvent* event);
116 void connectSignalsToSlots(EntityScriptingInterface* entityScriptingInterface);
119 QList<EntityItemID>& getEntitiesLastInScene() {
return _entityIDsLastInScene; }
121 std::pair<bool, bool> getZoneInteractionProperties();
125 bool wantsHandControllerPointerEvents(
const EntityItemID&
id)
const;
126 void setProxyWindow(
const EntityItemID&
id, QWindow* proxyWindow);
127 void setCollisionSound(
const EntityItemID&
id,
const SharedSoundPointer& sound);
133 workload::SpacePointer getWorkloadSpace()
const {
return _space; }
137 static void setAddMaterialToEntityOperator(std::function<
bool(
const QUuid&, graphics::MaterialLayer,
const std::string&)> addMaterialToEntityOperator) { _addMaterialToEntityOperator = addMaterialToEntityOperator; }
138 static void setRemoveMaterialFromEntityOperator(std::function<
bool(
const QUuid&, graphics::MaterialPointer,
const std::string&)> removeMaterialFromEntityOperator) { _removeMaterialFromEntityOperator = removeMaterialFromEntityOperator; }
139 static bool addMaterialToEntity(
const QUuid& entityID, graphics::MaterialLayer material,
const std::string& parentMaterialName);
140 static bool removeMaterialFromEntity(
const QUuid& entityID, graphics::MaterialPointer material,
const std::string& parentMaterialName);
142 static void setAddMaterialToAvatarOperator(std::function<
bool(
const QUuid&, graphics::MaterialLayer,
const std::string&)> addMaterialToAvatarOperator) { _addMaterialToAvatarOperator = addMaterialToAvatarOperator; }
143 static void setRemoveMaterialFromAvatarOperator(std::function<
bool(
const QUuid&, graphics::MaterialPointer,
const std::string&)> removeMaterialFromAvatarOperator) { _removeMaterialFromAvatarOperator = removeMaterialFromAvatarOperator; }
144 static bool addMaterialToAvatar(
const QUuid& avatarID, graphics::MaterialLayer material,
const std::string& parentMaterialName);
145 static bool removeMaterialFromAvatar(
const QUuid& avatarID, graphics::MaterialPointer material,
const std::string& parentMaterialName);
147 size_t getPrevNumEntityUpdates()
const {
return _prevNumEntityUpdates; }
148 size_t getPrevTotalNeededEntityUpdates()
const {
return _prevTotalNeededEntityUpdates; }
150 bool layeredZonesHaveFade(
const TransitionType type)
const {
return _layeredZones.hasFade(type); }
153 const QString& oldScriptURL =
"",
154 const QString& newScriptURL =
"");
155 void unloadEntityScript(
const EntityItemID& entityID,
const QString& scriptURL);
156 void updateScriptUserData(
const EntityItemID& entityID,
const QString& scriptURL,
const QString& userData);
166 void entityScriptChanging(
const EntityItemID& entityID,
const QString& oldScriptURL,
const QString& newScriptURL);
168 void updateEntityRenderStatus(
bool shouldRenderEntities);
172 void setDisplayModelBounds(
bool value) { _displayModelBounds = value; }
173 void setPrecisionPicking(
bool value) { _setPrecisionPickingOperator(_mouseRayPickID, value); }
174 EntityRendererPointer renderableForEntityId(
const EntityItemID&
id)
const;
175 render::ItemID renderableIdForEntityId(
const EntityItemID&
id)
const;
177 void handleSpaceUpdate(std::pair<int32_t, glm::vec4> proxyUpdate);
180 virtual OctreePointer createTree()
override {
181 EntityTreePointer newTree = std::make_shared<EntityTree>(
true);
182 newTree->createRootElement();
187 void addPendingEntities(
const render::ScenePointer& scene,
render::Transaction& transaction);
188 void updateChangedEntities(
const render::ScenePointer& scene,
render::Transaction& transaction);
189 EntityRendererPointer renderableForEntity(
const EntityItemPointer& entity)
const {
return renderableForEntityId(entity->getID()); }
190 render::ItemID renderableIdForEntity(
const EntityItemPointer& entity)
const {
return renderableIdForEntityId(entity->getID()); }
192 void resetPersistentEntitiesScriptEngine();
193 void resetNonPersistentEntitiesScriptEngine();
194 void setupEntityScriptEngineSignals(
const ScriptManagerPointer& scriptManager);
196 void findBestZoneAndMaybeContainingEntities(QSet<EntityItemID>& entitiesContainingAvatar);
198 bool applyLayeredZones();
199 void stopDomainAndNonOwnedEntities();
204 ScriptValueList createEntityArgs(
const EntityItemID& entityID);
205 void checkEnterLeaveEntities();
206 void leaveDomainAndNonOwnedEntities();
207 void leaveAllEntities();
208 void forceRecheckEntities();
210 glm::vec3 _avatarPosition { 0.0f };
211 bool _forceRecheckEntities {
true };
212 QSet<EntityItemID> _currentEntitiesInside;
215 ScriptManagerPointer _nonPersistentEntitiesScriptManager;
216 ScriptManagerPointer _persistentEntitiesScriptManager;
218 void playEntityCollisionSound(
const EntityItemPointer& entity,
const Collision& collision);
220 bool _lastPointerEventValid;
224 bool _displayModelBounds;
226 bool _shuttingDown {
false };
228 QMultiMap<QUrl, EntityItemID> _waitingOnPreload;
230 unsigned int _mouseRayPickID;
231 std::function<RayToEntityIntersectionResult(
unsigned int)> _getPrevRayPickResultOperator;
232 std::function<void(
unsigned int,
bool)> _setPrecisionPickingOperator;
236 LayeredZone(std::shared_ptr<ZoneEntityItem> zone) : zone(zone), id(zone->getID()), volume(zone->getVolumeEstimate()) {}
239 bool operator<(
const LayeredZone& r)
const {
return volume < r.volume || (volume == r.volume &&
id < r.id); }
240 bool operator==(
const LayeredZone& r)
const {
return zone.lock() && zone.lock() == r.zone.lock(); }
241 bool operator!=(
const LayeredZone& r)
const {
return !(*
this == r); }
242 bool operator<=(
const LayeredZone& r)
const {
return (*
this < r) || (*
this == r); }
244 std::weak_ptr<ZoneEntityItem> zone;
249 class LayeredZones :
public std::vector<LayeredZone> {
251 bool clearDomainAndNonOwnedZones();
253 void sort() { std::sort(begin(), end(), std::less<LayeredZone>()); }
254 bool equals(
const LayeredZones& other)
const;
255 bool update(std::shared_ptr<ZoneEntityItem> zone,
const glm::vec3& position, EntityTreeRenderer* entityTreeRenderer);
257 void appendRenderIDs(render::ItemIDs& list, EntityTreeRenderer* entityTreeRenderer)
const;
258 std::pair<bool, bool> getZoneInteractionProperties()
const;
259 FadeProperties getFadeProperties(
const TransitionType type)
const;
260 bool hasFade(
const TransitionType type)
const;
263 LayeredZones _layeredZones;
264 uint64_t _lastZoneCheck { 0 };
265 const uint64_t ZONE_CHECK_INTERVAL = USECS_PER_MSEC * 100;
266 const float ZONE_CHECK_DISTANCE = 0.001f;
268 float _avgRenderableUpdateCost { 0.0f };
270 ReadWriteLockable _changedEntitiesGuard;
271 std::unordered_set<EntityItemID> _changedEntities;
272 size_t _prevNumEntityUpdates { 0 };
273 size_t _prevTotalNeededEntityUpdates { 0 };
275 std::unordered_set<EntityRendererPointer> _renderablesToUpdate;
276 std::unordered_map<EntityItemID, EntityRendererPointer> _entitiesInScene;
277 std::unordered_map<EntityItemID, EntityItemWeakPointer> _entitiesToAdd;
280 QList<EntityItemID> _entityIDsLastInScene;
282 static int _entitiesScriptEngineCount;
283 static CalculateEntityLoadingPriority _calculateEntityLoadingPriorityFunc;
285 mutable std::mutex _spaceLock;
286 workload::SpacePointer _space{
new workload::Space() };
287 workload::Transaction::Updates _spaceUpdates;
289 static std::function<bool(
const QUuid&, graphics::MaterialLayer,
const std::string&)> _addMaterialToEntityOperator;
290 static std::function<bool(
const QUuid&, graphics::MaterialPointer,
const std::string&)> _removeMaterialFromEntityOperator;
291 static std::function<bool(
const QUuid&, graphics::MaterialLayer,
const std::string&)> _addMaterialToAvatarOperator;
292 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
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:85
Represents a 2D or 3D pointer to the scripting engine. Exposed as PointerEvent
Definition: PointerEvent.h:32
Provides an engine-independent interface for a scripting engine.
Definition: ScriptEngine.h:93
Manages a single scripting engine.
Definition: ScriptManager.h:282
Transaction is the mechanism to make any change to the scene. Whenever a new item need to be reset,...
Definition: Scene.h:40