decalRoad.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #ifndef _DECALROAD_H_
  23. #define _DECALROAD_H_
  24. #ifndef _SCENEOBJECT_H_
  25. #include "scene/sceneObject.h"
  26. #endif
  27. #ifndef _GFXVERTEXBUFFER_H_
  28. #include "gfx/gfxVertexBuffer.h"
  29. #endif
  30. #ifndef _GFXPRIMITIVEBUFFER_H_
  31. #include "gfx/gfxPrimitiveBuffer.h"
  32. #endif
  33. #ifndef _CLIPPEDPOLYLIST_H_
  34. #include "collision/clippedPolyList.h"
  35. #endif
  36. #include "T3D/assets/MaterialAsset.h"
  37. class Path;
  38. class TerrainBlock;
  39. struct ObjectRenderInst;
  40. class Material;
  41. struct DecalRoadNodeList;
  42. class DecalRoadUpdateEvent : public SimEvent
  43. {
  44. typedef SimEvent Parent;
  45. public:
  46. DecalRoadUpdateEvent( U32 mask, U32 ms ) { mMask = mask; mMs = ms; }
  47. void process( SimObject *object ) override;
  48. U32 mMask;
  49. U32 mMs;
  50. };
  51. struct RoadNode
  52. {
  53. /// The 3D position of the node.
  54. Point3F point;
  55. /// The width of the road at this node.
  56. F32 width;
  57. /// Alpha of the road at this node.
  58. //F32 alpha;
  59. };
  60. typedef Vector<RoadNode> RoadNodeVector;
  61. struct RoadEdge
  62. {
  63. RoadEdge()
  64. {
  65. p0.zero();
  66. p1.zero();
  67. p2.zero();
  68. uvec.zero();
  69. fvec.zero();
  70. rvec.zero();
  71. width = 0.0f;
  72. parentNodeIdx = -1;
  73. };
  74. Point3F p0;
  75. Point3F p1;
  76. Point3F p2;
  77. VectorF uvec;
  78. VectorF fvec;
  79. VectorF rvec;
  80. F32 width;
  81. U32 parentNodeIdx;
  82. };
  83. typedef Vector<RoadEdge> RoadEdgeVector;
  84. struct RoadBatch
  85. {
  86. U32 startVert;
  87. U32 endVert;
  88. U32 startIndex;
  89. U32 endIndex;
  90. Box3F bounds;
  91. };
  92. typedef Vector<RoadBatch> RoadBatchVector;
  93. //------------------------------------------------------------------------------
  94. // DecalRoad Class
  95. //------------------------------------------------------------------------------
  96. class DecalRoad : public SceneObject
  97. {
  98. private:
  99. friend class DecalRoadUpdateEvent;
  100. friend class GuiRoadEditorCtrl;
  101. friend class GuiRoadEditorUndoAction;
  102. typedef SceneObject Parent;
  103. protected:
  104. // Internal defines, enums, structs, classes
  105. struct Triangle
  106. {
  107. GFXVertexPT v0, v1, v2;
  108. };
  109. enum
  110. {
  111. DecalRoadMask = Parent::NextFreeMask,
  112. NodeMask = Parent::NextFreeMask << 1,
  113. GenEdgesMask = Parent::NextFreeMask << 2,
  114. ReClipMask = Parent::NextFreeMask << 3,
  115. TerrainChangedMask = Parent::NextFreeMask << 4,
  116. NextFreeMask = Parent::NextFreeMask << 5,
  117. };
  118. #define StepSize_Normal 10.0f
  119. #define MIN_METERS_PER_SEGMENT 1.0f
  120. public:
  121. DecalRoad();
  122. ~DecalRoad();
  123. DECLARE_CONOBJECT(DecalRoad);
  124. DECLARE_CATEGORY("Environment \t BackGround");
  125. // ConsoleObject
  126. static void initPersistFields();
  127. static void consoleInit();
  128. // SimObject
  129. bool onAdd() override;
  130. void onRemove() override;
  131. void onEditorEnable() override;
  132. void onEditorDisable() override;
  133. void inspectPostApply() override;
  134. void onStaticModified(const char* slotName, const char*newValue = NULL) override;
  135. void writeFields(Stream &stream, U32 tabStop) override;
  136. bool writeField( StringTableEntry fieldname, const char *value ) override;
  137. U32 getSpecialFieldSize(StringTableEntry fieldName) override;
  138. const char* getSpecialFieldOut(StringTableEntry fieldName, const U32& index) override;
  139. // NetObject
  140. U32 packUpdate(NetConnection *, U32, BitStream *) override;
  141. void unpackUpdate(NetConnection *, BitStream *) override;
  142. // SceneObject
  143. void prepRenderImage( SceneRenderState* state ) override;
  144. void setTransform( const MatrixF &mat ) override;
  145. void setScale( const VectorF &scale ) override;
  146. virtual bool containsPoint( const Point3F& point ) const { return containsPoint( point, NULL ); }
  147. // fxRoad Public Methods
  148. void scheduleUpdate( U32 updateMask );
  149. void scheduleUpdate( U32 updateMask, U32 delayMs, bool restartTimer );
  150. void regenerate();
  151. void setTextureLength( F32 meters );
  152. void setBreakAngle( F32 degrees );
  153. /// Insert a node anywhere in the road.
  154. /// Pass idx zero to add to the front and idx U32_MAX to add to the end
  155. U32 insertNode( const Point3F &pos, const F32 &width, const U32 &idx );
  156. U32 addNode( const Point3F &pos, F32 width = 10.0f );
  157. void deleteNode( U32 idx );
  158. void buildNodesFromList( DecalRoadNodeList* list );
  159. bool getClosestNode( const Point3F &pos, U32 &idx );
  160. bool containsPoint( const Point3F &worldPos, U32 *nodeIdx ) const;
  161. bool castray( const Point3F &start, const Point3F &end ) const;
  162. Point3F getNodePosition( U32 idx );
  163. void setNodePosition( U32 idx, const Point3F &pos );
  164. F32 getNodeWidth( U32 idx );
  165. void setNodeWidth( U32 idx, F32 width );
  166. /// Protected 'Node' Field setter that will add a node to the list.
  167. static bool addNodeFromField( void *object, const char *index, const char *data );
  168. static SimSet* getServerSet();
  169. protected:
  170. // Internal Helper Methods
  171. void _initMaterial();
  172. void _debugRender( ObjectRenderInst *ri, SceneRenderState *state, BaseMatInstance *matInst );
  173. U32 _insertNode( const Point3F &pos, const F32 &width, const U32 &idx );
  174. U32 _addNode( const Point3F &pos, F32 width );
  175. void _generateEdges();
  176. void _captureVerts();
  177. bool _getTerrainHeight( Point3F &pos );
  178. bool _getTerrainHeight( const Point2F &pos, F32 &height );
  179. bool _getTerrainHeight( const F32 &x, const F32 &y, F32 &height );
  180. void _onTerrainChanged( U32 type, TerrainBlock* tblock, const Point2I &min, const Point2I &max );
  181. // static protected field set methods
  182. static bool ptSetBreakAngle( void *object, const char *index, const char *data );
  183. static bool ptSetTextureLength( void *object, const char *index, const char *data );
  184. protected:
  185. // Field Vars
  186. F32 mBreakAngle;
  187. U32 mSegmentsPerBatch;
  188. F32 mTextureLength;
  189. BaseMatInstance* mMaterialInst;
  190. DECLARE_MATERIALASSET(DecalRoad, Material);
  191. DECLARE_ASSET_NET_SETGET(DecalRoad, Material, DecalRoadMask);
  192. S16 mRenderPriority;
  193. // Static ConsoleVars for editor
  194. static bool smEditorOpen;
  195. static bool smWireframe;
  196. static bool smShowBatches;
  197. static bool smDiscardAll;
  198. static bool smShowSpline;
  199. static bool smShowRoad;
  200. static S32 smUpdateDelay;
  201. static SimObjectPtr<SimSet> smServerDecalRoadSet;
  202. // Other Internal Vars
  203. RoadEdgeVector mEdges;
  204. RoadNodeVector mNodes;
  205. RoadBatchVector mBatches;
  206. bool mLoadRenderData;
  207. GFXVertexBufferHandle<GFXVertexPNTBT> mVB;
  208. GFXPrimitiveBufferHandle mPB;
  209. U32 mTriangleCount;
  210. U32 mVertCount;
  211. S32 mUpdateEventId;
  212. DecalRoadUpdateEvent *mLastEvent;
  213. Box3F mTerrainUpdateRect;
  214. };
  215. #endif // _DECALROAD_H_