12 #ifndef hifi_RenderablePolyVoxEntityItem_h
13 #define hifi_RenderablePolyVoxEntityItem_h
19 #include <PolyVoxCore/SimpleVolume.h>
20 #include <PolyVoxCore/Raycast.h>
22 #include <gpu/Forward.h>
23 #include <gpu/Context.h>
24 #include <graphics/Forward.h>
25 #include <graphics/Geometry.h>
26 #include <TextureCache.h>
27 #include <PolyVoxEntityItem.h>
29 #include "RenderableEntityItem.h"
31 namespace render {
namespace entities {
32 class PolyVoxEntityRenderer;
36 enum class PolyVoxState {
39 UncompressingFinished,
43 BakingMeshNoCompressFinished,
50 QDebug operator<<(QDebug debug, PolyVoxState state);
53 class RenderablePolyVoxEntityItem :
public PolyVoxEntityItem,
public scriptable::ModelProvider {
54 friend class render::entities::PolyVoxEntityRenderer;
58 RenderablePolyVoxEntityItem(
const EntityItemID& entityItemID);
60 virtual ~RenderablePolyVoxEntityItem();
62 void initializePolyVox();
64 using PolyVoxEntityItem::getVoxel;
65 virtual uint8_t getVoxel(
const ivec3& v)
const override;
67 using PolyVoxEntityItem::setVoxel;
68 virtual bool setVoxel(
const ivec3& v, uint8_t toValue)
override;
70 int getOnCount()
const override {
return _onCount; }
72 virtual bool supportsDetailedIntersection()
const override {
return true; }
73 virtual bool findDetailedRayIntersection(
const glm::vec3& origin,
const glm::vec3& direction,
74 const glm::vec3& viewFrustumPos, OctreeElementPointer& element,
float& distance,
75 BoxFace& face, glm::vec3& surfaceNormal,
76 QVariantMap& extraInfo,
bool precisionPicking)
const override;
77 virtual bool findDetailedParabolaIntersection(
const glm::vec3& origin,
const glm::vec3& velocity,
const vec3& accleration,
78 const glm::vec3& viewFrustumPos, OctreeElementPointer& element,
79 float& parabolicDistance, BoxFace& face, glm::vec3& surfaceNormal,
80 QVariantMap& extraInfo,
bool precisionPicking)
const override;
82 virtual void setVoxelData(
const QByteArray& voxelData)
override;
83 virtual void setVoxelVolumeSize(
const glm::vec3& voxelVolumeSize)
override;
84 virtual void setVoxelSurfaceStyle(PolyVoxSurfaceStyle voxelSurfaceStyle)
override;
86 virtual ShapeType getShapeType()
const override;
87 virtual bool isReadyToComputeShape()
const override;
88 virtual void computeShapeInfo(ShapeInfo& info)
override;
91 virtual bool setSphereInVolume(
const vec3& center,
float radius, uint8_t toValue)
override;
92 virtual bool setVoxelInVolume(
const vec3& position, uint8_t toValue)
override;
95 virtual bool setSphere(
const vec3& center,
float radius, uint8_t toValue)
override;
96 virtual bool setCapsule(
const vec3& startWorldCoords,
const vec3& endWorldCoords,
97 float radiusWorldCoords, uint8_t toValue)
override;
98 virtual bool setAll(uint8_t toValue)
override;
99 virtual bool setCuboid(
const vec3& lowPosition,
const vec3& cuboidSize,
int toValue)
override;
101 virtual void setXNNeighborID(
const EntityItemID& xNNeighborID)
override;
102 virtual void setYNNeighborID(
const EntityItemID& yNNeighborID)
override;
103 virtual void setZNNeighborID(
const EntityItemID& zNNeighborID)
override;
105 virtual void setXPNeighborID(
const EntityItemID& xPNeighborID)
override;
106 virtual void setYPNeighborID(
const EntityItemID& yPNeighborID)
override;
107 virtual void setZPNeighborID(
const EntityItemID& zPNeighborID)
override;
109 std::shared_ptr<RenderablePolyVoxEntityItem> getXNNeighbor();
110 std::shared_ptr<RenderablePolyVoxEntityItem> getYNNeighbor();
111 std::shared_ptr<RenderablePolyVoxEntityItem> getZNNeighbor();
112 std::shared_ptr<RenderablePolyVoxEntityItem> getXPNeighbor();
113 std::shared_ptr<RenderablePolyVoxEntityItem> getYPNeighbor();
114 std::shared_ptr<RenderablePolyVoxEntityItem> getZPNeighbor();
116 virtual void setRegistrationPoint(
const glm::vec3& value)
override;
118 void setVoxelsFromData(QByteArray uncompressedData, quint16 voxelXSize, quint16 voxelYSize, quint16 voxelZSize);
119 void forEachVoxelValue(
const ivec3& voxelSize, std::function<
void(
const ivec3&, uint8_t)> thunk);
120 QByteArray volDataToArray(quint16 voxelXSize, quint16 voxelYSize, quint16 voxelZSize)
const;
122 void setMesh(graphics::MeshPointer mesh);
123 void setCollisionPoints(ShapeInfo::PointCollection points, AABox box);
124 PolyVox::SimpleVolume<uint8_t>* getVolData() {
return _volData.get(); }
126 uint8_t getVoxelInternal(
const ivec3& v)
const;
127 bool setVoxelInternal(
const ivec3& v, uint8_t toValue);
128 void setVoxelMarkNeighbors(
int x,
int y,
int z, uint8_t toValue);
130 void compressVolumeDataFinished(
const QByteArray& voxelData);
131 void neighborXEdgeChanged() { withWriteLock([&] { _updateFromNeighborXEdge =
true; }); startUpdates(); }
132 void neighborYEdgeChanged() { withWriteLock([&] { _updateFromNeighborYEdge =
true; }); startUpdates(); }
133 void neighborZEdgeChanged() { withWriteLock([&] { _updateFromNeighborZEdge =
true; }); startUpdates(); }
135 bool getMeshes(MeshProxyList& result)
override;
136 virtual scriptable::ScriptableModelBase getScriptableModel()
override;
138 virtual void update(
const quint64& now)
override;
139 bool needsToCallUpdate()
const override {
return _updateNeeded; }
142 bool updateOnCount(
const ivec3& v, uint8_t toValue);
143 PolyVox::RaycastResult doRayCast(glm::vec4 originInVoxel, glm::vec4 farInVoxel, glm::vec4& result)
const;
145 void changeUpdates(
bool value);
149 void recomputeMesh();
150 void cacheNeighbors();
151 void copyUpperEdgesFromNeighbors();
152 void tellNeighborsToRecopyEdges(
bool force);
153 bool updateDependents();
156 void uncompressVolumeData();
157 void compressVolumeDataAndSendEditPacket();
158 void computeShapeInfoWorker();
162 bool _meshReady {
false };
163 bool _voxelDataDirty {
false };
164 bool _volDataDirty {
false };
165 bool _shapeReady {
false };
166 PolyVoxState _state { PolyVoxState::Ready };
167 bool _updateNeeded {
true };
169 graphics::MeshPointer _mesh;
171 ShapeInfo _shapeInfo;
173 std::shared_ptr<PolyVox::SimpleVolume<uint8_t>> _volData;
176 bool _neighborXNeedsUpdate {
false };
177 bool _neighborYNeedsUpdate {
false };
178 bool _neighborZNeedsUpdate {
false };
180 bool _updateFromNeighborXEdge {
false };
181 bool _updateFromNeighborYEdge {
false };
182 bool _updateFromNeighborZEdge {
false };
185 EntityItemWeakPointer _xNNeighbor;
186 EntityItemWeakPointer _yNNeighbor;
187 EntityItemWeakPointer _zNNeighbor;
188 EntityItemWeakPointer _xPNeighbor;
189 EntityItemWeakPointer _yPNeighbor;
190 EntityItemWeakPointer _zPNeighbor;
193 namespace render {
namespace entities {
195 class PolyVoxEntityRenderer :
public TypedEntityRenderer<RenderablePolyVoxEntityItem> {
196 using Parent = TypedEntityRenderer<RenderablePolyVoxEntityItem>;
197 friend class EntityRenderer;
200 PolyVoxEntityRenderer(
const EntityItemPointer& entity);
201 virtual scriptable::ScriptableModelBase getScriptableModel()
override {
202 return asTypedEntity<RenderablePolyVoxEntityItem>()->getScriptableModel();
206 virtual ShapeKey getShapeKey()
override;
207 virtual bool needsRenderUpdateFromTypedEntity(
const TypedEntityPointer& entity)
const override;
208 virtual void doRenderUpdateSynchronousTyped(
const ScenePointer& scene, Transaction& transaction,
const TypedEntityPointer& entity)
override;
209 virtual void doRenderUpdateAsynchronousTyped(
const TypedEntityPointer& entity)
override;
210 virtual void doRender(RenderArgs* args)
override;
211 virtual bool isTransparent()
const override {
return false; }
214 #ifdef POLYVOX_ENTITY_USE_FADE_EFFECT
215 bool _hasTransitioned{
false };
218 graphics::MeshPointer _mesh;
219 gpu::BufferPointer _params;
220 std::array<NetworkTexturePointer, 3> _xyzTextures;
221 glm::vec3 _lastVoxelVolumeSize;
222 glm::mat4 _lastVoxelToLocalMatrix;
224 glm::quat _orientation;
225 PolyVoxEntityItem::PolyVoxSurfaceStyle _lastSurfaceStyle { PolyVoxEntityItem::SURFACE_MARCHING_CUBES };
226 std::array<QString, 3> _xyzTextureUrls;
Abstract ID for editing model items. Used in EntityItem JS API.
Definition: EntityItemID.h:28
Definition: EntityItemProperties.h:106