Cloth.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /******************************************************************************
  2. Use 'Cloth' to simulate realistic cloth behavior.
  3. 'ClothMesh' may not be deleted manually if there's still at least one 'Cloth' using the mesh.
  4. This means that the 'ClothMesh' may be deleted only after deleting all 'Cloth' objects using the mesh.
  5. /******************************************************************************/
  6. const_mem_addr struct ClothMesh // Physical Cloth Mesh, it is created from MeshBase and is used to create Cloth !! must be stored in constant memory address !!
  7. {
  8. // manage
  9. ClothMesh& del ( ); // delete manually
  10. ClothMesh& create(C MeshBase &mesh, C MaterialPtr &material, Skeleton *skeleton=null); // create from 'mesh' with 'material', set 'skeleton' for a Skeleton file to automatically adjust ClothMesh vertex bone indexes when loading to match the skeleton bone order
  11. // get
  12. Int vtxs()C {return _phys.vtxs();} // get physical cloth vertex number
  13. C MeshBase& mesh()C {return _phys ;} // get physical cloth mesh
  14. Skeleton* skeleton( )C {return _skeleton;} // get Skeleton linked with this ClothMesh
  15. ClothMesh& skeleton(Skeleton *skeleton); // link ClothMesh with specified Skeleton file, avoid calling this realtime as it requires adjusting the vertex skinning information (bone indexes) and re-creating the hardware mesh version
  16. C MaterialPtr& material()C {return _material;} // get ClothMesh material
  17. // operations
  18. void boneRemap(C MemPtr<Byte, 256> &old_to_new, Bool remap_names=true); // remap vertex bone/matrix indexes according to bone 'old_to_new' remap, 'remap_names'=if remap the bone names as well
  19. // io
  20. Bool save(C Str &name)C; // save, false on fail
  21. Bool load(C Str &name) ; // load, false on fail
  22. Bool save(File &f, CChar *path=null)C; // save, 'path'=path at which resource is located (this is needed so that the sub-resources can be accessed with relative path), false on fail
  23. Bool load(File &f, CChar *path=null) ; // load, 'path'=path at which resource is located (this is needed so that the sub-resources can be accessed with relative path), false on fail
  24. #if EE_PRIVATE
  25. Bool saveData(File &f, CChar *path=null)C; // save, false on fail
  26. Bool loadData(File &f, CChar *path=null) ; // load, false on fail
  27. #endif
  28. ~ClothMesh() {del();}
  29. ClothMesh();
  30. #if EE_PRIVATE
  31. void setShader();
  32. #if PHYSX
  33. PxClothFabric* scaledMesh(Flt scale);
  34. #endif
  35. C Material& getMaterial ( )C {return GetMaterial (_material());}
  36. C Material& getShadowMaterial(Bool reuse_default)C {return GetShadowMaterial(_material(), reuse_default);}
  37. #endif
  38. #if !EE_PRIVATE
  39. private:
  40. #endif
  41. struct Scale
  42. {
  43. Flt scale;
  44. #if EE_PRIVATE
  45. PHYS_API(PxClothFabric, void) *mesh;
  46. #else
  47. Ptr mesh;
  48. #endif
  49. };
  50. MeshBase _phys;
  51. MeshRender _skin;
  52. Memc<Scale> _scales;
  53. MaterialPtr _material;
  54. IndBuf _ind_buf;
  55. Shader *_skin_shader[RM_SHADER_NUM],
  56. *_phys_shader[RM_SHADER_NUM];
  57. #if EE_PRIVATE
  58. FRST *_skin_frst, *_phys_frst;
  59. #else
  60. Ptr _skin_frst, _phys_frst;
  61. #endif
  62. Skeleton *_skeleton;
  63. BoneMap _bone_map;
  64. NO_COPY_CONSTRUCTOR(ClothMesh);
  65. };
  66. /******************************************************************************/
  67. struct Cloth // Physical Cloth
  68. {
  69. // manage
  70. Cloth& del(); // delete manually
  71. Bool create(ClothMesh &cloth_mesh, C Matrix &matrix); // create from 'cloth_mesh', 'matrix'=cloth matrix where its scale determines cloth scale, false on fail
  72. // get / set
  73. ClothMesh* clothMesh()C {return _cloth_mesh;} // get original 'ClothMesh' from which the 'Cloth' has been created
  74. Flt scale ()C {return _scale ;} // get the scale which was used during the cloth creation
  75. Int vtxs ()C; // get number of vertexes
  76. Vec wind ()C; Cloth& wind (C Vec &accel); // get/set wind acceleration
  77. Ptr obj ()C; Cloth& obj (Ptr obj ); // get/set pointer to object containing the cloth
  78. Bool sleep ()C; Cloth& sleep (Bool sleep); // get/set sleeping
  79. Box box ()C; // get world box containing the physical cloth
  80. Flt damping ()C; Cloth& damping (Flt damping ); // get/set damping , 0..1
  81. Flt friction()C; Cloth& friction(Flt friction); // get/set friction, 0..1
  82. Flt bending ()C; Cloth& bending (Flt bending ); // get/set bending , 0..1
  83. Bool ccd()C; Cloth& ccd(Bool on); // get/set continuous collision detection
  84. Bool gpu()C; Cloth& gpu(Bool on); // get/set cloth processing on the GPU
  85. Flt drag ()C; Cloth& drag (Flt drag ); // get/set drag coefficient, 0..1, default=0.0
  86. Flt angDrag ()C; Cloth& angDrag (Flt drag ); // get/set angular drag coefficient, 0..1, default=0.0
  87. Flt inertiaScale()C; Cloth& inertiaScale(Flt scale); // get/set inertia scale , 0..1, default=1.0
  88. Flt angInertiaScale()C; Cloth& angInertiaScale(Flt scale); // get/set angular inertia scale , 0..1, default=1.0
  89. struct Particle
  90. {
  91. Vec pos ; // position of a single particle
  92. Flt inverse_mass; // set 0 for static or 1 for dynamic particle (static will not move at all, dynamic will move according to physics simulation)
  93. };
  94. C Particle* lockRead(); // access cloth particles for reading, length of returned array is equal to 'vtxs', after calling this method you need to call 'unlock'
  95. Cloth& unlock (); // unlock read access, this must be called after 'lockRead'
  96. Cloth& set (C Particle *particle, Int particles); // set custom particles for the cloth, length of this array must be at least as long as 'vtxs'
  97. Cloth& setCollisionBalls (C MemPtr<Ball > &balls ); // set balls that collide with this Cloth (up to 32 balls are supported)
  98. Cloth& setCollisionCapsules(C MemPtr<VecI2> &capsules); // set capsules that collide with this Cloth (up to 32 capsules are supported), they are specified using indexes for both ends of capsule balls from the 'balls' specified using 'setCollisionBalls' (they must be set prior to calling 'setCollisionCapsules')
  99. // draw
  100. void drawSkinned (C AnimatedSkeleton &anim_skel )C; // draw normally skinned cloth part using 'anim_skel', doesn't use Frustum culling, this can be called only in RM_PREPARE
  101. void drawSkinnedShadow (C AnimatedSkeleton &anim_skel )C; // draw normally skinned cloth part using 'anim_skel', doesn't use Frustum culling, this can be called only in RM_SHADOW
  102. void drawSkinnedOutline (C AnimatedSkeleton &anim_skel, C Color &color )C; // draw normally skinned cloth part using 'anim_skel', doesn't use Frustum culling, this can be called only in RM_OUTLINE in order to outline the mesh
  103. void drawPhysical ( C Vec &vel=VecZero)C; // draw physically simulated cloth part , automatically uses Frustum culling, this can be called only in RM_PREPARE
  104. void drawPhysicalShadow ( )C; // draw physically simulated cloth part , automatically uses Frustum culling, this can be called only in RM_SHADOW
  105. void drawPhysicalOutline( C Color &color )C; // draw physically simulated cloth part , automatically uses Frustum culling, this can be called only in RM_OUTLINE in order to outline the mesh
  106. ~Cloth() {del();}
  107. Cloth();
  108. #if !EE_PRIVATE
  109. private:
  110. #endif
  111. struct Vtx // Cloth Vertex
  112. {
  113. Vec pos, // position
  114. nrm; // normal
  115. Vec2 tex; // texture coordinates
  116. };
  117. UInt _update_count;
  118. Flt _scale;
  119. ClothMesh *_cloth_mesh;
  120. UInt *_vtxs;
  121. Vtx *_vtx;
  122. #if EE_PRIVATE
  123. PHYS_API(PxCloth , void) *_cloth;
  124. PHYS_API(PxClothParticleData, void) *_lock;
  125. #else
  126. Ptr _cloth, _lock;
  127. #endif
  128. VtxBuf _vtx_buf;
  129. #if EE_PRIVATE
  130. void update () ;
  131. void _drawPhysical()C;
  132. #endif
  133. NO_COPY_CONSTRUCTOR(Cloth);
  134. };
  135. /******************************************************************************/
  136. extern Cache<ClothMesh> ClothMeshes; // ClothMesh Cache
  137. /******************************************************************************/