RuntimeMeshSection.cpp
Go to the documentation of this file.
1 // Copyright 2016-2018 Chris Conway (Koderz). All Rights Reserved.
2 
3 #include "RuntimeMeshSection.h"
5 #include "PhysicsEngine/PhysicsSettings.h"
7 
8 template<typename Type>
10 {
11  const TArray<uint8>* Data;
12  int32 Offset;
13  int32 Stride;
14 public:
15  FRuntimeMeshStreamAccessor(const TArray<uint8>* InData, int32 InOffset, int32 InStride)
16  : Data(InData), Offset(InOffset), Stride(InStride)
17  {
18  }
20 
21  int32 Num() const { return Data->Num() / Stride; }
22 
23  Type& Get(int32 Index)
24  {
25  int32 StartPosition = (Index * Stride + Offset);
26  return *((Type*)(&(*Data)[StartPosition]));
27  }
28 };
29 
30 // Helper for accessing position element within a vertex stream
32 {
33 public:
34  FRuntimeMeshVertexStreamPositionAccessor(TArray<uint8>* InData, const FRuntimeMeshVertexStreamStructure& StreamStructure)
35  : FRuntimeMeshStreamAccessor<FVector>(InData, StreamStructure.Position.Offset, StreamStructure.Position.Stride)
36  {
37  check(StreamStructure.Position.IsValid());
38  }
39 };
40 
42 {
44 
45  virtual FVector2D GetUV(int32 Index) = 0;
46  virtual int32 Num() = 0;
47 };
48 
49 // Helper for accessing position element within a vertex stream
51 {
52 public:
54  : FRuntimeMeshStreamAccessor<FVector2D>(InData, Element.Offset, Element.Stride)
55  {
56  check(Element.IsValid());
57  }
58 
59  virtual FVector2D GetUV(int32 Index) override
60  {
61  return Get(Index);
62  }
63 
64  virtual int32 Num() override
65  {
67  }
68 };
70 {
71 public:
73  : FRuntimeMeshStreamAccessor<FVector2DHalf>(InData, Element.Offset, Element.Stride)
74  {
75  check(Element.IsValid());
76  }
77 
78  virtual FVector2D GetUV(int32 Index) override
79  {
80  return Get(Index);
81  }
82 
83  virtual int32 Num() override
84  {
86  }
87 };
88 
89 
91 {
92  Params.Data = Data;
93  Params.NumVertices = GetNumVertices();
94 }
95 
97 {
98  Params.b32BitIndices = b32BitIndices;
99  Params.Data = Data;
100  Params.NumIndices = GetNumIndices();
101 }
102 
103 FRuntimeMeshSection::FRuntimeMeshSection(bool bInUseHighPrecisionTangents, bool bInUseHighPrecisionUVs, int32 InNumUVs, bool b32BitIndices, EUpdateFrequency InUpdateFrequency/*, FRuntimeMeshLockProvider* InSyncRoot*/)
104  : UpdateFrequency(InUpdateFrequency)
105  , TangentsBuffer(bInUseHighPrecisionTangents)
106  , UVsBuffer(bInUseHighPrecisionUVs, InNumUVs)
107  , IndexBuffer(b32BitIndices)
108  , AdjacencyIndexBuffer(b32BitIndices)
109  , LocalBoundingBox(EForceInit::ForceInitToZero)
110  , bCollisionEnabled(false)
111  , bIsVisible(true)
112  , bCastsShadow(true)
113 // , SyncRoot(InSyncRoot)
114 {
115 
116 
117 }
118 
121  , TangentsBuffer(false)
122  , UVsBuffer(false, 1)
123  , IndexBuffer(false)
124  , AdjacencyIndexBuffer(false)
125  , LocalBoundingBox(EForceInit::ForceInitToZero)
126  , bCollisionEnabled(false)
127  , bIsVisible(true)
128  , bCastsShadow(true)
129 {
130  Ar << *this;
131 }
132 
134 {
135  FRuntimeMeshSectionCreationParamsPtr CreationParams = MakeShared<FRuntimeMeshSectionCreationParams, ESPMode::NotThreadSafe>();
136 
137  CreationParams->UpdateFrequency = UpdateFrequency;
138 
139  PositionBuffer.FillUpdateParams(CreationParams->PositionVertexBuffer);
140  TangentsBuffer.FillUpdateParams(CreationParams->TangentsVertexBuffer);
141  UVsBuffer.FillUpdateParams(CreationParams->UVsVertexBuffer);
142  ColorBuffer.FillUpdateParams(CreationParams->ColorVertexBuffer);
143 
144  IndexBuffer.FillUpdateParams(CreationParams->IndexBuffer);
145  AdjacencyIndexBuffer.FillUpdateParams(CreationParams->AdjacencyIndexBuffer);
146 
147  CreationParams->bIsVisible = bIsVisible;
148  CreationParams->bCastsShadow = bCastsShadow;
149 
150  return CreationParams;
151 }
152 
154 {
155  FRuntimeMeshSectionUpdateParamsPtr UpdateParams = MakeShared<FRuntimeMeshSectionUpdateParams, ESPMode::NotThreadSafe>();
156 
157  UpdateParams->BuffersToUpdate = BuffersToUpdate;
158 
159  if (!!(BuffersToUpdate & ERuntimeMeshBuffersToUpdate::PositionBuffer))
160  {
161  PositionBuffer.FillUpdateParams(UpdateParams->PositionVertexBuffer);
162  }
163 
164  if (!!(BuffersToUpdate & ERuntimeMeshBuffersToUpdate::TangentBuffer))
165  {
166  TangentsBuffer.FillUpdateParams(UpdateParams->TangentsVertexBuffer);
167  }
168 
169  if (!!(BuffersToUpdate & ERuntimeMeshBuffersToUpdate::UVBuffer))
170  {
171  UVsBuffer.FillUpdateParams(UpdateParams->UVsVertexBuffer);
172  }
173 
174  if (!!(BuffersToUpdate & ERuntimeMeshBuffersToUpdate::ColorBuffer))
175  {
176  ColorBuffer.FillUpdateParams(UpdateParams->ColorVertexBuffer);
177  }
178 
179  if (!!(BuffersToUpdate & ERuntimeMeshBuffersToUpdate::IndexBuffer))
180  {
181  IndexBuffer.FillUpdateParams(UpdateParams->IndexBuffer);
182  }
183 
184  if (!!(BuffersToUpdate & ERuntimeMeshBuffersToUpdate::AdjacencyIndexBuffer))
185  {
186  AdjacencyIndexBuffer.FillUpdateParams(UpdateParams->AdjacencyIndexBuffer);
187  }
188 
189  return UpdateParams;
190 }
191 
192 TSharedPtr<struct FRuntimeMeshSectionPropertyUpdateParams, ESPMode::NotThreadSafe> FRuntimeMeshSection::GetSectionPropertyUpdateData()
193 {
194  FRuntimeMeshSectionPropertyUpdateParamsPtr UpdateParams = MakeShared<FRuntimeMeshSectionPropertyUpdateParams, ESPMode::NotThreadSafe>();
195 
196  UpdateParams->bCastsShadow = bCastsShadow;
197  UpdateParams->bIsVisible = bIsVisible;
198 
199  return UpdateParams;
200 }
201 
203 {
204  FBox NewBoundingBox(reinterpret_cast<FVector*>(PositionBuffer.GetData().GetData()), PositionBuffer.GetNumVertices());
205 
206  LocalBoundingBox = NewBoundingBox;
207 }
208 
209 int32 FRuntimeMeshSection::GetCollisionData(TArray<FVector>& OutPositions, TArray<FTriIndices>& OutIndices, TArray<FVector2D>& OutUVs)
210 {
211  int32 StartVertexPosition = OutPositions.Num();
212  OutPositions.Append(reinterpret_cast<FVector*>(PositionBuffer.GetData().GetData()), PositionBuffer.GetNumVertices());
213 
214  bool bCopyUVs = UPhysicsSettings::Get()->bSupportUVFromHitResults;
215 
216  //if (bCopyUVs)
217  //{
218  // TUniquePtr<FRuntimeMeshVertexStreamUVAccessor> StreamAccessor = nullptr;
219 
220  // const auto SetupStreamAccessor = [](TArray<uint8>* Data, const FRuntimeMeshVertexStreamStructureElement& Element) -> TUniquePtr<FRuntimeMeshVertexStreamUVAccessor>
221  // {
222  // if (Element.Type == VET_Float2 || Element.Type == VET_Float4)
223  // {
224  // return MakeUnique<FRuntimeMeshVertexStreamUVFullPrecisionAccessor>(Data, Element);
225  // }
226  // else
227  // {
228  // check(Element.Type == VET_Half2 || Element.Type == VET_Half4);
229  // return MakeUnique<FRuntimeMeshVertexStreamUVHalfPrecisionAccessor>(Data, Element);
230  // }
231  // };
232 
233  // if (VertexBuffer0.GetStructure().HasUVs())
234  // {
235  // StreamAccessor = SetupStreamAccessor(&VertexBuffer0.GetData(), VertexBuffer0.GetStructure().UVs[0]);
236  // }
237  // else if (VertexBuffer1.GetStructure().HasUVs())
238  // {
239  // StreamAccessor = SetupStreamAccessor(&VertexBuffer1.GetData(), VertexBuffer1.GetStructure().UVs[0]);
240  // }
241  // else if (VertexBuffer2.GetStructure().HasUVs())
242  // {
243  // StreamAccessor = SetupStreamAccessor(&VertexBuffer2.GetData(), VertexBuffer2.GetStructure().UVs[0]);
244  // }
245  // else
246  // {
247  // // Add blank entries since we can't get UV's for this section
248  // OutUVs.AddZeroed(PositionAccessor.Num());
249  // }
250 
251  // if (StreamAccessor.IsValid())
252  // {
253  // OutUVs.Reserve(OutUVs.Num() + StreamAccessor->Num());
254  // for (int32 Index = 0; Index < StreamAccessor->Num(); Index++)
255  // {
256  // OutUVs.Add(StreamAccessor->GetUV(Index));
257  // }
258  // }
259  //}
260 
261  TArray<uint8>& IndexData = IndexBuffer.GetData();
262 
264  {
265  int32 NumIndices = IndexBuffer.GetNumIndices();
266  for (int32 Index = 0; Index < NumIndices; Index += 3)
267  {
268  // Add the triangle
269  FTriIndices& Triangle = *new (OutIndices) FTriIndices;
270  Triangle.v0 = (*((int32*)&IndexData[(Index + 0) * 4])) + StartVertexPosition;
271  Triangle.v1 = (*((int32*)&IndexData[(Index + 1) * 4])) + StartVertexPosition;
272  Triangle.v2 = (*((int32*)&IndexData[(Index + 2) * 4])) + StartVertexPosition;
273  }
274  }
275  else
276  {
277  int32 NumIndices = IndexBuffer.GetNumIndices();
278  for (int32 Index = 0; Index < NumIndices; Index += 3)
279  {
280  // Add the triangle
281  FTriIndices& Triangle = *new (OutIndices) FTriIndices;
282  Triangle.v0 = (*((uint16*)&IndexData[(Index + 0) * 2])) + StartVertexPosition;
283  Triangle.v1 = (*((uint16*)&IndexData[(Index + 1) * 2])) + StartVertexPosition;
284  Triangle.v2 = (*((uint16*)&IndexData[(Index + 2) * 2])) + StartVertexPosition;
285  }
286  }
287 
288 
289  return IndexBuffer.GetNumIndices() / 3;
290 }
291 
292 
294 {
295  Params.bUsingHighPrecision = bUseHighPrecision;
297 }
298 
300 {
301  Params.bUsingHighPrecision = bUseHighPrecision;
302  Params.NumUVs = UVCount;
304 }
virtual FVector2D GetUV(int32 Index) override
FRuntimeMeshStreamAccessor(const TArray< uint8 > *InData, int32 InOffset, int32 InStride)
FRuntimeMeshSection(FArchive &Ar)
FRuntimeMeshVertexStreamUVFullPrecisionAccessor(TArray< uint8 > *InData, const FRuntimeMeshVertexStreamStructureElement &Element)
FSectionPositionVertexBuffer PositionBuffer
EUpdateFrequency
FRuntimeMeshVertexStreamStructureElement Position
TSharedPtr< struct FRuntimeMeshSectionCreationParams, ESPMode::NotThreadSafe > GetSectionCreationParams()
FSectionColorVertexBuffer ColorBuffer
FSectionTangentsVertexBuffer TangentsBuffer
TSharedPtr< FRuntimeMeshSectionPropertyUpdateParams, ESPMode::NotThreadSafe > FRuntimeMeshSectionPropertyUpdateParamsPtr
TSharedPtr< FRuntimeMeshSectionUpdateParams, ESPMode::NotThreadSafe > FRuntimeMeshSectionUpdateParamsPtr
const TArray< uint8 > * Data
FSectionIndexBuffer AdjacencyIndexBuffer
FSectionUVsVertexBuffer UVsBuffer
FRuntimeMeshVertexStreamUVHalfPrecisionAccessor(TArray< uint8 > *InData, const FRuntimeMeshVertexStreamStructureElement &Element)
TSharedPtr< struct FRuntimeMeshSectionUpdateParams, ESPMode::NotThreadSafe > GetSectionUpdateData(ERuntimeMeshBuffersToUpdate BuffersToUpdate)
void FillUpdateParams(FRuntimeMeshSectionIndexBufferParams &Params)
FSectionIndexBuffer IndexBuffer
UTexture2D * Get(TUniquePtr< T > &Dtex)
static const textual_icon check
Definition: model-views.h:260
const EUpdateFrequency UpdateFrequency
int32 GetCollisionData(TArray< FVector > &OutPositions, TArray< FTriIndices > &OutIndices, TArray< FVector2D > &OutUVs)
TSharedPtr< FRuntimeMeshSectionCreationParams, ESPMode::NotThreadSafe > FRuntimeMeshSectionCreationParamsPtr
ERuntimeMeshBuffersToUpdate
virtual FVector2D GetUV(int32 Index) override
void FillUpdateParams(FRuntimeMeshSectionUVVertexBufferParams &Params)
void FillUpdateParams(FRuntimeMeshSectionTangentVertexBufferParams &Params)
TSharedPtr< struct FRuntimeMeshSectionPropertyUpdateParams, ESPMode::NotThreadSafe > GetSectionPropertyUpdateData()
void FillUpdateParams(FRuntimeMeshSectionVertexBufferParams &Params)
FRuntimeMeshVertexStreamPositionAccessor(TArray< uint8 > *InData, const FRuntimeMeshVertexStreamStructure &StreamStructure)


librealsense2
Author(s): Sergey Dorodnicov , Doron Hirshberg , Mark Horn , Reagan Lopez , Itay Carpis
autogenerated on Mon May 3 2021 02:47:41