HeightFieldShape.cpp 99 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707
  1. // Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
  2. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  3. // SPDX-License-Identifier: MIT
  4. #include <Jolt/Jolt.h>
  5. #include <Jolt/Physics/Collision/Shape/HeightFieldShape.h>
  6. #include <Jolt/Physics/Collision/Shape/ConvexShape.h>
  7. #include <Jolt/Physics/Collision/Shape/ScaleHelpers.h>
  8. #include <Jolt/Physics/Collision/Shape/SphereShape.h>
  9. #include <Jolt/Physics/Collision/RayCast.h>
  10. #include <Jolt/Physics/Collision/ShapeCast.h>
  11. #include <Jolt/Physics/Collision/CastResult.h>
  12. #include <Jolt/Physics/Collision/CollidePointResult.h>
  13. #include <Jolt/Physics/Collision/ShapeFilter.h>
  14. #include <Jolt/Physics/Collision/CastConvexVsTriangles.h>
  15. #include <Jolt/Physics/Collision/CastSphereVsTriangles.h>
  16. #include <Jolt/Physics/Collision/CollideConvexVsTriangles.h>
  17. #include <Jolt/Physics/Collision/CollideSphereVsTriangles.h>
  18. #include <Jolt/Physics/Collision/TransformedShape.h>
  19. #include <Jolt/Physics/Collision/ActiveEdges.h>
  20. #include <Jolt/Physics/Collision/CollisionDispatch.h>
  21. #include <Jolt/Physics/Collision/SortReverseAndStore.h>
  22. #include <Jolt/Physics/Collision/CollideSoftBodyVerticesVsTriangles.h>
  23. #include <Jolt/Core/Profiler.h>
  24. #include <Jolt/Core/StringTools.h>
  25. #include <Jolt/Core/StreamIn.h>
  26. #include <Jolt/Core/StreamOut.h>
  27. #include <Jolt/Core/TempAllocator.h>
  28. #include <Jolt/Core/ScopeExit.h>
  29. #include <Jolt/Geometry/AABox4.h>
  30. #include <Jolt/Geometry/RayTriangle.h>
  31. #include <Jolt/Geometry/RayAABox.h>
  32. #include <Jolt/Geometry/OrientedBox.h>
  33. #include <Jolt/ObjectStream/TypeDeclarations.h>
  34. //#define JPH_DEBUG_HEIGHT_FIELD
  35. JPH_NAMESPACE_BEGIN
  36. #ifdef JPH_DEBUG_RENDERER
  37. bool HeightFieldShape::sDrawTriangleOutlines = false;
  38. #endif // JPH_DEBUG_RENDERER
  39. using namespace HeightFieldShapeConstants;
  40. JPH_IMPLEMENT_SERIALIZABLE_VIRTUAL(HeightFieldShapeSettings)
  41. {
  42. JPH_ADD_BASE_CLASS(HeightFieldShapeSettings, ShapeSettings)
  43. JPH_ADD_ATTRIBUTE(HeightFieldShapeSettings, mHeightSamples)
  44. JPH_ADD_ATTRIBUTE(HeightFieldShapeSettings, mOffset)
  45. JPH_ADD_ATTRIBUTE(HeightFieldShapeSettings, mScale)
  46. JPH_ADD_ATTRIBUTE(HeightFieldShapeSettings, mMinHeightValue)
  47. JPH_ADD_ATTRIBUTE(HeightFieldShapeSettings, mMaxHeightValue)
  48. JPH_ADD_ATTRIBUTE(HeightFieldShapeSettings, mMaterialsCapacity)
  49. JPH_ADD_ATTRIBUTE(HeightFieldShapeSettings, mSampleCount)
  50. JPH_ADD_ATTRIBUTE(HeightFieldShapeSettings, mBlockSize)
  51. JPH_ADD_ATTRIBUTE(HeightFieldShapeSettings, mBitsPerSample)
  52. JPH_ADD_ATTRIBUTE(HeightFieldShapeSettings, mMaterialIndices)
  53. JPH_ADD_ATTRIBUTE(HeightFieldShapeSettings, mMaterials)
  54. JPH_ADD_ATTRIBUTE(HeightFieldShapeSettings, mActiveEdgeCosThresholdAngle)
  55. }
  56. const uint HeightFieldShape::sGridOffsets[] =
  57. {
  58. 0, // level: 0, max x/y: 0, offset: 0
  59. 1, // level: 1, max x/y: 1, offset: 1
  60. 5, // level: 2, max x/y: 3, offset: 1 + 4
  61. 21, // level: 3, max x/y: 7, offset: 1 + 4 + 16
  62. 85, // level: 4, max x/y: 15, offset: 1 + 4 + 16 + 64
  63. 341, // level: 5, max x/y: 31, offset: 1 + 4 + 16 + 64 + 256
  64. 1365, // level: 6, max x/y: 63, offset: 1 + 4 + 16 + 64 + 256 + 1024
  65. 5461, // level: 7, max x/y: 127, offset: 1 + 4 + 16 + 64 + 256 + 1024 + 4096
  66. 21845, // level: 8, max x/y: 255, offset: 1 + 4 + 16 + 64 + 256 + 1024 + 4096 + ...
  67. 87381, // level: 9, max x/y: 511, offset: 1 + 4 + 16 + 64 + 256 + 1024 + 4096 + ...
  68. 349525, // level: 10, max x/y: 1023, offset: 1 + 4 + 16 + 64 + 256 + 1024 + 4096 + ...
  69. 1398101, // level: 11, max x/y: 2047, offset: 1 + 4 + 16 + 64 + 256 + 1024 + 4096 + ...
  70. 5592405, // level: 12, max x/y: 4095, offset: 1 + 4 + 16 + 64 + 256 + 1024 + 4096 + ...
  71. 22369621, // level: 13, max x/y: 8191, offset: 1 + 4 + 16 + 64 + 256 + 1024 + 4096 + ...
  72. 89478485, // level: 14, max x/y: 16383, offset: 1 + 4 + 16 + 64 + 256 + 1024 + 4096 + ...
  73. };
  74. HeightFieldShapeSettings::HeightFieldShapeSettings(const float *inSamples, Vec3Arg inOffset, Vec3Arg inScale, uint32 inSampleCount, const uint8 *inMaterialIndices, const PhysicsMaterialList &inMaterialList) :
  75. mOffset(inOffset),
  76. mScale(inScale),
  77. mSampleCount(inSampleCount)
  78. {
  79. mHeightSamples.assign(inSamples, inSamples + Square(inSampleCount));
  80. if (!inMaterialList.empty() && inMaterialIndices != nullptr)
  81. {
  82. mMaterialIndices.assign(inMaterialIndices, inMaterialIndices + Square(inSampleCount - 1));
  83. mMaterials = inMaterialList;
  84. }
  85. else
  86. {
  87. JPH_ASSERT(inMaterialList.empty());
  88. JPH_ASSERT(inMaterialIndices == nullptr);
  89. }
  90. }
  91. ShapeSettings::ShapeResult HeightFieldShapeSettings::Create() const
  92. {
  93. if (mCachedResult.IsEmpty())
  94. Ref<Shape> shape = new HeightFieldShape(*this, mCachedResult);
  95. return mCachedResult;
  96. }
  97. void HeightFieldShapeSettings::DetermineMinAndMaxSample(float &outMinValue, float &outMaxValue, float &outQuantizationScale) const
  98. {
  99. // Determine min and max value
  100. outMinValue = mMinHeightValue;
  101. outMaxValue = mMaxHeightValue;
  102. for (float h : mHeightSamples)
  103. if (h != cNoCollisionValue)
  104. {
  105. outMinValue = min(outMinValue, h);
  106. outMaxValue = max(outMaxValue, h);
  107. }
  108. // Prevent dividing by zero by setting a minimal height difference
  109. float height_diff = max(outMaxValue - outMinValue, 1.0e-6f);
  110. // Calculate the scale factor to quantize to 16 bits
  111. outQuantizationScale = float(cMaxHeightValue16) / height_diff;
  112. }
  113. uint32 HeightFieldShapeSettings::CalculateBitsPerSampleForError(float inMaxError) const
  114. {
  115. // Start with 1 bit per sample
  116. uint32 bits_per_sample = 1;
  117. // Determine total range
  118. float min_value, max_value, scale;
  119. DetermineMinAndMaxSample(min_value, max_value, scale);
  120. if (min_value < max_value)
  121. {
  122. // Loop over all blocks
  123. for (uint y = 0; y < mSampleCount; y += mBlockSize)
  124. for (uint x = 0; x < mSampleCount; x += mBlockSize)
  125. {
  126. // Determine min and max block value + take 1 sample border just like we do while building the hierarchical grids
  127. float block_min_value = FLT_MAX, block_max_value = -FLT_MAX;
  128. for (uint bx = x; bx < min(x + mBlockSize + 1, mSampleCount); ++bx)
  129. for (uint by = y; by < min(y + mBlockSize + 1, mSampleCount); ++by)
  130. {
  131. float h = mHeightSamples[by * mSampleCount + bx];
  132. if (h != cNoCollisionValue)
  133. {
  134. block_min_value = min(block_min_value, h);
  135. block_max_value = max(block_max_value, h);
  136. }
  137. }
  138. if (block_min_value < block_max_value)
  139. {
  140. // Quantize then dequantize block min/max value
  141. block_min_value = min_value + floor((block_min_value - min_value) * scale) / scale;
  142. block_max_value = min_value + ceil((block_max_value - min_value) * scale) / scale;
  143. float block_height = block_max_value - block_min_value;
  144. // Loop over the block again
  145. for (uint bx = x; bx < x + mBlockSize; ++bx)
  146. for (uint by = y; by < y + mBlockSize; ++by)
  147. {
  148. // Get the height
  149. float height = mHeightSamples[by * mSampleCount + bx];
  150. if (height != cNoCollisionValue)
  151. {
  152. for (;;)
  153. {
  154. // Determine bitmask for sample
  155. uint32 sample_mask = (1 << bits_per_sample) - 1;
  156. // Quantize
  157. float quantized_height = floor((height - block_min_value) * float(sample_mask) / block_height);
  158. quantized_height = Clamp(quantized_height, 0.0f, float(sample_mask - 1));
  159. // Dequantize and check error
  160. float dequantized_height = block_min_value + (quantized_height + 0.5f) * block_height / float(sample_mask);
  161. if (abs(dequantized_height - height) <= inMaxError)
  162. break;
  163. // Not accurate enough, increase bits per sample
  164. bits_per_sample++;
  165. // Don't go above 8 bits per sample
  166. if (bits_per_sample == 8)
  167. return bits_per_sample;
  168. }
  169. }
  170. }
  171. }
  172. }
  173. }
  174. return bits_per_sample;
  175. }
  176. void HeightFieldShape::CalculateActiveEdges(uint inX, uint inY, uint inSizeX, uint inSizeY, const float *inHeights, uint inHeightsStartX, uint inHeightsStartY, intptr_t inHeightsStride, float inHeightsScale, float inActiveEdgeCosThresholdAngle, TempAllocator &inAllocator)
  177. {
  178. // Allocate temporary buffer for normals
  179. uint normals_size = 2 * inSizeX * inSizeY * sizeof(Vec3);
  180. Vec3 *normals = (Vec3 *)inAllocator.Allocate(normals_size);
  181. JPH_SCOPE_EXIT([&inAllocator, normals, normals_size]{ inAllocator.Free(normals, normals_size); });
  182. // Calculate triangle normals and make normals zero for triangles that are missing
  183. Vec3 *out_normal = normals;
  184. for (uint y = 0; y < inSizeY; ++y)
  185. for (uint x = 0; x < inSizeX; ++x)
  186. {
  187. // Get height on diagonal
  188. const float *height_samples = inHeights + (inY - inHeightsStartY + y) * inHeightsStride + (inX - inHeightsStartX + x);
  189. float x1y1_h = height_samples[0];
  190. float x2y2_h = height_samples[inHeightsStride + 1];
  191. if (x1y1_h != cNoCollisionValue && x2y2_h != cNoCollisionValue)
  192. {
  193. // Calculate normal for lower left triangle (e.g. T1A)
  194. float x1y2_h = height_samples[inHeightsStride];
  195. if (x1y2_h != cNoCollisionValue)
  196. {
  197. Vec3 x2y2_minus_x1y2(mScale.GetX(), inHeightsScale * (x2y2_h - x1y2_h), 0);
  198. Vec3 x1y1_minus_x1y2(0, inHeightsScale * (x1y1_h - x1y2_h), -mScale.GetZ());
  199. out_normal[0] = x2y2_minus_x1y2.Cross(x1y1_minus_x1y2).Normalized();
  200. }
  201. else
  202. out_normal[0] = Vec3::sZero();
  203. // Calculate normal for upper right triangle (e.g. T1B)
  204. float x2y1_h = height_samples[1];
  205. if (x2y1_h != cNoCollisionValue)
  206. {
  207. Vec3 x1y1_minus_x2y1(-mScale.GetX(), inHeightsScale * (x1y1_h - x2y1_h), 0);
  208. Vec3 x2y2_minus_x2y1(0, inHeightsScale * (x2y2_h - x2y1_h), mScale.GetZ());
  209. out_normal[1] = x1y1_minus_x2y1.Cross(x2y2_minus_x2y1).Normalized();
  210. }
  211. else
  212. out_normal[1] = Vec3::sZero();
  213. }
  214. else
  215. {
  216. out_normal[0] = Vec3::sZero();
  217. out_normal[1] = Vec3::sZero();
  218. }
  219. out_normal += 2;
  220. }
  221. // Calculate active edges
  222. const Vec3 *in_normal = normals;
  223. uint global_bit_pos = 3 * (inY * (mSampleCount - 1) + inX);
  224. for (uint y = 0; y < inSizeY; ++y)
  225. {
  226. for (uint x = 0; x < inSizeX; ++x)
  227. {
  228. // Get vertex heights
  229. const float *height_samples = inHeights + (inY - inHeightsStartY + y) * inHeightsStride + (inX - inHeightsStartX + x);
  230. float x1y1_h = height_samples[0];
  231. float x1y2_h = height_samples[inHeightsStride];
  232. float x2y2_h = height_samples[inHeightsStride + 1];
  233. bool x1y1_valid = x1y1_h != cNoCollisionValue;
  234. bool x1y2_valid = x1y2_h != cNoCollisionValue;
  235. bool x2y2_valid = x2y2_h != cNoCollisionValue;
  236. // Calculate the edge flags (3 bits)
  237. // See diagram in the next function for the edge numbering
  238. uint16 edge_mask = 0b111;
  239. uint16 edge_flags = 0;
  240. // Edge 0
  241. if (x == 0)
  242. edge_mask &= 0b110; // We need normal x - 1 which we didn't calculate, don't update this edge
  243. else if (x1y1_valid && x1y2_valid)
  244. {
  245. Vec3 edge0_direction(0, inHeightsScale * (x1y2_h - x1y1_h), mScale.GetZ());
  246. if (ActiveEdges::IsEdgeActive(in_normal[0], in_normal[-1], edge0_direction, inActiveEdgeCosThresholdAngle))
  247. edge_flags |= 0b001;
  248. }
  249. // Edge 1
  250. if (y == inSizeY - 1)
  251. edge_mask &= 0b101; // We need normal y + 1 which we didn't calculate, don't update this edge
  252. else if (x1y2_valid && x2y2_valid)
  253. {
  254. Vec3 edge1_direction(mScale.GetX(), inHeightsScale * (x2y2_h - x1y2_h), 0);
  255. if (ActiveEdges::IsEdgeActive(in_normal[0], in_normal[2 * inSizeX + 1], edge1_direction, inActiveEdgeCosThresholdAngle))
  256. edge_flags |= 0b010;
  257. }
  258. // Edge 2
  259. if (x1y1_valid && x2y2_valid)
  260. {
  261. Vec3 edge2_direction(-mScale.GetX(), inHeightsScale * (x1y1_h - x2y2_h), -mScale.GetZ());
  262. if (ActiveEdges::IsEdgeActive(in_normal[0], in_normal[1], edge2_direction, inActiveEdgeCosThresholdAngle))
  263. edge_flags |= 0b100;
  264. }
  265. // Store the edge flags in the array
  266. uint byte_pos = global_bit_pos >> 3;
  267. uint bit_pos = global_bit_pos & 0b111;
  268. JPH_ASSERT(byte_pos < mActiveEdgesSize);
  269. uint8 *edge_flags_ptr = &mActiveEdges[byte_pos];
  270. uint16 combined_edge_flags = uint16(edge_flags_ptr[0]) | uint16(uint16(edge_flags_ptr[1]) << 8);
  271. combined_edge_flags &= ~(edge_mask << bit_pos);
  272. combined_edge_flags |= edge_flags << bit_pos;
  273. edge_flags_ptr[0] = uint8(combined_edge_flags);
  274. edge_flags_ptr[1] = uint8(combined_edge_flags >> 8);
  275. in_normal += 2;
  276. global_bit_pos += 3;
  277. }
  278. global_bit_pos += 3 * (mSampleCount - 1 - inSizeX);
  279. }
  280. }
  281. void HeightFieldShape::CalculateActiveEdges(const HeightFieldShapeSettings &inSettings)
  282. {
  283. /*
  284. Store active edges. The triangles are organized like this:
  285. x --->
  286. y + +
  287. | \ T1B | \ T2B
  288. | e0 e2 | \
  289. | | T1A \ | T2A \
  290. V +--e1---+-------+
  291. | \ T3B | \ T4B
  292. | \ | \
  293. | T3A \ | T4A \
  294. +-------+-------+
  295. We store active edges e0 .. e2 as bits 0 .. 2.
  296. We store triangles horizontally then vertically (order T1A, T2A, T3A and T4A).
  297. The top edge and right edge of the heightfield are always active so we do not need to store them,
  298. therefore we only need to store (mSampleCount - 1)^2 * 3-bit
  299. The triangles T1B, T2B, T3B and T4B do not need to be stored, their active edges can be constructed from adjacent triangles.
  300. Add 1 byte padding so we can always read 1 uint16 to get the bits that cross an 8 bit boundary
  301. */
  302. // Make all edges active (if mSampleCount is bigger than inSettings.mSampleCount we need to fill up the padding,
  303. // also edges at x = 0 and y = inSettings.mSampleCount - 1 are not updated)
  304. memset(mActiveEdges, 0xff, mActiveEdgesSize);
  305. // Now clear the edges that are not active
  306. TempAllocatorMalloc allocator;
  307. CalculateActiveEdges(0, 0, inSettings.mSampleCount - 1, inSettings.mSampleCount - 1, inSettings.mHeightSamples.data(), 0, 0, inSettings.mSampleCount, inSettings.mScale.GetY(), inSettings.mActiveEdgeCosThresholdAngle, allocator);
  308. }
  309. void HeightFieldShape::StoreMaterialIndices(const HeightFieldShapeSettings &inSettings)
  310. {
  311. // We need to account for any rounding of the sample count to the nearest block size
  312. uint in_count_min_1 = inSettings.mSampleCount - 1;
  313. uint out_count_min_1 = mSampleCount - 1;
  314. mNumBitsPerMaterialIndex = 32 - CountLeadingZeros(max((uint32)mMaterials.size(), inSettings.mMaterialsCapacity) - 1);
  315. mMaterialIndices.resize(((Square(out_count_min_1) * mNumBitsPerMaterialIndex + 7) >> 3) + 1, 0); // Add 1 byte so we don't read out of bounds when reading an uint16
  316. if (mMaterials.size() > 1)
  317. for (uint y = 0; y < out_count_min_1; ++y)
  318. for (uint x = 0; x < out_count_min_1; ++x)
  319. {
  320. // Read material
  321. uint16 material_index = x < in_count_min_1 && y < in_count_min_1? uint16(inSettings.mMaterialIndices[x + y * in_count_min_1]) : 0;
  322. // Calculate byte and bit position where the material index needs to go
  323. uint sample_pos = x + y * out_count_min_1;
  324. uint bit_pos = sample_pos * mNumBitsPerMaterialIndex;
  325. uint byte_pos = bit_pos >> 3;
  326. bit_pos &= 0b111;
  327. // Write the material index
  328. material_index <<= bit_pos;
  329. JPH_ASSERT(byte_pos + 1 < mMaterialIndices.size());
  330. mMaterialIndices[byte_pos] |= uint8(material_index);
  331. mMaterialIndices[byte_pos + 1] |= uint8(material_index >> 8);
  332. }
  333. }
  334. void HeightFieldShape::CacheValues()
  335. {
  336. mSampleMask = uint8((uint32(1) << mBitsPerSample) - 1);
  337. }
  338. void HeightFieldShape::AllocateBuffers()
  339. {
  340. uint num_blocks = GetNumBlocks();
  341. uint max_stride = (num_blocks + 1) >> 1;
  342. mRangeBlocksSize = sGridOffsets[sGetMaxLevel(num_blocks) - 1] + Square(max_stride);
  343. mHeightSamplesSize = (mSampleCount * mSampleCount * mBitsPerSample + 7) / 8 + 1;
  344. mActiveEdgesSize = (Square(mSampleCount - 1) * 3 + 7) / 8 + 1; // See explanation at HeightFieldShape::CalculateActiveEdges
  345. JPH_ASSERT(mRangeBlocks == nullptr && mHeightSamples == nullptr && mActiveEdges == nullptr);
  346. void *data = AlignedAllocate(mRangeBlocksSize * sizeof(RangeBlock) + mHeightSamplesSize + mActiveEdgesSize, alignof(RangeBlock));
  347. mRangeBlocks = reinterpret_cast<RangeBlock *>(data);
  348. mHeightSamples = reinterpret_cast<uint8 *>(mRangeBlocks + mRangeBlocksSize);
  349. mActiveEdges = mHeightSamples + mHeightSamplesSize;
  350. }
  351. HeightFieldShape::HeightFieldShape(const HeightFieldShapeSettings &inSettings, ShapeResult &outResult) :
  352. Shape(EShapeType::HeightField, EShapeSubType::HeightField, inSettings, outResult),
  353. mOffset(inSettings.mOffset),
  354. mScale(inSettings.mScale),
  355. mSampleCount(((inSettings.mSampleCount + inSettings.mBlockSize - 1) / inSettings.mBlockSize) * inSettings.mBlockSize), // Round sample count to nearest block size
  356. mBlockSize(inSettings.mBlockSize),
  357. mBitsPerSample(uint8(inSettings.mBitsPerSample))
  358. {
  359. CacheValues();
  360. // Reserve a bigger materials list if requested
  361. if (inSettings.mMaterialsCapacity > 0)
  362. mMaterials.reserve(inSettings.mMaterialsCapacity);
  363. mMaterials = inSettings.mMaterials;
  364. // Check block size
  365. if (mBlockSize < 2 || mBlockSize > 8)
  366. {
  367. outResult.SetError("HeightFieldShape: Block size must be in the range [2, 8]!");
  368. return;
  369. }
  370. // Check bits per sample
  371. if (inSettings.mBitsPerSample < 1 || inSettings.mBitsPerSample > 8)
  372. {
  373. outResult.SetError("HeightFieldShape: Bits per sample must be in the range [1, 8]!");
  374. return;
  375. }
  376. // We stop at mBlockSize x mBlockSize height sample blocks
  377. uint num_blocks = GetNumBlocks();
  378. // We want at least 1 grid layer
  379. if (num_blocks < 2)
  380. {
  381. outResult.SetError("HeightFieldShape: Sample count too low!");
  382. return;
  383. }
  384. // Check that we don't overflow our 32 bit 'properties'
  385. if (num_blocks > (1 << cNumBitsXY))
  386. {
  387. outResult.SetError("HeightFieldShape: Sample count too high!");
  388. return;
  389. }
  390. // Check if we're not exceeding the amount of sub shape id bits
  391. if (GetSubShapeIDBitsRecursive() > SubShapeID::MaxBits)
  392. {
  393. outResult.SetError("HeightFieldShape: Size exceeds the amount of available sub shape ID bits!");
  394. return;
  395. }
  396. if (!mMaterials.empty())
  397. {
  398. // Validate materials
  399. if (mMaterials.size() > 256)
  400. {
  401. outResult.SetError("Supporting max 256 materials per height field");
  402. return;
  403. }
  404. for (uint8 s : inSettings.mMaterialIndices)
  405. if (s >= mMaterials.size())
  406. {
  407. outResult.SetError(StringFormat("Material %u is beyond material list (size: %u)", s, (uint)mMaterials.size()));
  408. return;
  409. }
  410. }
  411. else
  412. {
  413. // No materials assigned, validate that no materials have been specified
  414. if (!inSettings.mMaterialIndices.empty())
  415. {
  416. outResult.SetError("No materials present, mMaterialIndices should be empty");
  417. return;
  418. }
  419. }
  420. // Determine range
  421. float min_value, max_value, scale;
  422. inSettings.DetermineMinAndMaxSample(min_value, max_value, scale);
  423. if (min_value > max_value)
  424. {
  425. // If there is no collision with this heightmap, leave everything empty
  426. mMaterials.clear();
  427. outResult.Set(this);
  428. return;
  429. }
  430. // Allocate space for this shape
  431. AllocateBuffers();
  432. // Quantize to uint16
  433. Array<uint16> quantized_samples;
  434. quantized_samples.reserve(mSampleCount * mSampleCount);
  435. for (uint y = 0; y < inSettings.mSampleCount; ++y)
  436. {
  437. for (uint x = 0; x < inSettings.mSampleCount; ++x)
  438. {
  439. float h = inSettings.mHeightSamples[x + y * inSettings.mSampleCount];
  440. if (h == cNoCollisionValue)
  441. {
  442. quantized_samples.push_back(cNoCollisionValue16);
  443. }
  444. else
  445. {
  446. // Floor the quantized height to get a lower bound for the quantized value
  447. int quantized_height = (int)floor(scale * (h - min_value));
  448. // Ensure that the height says below the max height value so we can safely add 1 to get the upper bound for the quantized value
  449. quantized_height = Clamp(quantized_height, 0, int(cMaxHeightValue16 - 1));
  450. quantized_samples.push_back(uint16(quantized_height));
  451. }
  452. }
  453. // Pad remaining columns with no collision
  454. for (uint x = inSettings.mSampleCount; x < mSampleCount; ++x)
  455. quantized_samples.push_back(cNoCollisionValue16);
  456. }
  457. // Pad remaining rows with no collision
  458. for (uint y = inSettings.mSampleCount; y < mSampleCount; ++y)
  459. for (uint x = 0; x < mSampleCount; ++x)
  460. quantized_samples.push_back(cNoCollisionValue16);
  461. // Update offset and scale to account for the compression to uint16
  462. if (min_value <= max_value) // Only when there was collision
  463. {
  464. // In GetPosition we always add 0.5 to the quantized sample in order to reduce the average error.
  465. // We want to be able to exactly quantize min_value (this is important in case the heightfield is entirely flat) so we subtract that value from min_value.
  466. min_value -= 0.5f / (scale * mSampleMask);
  467. mOffset.SetY(mOffset.GetY() + mScale.GetY() * min_value);
  468. }
  469. mScale.SetY(mScale.GetY() / scale);
  470. // Calculate amount of grids
  471. uint max_level = sGetMaxLevel(num_blocks);
  472. // Temporary data structure used during creating of a hierarchy of grids
  473. struct Range
  474. {
  475. uint16 mMin;
  476. uint16 mMax;
  477. };
  478. // Reserve size for temporary range data + reserve 1 extra for a 1x1 grid that we won't store but use for calculating the bounding box
  479. Array<Array<Range>> ranges;
  480. ranges.resize(max_level + 1);
  481. // Calculate highest detail grid by combining mBlockSize x mBlockSize height samples
  482. Array<Range> *cur_range_vector = &ranges.back();
  483. uint num_blocks_pow2 = GetNextPowerOf2(num_blocks); // We calculate the range blocks as if the heightfield was a power of 2, when we save the range blocks we'll ignore the extra samples (this makes downsampling easier)
  484. cur_range_vector->resize(num_blocks_pow2 * num_blocks_pow2);
  485. Range *range_dst = &cur_range_vector->front();
  486. for (uint y = 0; y < num_blocks_pow2; ++y)
  487. for (uint x = 0; x < num_blocks_pow2; ++x)
  488. {
  489. range_dst->mMin = 0xffff;
  490. range_dst->mMax = 0;
  491. uint max_bx = x == num_blocks_pow2 - 1? mBlockSize : mBlockSize + 1; // for interior blocks take 1 more because the triangles connect to the next block so we must include their height too
  492. uint max_by = y == num_blocks_pow2 - 1? mBlockSize : mBlockSize + 1;
  493. for (uint by = 0; by < max_by; ++by)
  494. for (uint bx = 0; bx < max_bx; ++bx)
  495. {
  496. uint sx = x * mBlockSize + bx;
  497. uint sy = y * mBlockSize + by;
  498. if (sx < mSampleCount && sy < mSampleCount)
  499. {
  500. uint16 h = quantized_samples[sy * mSampleCount + sx];
  501. if (h != cNoCollisionValue16)
  502. {
  503. range_dst->mMin = min(range_dst->mMin, h);
  504. range_dst->mMax = max(range_dst->mMax, uint16(h + 1)); // Add 1 to the max so we know the real value is between mMin and mMax
  505. }
  506. }
  507. }
  508. ++range_dst;
  509. }
  510. // Calculate remaining grids
  511. for (uint n = num_blocks_pow2 >> 1; n >= 1; n >>= 1)
  512. {
  513. // Get source buffer
  514. const Range *range_src = &cur_range_vector->front();
  515. // Previous array element
  516. --cur_range_vector;
  517. // Make space for this grid
  518. cur_range_vector->resize(n * n);
  519. // Get target buffer
  520. range_dst = &cur_range_vector->front();
  521. // Combine the results of 2x2 ranges
  522. for (uint y = 0; y < n; ++y)
  523. for (uint x = 0; x < n; ++x)
  524. {
  525. range_dst->mMin = 0xffff;
  526. range_dst->mMax = 0;
  527. for (uint by = 0; by < 2; ++by)
  528. for (uint bx = 0; bx < 2; ++bx)
  529. {
  530. const Range &r = range_src[(y * 2 + by) * n * 2 + x * 2 + bx];
  531. range_dst->mMin = min(range_dst->mMin, r.mMin);
  532. range_dst->mMax = max(range_dst->mMax, r.mMax);
  533. }
  534. ++range_dst;
  535. }
  536. }
  537. JPH_ASSERT(cur_range_vector == &ranges.front());
  538. // Store global range for bounding box calculation
  539. mMinSample = ranges[0][0].mMin;
  540. mMaxSample = ranges[0][0].mMax;
  541. #ifdef JPH_ENABLE_ASSERTS
  542. // Validate that we did not lose range along the way
  543. uint16 minv = 0xffff, maxv = 0;
  544. for (uint16 v : quantized_samples)
  545. if (v != cNoCollisionValue16)
  546. {
  547. minv = min(minv, v);
  548. maxv = max(maxv, uint16(v + 1));
  549. }
  550. JPH_ASSERT(mMinSample == minv && mMaxSample == maxv);
  551. #endif
  552. // Now erase the first element, we need a 2x2 grid to start with
  553. ranges.erase(ranges.begin());
  554. // Create blocks
  555. uint max_stride = (num_blocks + 1) >> 1;
  556. RangeBlock *current_block = mRangeBlocks;
  557. for (uint level = 0; level < ranges.size(); ++level)
  558. {
  559. JPH_ASSERT(uint(current_block - mRangeBlocks) == sGridOffsets[level]);
  560. uint in_n = 1 << level;
  561. uint out_n = min(in_n, max_stride); // At the most detailed level we store a non-power of 2 number of blocks
  562. for (uint y = 0; y < out_n; ++y)
  563. for (uint x = 0; x < out_n; ++x)
  564. {
  565. // Convert from 2x2 Range structure to 1 RangeBlock structure
  566. RangeBlock &rb = *current_block++;
  567. for (uint by = 0; by < 2; ++by)
  568. for (uint bx = 0; bx < 2; ++bx)
  569. {
  570. uint src_pos = (y * 2 + by) * 2 * in_n + (x * 2 + bx);
  571. uint dst_pos = by * 2 + bx;
  572. rb.mMin[dst_pos] = ranges[level][src_pos].mMin;
  573. rb.mMax[dst_pos] = ranges[level][src_pos].mMax;
  574. }
  575. }
  576. }
  577. JPH_ASSERT(uint32(current_block - mRangeBlocks) == mRangeBlocksSize);
  578. // Quantize height samples
  579. memset(mHeightSamples, 0, mHeightSamplesSize);
  580. int sample = 0;
  581. for (uint y = 0; y < mSampleCount; ++y)
  582. for (uint x = 0; x < mSampleCount; ++x)
  583. {
  584. uint32 output_value;
  585. float h = x < inSettings.mSampleCount && y < inSettings.mSampleCount? inSettings.mHeightSamples[x + y * inSettings.mSampleCount] : cNoCollisionValue;
  586. if (h == cNoCollisionValue)
  587. {
  588. // No collision
  589. output_value = mSampleMask;
  590. }
  591. else
  592. {
  593. // Get range of block so we know what range to compress to
  594. uint bx = x / mBlockSize;
  595. uint by = y / mBlockSize;
  596. const Range &range = ranges.back()[by * num_blocks_pow2 + bx];
  597. JPH_ASSERT(range.mMin < range.mMax);
  598. // Quantize to mBitsPerSample bits, note that mSampleMask is reserved for indicating that there's no collision.
  599. // We divide the range into mSampleMask segments and use the mid points of these segments as the quantized values.
  600. // This results in a lower error than if we had quantized our data using the lowest point of all these segments.
  601. float h_min = min_value + range.mMin / scale;
  602. float h_delta = float(range.mMax - range.mMin) / scale;
  603. float quantized_height = floor((h - h_min) * float(mSampleMask) / h_delta);
  604. output_value = uint32(Clamp((int)quantized_height, 0, int(mSampleMask) - 1)); // mSampleMask is reserved as 'no collision value'
  605. }
  606. // Store the sample
  607. uint byte_pos = sample >> 3;
  608. uint bit_pos = sample & 0b111;
  609. output_value <<= bit_pos;
  610. JPH_ASSERT(byte_pos + 1 < mHeightSamplesSize);
  611. mHeightSamples[byte_pos] |= uint8(output_value);
  612. mHeightSamples[byte_pos + 1] |= uint8(output_value >> 8);
  613. sample += inSettings.mBitsPerSample;
  614. }
  615. // Calculate the active edges
  616. CalculateActiveEdges(inSettings);
  617. // Compress material indices
  618. if (mMaterials.size() > 1 || inSettings.mMaterialsCapacity > 1)
  619. StoreMaterialIndices(inSettings);
  620. outResult.Set(this);
  621. }
  622. HeightFieldShape::~HeightFieldShape()
  623. {
  624. if (mRangeBlocks != nullptr)
  625. AlignedFree(mRangeBlocks);
  626. }
  627. Ref<HeightFieldShape> HeightFieldShape::Clone() const
  628. {
  629. Ref<HeightFieldShape> clone = new HeightFieldShape;
  630. clone->SetUserData(GetUserData());
  631. clone->mOffset = mOffset;
  632. clone->mScale = mScale;
  633. clone->mSampleCount = mSampleCount;
  634. clone->mBlockSize = mBlockSize;
  635. clone->mBitsPerSample = mBitsPerSample;
  636. clone->mSampleMask = mSampleMask;
  637. clone->mMinSample = mMinSample;
  638. clone->mMaxSample = mMaxSample;
  639. clone->AllocateBuffers();
  640. memcpy(clone->mRangeBlocks, mRangeBlocks, mRangeBlocksSize * sizeof(RangeBlock) + mHeightSamplesSize + mActiveEdgesSize); // Copy the entire buffer in 1 go
  641. clone->mMaterials.reserve(mMaterials.capacity()); // Ensure we keep the capacity of the original
  642. clone->mMaterials = mMaterials;
  643. clone->mMaterialIndices = mMaterialIndices;
  644. clone->mNumBitsPerMaterialIndex = mNumBitsPerMaterialIndex;
  645. #ifdef JPH_DEBUG_RENDERER
  646. clone->mGeometry = mGeometry;
  647. clone->mCachedUseMaterialColors = mCachedUseMaterialColors;
  648. #endif // JPH_DEBUG_RENDERER
  649. return clone;
  650. }
  651. inline void HeightFieldShape::sGetRangeBlockOffsetAndStride(uint inNumBlocks, uint inMaxLevel, uint &outRangeBlockOffset, uint &outRangeBlockStride)
  652. {
  653. outRangeBlockOffset = sGridOffsets[inMaxLevel - 1];
  654. outRangeBlockStride = (inNumBlocks + 1) >> 1;
  655. }
  656. inline void HeightFieldShape::GetRangeBlock(uint inBlockX, uint inBlockY, uint inRangeBlockOffset, uint inRangeBlockStride, RangeBlock *&outBlock, uint &outIndexInBlock)
  657. {
  658. JPH_ASSERT(inBlockX < GetNumBlocks() && inBlockY < GetNumBlocks());
  659. // Convert to location of range block
  660. uint rbx = inBlockX >> 1;
  661. uint rby = inBlockY >> 1;
  662. outIndexInBlock = ((inBlockY & 1) << 1) + (inBlockX & 1);
  663. uint offset = inRangeBlockOffset + rby * inRangeBlockStride + rbx;
  664. JPH_ASSERT(offset < mRangeBlocksSize);
  665. outBlock = mRangeBlocks + offset;
  666. }
  667. inline void HeightFieldShape::GetBlockOffsetAndScale(uint inBlockX, uint inBlockY, uint inRangeBlockOffset, uint inRangeBlockStride, float &outBlockOffset, float &outBlockScale) const
  668. {
  669. JPH_ASSERT(inBlockX < GetNumBlocks() && inBlockY < GetNumBlocks());
  670. // Convert to location of range block
  671. uint rbx = inBlockX >> 1;
  672. uint rby = inBlockY >> 1;
  673. uint n = ((inBlockY & 1) << 1) + (inBlockX & 1);
  674. // Calculate offset and scale
  675. uint offset = inRangeBlockOffset + rby * inRangeBlockStride + rbx;
  676. JPH_ASSERT(offset < mRangeBlocksSize);
  677. const RangeBlock &block = mRangeBlocks[offset];
  678. outBlockOffset = float(block.mMin[n]);
  679. outBlockScale = float(block.mMax[n] - block.mMin[n]) / float(mSampleMask);
  680. }
  681. inline uint8 HeightFieldShape::GetHeightSample(uint inX, uint inY) const
  682. {
  683. JPH_ASSERT(inX < mSampleCount);
  684. JPH_ASSERT(inY < mSampleCount);
  685. // Determine bit position of sample
  686. uint sample = (inY * mSampleCount + inX) * uint(mBitsPerSample);
  687. uint byte_pos = sample >> 3;
  688. uint bit_pos = sample & 0b111;
  689. // Fetch the height sample value
  690. JPH_ASSERT(byte_pos + 1 < mHeightSamplesSize);
  691. const uint8 *height_samples = mHeightSamples + byte_pos;
  692. uint16 height_sample = uint16(height_samples[0]) | uint16(uint16(height_samples[1]) << 8);
  693. return uint8(height_sample >> bit_pos) & mSampleMask;
  694. }
  695. inline Vec3 HeightFieldShape::GetPosition(uint inX, uint inY, float inBlockOffset, float inBlockScale, bool &outNoCollision) const
  696. {
  697. // Get quantized value
  698. uint8 height_sample = GetHeightSample(inX, inY);
  699. outNoCollision = height_sample == mSampleMask;
  700. // Add 0.5 to the quantized value to minimize the error (see constructor)
  701. return mOffset + mScale * Vec3(float(inX), inBlockOffset + (0.5f + height_sample) * inBlockScale, float(inY));
  702. }
  703. Vec3 HeightFieldShape::GetPosition(uint inX, uint inY) const
  704. {
  705. // Test if there are any samples
  706. if (mHeightSamplesSize == 0)
  707. return mOffset + mScale * Vec3(float(inX), 0.0f, float(inY));
  708. // Get block location
  709. uint bx = inX / mBlockSize;
  710. uint by = inY / mBlockSize;
  711. // Calculate offset and stride
  712. uint num_blocks = GetNumBlocks();
  713. uint range_block_offset, range_block_stride;
  714. sGetRangeBlockOffsetAndStride(num_blocks, sGetMaxLevel(num_blocks), range_block_offset, range_block_stride);
  715. float offset, scale;
  716. GetBlockOffsetAndScale(bx, by, range_block_offset, range_block_stride, offset, scale);
  717. bool no_collision;
  718. return GetPosition(inX, inY, offset, scale, no_collision);
  719. }
  720. bool HeightFieldShape::IsNoCollision(uint inX, uint inY) const
  721. {
  722. return mHeightSamplesSize == 0 || GetHeightSample(inX, inY) == mSampleMask;
  723. }
  724. bool HeightFieldShape::ProjectOntoSurface(Vec3Arg inLocalPosition, Vec3 &outSurfacePosition, SubShapeID &outSubShapeID) const
  725. {
  726. // Check if we have collision
  727. if (mHeightSamplesSize == 0)
  728. return false;
  729. // Convert coordinate to integer space
  730. Vec3 integer_space = (inLocalPosition - mOffset) / mScale;
  731. // Get x coordinate and fraction
  732. float x_frac = integer_space.GetX();
  733. if (x_frac < 0.0f || x_frac >= mSampleCount - 1)
  734. return false;
  735. uint x = (uint)floor(x_frac);
  736. x_frac -= x;
  737. // Get y coordinate and fraction
  738. float y_frac = integer_space.GetZ();
  739. if (y_frac < 0.0f || y_frac >= mSampleCount - 1)
  740. return false;
  741. uint y = (uint)floor(y_frac);
  742. y_frac -= y;
  743. // If one of the diagonal points doesn't have collision, we don't have a height at this location
  744. if (IsNoCollision(x, y) || IsNoCollision(x + 1, y + 1))
  745. return false;
  746. if (y_frac >= x_frac)
  747. {
  748. // Left bottom triangle, test the 3rd point
  749. if (IsNoCollision(x, y + 1))
  750. return false;
  751. // Interpolate height value
  752. Vec3 v1 = GetPosition(x, y);
  753. Vec3 v2 = GetPosition(x, y + 1);
  754. Vec3 v3 = GetPosition(x + 1, y + 1);
  755. outSurfacePosition = v1 + y_frac * (v2 - v1) + x_frac * (v3 - v2);
  756. SubShapeIDCreator creator;
  757. outSubShapeID = EncodeSubShapeID(creator, x, y, 0);
  758. return true;
  759. }
  760. else
  761. {
  762. // Right top triangle, test the third point
  763. if (IsNoCollision(x + 1, y))
  764. return false;
  765. // Interpolate height value
  766. Vec3 v1 = GetPosition(x, y);
  767. Vec3 v2 = GetPosition(x + 1, y + 1);
  768. Vec3 v3 = GetPosition(x + 1, y);
  769. outSurfacePosition = v1 + y_frac * (v2 - v3) + x_frac * (v3 - v1);
  770. SubShapeIDCreator creator;
  771. outSubShapeID = EncodeSubShapeID(creator, x, y, 1);
  772. return true;
  773. }
  774. }
  775. void HeightFieldShape::GetHeights(uint inX, uint inY, uint inSizeX, uint inSizeY, float *outHeights, intptr_t inHeightsStride) const
  776. {
  777. if (inSizeX == 0 || inSizeY == 0)
  778. return;
  779. JPH_ASSERT(inX % mBlockSize == 0 && inY % mBlockSize == 0);
  780. JPH_ASSERT(inX < mSampleCount && inY < mSampleCount);
  781. JPH_ASSERT(inX + inSizeX <= mSampleCount && inY + inSizeY <= mSampleCount);
  782. // Test if there are any samples
  783. if (mHeightSamplesSize == 0)
  784. {
  785. // No samples, return the offset
  786. float offset = mOffset.GetY();
  787. for (uint y = 0; y < inSizeY; ++y, outHeights += inHeightsStride)
  788. for (uint x = 0; x < inSizeX; ++x)
  789. outHeights[x] = offset;
  790. }
  791. else
  792. {
  793. // Calculate offset and stride
  794. uint num_blocks = GetNumBlocks();
  795. uint range_block_offset, range_block_stride;
  796. sGetRangeBlockOffsetAndStride(num_blocks, sGetMaxLevel(num_blocks), range_block_offset, range_block_stride);
  797. // Loop over blocks
  798. uint block_start_x = inX / mBlockSize;
  799. uint block_start_y = inY / mBlockSize;
  800. uint num_blocks_x = inSizeX / mBlockSize;
  801. uint num_blocks_y = inSizeY / mBlockSize;
  802. for (uint block_y = 0; block_y < num_blocks_y; ++block_y)
  803. for (uint block_x = 0; block_x < num_blocks_x; ++block_x)
  804. {
  805. // Get offset and scale for block
  806. float offset, scale;
  807. GetBlockOffsetAndScale(block_start_x + block_x, block_start_y + block_y, range_block_offset, range_block_stride, offset, scale);
  808. // Adjust by global offset and scale
  809. // Note: This is the math applied in GetPosition() written out to reduce calculations in the inner loop
  810. scale *= mScale.GetY();
  811. offset = mOffset.GetY() + mScale.GetY() * offset + 0.5f * scale;
  812. // Loop over samples in block
  813. for (uint sample_y = 0; sample_y < mBlockSize; ++sample_y)
  814. for (uint sample_x = 0; sample_x < mBlockSize; ++sample_x)
  815. {
  816. // Calculate output coordinate
  817. uint output_x = block_x * mBlockSize + sample_x;
  818. uint output_y = block_y * mBlockSize + sample_y;
  819. // Get quantized value
  820. uint8 height_sample = GetHeightSample(inX + output_x, inY + output_y);
  821. // Dequantize
  822. float h = height_sample != mSampleMask? offset + height_sample * scale : cNoCollisionValue;
  823. outHeights[output_y * inHeightsStride + output_x] = h;
  824. }
  825. }
  826. }
  827. }
  828. void HeightFieldShape::SetHeights(uint inX, uint inY, uint inSizeX, uint inSizeY, const float *inHeights, intptr_t inHeightsStride, TempAllocator &inAllocator, float inActiveEdgeCosThresholdAngle)
  829. {
  830. if (inSizeX == 0 || inSizeY == 0)
  831. return;
  832. JPH_ASSERT(mHeightSamplesSize > 0);
  833. JPH_ASSERT(inX % mBlockSize == 0 && inY % mBlockSize == 0);
  834. JPH_ASSERT(inX < mSampleCount && inY < mSampleCount);
  835. JPH_ASSERT(inX + inSizeX <= mSampleCount && inY + inSizeY <= mSampleCount);
  836. // If we have a block in negative x/y direction, we will affect its range so we need to take it into account
  837. bool need_temp_heights = false;
  838. uint affected_x = inX;
  839. uint affected_y = inY;
  840. uint affected_size_x = inSizeX;
  841. uint affected_size_y = inSizeY;
  842. if (inX > 0) { affected_x -= mBlockSize; affected_size_x += mBlockSize; need_temp_heights = true; }
  843. if (inY > 0) { affected_y -= mBlockSize; affected_size_y += mBlockSize; need_temp_heights = true; }
  844. // If we have a block in positive x/y direction, our ranges are affected by it so we need to take it into account
  845. uint heights_size_x = affected_size_x;
  846. uint heights_size_y = affected_size_y;
  847. if (inX + inSizeX < mSampleCount) { heights_size_x += mBlockSize; need_temp_heights = true; }
  848. if (inY + inSizeY < mSampleCount) { heights_size_y += mBlockSize; need_temp_heights = true; }
  849. // Get heights for affected area
  850. const float *heights;
  851. intptr_t heights_stride;
  852. float *temp_heights;
  853. if (need_temp_heights)
  854. {
  855. // Fetch the surrounding height data (note we're forced to recompress this data with a potentially different range so there will be some precision loss here)
  856. temp_heights = (float *)inAllocator.Allocate(heights_size_x * heights_size_y * sizeof(float));
  857. heights = temp_heights;
  858. heights_stride = heights_size_x;
  859. // We need to fill in the following areas:
  860. //
  861. // +-----------------+
  862. // | 2 |
  863. // |---+---------+---|
  864. // | | | |
  865. // | 3 | 1 | 4 |
  866. // | | | |
  867. // |---+---------+---|
  868. // | 5 |
  869. // +-----------------+
  870. //
  871. // 1. The area that is affected by the new heights (we just copy these)
  872. // 2-5. These areas are either needed to calculate the range of the affected blocks or they need to be recompressed with a different range
  873. uint offset_x = inX - affected_x;
  874. uint offset_y = inY - affected_y;
  875. // Area 2
  876. GetHeights(affected_x, affected_y, heights_size_x, offset_y, temp_heights, heights_size_x);
  877. float *area3_start = temp_heights + offset_y * heights_size_x;
  878. // Area 3
  879. GetHeights(affected_x, inY, offset_x, inSizeY, area3_start, heights_size_x);
  880. // Area 1
  881. float *area1_start = area3_start + offset_x;
  882. for (uint y = 0; y < inSizeY; ++y, area1_start += heights_size_x, inHeights += inHeightsStride)
  883. memcpy(area1_start, inHeights, inSizeX * sizeof(float));
  884. // Area 4
  885. uint area4_x = inX + inSizeX;
  886. GetHeights(area4_x, inY, affected_x + heights_size_x - area4_x, inSizeY, area3_start + area4_x - affected_x, heights_size_x);
  887. // Area 5
  888. uint area5_y = inY + inSizeY;
  889. float *area5_start = temp_heights + (area5_y - affected_y) * heights_size_x;
  890. GetHeights(affected_x, area5_y, heights_size_x, affected_y + heights_size_y - area5_y, area5_start, heights_size_x);
  891. }
  892. else
  893. {
  894. // We can directly use the input buffer because there are no extra edges to take into account
  895. heights = inHeights;
  896. heights_stride = inHeightsStride;
  897. temp_heights = nullptr;
  898. }
  899. // Calculate offset and stride
  900. uint num_blocks = GetNumBlocks();
  901. uint range_block_offset, range_block_stride;
  902. uint max_level = sGetMaxLevel(num_blocks);
  903. sGetRangeBlockOffsetAndStride(num_blocks, max_level, range_block_offset, range_block_stride);
  904. // Loop over blocks
  905. uint block_start_x = affected_x / mBlockSize;
  906. uint block_start_y = affected_y / mBlockSize;
  907. uint num_blocks_x = affected_size_x / mBlockSize;
  908. uint num_blocks_y = affected_size_y / mBlockSize;
  909. for (uint block_y = 0, sample_start_y = 0; block_y < num_blocks_y; ++block_y, sample_start_y += mBlockSize)
  910. for (uint block_x = 0, sample_start_x = 0; block_x < num_blocks_x; ++block_x, sample_start_x += mBlockSize)
  911. {
  912. // Determine quantized min and max value for block
  913. // Note that we need to include 1 extra row in the positive x/y direction to account for connecting triangles
  914. int min_value = 0xffff;
  915. int max_value = 0;
  916. uint sample_x_end = min(sample_start_x + mBlockSize + 1, mSampleCount - affected_x);
  917. uint sample_y_end = min(sample_start_y + mBlockSize + 1, mSampleCount - affected_y);
  918. for (uint sample_y = sample_start_y; sample_y < sample_y_end; ++sample_y)
  919. for (uint sample_x = sample_start_x; sample_x < sample_x_end; ++sample_x)
  920. {
  921. float h = heights[sample_y * heights_stride + sample_x];
  922. if (h != cNoCollisionValue)
  923. {
  924. int quantized_height = Clamp((int)floor((h - mOffset.GetY()) / mScale.GetY()), 0, int(cMaxHeightValue16 - 1));
  925. min_value = min(min_value, quantized_height);
  926. max_value = max(max_value, quantized_height + 1);
  927. }
  928. }
  929. if (min_value > max_value)
  930. min_value = max_value = cNoCollisionValue16;
  931. // Update range for block
  932. RangeBlock *range_block;
  933. uint index_in_block;
  934. GetRangeBlock(block_start_x + block_x, block_start_y + block_y, range_block_offset, range_block_stride, range_block, index_in_block);
  935. range_block->mMin[index_in_block] = uint16(min_value);
  936. range_block->mMax[index_in_block] = uint16(max_value);
  937. // Get offset and scale for block
  938. float offset_block = float(min_value);
  939. float scale_block = float(max_value - min_value) / float(mSampleMask);
  940. // Calculate scale and offset using the formula used in GetPosition() solved for the quantized height (excluding 0.5 because we round down while quantizing)
  941. float scale = scale_block * mScale.GetY();
  942. float offset = mOffset.GetY() + offset_block * mScale.GetY();
  943. // Loop over samples in block
  944. sample_x_end = sample_start_x + mBlockSize;
  945. sample_y_end = sample_start_y + mBlockSize;
  946. for (uint sample_y = sample_start_y; sample_y < sample_y_end; ++sample_y)
  947. for (uint sample_x = sample_start_x; sample_x < sample_x_end; ++sample_x)
  948. {
  949. // Quantize height
  950. float h = heights[sample_y * heights_stride + sample_x];
  951. uint8 quantized_height = h != cNoCollisionValue? uint8(Clamp((int)floor((h - offset) / scale), 0, int(mSampleMask) - 1)) : mSampleMask;
  952. // Determine bit position of sample
  953. uint sample = ((affected_y + sample_y) * mSampleCount + affected_x + sample_x) * uint(mBitsPerSample);
  954. uint byte_pos = sample >> 3;
  955. uint bit_pos = sample & 0b111;
  956. // Update the height value sample
  957. JPH_ASSERT(byte_pos + 1 < mHeightSamplesSize);
  958. uint8 *height_samples = mHeightSamples + byte_pos;
  959. uint16 height_sample = uint16(height_samples[0]) | uint16(uint16(height_samples[1]) << 8);
  960. height_sample &= ~(uint16(mSampleMask) << bit_pos);
  961. height_sample |= uint16(quantized_height) << bit_pos;
  962. height_samples[0] = uint8(height_sample);
  963. height_samples[1] = uint8(height_sample >> 8);
  964. }
  965. }
  966. // Update active edges
  967. // Note that we must take an extra row on all sides to account for connecting triangles
  968. uint ae_x = inX > 1? inX - 2 : 0;
  969. uint ae_y = inY > 1? inY - 2 : 0;
  970. uint ae_sx = min(inX + inSizeX + 1, mSampleCount - 1) - ae_x;
  971. uint ae_sy = min(inY + inSizeY + 1, mSampleCount - 1) - ae_y;
  972. CalculateActiveEdges(ae_x, ae_y, ae_sx, ae_sy, heights, affected_x, affected_y, heights_stride, 1.0f, inActiveEdgeCosThresholdAngle, inAllocator);
  973. // Free temporary buffer
  974. if (temp_heights != nullptr)
  975. inAllocator.Free(temp_heights, heights_size_x * heights_size_y * sizeof(float));
  976. // Update hierarchy of range blocks
  977. while (max_level > 1)
  978. {
  979. // Get offset and stride for destination blocks
  980. uint dst_range_block_offset, dst_range_block_stride;
  981. sGetRangeBlockOffsetAndStride(num_blocks >> 1, max_level - 1, dst_range_block_offset, dst_range_block_stride);
  982. // We'll be processing 2x2 blocks below so we need the start coordinates to be even and we extend the number of blocks to correct for that
  983. if (block_start_x & 1) { --block_start_x; ++num_blocks_x; }
  984. if (block_start_y & 1) { --block_start_y; ++num_blocks_y; }
  985. // Loop over all affected blocks
  986. uint block_end_x = block_start_x + num_blocks_x;
  987. uint block_end_y = block_start_y + num_blocks_y;
  988. for (uint block_y = block_start_y; block_y < block_end_y; block_y += 2)
  989. for (uint block_x = block_start_x; block_x < block_end_x; block_x += 2)
  990. {
  991. // Get source range block
  992. RangeBlock *src_range_block;
  993. uint index_in_src_block;
  994. GetRangeBlock(block_x, block_y, range_block_offset, range_block_stride, src_range_block, index_in_src_block);
  995. // Determine quantized min and max value for the entire 2x2 block
  996. uint16 min_value = 0xffff;
  997. uint16 max_value = 0;
  998. for (uint i = 0; i < 4; ++i)
  999. if (src_range_block->mMin[i] != cNoCollisionValue16)
  1000. {
  1001. min_value = min(min_value, src_range_block->mMin[i]);
  1002. max_value = max(max_value, src_range_block->mMax[i]);
  1003. }
  1004. // Write to destination block
  1005. RangeBlock *dst_range_block;
  1006. uint index_in_dst_block;
  1007. GetRangeBlock(block_x >> 1, block_y >> 1, dst_range_block_offset, dst_range_block_stride, dst_range_block, index_in_dst_block);
  1008. dst_range_block->mMin[index_in_dst_block] = uint16(min_value);
  1009. dst_range_block->mMax[index_in_dst_block] = uint16(max_value);
  1010. }
  1011. // Go up one level
  1012. --max_level;
  1013. num_blocks >>= 1;
  1014. block_start_x >>= 1;
  1015. block_start_y >>= 1;
  1016. num_blocks_x = min((num_blocks_x + 1) >> 1, num_blocks);
  1017. num_blocks_y = min((num_blocks_y + 1) >> 1, num_blocks);
  1018. // Update stride and offset for source to old destination
  1019. range_block_offset = dst_range_block_offset;
  1020. range_block_stride = dst_range_block_stride;
  1021. }
  1022. // Calculate new min and max sample for the entire height field
  1023. mMinSample = 0xffff;
  1024. mMaxSample = 0;
  1025. for (uint i = 0; i < 4; ++i)
  1026. if (mRangeBlocks[0].mMin[i] != cNoCollisionValue16)
  1027. {
  1028. mMinSample = min(mMinSample, mRangeBlocks[0].mMin[i]);
  1029. mMaxSample = max(mMaxSample, mRangeBlocks[0].mMax[i]);
  1030. }
  1031. #ifdef JPH_DEBUG_RENDERER
  1032. // Invalidate temporary rendering data
  1033. mGeometry.clear();
  1034. #endif
  1035. }
  1036. void HeightFieldShape::GetMaterials(uint inX, uint inY, uint inSizeX, uint inSizeY, uint8 *outMaterials, intptr_t inMaterialsStride) const
  1037. {
  1038. if (inSizeX == 0 || inSizeY == 0)
  1039. return;
  1040. if (mMaterialIndices.empty())
  1041. {
  1042. // Return all 0's
  1043. for (uint y = 0; y < inSizeY; ++y)
  1044. {
  1045. uint8 *out_indices = outMaterials + y * inMaterialsStride;
  1046. for (uint x = 0; x < inSizeX; ++x)
  1047. *out_indices++ = 0;
  1048. }
  1049. return;
  1050. }
  1051. JPH_ASSERT(inX < mSampleCount && inY < mSampleCount);
  1052. JPH_ASSERT(inX + inSizeX < mSampleCount && inY + inSizeY < mSampleCount);
  1053. uint count_min_1 = mSampleCount - 1;
  1054. uint16 material_index_mask = uint16((1 << mNumBitsPerMaterialIndex) - 1);
  1055. for (uint y = 0; y < inSizeY; ++y)
  1056. {
  1057. // Calculate input position
  1058. uint bit_pos = (inX + (inY + y) * count_min_1) * mNumBitsPerMaterialIndex;
  1059. const uint8 *in_indices = mMaterialIndices.data() + (bit_pos >> 3);
  1060. bit_pos &= 0b111;
  1061. // Calculate output position
  1062. uint8 *out_indices = outMaterials + y * inMaterialsStride;
  1063. for (uint x = 0; x < inSizeX; ++x)
  1064. {
  1065. // Get material index
  1066. uint16 material_index = uint16(in_indices[0]) + uint16(uint16(in_indices[1]) << 8);
  1067. material_index >>= bit_pos;
  1068. material_index &= material_index_mask;
  1069. *out_indices = uint8(material_index);
  1070. // Go to the next index
  1071. bit_pos += mNumBitsPerMaterialIndex;
  1072. in_indices += bit_pos >> 3;
  1073. bit_pos &= 0b111;
  1074. ++out_indices;
  1075. }
  1076. }
  1077. }
  1078. bool HeightFieldShape::SetMaterials(uint inX, uint inY, uint inSizeX, uint inSizeY, const uint8 *inMaterials, intptr_t inMaterialsStride, const PhysicsMaterialList *inMaterialList, TempAllocator &inAllocator)
  1079. {
  1080. if (inSizeX == 0 || inSizeY == 0)
  1081. return true;
  1082. JPH_ASSERT(inX < mSampleCount && inY < mSampleCount);
  1083. JPH_ASSERT(inX + inSizeX < mSampleCount && inY + inSizeY < mSampleCount);
  1084. // Remap materials
  1085. uint material_remap_table_size = uint(inMaterialList != nullptr? inMaterialList->size() : mMaterials.size());
  1086. uint8 *material_remap_table = (uint8 *)inAllocator.Allocate(material_remap_table_size);
  1087. JPH_SCOPE_EXIT([&inAllocator, material_remap_table, material_remap_table_size]{ inAllocator.Free(material_remap_table, material_remap_table_size); });
  1088. if (inMaterialList != nullptr)
  1089. {
  1090. // Conservatively reserve more space if the incoming material list is bigger
  1091. if (inMaterialList->size() > mMaterials.size())
  1092. mMaterials.reserve(inMaterialList->size());
  1093. // Create a remap table
  1094. uint8 *remap_entry = material_remap_table;
  1095. for (const PhysicsMaterial *material : *inMaterialList)
  1096. {
  1097. // Try to find it in the existing list
  1098. PhysicsMaterialList::const_iterator it = std::find(mMaterials.begin(), mMaterials.end(), material);
  1099. if (it != mMaterials.end())
  1100. {
  1101. // Found it, calculate index
  1102. *remap_entry = uint8(it - mMaterials.begin());
  1103. }
  1104. else
  1105. {
  1106. // Not found, add it
  1107. if (mMaterials.size() >= 256)
  1108. {
  1109. // We can't have more than 256 materials since we use uint8 as indices
  1110. return false;
  1111. }
  1112. *remap_entry = uint8(mMaterials.size());
  1113. mMaterials.push_back(material);
  1114. }
  1115. ++remap_entry;
  1116. }
  1117. }
  1118. else
  1119. {
  1120. // No remapping
  1121. for (uint i = 0; i < material_remap_table_size; ++i)
  1122. material_remap_table[i] = uint8(i);
  1123. }
  1124. if (mMaterials.size() == 1)
  1125. {
  1126. // Only 1 material, we don't need to store the material indices
  1127. return true;
  1128. }
  1129. // Check if we need to resize the material indices array
  1130. uint count_min_1 = mSampleCount - 1;
  1131. uint32 new_bits_per_material_index = 32 - CountLeadingZeros((uint32)mMaterials.size() - 1);
  1132. JPH_ASSERT(mNumBitsPerMaterialIndex <= 8 && new_bits_per_material_index <= 8);
  1133. if (new_bits_per_material_index > mNumBitsPerMaterialIndex)
  1134. {
  1135. // Resize the material indices array
  1136. mMaterialIndices.resize(((Square(count_min_1) * new_bits_per_material_index + 7) >> 3) + 1, 0); // Add 1 byte so we don't read out of bounds when reading an uint16
  1137. // Calculate old and new mask
  1138. uint16 old_material_index_mask = uint16((1 << mNumBitsPerMaterialIndex) - 1);
  1139. uint16 new_material_index_mask = uint16((1 << new_bits_per_material_index) - 1);
  1140. // Loop through the array backwards to avoid overwriting data
  1141. int in_bit_pos = (count_min_1 * count_min_1 - 1) * mNumBitsPerMaterialIndex;
  1142. const uint8 *in_indices = mMaterialIndices.data() + (in_bit_pos >> 3);
  1143. in_bit_pos &= 0b111;
  1144. int out_bit_pos = (count_min_1 * count_min_1 - 1) * new_bits_per_material_index;
  1145. uint8 *out_indices = mMaterialIndices.data() + (out_bit_pos >> 3);
  1146. out_bit_pos &= 0b111;
  1147. while (out_indices >= mMaterialIndices.data())
  1148. {
  1149. // Read the material index
  1150. uint16 material_index = uint16(in_indices[0]) + uint16(uint16(in_indices[1]) << 8);
  1151. material_index >>= in_bit_pos;
  1152. material_index &= old_material_index_mask;
  1153. // Write the material index
  1154. uint16 output_data = uint16(out_indices[0]) + uint16(uint16(out_indices[1]) << 8);
  1155. output_data &= ~(new_material_index_mask << out_bit_pos);
  1156. output_data |= material_index << out_bit_pos;
  1157. out_indices[0] = uint8(output_data);
  1158. out_indices[1] = uint8(output_data >> 8);
  1159. // Go to the previous index
  1160. in_bit_pos -= int(mNumBitsPerMaterialIndex);
  1161. in_indices += in_bit_pos >> 3;
  1162. in_bit_pos &= 0b111;
  1163. out_bit_pos -= int(new_bits_per_material_index);
  1164. out_indices += out_bit_pos >> 3;
  1165. out_bit_pos &= 0b111;
  1166. }
  1167. // Accept the new bits per material index
  1168. mNumBitsPerMaterialIndex = new_bits_per_material_index;
  1169. }
  1170. uint16 material_index_mask = uint16((1 << mNumBitsPerMaterialIndex) - 1);
  1171. for (uint y = 0; y < inSizeY; ++y)
  1172. {
  1173. // Calculate input position
  1174. const uint8 *in_indices = inMaterials + y * inMaterialsStride;
  1175. // Calculate output position
  1176. uint bit_pos = (inX + (inY + y) * count_min_1) * mNumBitsPerMaterialIndex;
  1177. uint8 *out_indices = mMaterialIndices.data() + (bit_pos >> 3);
  1178. bit_pos &= 0b111;
  1179. for (uint x = 0; x < inSizeX; ++x)
  1180. {
  1181. // Update material
  1182. uint16 output_data = uint16(out_indices[0]) + uint16(uint16(out_indices[1]) << 8);
  1183. output_data &= ~(material_index_mask << bit_pos);
  1184. output_data |= material_remap_table[*in_indices] << bit_pos;
  1185. out_indices[0] = uint8(output_data);
  1186. out_indices[1] = uint8(output_data >> 8);
  1187. // Go to the next index
  1188. in_indices++;
  1189. bit_pos += mNumBitsPerMaterialIndex;
  1190. out_indices += bit_pos >> 3;
  1191. bit_pos &= 0b111;
  1192. }
  1193. }
  1194. return true;
  1195. }
  1196. MassProperties HeightFieldShape::GetMassProperties() const
  1197. {
  1198. // Object should always be static, return default mass properties
  1199. return MassProperties();
  1200. }
  1201. const PhysicsMaterial *HeightFieldShape::GetMaterial(uint inX, uint inY) const
  1202. {
  1203. if (mMaterials.empty())
  1204. return PhysicsMaterial::sDefault;
  1205. if (mMaterials.size() == 1)
  1206. return mMaterials[0];
  1207. uint count_min_1 = mSampleCount - 1;
  1208. JPH_ASSERT(inX < count_min_1);
  1209. JPH_ASSERT(inY < count_min_1);
  1210. // Calculate at which bit the material index starts
  1211. uint bit_pos = (inX + inY * count_min_1) * mNumBitsPerMaterialIndex;
  1212. uint byte_pos = bit_pos >> 3;
  1213. bit_pos &= 0b111;
  1214. // Read the material index
  1215. JPH_ASSERT(byte_pos + 1 < mMaterialIndices.size());
  1216. const uint8 *material_indices = mMaterialIndices.data() + byte_pos;
  1217. uint16 material_index = uint16(material_indices[0]) + uint16(uint16(material_indices[1]) << 8);
  1218. material_index >>= bit_pos;
  1219. material_index &= (1 << mNumBitsPerMaterialIndex) - 1;
  1220. // Return the material
  1221. return mMaterials[material_index];
  1222. }
  1223. uint HeightFieldShape::GetSubShapeIDBits() const
  1224. {
  1225. // Need to store X, Y and 1 extra bit to specify the triangle number in the quad
  1226. return 2 * (32 - CountLeadingZeros(mSampleCount - 1)) + 1;
  1227. }
  1228. SubShapeID HeightFieldShape::EncodeSubShapeID(const SubShapeIDCreator &inCreator, uint inX, uint inY, uint inTriangle) const
  1229. {
  1230. return inCreator.PushID((inX + inY * mSampleCount) * 2 + inTriangle, GetSubShapeIDBits()).GetID();
  1231. }
  1232. void HeightFieldShape::DecodeSubShapeID(const SubShapeID &inSubShapeID, uint &outX, uint &outY, uint &outTriangle) const
  1233. {
  1234. // Decode sub shape id
  1235. SubShapeID remainder;
  1236. uint32 id = inSubShapeID.PopID(GetSubShapeIDBits(), remainder);
  1237. JPH_ASSERT(remainder.IsEmpty(), "Invalid subshape ID");
  1238. // Get triangle index
  1239. outTriangle = id & 1;
  1240. id >>= 1;
  1241. // Fetch the x and y coordinate
  1242. outX = id % mSampleCount;
  1243. outY = id / mSampleCount;
  1244. }
  1245. void HeightFieldShape::GetSubShapeCoordinates(const SubShapeID &inSubShapeID, uint &outX, uint &outY, uint &outTriangleIndex) const
  1246. {
  1247. DecodeSubShapeID(inSubShapeID, outX, outY, outTriangleIndex);
  1248. }
  1249. const PhysicsMaterial *HeightFieldShape::GetMaterial(const SubShapeID &inSubShapeID) const
  1250. {
  1251. // Decode ID
  1252. uint x, y, triangle;
  1253. DecodeSubShapeID(inSubShapeID, x, y, triangle);
  1254. // Fetch the material
  1255. return GetMaterial(x, y);
  1256. }
  1257. Vec3 HeightFieldShape::GetSurfaceNormal(const SubShapeID &inSubShapeID, Vec3Arg inLocalSurfacePosition) const
  1258. {
  1259. // Decode ID
  1260. uint x, y, triangle;
  1261. DecodeSubShapeID(inSubShapeID, x, y, triangle);
  1262. // Fetch vertices that both triangles share
  1263. Vec3 x1y1 = GetPosition(x, y);
  1264. Vec3 x2y2 = GetPosition(x + 1, y + 1);
  1265. // Get normal depending on which triangle was selected
  1266. Vec3 normal;
  1267. if (triangle == 0)
  1268. {
  1269. Vec3 x1y2 = GetPosition(x, y + 1);
  1270. normal = (x2y2 - x1y2).Cross(x1y1 - x1y2);
  1271. }
  1272. else
  1273. {
  1274. Vec3 x2y1 = GetPosition(x + 1, y);
  1275. normal = (x1y1 - x2y1).Cross(x2y2 - x2y1);
  1276. }
  1277. return normal.Normalized();
  1278. }
  1279. void HeightFieldShape::GetSupportingFace(const SubShapeID &inSubShapeID, Vec3Arg inDirection, Vec3Arg inScale, Mat44Arg inCenterOfMassTransform, SupportingFace &outVertices) const
  1280. {
  1281. // Decode ID
  1282. uint x, y, triangle;
  1283. DecodeSubShapeID(inSubShapeID, x, y, triangle);
  1284. // Fetch the triangle
  1285. outVertices.resize(3);
  1286. outVertices[0] = GetPosition(x, y);
  1287. Vec3 v2 = GetPosition(x + 1, y + 1);
  1288. if (triangle == 0)
  1289. {
  1290. outVertices[1] = GetPosition(x, y + 1);
  1291. outVertices[2] = v2;
  1292. }
  1293. else
  1294. {
  1295. outVertices[1] = v2;
  1296. outVertices[2] = GetPosition(x + 1, y);
  1297. }
  1298. // Flip triangle if scaled inside out
  1299. if (ScaleHelpers::IsInsideOut(inScale))
  1300. swap(outVertices[1], outVertices[2]);
  1301. // Transform to world space
  1302. Mat44 transform = inCenterOfMassTransform.PreScaled(inScale);
  1303. for (Vec3 &v : outVertices)
  1304. v = transform * v;
  1305. }
  1306. inline uint8 HeightFieldShape::GetEdgeFlags(uint inX, uint inY, uint inTriangle) const
  1307. {
  1308. JPH_ASSERT(inX < mSampleCount - 1 && inY < mSampleCount - 1);
  1309. if (inTriangle == 0)
  1310. {
  1311. // The edge flags for this triangle are directly stored, find the right 3 bits
  1312. uint bit_pos = 3 * (inX + inY * (mSampleCount - 1));
  1313. uint byte_pos = bit_pos >> 3;
  1314. bit_pos &= 0b111;
  1315. JPH_ASSERT(byte_pos + 1 < mActiveEdgesSize);
  1316. const uint8 *active_edges = mActiveEdges + byte_pos;
  1317. uint16 edge_flags = uint16(active_edges[0]) + uint16(uint16(active_edges[1]) << 8);
  1318. return uint8(edge_flags >> bit_pos) & 0b111;
  1319. }
  1320. else
  1321. {
  1322. // We don't store this triangle directly, we need to look at our three neighbours to construct the edge flags
  1323. uint8 edge0 = (GetEdgeFlags(inX, inY, 0) & 0b100) != 0? 0b001 : 0; // Diagonal edge
  1324. uint8 edge1 = inX == mSampleCount - 2 || (GetEdgeFlags(inX + 1, inY, 0) & 0b001) != 0? 0b010 : 0; // Vertical edge
  1325. uint8 edge2 = inY == 0 || (GetEdgeFlags(inX, inY - 1, 0) & 0b010) != 0? 0b100 : 0; // Horizontal edge
  1326. return edge0 | edge1 | edge2;
  1327. }
  1328. }
  1329. AABox HeightFieldShape::GetLocalBounds() const
  1330. {
  1331. if (mMinSample == cNoCollisionValue16)
  1332. {
  1333. // This whole height field shape doesn't have any collision, return the center point
  1334. Vec3 center = mOffset + 0.5f * mScale * Vec3(float(mSampleCount - 1), 0.0f, float(mSampleCount - 1));
  1335. return AABox(center, center);
  1336. }
  1337. else
  1338. {
  1339. // Bounding box based on min and max sample height
  1340. Vec3 bmin = mOffset + mScale * Vec3(0.0f, float(mMinSample), 0.0f);
  1341. Vec3 bmax = mOffset + mScale * Vec3(float(mSampleCount - 1), float(mMaxSample), float(mSampleCount - 1));
  1342. return AABox(bmin, bmax);
  1343. }
  1344. }
  1345. #ifdef JPH_DEBUG_RENDERER
  1346. void HeightFieldShape::Draw(DebugRenderer *inRenderer, RMat44Arg inCenterOfMassTransform, Vec3Arg inScale, ColorArg inColor, bool inUseMaterialColors, bool inDrawWireframe) const
  1347. {
  1348. // Don't draw anything if we don't have any collision
  1349. if (mHeightSamplesSize == 0)
  1350. return;
  1351. // Reset the batch if we switch coloring mode
  1352. if (mCachedUseMaterialColors != inUseMaterialColors)
  1353. {
  1354. mGeometry.clear();
  1355. mCachedUseMaterialColors = inUseMaterialColors;
  1356. }
  1357. if (mGeometry.empty())
  1358. {
  1359. // Divide terrain in triangle batches of max 64x64x2 triangles to allow better culling of the terrain
  1360. uint32 block_size = min<uint32>(mSampleCount, 64);
  1361. for (uint32 by = 0; by < mSampleCount; by += block_size)
  1362. for (uint32 bx = 0; bx < mSampleCount; bx += block_size)
  1363. {
  1364. // Create vertices for a block
  1365. Array<DebugRenderer::Triangle> triangles;
  1366. triangles.resize(block_size * block_size * 2);
  1367. DebugRenderer::Triangle *out_tri = &triangles[0];
  1368. for (uint32 y = by, max_y = min(by + block_size, mSampleCount - 1); y < max_y; ++y)
  1369. for (uint32 x = bx, max_x = min(bx + block_size, mSampleCount - 1); x < max_x; ++x)
  1370. if (!IsNoCollision(x, y) && !IsNoCollision(x + 1, y + 1))
  1371. {
  1372. Vec3 x1y1 = GetPosition(x, y);
  1373. Vec3 x2y2 = GetPosition(x + 1, y + 1);
  1374. Color color = inUseMaterialColors? GetMaterial(x, y)->GetDebugColor() : Color::sWhite;
  1375. if (!IsNoCollision(x, y + 1))
  1376. {
  1377. Vec3 x1y2 = GetPosition(x, y + 1);
  1378. x1y1.StoreFloat3(&out_tri->mV[0].mPosition);
  1379. x1y2.StoreFloat3(&out_tri->mV[1].mPosition);
  1380. x2y2.StoreFloat3(&out_tri->mV[2].mPosition);
  1381. Vec3 normal = (x2y2 - x1y2).Cross(x1y1 - x1y2).Normalized();
  1382. for (DebugRenderer::Vertex &v : out_tri->mV)
  1383. {
  1384. v.mColor = color;
  1385. v.mUV = Float2(0, 0);
  1386. normal.StoreFloat3(&v.mNormal);
  1387. }
  1388. ++out_tri;
  1389. }
  1390. if (!IsNoCollision(x + 1, y))
  1391. {
  1392. Vec3 x2y1 = GetPosition(x + 1, y);
  1393. x1y1.StoreFloat3(&out_tri->mV[0].mPosition);
  1394. x2y2.StoreFloat3(&out_tri->mV[1].mPosition);
  1395. x2y1.StoreFloat3(&out_tri->mV[2].mPosition);
  1396. Vec3 normal = (x1y1 - x2y1).Cross(x2y2 - x2y1).Normalized();
  1397. for (DebugRenderer::Vertex &v : out_tri->mV)
  1398. {
  1399. v.mColor = color;
  1400. v.mUV = Float2(0, 0);
  1401. normal.StoreFloat3(&v.mNormal);
  1402. }
  1403. ++out_tri;
  1404. }
  1405. }
  1406. // Resize triangles array to actual amount of triangles written
  1407. size_t num_triangles = out_tri - &triangles[0];
  1408. triangles.resize(num_triangles);
  1409. // Create batch
  1410. if (num_triangles > 0)
  1411. mGeometry.push_back(new DebugRenderer::Geometry(inRenderer->CreateTriangleBatch(triangles), DebugRenderer::sCalculateBounds(&triangles[0].mV[0], int(3 * num_triangles))));
  1412. }
  1413. }
  1414. // Get transform including scale
  1415. RMat44 transform = inCenterOfMassTransform.PreScaled(inScale);
  1416. // Test if the shape is scaled inside out
  1417. DebugRenderer::ECullMode cull_mode = ScaleHelpers::IsInsideOut(inScale)? DebugRenderer::ECullMode::CullFrontFace : DebugRenderer::ECullMode::CullBackFace;
  1418. // Determine the draw mode
  1419. DebugRenderer::EDrawMode draw_mode = inDrawWireframe? DebugRenderer::EDrawMode::Wireframe : DebugRenderer::EDrawMode::Solid;
  1420. // Draw the geometry
  1421. for (const DebugRenderer::GeometryRef &b : mGeometry)
  1422. inRenderer->DrawGeometry(transform, inColor, b, cull_mode, DebugRenderer::ECastShadow::On, draw_mode);
  1423. if (sDrawTriangleOutlines)
  1424. {
  1425. struct Visitor
  1426. {
  1427. JPH_INLINE explicit Visitor(const HeightFieldShape *inShape, DebugRenderer *inRenderer, RMat44Arg inTransform) :
  1428. mShape(inShape),
  1429. mRenderer(inRenderer),
  1430. mTransform(inTransform)
  1431. {
  1432. }
  1433. JPH_INLINE bool ShouldAbort() const
  1434. {
  1435. return false;
  1436. }
  1437. JPH_INLINE bool ShouldVisitRangeBlock([[maybe_unused]] int inStackTop) const
  1438. {
  1439. return true;
  1440. }
  1441. JPH_INLINE int VisitRangeBlock(Vec4Arg inBoundsMinX, Vec4Arg inBoundsMinY, Vec4Arg inBoundsMinZ, Vec4Arg inBoundsMaxX, Vec4Arg inBoundsMaxY, Vec4Arg inBoundsMaxZ, UVec4 &ioProperties, [[maybe_unused]] int inStackTop) const
  1442. {
  1443. UVec4 valid = Vec4::sLessOrEqual(inBoundsMinY, inBoundsMaxY);
  1444. return CountAndSortTrues(valid, ioProperties);
  1445. }
  1446. JPH_INLINE void VisitTriangle(uint inX, uint inY, uint inTriangle, Vec3Arg inV0, Vec3Arg inV1, Vec3Arg inV2) const
  1447. {
  1448. // Determine active edges
  1449. uint8 active_edges = mShape->GetEdgeFlags(inX, inY, inTriangle);
  1450. // Loop through edges
  1451. Vec3 v[] = { inV0, inV1, inV2 };
  1452. for (uint edge_idx = 0; edge_idx < 3; ++edge_idx)
  1453. {
  1454. RVec3 v1 = mTransform * v[edge_idx];
  1455. RVec3 v2 = mTransform * v[(edge_idx + 1) % 3];
  1456. // Draw active edge as a green arrow, other edges as grey
  1457. if (active_edges & (1 << edge_idx))
  1458. mRenderer->DrawArrow(v1, v2, Color::sGreen, 0.01f);
  1459. else
  1460. mRenderer->DrawLine(v1, v2, Color::sGrey);
  1461. }
  1462. }
  1463. const HeightFieldShape *mShape;
  1464. DebugRenderer * mRenderer;
  1465. RMat44 mTransform;
  1466. };
  1467. Visitor visitor(this, inRenderer, inCenterOfMassTransform.PreScaled(inScale));
  1468. WalkHeightField(visitor);
  1469. }
  1470. }
  1471. #endif // JPH_DEBUG_RENDERER
  1472. class HeightFieldShape::DecodingContext
  1473. {
  1474. public:
  1475. JPH_INLINE explicit DecodingContext(const HeightFieldShape *inShape) :
  1476. mShape(inShape)
  1477. {
  1478. static_assert(sizeof(sGridOffsets) / sizeof(uint) == cNumBitsXY + 1, "Offsets array is not long enough");
  1479. // Construct root stack entry
  1480. mPropertiesStack[0] = 0; // level: 0, x: 0, y: 0
  1481. }
  1482. template <class Visitor>
  1483. JPH_INLINE void WalkHeightField(Visitor &ioVisitor)
  1484. {
  1485. // Early out if there's no collision
  1486. if (mShape->mHeightSamplesSize == 0)
  1487. return;
  1488. // Precalculate values relating to sample count
  1489. uint32 sample_count = mShape->mSampleCount;
  1490. UVec4 sample_count_min_1 = UVec4::sReplicate(sample_count - 1);
  1491. // Precalculate values relating to block size
  1492. uint32 block_size = mShape->mBlockSize;
  1493. uint32 block_size_plus_1 = block_size + 1;
  1494. uint num_blocks = mShape->GetNumBlocks();
  1495. uint num_blocks_min_1 = num_blocks - 1;
  1496. uint max_level = HeightFieldShape::sGetMaxLevel(num_blocks);
  1497. uint32 max_stride = (num_blocks + 1) >> 1;
  1498. // Precalculate range block offset and stride for GetBlockOffsetAndScale
  1499. uint range_block_offset, range_block_stride;
  1500. sGetRangeBlockOffsetAndStride(num_blocks, max_level, range_block_offset, range_block_stride);
  1501. // Allocate space for vertices and 'no collision' flags
  1502. int array_size = Square(block_size_plus_1);
  1503. Vec3 *vertices = reinterpret_cast<Vec3 *>(JPH_STACK_ALLOC(array_size * sizeof(Vec3)));
  1504. bool *no_collision = reinterpret_cast<bool *>(JPH_STACK_ALLOC(array_size * sizeof(bool)));
  1505. // Splat offsets
  1506. Vec4 ox = mShape->mOffset.SplatX();
  1507. Vec4 oy = mShape->mOffset.SplatY();
  1508. Vec4 oz = mShape->mOffset.SplatZ();
  1509. // Splat scales
  1510. Vec4 sx = mShape->mScale.SplatX();
  1511. Vec4 sy = mShape->mScale.SplatY();
  1512. Vec4 sz = mShape->mScale.SplatZ();
  1513. do
  1514. {
  1515. // Decode properties
  1516. uint32 properties_top = mPropertiesStack[mTop];
  1517. uint32 x = properties_top & cMaskBitsXY;
  1518. uint32 y = (properties_top >> cNumBitsXY) & cMaskBitsXY;
  1519. uint32 level = properties_top >> cLevelShift;
  1520. if (level >= max_level)
  1521. {
  1522. // Determine actual range of samples (minus one because we eventually want to iterate over the triangles, not the samples)
  1523. uint32 min_x = x * block_size;
  1524. uint32 max_x = min_x + block_size;
  1525. uint32 min_y = y * block_size;
  1526. uint32 max_y = min_y + block_size;
  1527. // Decompress vertices of block at (x, y)
  1528. Vec3 *dst_vertex = vertices;
  1529. bool *dst_no_collision = no_collision;
  1530. float block_offset, block_scale;
  1531. mShape->GetBlockOffsetAndScale(x, y, range_block_offset, range_block_stride, block_offset, block_scale);
  1532. for (uint32 v_y = min_y; v_y < max_y; ++v_y)
  1533. {
  1534. for (uint32 v_x = min_x; v_x < max_x; ++v_x)
  1535. {
  1536. *dst_vertex = mShape->GetPosition(v_x, v_y, block_offset, block_scale, *dst_no_collision);
  1537. ++dst_vertex;
  1538. ++dst_no_collision;
  1539. }
  1540. // Skip last column, these values come from a different block
  1541. ++dst_vertex;
  1542. ++dst_no_collision;
  1543. }
  1544. // Decompress block (x + 1, y)
  1545. uint32 max_x_decrement = 0;
  1546. if (x < num_blocks_min_1)
  1547. {
  1548. dst_vertex = vertices + block_size;
  1549. dst_no_collision = no_collision + block_size;
  1550. mShape->GetBlockOffsetAndScale(x + 1, y, range_block_offset, range_block_stride, block_offset, block_scale);
  1551. for (uint32 v_y = min_y; v_y < max_y; ++v_y)
  1552. {
  1553. *dst_vertex = mShape->GetPosition(max_x, v_y, block_offset, block_scale, *dst_no_collision);
  1554. dst_vertex += block_size_plus_1;
  1555. dst_no_collision += block_size_plus_1;
  1556. }
  1557. }
  1558. else
  1559. max_x_decrement = 1; // We don't have a next block, one less triangle to test
  1560. // Decompress block (x, y + 1)
  1561. if (y < num_blocks_min_1)
  1562. {
  1563. uint start = block_size * block_size_plus_1;
  1564. dst_vertex = vertices + start;
  1565. dst_no_collision = no_collision + start;
  1566. mShape->GetBlockOffsetAndScale(x, y + 1, range_block_offset, range_block_stride, block_offset, block_scale);
  1567. for (uint32 v_x = min_x; v_x < max_x; ++v_x)
  1568. {
  1569. *dst_vertex = mShape->GetPosition(v_x, max_y, block_offset, block_scale, *dst_no_collision);
  1570. ++dst_vertex;
  1571. ++dst_no_collision;
  1572. }
  1573. // Decompress single sample of block at (x + 1, y + 1)
  1574. if (x < num_blocks_min_1)
  1575. {
  1576. mShape->GetBlockOffsetAndScale(x + 1, y + 1, range_block_offset, range_block_stride, block_offset, block_scale);
  1577. *dst_vertex = mShape->GetPosition(max_x, max_y, block_offset, block_scale, *dst_no_collision);
  1578. }
  1579. }
  1580. else
  1581. --max_y; // We don't have a next block, one less triangle to test
  1582. // Update max_x (we've been using it so we couldn't update it earlier)
  1583. max_x -= max_x_decrement;
  1584. // We're going to divide the vertices in 4 blocks to do one more runtime sub-division, calculate the ranges of those blocks
  1585. struct Range
  1586. {
  1587. uint32 mMinX, mMinY, mNumTrianglesX, mNumTrianglesY;
  1588. };
  1589. uint32 half_block_size = block_size >> 1;
  1590. uint32 block_size_x = max_x - min_x - half_block_size;
  1591. uint32 block_size_y = max_y - min_y - half_block_size;
  1592. Range ranges[] =
  1593. {
  1594. { 0, 0, half_block_size, half_block_size },
  1595. { half_block_size, 0, block_size_x, half_block_size },
  1596. { 0, half_block_size, half_block_size, block_size_y },
  1597. { half_block_size, half_block_size, block_size_x, block_size_y },
  1598. };
  1599. // Calculate the min and max of each of the blocks
  1600. Mat44 block_min, block_max;
  1601. for (int block = 0; block < 4; ++block)
  1602. {
  1603. // Get the range for this block
  1604. const Range &range = ranges[block];
  1605. uint32 start = range.mMinX + range.mMinY * block_size_plus_1;
  1606. uint32 size_x_plus_1 = range.mNumTrianglesX + 1;
  1607. uint32 size_y_plus_1 = range.mNumTrianglesY + 1;
  1608. // Calculate where to start reading
  1609. const Vec3 *src_vertex = vertices + start;
  1610. const bool *src_no_collision = no_collision + start;
  1611. uint32 stride = block_size_plus_1 - size_x_plus_1;
  1612. // Start range with a very large inside-out box
  1613. Vec3 value_min = Vec3::sReplicate(1.0e30f);
  1614. Vec3 value_max = Vec3::sReplicate(-1.0e30f);
  1615. // Loop over the samples to determine the min and max of this block
  1616. for (uint32 block_y = 0; block_y < size_y_plus_1; ++block_y)
  1617. {
  1618. for (uint32 block_x = 0; block_x < size_x_plus_1; ++block_x)
  1619. {
  1620. if (!*src_no_collision)
  1621. {
  1622. value_min = Vec3::sMin(value_min, *src_vertex);
  1623. value_max = Vec3::sMax(value_max, *src_vertex);
  1624. }
  1625. ++src_vertex;
  1626. ++src_no_collision;
  1627. }
  1628. src_vertex += stride;
  1629. src_no_collision += stride;
  1630. }
  1631. block_min.SetColumn4(block, Vec4(value_min));
  1632. block_max.SetColumn4(block, Vec4(value_max));
  1633. }
  1634. #ifdef JPH_DEBUG_HEIGHT_FIELD
  1635. // Draw the bounding boxes of the sub-nodes
  1636. for (int block = 0; block < 4; ++block)
  1637. {
  1638. AABox bounds(block_min.GetColumn3(block), block_max.GetColumn3(block));
  1639. if (bounds.IsValid())
  1640. DebugRenderer::sInstance->DrawWireBox(bounds, Color::sYellow);
  1641. }
  1642. #endif // JPH_DEBUG_HEIGHT_FIELD
  1643. // Transpose so we have the mins and maxes of each of the blocks in rows instead of columns
  1644. Mat44 transposed_min = block_min.Transposed();
  1645. Mat44 transposed_max = block_max.Transposed();
  1646. // Check which blocks collide
  1647. // Note: At this point we don't use our own stack but we do allow the visitor to use its own stack
  1648. // to store collision distances so that we can still early out when no closer hits have been found.
  1649. UVec4 colliding_blocks(0, 1, 2, 3);
  1650. int num_results = ioVisitor.VisitRangeBlock(transposed_min.GetColumn4(0), transposed_min.GetColumn4(1), transposed_min.GetColumn4(2), transposed_max.GetColumn4(0), transposed_max.GetColumn4(1), transposed_max.GetColumn4(2), colliding_blocks, mTop);
  1651. // Loop through the results backwards (closest first)
  1652. int result = num_results - 1;
  1653. while (result >= 0)
  1654. {
  1655. // Calculate the min and max of this block
  1656. uint32 block = colliding_blocks[result];
  1657. const Range &range = ranges[block];
  1658. uint32 block_min_x = min_x + range.mMinX;
  1659. uint32 block_max_x = block_min_x + range.mNumTrianglesX;
  1660. uint32 block_min_y = min_y + range.mMinY;
  1661. uint32 block_max_y = block_min_y + range.mNumTrianglesY;
  1662. // Loop triangles
  1663. for (uint32 v_y = block_min_y; v_y < block_max_y; ++v_y)
  1664. for (uint32 v_x = block_min_x; v_x < block_max_x; ++v_x)
  1665. {
  1666. // Get first vertex
  1667. const int offset = (v_y - min_y) * block_size_plus_1 + (v_x - min_x);
  1668. const Vec3 *start_vertex = vertices + offset;
  1669. const bool *start_no_collision = no_collision + offset;
  1670. // Check if vertices shared by both triangles have collision
  1671. if (!start_no_collision[0] && !start_no_collision[block_size_plus_1 + 1])
  1672. {
  1673. // Loop 2 triangles
  1674. for (uint t = 0; t < 2; ++t)
  1675. {
  1676. // Determine triangle vertices
  1677. Vec3 v0, v1, v2;
  1678. if (t == 0)
  1679. {
  1680. // Check third vertex
  1681. if (start_no_collision[block_size_plus_1])
  1682. continue;
  1683. // Get vertices for triangle
  1684. v0 = start_vertex[0];
  1685. v1 = start_vertex[block_size_plus_1];
  1686. v2 = start_vertex[block_size_plus_1 + 1];
  1687. }
  1688. else
  1689. {
  1690. // Check third vertex
  1691. if (start_no_collision[1])
  1692. continue;
  1693. // Get vertices for triangle
  1694. v0 = start_vertex[0];
  1695. v1 = start_vertex[block_size_plus_1 + 1];
  1696. v2 = start_vertex[1];
  1697. }
  1698. #ifdef JPH_DEBUG_HEIGHT_FIELD
  1699. DebugRenderer::sInstance->DrawWireTriangle(RVec3(v0), RVec3(v1), RVec3(v2), Color::sWhite);
  1700. #endif
  1701. // Call visitor
  1702. ioVisitor.VisitTriangle(v_x, v_y, t, v0, v1, v2);
  1703. // Check if we're done
  1704. if (ioVisitor.ShouldAbort())
  1705. return;
  1706. }
  1707. }
  1708. }
  1709. // Fetch next block until we find one that the visitor wants to see
  1710. do
  1711. --result;
  1712. while (result >= 0 && !ioVisitor.ShouldVisitRangeBlock(mTop + result));
  1713. }
  1714. }
  1715. else
  1716. {
  1717. // Visit child grid
  1718. uint32 stride = min(1U << level, max_stride); // At the most detailed level we store a non-power of 2 number of blocks
  1719. uint32 offset = sGridOffsets[level] + stride * y + x;
  1720. // Decode min/max height
  1721. JPH_ASSERT(offset < mShape->mRangeBlocksSize);
  1722. UVec4 block = UVec4::sLoadInt4Aligned(reinterpret_cast<const uint32 *>(&mShape->mRangeBlocks[offset]));
  1723. Vec4 bounds_miny = oy + sy * block.Expand4Uint16Lo().ToFloat();
  1724. Vec4 bounds_maxy = oy + sy * block.Expand4Uint16Hi().ToFloat();
  1725. // Calculate size of one cell at this grid level
  1726. UVec4 internal_cell_size = UVec4::sReplicate(block_size << (max_level - level - 1)); // subtract 1 from level because we have an internal grid of 2x2
  1727. // Calculate min/max x and z
  1728. UVec4 two_x = UVec4::sReplicate(2 * x); // multiply by two because we have an internal grid of 2x2
  1729. Vec4 bounds_minx = ox + sx * (internal_cell_size * (two_x + UVec4(0, 1, 0, 1))).ToFloat();
  1730. Vec4 bounds_maxx = ox + sx * UVec4::sMin(internal_cell_size * (two_x + UVec4(1, 2, 1, 2)), sample_count_min_1).ToFloat();
  1731. UVec4 two_y = UVec4::sReplicate(2 * y);
  1732. Vec4 bounds_minz = oz + sz * (internal_cell_size * (two_y + UVec4(0, 0, 1, 1))).ToFloat();
  1733. Vec4 bounds_maxz = oz + sz * UVec4::sMin(internal_cell_size * (two_y + UVec4(1, 1, 2, 2)), sample_count_min_1).ToFloat();
  1734. // Calculate properties of child blocks
  1735. UVec4 properties = UVec4::sReplicate(((level + 1) << cLevelShift) + (y << (cNumBitsXY + 1)) + (x << 1)) + UVec4(0, 1, 1 << cNumBitsXY, (1 << cNumBitsXY) + 1);
  1736. #ifdef JPH_DEBUG_HEIGHT_FIELD
  1737. // Draw boxes
  1738. for (int i = 0; i < 4; ++i)
  1739. {
  1740. AABox b(Vec3(bounds_minx[i], bounds_miny[i], bounds_minz[i]), Vec3(bounds_maxx[i], bounds_maxy[i], bounds_maxz[i]));
  1741. if (b.IsValid())
  1742. DebugRenderer::sInstance->DrawWireBox(b, Color::sGreen);
  1743. }
  1744. #endif
  1745. // Check which sub nodes to visit
  1746. int num_results = ioVisitor.VisitRangeBlock(bounds_minx, bounds_miny, bounds_minz, bounds_maxx, bounds_maxy, bounds_maxz, properties, mTop);
  1747. // Push them onto the stack
  1748. JPH_ASSERT(mTop + 4 < cStackSize);
  1749. properties.StoreInt4(&mPropertiesStack[mTop]);
  1750. mTop += num_results;
  1751. }
  1752. // Check if we're done
  1753. if (ioVisitor.ShouldAbort())
  1754. return;
  1755. // Fetch next node until we find one that the visitor wants to see
  1756. do
  1757. --mTop;
  1758. while (mTop >= 0 && !ioVisitor.ShouldVisitRangeBlock(mTop));
  1759. }
  1760. while (mTop >= 0);
  1761. }
  1762. // This can be used to have the visitor early out (ioVisitor.ShouldAbort() returns true) and later continue again (call WalkHeightField() again)
  1763. JPH_INLINE bool IsDoneWalking() const
  1764. {
  1765. return mTop < 0;
  1766. }
  1767. private:
  1768. const HeightFieldShape * mShape;
  1769. int mTop = 0;
  1770. uint32 mPropertiesStack[cStackSize];
  1771. };
  1772. template <class Visitor>
  1773. void HeightFieldShape::WalkHeightField(Visitor &ioVisitor) const
  1774. {
  1775. DecodingContext ctx(this);
  1776. ctx.WalkHeightField(ioVisitor);
  1777. }
  1778. bool HeightFieldShape::CastRay(const RayCast &inRay, const SubShapeIDCreator &inSubShapeIDCreator, RayCastResult &ioHit) const
  1779. {
  1780. JPH_PROFILE_FUNCTION();
  1781. struct Visitor
  1782. {
  1783. JPH_INLINE explicit Visitor(const HeightFieldShape *inShape, const RayCast &inRay, const SubShapeIDCreator &inSubShapeIDCreator, RayCastResult &ioHit) :
  1784. mHit(ioHit),
  1785. mRayOrigin(inRay.mOrigin),
  1786. mRayDirection(inRay.mDirection),
  1787. mRayInvDirection(inRay.mDirection),
  1788. mShape(inShape),
  1789. mSubShapeIDCreator(inSubShapeIDCreator)
  1790. {
  1791. }
  1792. JPH_INLINE bool ShouldAbort() const
  1793. {
  1794. return mHit.mFraction <= 0.0f;
  1795. }
  1796. JPH_INLINE bool ShouldVisitRangeBlock(int inStackTop) const
  1797. {
  1798. return mDistanceStack[inStackTop] < mHit.mFraction;
  1799. }
  1800. JPH_INLINE int VisitRangeBlock(Vec4Arg inBoundsMinX, Vec4Arg inBoundsMinY, Vec4Arg inBoundsMinZ, Vec4Arg inBoundsMaxX, Vec4Arg inBoundsMaxY, Vec4Arg inBoundsMaxZ, UVec4 &ioProperties, int inStackTop)
  1801. {
  1802. // Test bounds of 4 children
  1803. Vec4 distance = RayAABox4(mRayOrigin, mRayInvDirection, inBoundsMinX, inBoundsMinY, inBoundsMinZ, inBoundsMaxX, inBoundsMaxY, inBoundsMaxZ);
  1804. // Sort so that highest values are first (we want to first process closer hits and we process stack top to bottom)
  1805. return SortReverseAndStore(distance, mHit.mFraction, ioProperties, &mDistanceStack[inStackTop]);
  1806. }
  1807. JPH_INLINE void VisitTriangle(uint inX, uint inY, uint inTriangle, Vec3Arg inV0, Vec3Arg inV1, Vec3Arg inV2)
  1808. {
  1809. float fraction = RayTriangle(mRayOrigin, mRayDirection, inV0, inV1, inV2);
  1810. if (fraction < mHit.mFraction)
  1811. {
  1812. // It's a closer hit
  1813. mHit.mFraction = fraction;
  1814. mHit.mSubShapeID2 = mShape->EncodeSubShapeID(mSubShapeIDCreator, inX, inY, inTriangle);
  1815. mReturnValue = true;
  1816. }
  1817. }
  1818. RayCastResult & mHit;
  1819. Vec3 mRayOrigin;
  1820. Vec3 mRayDirection;
  1821. RayInvDirection mRayInvDirection;
  1822. const HeightFieldShape *mShape;
  1823. SubShapeIDCreator mSubShapeIDCreator;
  1824. bool mReturnValue = false;
  1825. float mDistanceStack[cStackSize];
  1826. };
  1827. Visitor visitor(this, inRay, inSubShapeIDCreator, ioHit);
  1828. WalkHeightField(visitor);
  1829. return visitor.mReturnValue;
  1830. }
  1831. void HeightFieldShape::CastRay(const RayCast &inRay, const RayCastSettings &inRayCastSettings, const SubShapeIDCreator &inSubShapeIDCreator, CastRayCollector &ioCollector, const ShapeFilter &inShapeFilter) const
  1832. {
  1833. JPH_PROFILE_FUNCTION();
  1834. // Test shape filter
  1835. if (!inShapeFilter.ShouldCollide(this, inSubShapeIDCreator.GetID()))
  1836. return;
  1837. struct Visitor
  1838. {
  1839. JPH_INLINE explicit Visitor(const HeightFieldShape *inShape, const RayCast &inRay, const RayCastSettings &inRayCastSettings, const SubShapeIDCreator &inSubShapeIDCreator, CastRayCollector &ioCollector) :
  1840. mCollector(ioCollector),
  1841. mRayOrigin(inRay.mOrigin),
  1842. mRayDirection(inRay.mDirection),
  1843. mRayInvDirection(inRay.mDirection),
  1844. mBackFaceMode(inRayCastSettings.mBackFaceMode),
  1845. mShape(inShape),
  1846. mSubShapeIDCreator(inSubShapeIDCreator)
  1847. {
  1848. }
  1849. JPH_INLINE bool ShouldAbort() const
  1850. {
  1851. return mCollector.ShouldEarlyOut();
  1852. }
  1853. JPH_INLINE bool ShouldVisitRangeBlock(int inStackTop) const
  1854. {
  1855. return mDistanceStack[inStackTop] < mCollector.GetEarlyOutFraction();
  1856. }
  1857. JPH_INLINE int VisitRangeBlock(Vec4Arg inBoundsMinX, Vec4Arg inBoundsMinY, Vec4Arg inBoundsMinZ, Vec4Arg inBoundsMaxX, Vec4Arg inBoundsMaxY, Vec4Arg inBoundsMaxZ, UVec4 &ioProperties, int inStackTop)
  1858. {
  1859. // Test bounds of 4 children
  1860. Vec4 distance = RayAABox4(mRayOrigin, mRayInvDirection, inBoundsMinX, inBoundsMinY, inBoundsMinZ, inBoundsMaxX, inBoundsMaxY, inBoundsMaxZ);
  1861. // Sort so that highest values are first (we want to first process closer hits and we process stack top to bottom)
  1862. return SortReverseAndStore(distance, mCollector.GetEarlyOutFraction(), ioProperties, &mDistanceStack[inStackTop]);
  1863. }
  1864. JPH_INLINE void VisitTriangle(uint inX, uint inY, uint inTriangle, Vec3Arg inV0, Vec3Arg inV1, Vec3Arg inV2) const
  1865. {
  1866. // Back facing check
  1867. if (mBackFaceMode == EBackFaceMode::IgnoreBackFaces && (inV2 - inV0).Cross(inV1 - inV0).Dot(mRayDirection) < 0)
  1868. return;
  1869. // Check the triangle
  1870. float fraction = RayTriangle(mRayOrigin, mRayDirection, inV0, inV1, inV2);
  1871. if (fraction < mCollector.GetEarlyOutFraction())
  1872. {
  1873. RayCastResult hit;
  1874. hit.mBodyID = TransformedShape::sGetBodyID(mCollector.GetContext());
  1875. hit.mFraction = fraction;
  1876. hit.mSubShapeID2 = mShape->EncodeSubShapeID(mSubShapeIDCreator, inX, inY, inTriangle);
  1877. mCollector.AddHit(hit);
  1878. }
  1879. }
  1880. CastRayCollector & mCollector;
  1881. Vec3 mRayOrigin;
  1882. Vec3 mRayDirection;
  1883. RayInvDirection mRayInvDirection;
  1884. EBackFaceMode mBackFaceMode;
  1885. const HeightFieldShape *mShape;
  1886. SubShapeIDCreator mSubShapeIDCreator;
  1887. float mDistanceStack[cStackSize];
  1888. };
  1889. Visitor visitor(this, inRay, inRayCastSettings, inSubShapeIDCreator, ioCollector);
  1890. WalkHeightField(visitor);
  1891. }
  1892. void HeightFieldShape::CollidePoint(Vec3Arg inPoint, const SubShapeIDCreator &inSubShapeIDCreator, CollidePointCollector &ioCollector, const ShapeFilter &inShapeFilter) const
  1893. {
  1894. // A height field doesn't have volume, so we can't test insideness
  1895. }
  1896. void HeightFieldShape::CollideSoftBodyVertices(Mat44Arg inCenterOfMassTransform, Vec3Arg inScale, SoftBodyVertex *ioVertices, uint inNumVertices, [[maybe_unused]] float inDeltaTime, [[maybe_unused]] Vec3Arg inDisplacementDueToGravity, int inCollidingShapeIndex) const
  1897. {
  1898. JPH_PROFILE_FUNCTION();
  1899. struct Visitor : public CollideSoftBodyVerticesVsTriangles
  1900. {
  1901. using CollideSoftBodyVerticesVsTriangles::CollideSoftBodyVerticesVsTriangles;
  1902. JPH_INLINE bool ShouldAbort() const
  1903. {
  1904. return false;
  1905. }
  1906. JPH_INLINE bool ShouldVisitRangeBlock([[maybe_unused]] int inStackTop) const
  1907. {
  1908. return mDistanceStack[inStackTop] < mClosestDistanceSq;
  1909. }
  1910. JPH_INLINE int VisitRangeBlock(Vec4Arg inBoundsMinX, Vec4Arg inBoundsMinY, Vec4Arg inBoundsMinZ, Vec4Arg inBoundsMaxX, Vec4Arg inBoundsMaxY, Vec4Arg inBoundsMaxZ, UVec4 &ioProperties, int inStackTop)
  1911. {
  1912. // Get distance to vertex
  1913. Vec4 dist_sq = AABox4DistanceSqToPoint(mLocalPosition, inBoundsMinX, inBoundsMinY, inBoundsMinZ, inBoundsMaxX, inBoundsMaxY, inBoundsMaxZ);
  1914. // Sort so that highest values are first (we want to first process closer hits and we process stack top to bottom)
  1915. return SortReverseAndStore(dist_sq, mClosestDistanceSq, ioProperties, &mDistanceStack[inStackTop]);
  1916. }
  1917. JPH_INLINE void VisitTriangle([[maybe_unused]] uint inX, [[maybe_unused]] uint inY, [[maybe_unused]] uint inTriangle, Vec3Arg inV0, Vec3Arg inV1, Vec3Arg inV2)
  1918. {
  1919. ProcessTriangle(inV0, inV1, inV2);
  1920. }
  1921. float mDistanceStack[cStackSize];
  1922. };
  1923. Visitor visitor(inCenterOfMassTransform, inScale);
  1924. for (SoftBodyVertex *v = ioVertices, *sbv_end = ioVertices + inNumVertices; v < sbv_end; ++v)
  1925. if (v->mInvMass > 0.0f)
  1926. {
  1927. visitor.StartVertex(*v);
  1928. WalkHeightField(visitor);
  1929. visitor.FinishVertex(*v, inCollidingShapeIndex);
  1930. }
  1931. }
  1932. void HeightFieldShape::sCastConvexVsHeightField(const ShapeCast &inShapeCast, const ShapeCastSettings &inShapeCastSettings, const Shape *inShape, Vec3Arg inScale, [[maybe_unused]] const ShapeFilter &inShapeFilter, Mat44Arg inCenterOfMassTransform2, const SubShapeIDCreator &inSubShapeIDCreator1, const SubShapeIDCreator &inSubShapeIDCreator2, CastShapeCollector &ioCollector)
  1933. {
  1934. JPH_PROFILE_FUNCTION();
  1935. struct Visitor : public CastConvexVsTriangles
  1936. {
  1937. using CastConvexVsTriangles::CastConvexVsTriangles;
  1938. JPH_INLINE bool ShouldAbort() const
  1939. {
  1940. return mCollector.ShouldEarlyOut();
  1941. }
  1942. JPH_INLINE bool ShouldVisitRangeBlock(int inStackTop) const
  1943. {
  1944. return mDistanceStack[inStackTop] < mCollector.GetPositiveEarlyOutFraction();
  1945. }
  1946. JPH_INLINE int VisitRangeBlock(Vec4Arg inBoundsMinX, Vec4Arg inBoundsMinY, Vec4Arg inBoundsMinZ, Vec4Arg inBoundsMaxX, Vec4Arg inBoundsMaxY, Vec4Arg inBoundsMaxZ, UVec4 &ioProperties, int inStackTop)
  1947. {
  1948. // Scale the bounding boxes of this node
  1949. Vec4 bounds_min_x, bounds_min_y, bounds_min_z, bounds_max_x, bounds_max_y, bounds_max_z;
  1950. AABox4Scale(mScale, inBoundsMinX, inBoundsMinY, inBoundsMinZ, inBoundsMaxX, inBoundsMaxY, inBoundsMaxZ, bounds_min_x, bounds_min_y, bounds_min_z, bounds_max_x, bounds_max_y, bounds_max_z);
  1951. // Enlarge them by the casted shape's box extents
  1952. AABox4EnlargeWithExtent(mBoxExtent, bounds_min_x, bounds_min_y, bounds_min_z, bounds_max_x, bounds_max_y, bounds_max_z);
  1953. // Test bounds of 4 children
  1954. Vec4 distance = RayAABox4(mBoxCenter, mInvDirection, bounds_min_x, bounds_min_y, bounds_min_z, bounds_max_x, bounds_max_y, bounds_max_z);
  1955. // Clear distance for invalid bounds
  1956. distance = Vec4::sSelect(Vec4::sReplicate(FLT_MAX), distance, Vec4::sLessOrEqual(inBoundsMinY, inBoundsMaxY));
  1957. // Sort so that highest values are first (we want to first process closer hits and we process stack top to bottom)
  1958. return SortReverseAndStore(distance, mCollector.GetPositiveEarlyOutFraction(), ioProperties, &mDistanceStack[inStackTop]);
  1959. }
  1960. JPH_INLINE void VisitTriangle(uint inX, uint inY, uint inTriangle, Vec3Arg inV0, Vec3Arg inV1, Vec3Arg inV2)
  1961. {
  1962. // Create sub shape id for this part
  1963. SubShapeID triangle_sub_shape_id = mShape2->EncodeSubShapeID(mSubShapeIDCreator2, inX, inY, inTriangle);
  1964. // Determine active edges
  1965. uint8 active_edges = mShape2->GetEdgeFlags(inX, inY, inTriangle);
  1966. Cast(inV0, inV1, inV2, active_edges, triangle_sub_shape_id);
  1967. }
  1968. const HeightFieldShape * mShape2;
  1969. RayInvDirection mInvDirection;
  1970. Vec3 mBoxCenter;
  1971. Vec3 mBoxExtent;
  1972. SubShapeIDCreator mSubShapeIDCreator2;
  1973. float mDistanceStack[cStackSize];
  1974. };
  1975. JPH_ASSERT(inShape->GetSubType() == EShapeSubType::HeightField);
  1976. const HeightFieldShape *shape = static_cast<const HeightFieldShape *>(inShape);
  1977. Visitor visitor(inShapeCast, inShapeCastSettings, inScale, inCenterOfMassTransform2, inSubShapeIDCreator1, ioCollector);
  1978. visitor.mShape2 = shape;
  1979. visitor.mInvDirection.Set(inShapeCast.mDirection);
  1980. visitor.mBoxCenter = inShapeCast.mShapeWorldBounds.GetCenter();
  1981. visitor.mBoxExtent = inShapeCast.mShapeWorldBounds.GetExtent();
  1982. visitor.mSubShapeIDCreator2 = inSubShapeIDCreator2;
  1983. shape->WalkHeightField(visitor);
  1984. }
  1985. void HeightFieldShape::sCastSphereVsHeightField(const ShapeCast &inShapeCast, const ShapeCastSettings &inShapeCastSettings, const Shape *inShape, Vec3Arg inScale, [[maybe_unused]] const ShapeFilter &inShapeFilter, Mat44Arg inCenterOfMassTransform2, const SubShapeIDCreator &inSubShapeIDCreator1, const SubShapeIDCreator &inSubShapeIDCreator2, CastShapeCollector &ioCollector)
  1986. {
  1987. JPH_PROFILE_FUNCTION();
  1988. struct Visitor : public CastSphereVsTriangles
  1989. {
  1990. using CastSphereVsTriangles::CastSphereVsTriangles;
  1991. JPH_INLINE bool ShouldAbort() const
  1992. {
  1993. return mCollector.ShouldEarlyOut();
  1994. }
  1995. JPH_INLINE bool ShouldVisitRangeBlock(int inStackTop) const
  1996. {
  1997. return mDistanceStack[inStackTop] < mCollector.GetPositiveEarlyOutFraction();
  1998. }
  1999. JPH_INLINE int VisitRangeBlock(Vec4Arg inBoundsMinX, Vec4Arg inBoundsMinY, Vec4Arg inBoundsMinZ, Vec4Arg inBoundsMaxX, Vec4Arg inBoundsMaxY, Vec4Arg inBoundsMaxZ, UVec4 &ioProperties, int inStackTop)
  2000. {
  2001. // Scale the bounding boxes of this node
  2002. Vec4 bounds_min_x, bounds_min_y, bounds_min_z, bounds_max_x, bounds_max_y, bounds_max_z;
  2003. AABox4Scale(mScale, inBoundsMinX, inBoundsMinY, inBoundsMinZ, inBoundsMaxX, inBoundsMaxY, inBoundsMaxZ, bounds_min_x, bounds_min_y, bounds_min_z, bounds_max_x, bounds_max_y, bounds_max_z);
  2004. // Enlarge them by the radius of the sphere
  2005. AABox4EnlargeWithExtent(Vec3::sReplicate(mRadius), bounds_min_x, bounds_min_y, bounds_min_z, bounds_max_x, bounds_max_y, bounds_max_z);
  2006. // Test bounds of 4 children
  2007. Vec4 distance = RayAABox4(mStart, mInvDirection, bounds_min_x, bounds_min_y, bounds_min_z, bounds_max_x, bounds_max_y, bounds_max_z);
  2008. // Clear distance for invalid bounds
  2009. distance = Vec4::sSelect(Vec4::sReplicate(FLT_MAX), distance, Vec4::sLessOrEqual(inBoundsMinY, inBoundsMaxY));
  2010. // Sort so that highest values are first (we want to first process closer hits and we process stack top to bottom)
  2011. return SortReverseAndStore(distance, mCollector.GetPositiveEarlyOutFraction(), ioProperties, &mDistanceStack[inStackTop]);
  2012. }
  2013. JPH_INLINE void VisitTriangle(uint inX, uint inY, uint inTriangle, Vec3Arg inV0, Vec3Arg inV1, Vec3Arg inV2)
  2014. {
  2015. // Create sub shape id for this part
  2016. SubShapeID triangle_sub_shape_id = mShape2->EncodeSubShapeID(mSubShapeIDCreator2, inX, inY, inTriangle);
  2017. // Determine active edges
  2018. uint8 active_edges = mShape2->GetEdgeFlags(inX, inY, inTriangle);
  2019. Cast(inV0, inV1, inV2, active_edges, triangle_sub_shape_id);
  2020. }
  2021. const HeightFieldShape * mShape2;
  2022. RayInvDirection mInvDirection;
  2023. SubShapeIDCreator mSubShapeIDCreator2;
  2024. float mDistanceStack[cStackSize];
  2025. };
  2026. JPH_ASSERT(inShape->GetSubType() == EShapeSubType::HeightField);
  2027. const HeightFieldShape *shape = static_cast<const HeightFieldShape *>(inShape);
  2028. Visitor visitor(inShapeCast, inShapeCastSettings, inScale, inCenterOfMassTransform2, inSubShapeIDCreator1, ioCollector);
  2029. visitor.mShape2 = shape;
  2030. visitor.mInvDirection.Set(inShapeCast.mDirection);
  2031. visitor.mSubShapeIDCreator2 = inSubShapeIDCreator2;
  2032. shape->WalkHeightField(visitor);
  2033. }
  2034. struct HeightFieldShape::HSGetTrianglesContext
  2035. {
  2036. HSGetTrianglesContext(const HeightFieldShape *inShape, const AABox &inBox, Vec3Arg inPositionCOM, QuatArg inRotation, Vec3Arg inScale) :
  2037. mDecodeCtx(inShape),
  2038. mShape(inShape),
  2039. mLocalBox(Mat44::sInverseRotationTranslation(inRotation, inPositionCOM), inBox),
  2040. mHeightFieldScale(inScale),
  2041. mLocalToWorld(Mat44::sRotationTranslation(inRotation, inPositionCOM) * Mat44::sScale(inScale)),
  2042. mIsInsideOut(ScaleHelpers::IsInsideOut(inScale))
  2043. {
  2044. }
  2045. bool ShouldAbort() const
  2046. {
  2047. return mShouldAbort;
  2048. }
  2049. bool ShouldVisitRangeBlock([[maybe_unused]] int inStackTop) const
  2050. {
  2051. return true;
  2052. }
  2053. int VisitRangeBlock(Vec4Arg inBoundsMinX, Vec4Arg inBoundsMinY, Vec4Arg inBoundsMinZ, Vec4Arg inBoundsMaxX, Vec4Arg inBoundsMaxY, Vec4Arg inBoundsMaxZ, UVec4 &ioProperties, [[maybe_unused]] int inStackTop) const
  2054. {
  2055. // Scale the bounding boxes of this node
  2056. Vec4 bounds_min_x, bounds_min_y, bounds_min_z, bounds_max_x, bounds_max_y, bounds_max_z;
  2057. AABox4Scale(mHeightFieldScale, inBoundsMinX, inBoundsMinY, inBoundsMinZ, inBoundsMaxX, inBoundsMaxY, inBoundsMaxZ, bounds_min_x, bounds_min_y, bounds_min_z, bounds_max_x, bounds_max_y, bounds_max_z);
  2058. // Test which nodes collide
  2059. UVec4 collides = AABox4VsBox(mLocalBox, bounds_min_x, bounds_min_y, bounds_min_z, bounds_max_x, bounds_max_y, bounds_max_z);
  2060. // Filter out invalid bounding boxes
  2061. collides = UVec4::sAnd(collides, Vec4::sLessOrEqual(inBoundsMinY, inBoundsMaxY));
  2062. return CountAndSortTrues(collides, ioProperties);
  2063. }
  2064. void VisitTriangle(uint inX, uint inY, [[maybe_unused]] uint inTriangle, Vec3Arg inV0, Vec3Arg inV1, Vec3Arg inV2)
  2065. {
  2066. // When the buffer is full and we cannot process the triangles, abort the height field walk. The next time GetTrianglesNext is called we will continue here.
  2067. if (mNumTrianglesFound + 1 > mMaxTrianglesRequested)
  2068. {
  2069. mShouldAbort = true;
  2070. return;
  2071. }
  2072. // Store vertices as Float3
  2073. if (mIsInsideOut)
  2074. {
  2075. // Reverse vertices
  2076. (mLocalToWorld * inV0).StoreFloat3(mTriangleVertices++);
  2077. (mLocalToWorld * inV2).StoreFloat3(mTriangleVertices++);
  2078. (mLocalToWorld * inV1).StoreFloat3(mTriangleVertices++);
  2079. }
  2080. else
  2081. {
  2082. // Normal scale
  2083. (mLocalToWorld * inV0).StoreFloat3(mTriangleVertices++);
  2084. (mLocalToWorld * inV1).StoreFloat3(mTriangleVertices++);
  2085. (mLocalToWorld * inV2).StoreFloat3(mTriangleVertices++);
  2086. }
  2087. // Decode material
  2088. if (mMaterials != nullptr)
  2089. *mMaterials++ = mShape->GetMaterial(inX, inY);
  2090. // Accumulate triangles found
  2091. mNumTrianglesFound++;
  2092. }
  2093. DecodingContext mDecodeCtx;
  2094. const HeightFieldShape * mShape;
  2095. OrientedBox mLocalBox;
  2096. Vec3 mHeightFieldScale;
  2097. Mat44 mLocalToWorld;
  2098. int mMaxTrianglesRequested;
  2099. Float3 * mTriangleVertices;
  2100. int mNumTrianglesFound;
  2101. const PhysicsMaterial ** mMaterials;
  2102. bool mShouldAbort;
  2103. bool mIsInsideOut;
  2104. };
  2105. void HeightFieldShape::GetTrianglesStart(GetTrianglesContext &ioContext, const AABox &inBox, Vec3Arg inPositionCOM, QuatArg inRotation, Vec3Arg inScale) const
  2106. {
  2107. static_assert(sizeof(HSGetTrianglesContext) <= sizeof(GetTrianglesContext), "GetTrianglesContext too small");
  2108. JPH_ASSERT(IsAligned(&ioContext, alignof(HSGetTrianglesContext)));
  2109. new (&ioContext) HSGetTrianglesContext(this, inBox, inPositionCOM, inRotation, inScale);
  2110. }
  2111. int HeightFieldShape::GetTrianglesNext(GetTrianglesContext &ioContext, int inMaxTrianglesRequested, Float3 *outTriangleVertices, const PhysicsMaterial **outMaterials) const
  2112. {
  2113. static_assert(cGetTrianglesMinTrianglesRequested >= 1, "cGetTrianglesMinTrianglesRequested is too small");
  2114. JPH_ASSERT(inMaxTrianglesRequested >= cGetTrianglesMinTrianglesRequested);
  2115. // Check if we're done
  2116. HSGetTrianglesContext &context = (HSGetTrianglesContext &)ioContext;
  2117. if (context.mDecodeCtx.IsDoneWalking())
  2118. return 0;
  2119. // Store parameters on context
  2120. context.mMaxTrianglesRequested = inMaxTrianglesRequested;
  2121. context.mTriangleVertices = outTriangleVertices;
  2122. context.mMaterials = outMaterials;
  2123. context.mShouldAbort = false; // Reset the abort flag
  2124. context.mNumTrianglesFound = 0;
  2125. // Continue (or start) walking the height field
  2126. context.mDecodeCtx.WalkHeightField(context);
  2127. return context.mNumTrianglesFound;
  2128. }
  2129. void HeightFieldShape::sCollideConvexVsHeightField(const Shape *inShape1, const Shape *inShape2, Vec3Arg inScale1, Vec3Arg inScale2, Mat44Arg inCenterOfMassTransform1, Mat44Arg inCenterOfMassTransform2, const SubShapeIDCreator &inSubShapeIDCreator1, const SubShapeIDCreator &inSubShapeIDCreator2, const CollideShapeSettings &inCollideShapeSettings, CollideShapeCollector &ioCollector, [[maybe_unused]] const ShapeFilter &inShapeFilter)
  2130. {
  2131. JPH_PROFILE_FUNCTION();
  2132. // Get the shapes
  2133. JPH_ASSERT(inShape1->GetType() == EShapeType::Convex);
  2134. JPH_ASSERT(inShape2->GetType() == EShapeType::HeightField);
  2135. const ConvexShape *shape1 = static_cast<const ConvexShape *>(inShape1);
  2136. const HeightFieldShape *shape2 = static_cast<const HeightFieldShape *>(inShape2);
  2137. struct Visitor : public CollideConvexVsTriangles
  2138. {
  2139. using CollideConvexVsTriangles::CollideConvexVsTriangles;
  2140. JPH_INLINE bool ShouldAbort() const
  2141. {
  2142. return mCollector.ShouldEarlyOut();
  2143. }
  2144. JPH_INLINE bool ShouldVisitRangeBlock([[maybe_unused]] int inStackTop) const
  2145. {
  2146. return true;
  2147. }
  2148. JPH_INLINE int VisitRangeBlock(Vec4Arg inBoundsMinX, Vec4Arg inBoundsMinY, Vec4Arg inBoundsMinZ, Vec4Arg inBoundsMaxX, Vec4Arg inBoundsMaxY, Vec4Arg inBoundsMaxZ, UVec4 &ioProperties, [[maybe_unused]] int inStackTop) const
  2149. {
  2150. // Scale the bounding boxes of this node
  2151. Vec4 bounds_min_x, bounds_min_y, bounds_min_z, bounds_max_x, bounds_max_y, bounds_max_z;
  2152. AABox4Scale(mScale2, inBoundsMinX, inBoundsMinY, inBoundsMinZ, inBoundsMaxX, inBoundsMaxY, inBoundsMaxZ, bounds_min_x, bounds_min_y, bounds_min_z, bounds_max_x, bounds_max_y, bounds_max_z);
  2153. // Test which nodes collide
  2154. UVec4 collides = AABox4VsBox(mBoundsOf1InSpaceOf2, bounds_min_x, bounds_min_y, bounds_min_z, bounds_max_x, bounds_max_y, bounds_max_z);
  2155. // Filter out invalid bounding boxes
  2156. collides = UVec4::sAnd(collides, Vec4::sLessOrEqual(inBoundsMinY, inBoundsMaxY));
  2157. return CountAndSortTrues(collides, ioProperties);
  2158. }
  2159. JPH_INLINE void VisitTriangle(uint inX, uint inY, uint inTriangle, Vec3Arg inV0, Vec3Arg inV1, Vec3Arg inV2)
  2160. {
  2161. // Create ID for triangle
  2162. SubShapeID triangle_sub_shape_id = mShape2->EncodeSubShapeID(mSubShapeIDCreator2, inX, inY, inTriangle);
  2163. // Determine active edges
  2164. uint8 active_edges = mShape2->GetEdgeFlags(inX, inY, inTriangle);
  2165. Collide(inV0, inV1, inV2, active_edges, triangle_sub_shape_id);
  2166. }
  2167. const HeightFieldShape * mShape2;
  2168. SubShapeIDCreator mSubShapeIDCreator2;
  2169. };
  2170. Visitor visitor(shape1, inScale1, inScale2, inCenterOfMassTransform1, inCenterOfMassTransform2, inSubShapeIDCreator1.GetID(), inCollideShapeSettings, ioCollector);
  2171. visitor.mShape2 = shape2;
  2172. visitor.mSubShapeIDCreator2 = inSubShapeIDCreator2;
  2173. shape2->WalkHeightField(visitor);
  2174. }
  2175. void HeightFieldShape::sCollideSphereVsHeightField(const Shape *inShape1, const Shape *inShape2, Vec3Arg inScale1, Vec3Arg inScale2, Mat44Arg inCenterOfMassTransform1, Mat44Arg inCenterOfMassTransform2, const SubShapeIDCreator &inSubShapeIDCreator1, const SubShapeIDCreator &inSubShapeIDCreator2, const CollideShapeSettings &inCollideShapeSettings, CollideShapeCollector &ioCollector, [[maybe_unused]] const ShapeFilter &inShapeFilter)
  2176. {
  2177. JPH_PROFILE_FUNCTION();
  2178. // Get the shapes
  2179. JPH_ASSERT(inShape1->GetSubType() == EShapeSubType::Sphere);
  2180. JPH_ASSERT(inShape2->GetType() == EShapeType::HeightField);
  2181. const SphereShape *shape1 = static_cast<const SphereShape *>(inShape1);
  2182. const HeightFieldShape *shape2 = static_cast<const HeightFieldShape *>(inShape2);
  2183. struct Visitor : public CollideSphereVsTriangles
  2184. {
  2185. using CollideSphereVsTriangles::CollideSphereVsTriangles;
  2186. JPH_INLINE bool ShouldAbort() const
  2187. {
  2188. return mCollector.ShouldEarlyOut();
  2189. }
  2190. JPH_INLINE bool ShouldVisitRangeBlock([[maybe_unused]] int inStackTop) const
  2191. {
  2192. return true;
  2193. }
  2194. JPH_INLINE int VisitRangeBlock(Vec4Arg inBoundsMinX, Vec4Arg inBoundsMinY, Vec4Arg inBoundsMinZ, Vec4Arg inBoundsMaxX, Vec4Arg inBoundsMaxY, Vec4Arg inBoundsMaxZ, UVec4 &ioProperties, [[maybe_unused]] int inStackTop) const
  2195. {
  2196. // Scale the bounding boxes of this node
  2197. Vec4 bounds_min_x, bounds_min_y, bounds_min_z, bounds_max_x, bounds_max_y, bounds_max_z;
  2198. AABox4Scale(mScale2, inBoundsMinX, inBoundsMinY, inBoundsMinZ, inBoundsMaxX, inBoundsMaxY, inBoundsMaxZ, bounds_min_x, bounds_min_y, bounds_min_z, bounds_max_x, bounds_max_y, bounds_max_z);
  2199. // Test which nodes collide
  2200. UVec4 collides = AABox4VsSphere(mSphereCenterIn2, mRadiusPlusMaxSeparationSq, bounds_min_x, bounds_min_y, bounds_min_z, bounds_max_x, bounds_max_y, bounds_max_z);
  2201. // Filter out invalid bounding boxes
  2202. collides = UVec4::sAnd(collides, Vec4::sLessOrEqual(inBoundsMinY, inBoundsMaxY));
  2203. return CountAndSortTrues(collides, ioProperties);
  2204. }
  2205. JPH_INLINE void VisitTriangle(uint inX, uint inY, uint inTriangle, Vec3Arg inV0, Vec3Arg inV1, Vec3Arg inV2)
  2206. {
  2207. // Create ID for triangle
  2208. SubShapeID triangle_sub_shape_id = mShape2->EncodeSubShapeID(mSubShapeIDCreator2, inX, inY, inTriangle);
  2209. // Determine active edges
  2210. uint8 active_edges = mShape2->GetEdgeFlags(inX, inY, inTriangle);
  2211. Collide(inV0, inV1, inV2, active_edges, triangle_sub_shape_id);
  2212. }
  2213. const HeightFieldShape * mShape2;
  2214. SubShapeIDCreator mSubShapeIDCreator2;
  2215. };
  2216. Visitor visitor(shape1, inScale1, inScale2, inCenterOfMassTransform1, inCenterOfMassTransform2, inSubShapeIDCreator1.GetID(), inCollideShapeSettings, ioCollector);
  2217. visitor.mShape2 = shape2;
  2218. visitor.mSubShapeIDCreator2 = inSubShapeIDCreator2;
  2219. shape2->WalkHeightField(visitor);
  2220. }
  2221. void HeightFieldShape::SaveBinaryState(StreamOut &inStream) const
  2222. {
  2223. Shape::SaveBinaryState(inStream);
  2224. inStream.Write(mOffset);
  2225. inStream.Write(mScale);
  2226. inStream.Write(mSampleCount);
  2227. inStream.Write(mBlockSize);
  2228. inStream.Write(mBitsPerSample);
  2229. inStream.Write(mMinSample);
  2230. inStream.Write(mMaxSample);
  2231. inStream.Write(mMaterialIndices);
  2232. inStream.Write(mNumBitsPerMaterialIndex);
  2233. if (mRangeBlocks != nullptr)
  2234. {
  2235. inStream.Write(true);
  2236. inStream.WriteBytes(mRangeBlocks, mRangeBlocksSize * sizeof(RangeBlock) + mHeightSamplesSize + mActiveEdgesSize);
  2237. }
  2238. else
  2239. {
  2240. inStream.Write(false);
  2241. }
  2242. }
  2243. void HeightFieldShape::RestoreBinaryState(StreamIn &inStream)
  2244. {
  2245. Shape::RestoreBinaryState(inStream);
  2246. inStream.Read(mOffset);
  2247. inStream.Read(mScale);
  2248. inStream.Read(mSampleCount);
  2249. inStream.Read(mBlockSize);
  2250. inStream.Read(mBitsPerSample);
  2251. inStream.Read(mMinSample);
  2252. inStream.Read(mMaxSample);
  2253. inStream.Read(mMaterialIndices);
  2254. inStream.Read(mNumBitsPerMaterialIndex);
  2255. // We don't have the exact number of reserved materials anymore, but ensure that our array is big enough
  2256. // TODO: Next time when we bump the binary serialization format of this class we should store the capacity and allocate the right amount, for now we accept a little bit of waste
  2257. mMaterials.reserve(PhysicsMaterialList::size_type(1) << mNumBitsPerMaterialIndex);
  2258. CacheValues();
  2259. bool has_heights = false;
  2260. inStream.Read(has_heights);
  2261. if (has_heights)
  2262. {
  2263. AllocateBuffers();
  2264. inStream.ReadBytes(mRangeBlocks, mRangeBlocksSize * sizeof(RangeBlock) + mHeightSamplesSize + mActiveEdgesSize);
  2265. }
  2266. }
  2267. void HeightFieldShape::SaveMaterialState(PhysicsMaterialList &outMaterials) const
  2268. {
  2269. outMaterials = mMaterials;
  2270. }
  2271. void HeightFieldShape::RestoreMaterialState(const PhysicsMaterialRefC *inMaterials, uint inNumMaterials)
  2272. {
  2273. mMaterials.assign(inMaterials, inMaterials + inNumMaterials);
  2274. }
  2275. Shape::Stats HeightFieldShape::GetStats() const
  2276. {
  2277. return Stats(
  2278. sizeof(*this)
  2279. + mMaterials.size() * sizeof(Ref<PhysicsMaterial>)
  2280. + mRangeBlocksSize * sizeof(RangeBlock)
  2281. + mHeightSamplesSize * sizeof(uint8)
  2282. + mActiveEdgesSize * sizeof(uint8)
  2283. + mMaterialIndices.size() * sizeof(uint8),
  2284. mHeightSamplesSize == 0? 0 : Square(mSampleCount - 1) * 2);
  2285. }
  2286. void HeightFieldShape::sRegister()
  2287. {
  2288. ShapeFunctions &f = ShapeFunctions::sGet(EShapeSubType::HeightField);
  2289. f.mConstruct = []() -> Shape * { return new HeightFieldShape; };
  2290. f.mColor = Color::sPurple;
  2291. for (EShapeSubType s : sConvexSubShapeTypes)
  2292. {
  2293. CollisionDispatch::sRegisterCollideShape(s, EShapeSubType::HeightField, sCollideConvexVsHeightField);
  2294. CollisionDispatch::sRegisterCastShape(s, EShapeSubType::HeightField, sCastConvexVsHeightField);
  2295. CollisionDispatch::sRegisterCastShape(EShapeSubType::HeightField, s, CollisionDispatch::sReversedCastShape);
  2296. CollisionDispatch::sRegisterCollideShape(EShapeSubType::HeightField, s, CollisionDispatch::sReversedCollideShape);
  2297. }
  2298. // Specialized collision functions
  2299. CollisionDispatch::sRegisterCollideShape(EShapeSubType::Sphere, EShapeSubType::HeightField, sCollideSphereVsHeightField);
  2300. CollisionDispatch::sRegisterCastShape(EShapeSubType::Sphere, EShapeSubType::HeightField, sCastSphereVsHeightField);
  2301. }
  2302. JPH_NAMESPACE_END