4 using SiliconStudio.Assets.Compiler;
5 using SiliconStudio.BuildEngine;
6 using SiliconStudio.Core.IO;
7 using SiliconStudio.Core.Mathematics;
8 using SiliconStudio.Core.Serialization.Assets;
9 using SiliconStudio.Paradox.Effects.Data;
10 using SiliconStudio.Paradox.Engine;
11 using SiliconStudio.Paradox.Graphics.Data;
13 using System.Collections.Generic;
15 using System.Threading.Tasks;
21 namespace SiliconStudio.Paradox.Assets.
Physics
23 internal class ColliderShapeAssetCompiler : AssetCompilerBase<ColliderShapeAsset>
29 new ColliderShapeCombineCommand(urlInStorage, asset),
32 result.ShouldWaitForPreviousBuilds = asset.Data.ColliderShapes.Any(shape => shape.GetType() == typeof(ConvexHullColliderShapeDesc));
35 private class ColliderShapeCombineCommand :
AssetCommand<ColliderShapeAsset>
37 public ColliderShapeCombineCommand(
string url, ColliderShapeAsset asset)
42 private ConvexHullMesh convexHullMesh;
44 public override void Cancel()
48 if (convexHullMesh != null) convexHullMesh.Cancel();
57 foreach (var shape
in asset.Data.ColliderShapes)
59 var type = shape.GetType();
60 if (type == typeof(ConvexHullColliderShapeDesc))
62 var convexHullDesc = (ConvexHullColliderShapeDesc)shape;
65 if (convexHullDesc.Model != null)
69 ContentFilter = AssetManagerLoaderSettings.NewContentFilterByType(typeof(
MeshData))
72 var modelAsset = assetManager.Load<
ModelData>(convexHullDesc.Model.Location, loadSettings);
73 if (modelAsset != null)
75 convexHullDesc.ConvexHulls =
new List<List<List<Vector3>>>();
76 convexHullDesc.ConvexHullsIndices =
new List<List<List<uint>>>();
78 commandContext.Logger.Info(
"Processing convex hull generation, this might take a while!");
80 var nodeTransforms =
new List<Matrix>();
83 var nodesLength = modelAsset.Hierarchy.Nodes.Length;
84 for (var i = 0; i < nodesLength; i++)
87 TransformationComponent.CreateMatrixTRS(
88 ref modelAsset.Hierarchy.Nodes[i].Transform.Translation,
89 ref modelAsset.Hierarchy.Nodes[i].Transform.Rotation,
90 ref modelAsset.Hierarchy.Nodes[i].Transform.Scaling, out localMatrix);
93 if (modelAsset.Hierarchy.Nodes[i].ParentIndex != -1)
95 var nodeTransform = nodeTransforms[modelAsset.Hierarchy.Nodes[i].ParentIndex];
96 Matrix.Multiply(ref localMatrix, ref nodeTransform, out worldMatrix);
100 worldMatrix = localMatrix;
103 nodeTransforms.Add(worldMatrix);
106 for (var i = 0; i < nodesLength; i++)
109 if (modelAsset.Meshes.All(x => x.NodeIndex != i1))
continue;
111 var combinedVerts =
new List<float>();
112 var combinedIndices =
new List<uint>();
114 var hullsList =
new List<List<Vector3>>();
115 convexHullDesc.ConvexHulls.Add(hullsList);
117 var indicesList =
new List<List<uint>>();
118 convexHullDesc.ConvexHullsIndices.Add(indicesList);
120 foreach (var meshData
in modelAsset.Meshes.Where(x => x.NodeIndex == i1))
122 var indexOffset = (uint)combinedVerts.Count / 3;
124 var stride = meshData.Draw.VertexBuffers[0].Declaration.VertexStride;
125 var vertexDataAsset = assetManager.Load<
BufferData>(meshData.Draw.VertexBuffers[0].Buffer.Location);
127 var vertexData = vertexDataAsset.Content;
128 var vertexIndex = meshData.Draw.VertexBuffers[0].Offset;
129 for (var v = 0; v < meshData.Draw.VertexBuffers[0].Count; v++)
131 var posMatrix = Matrix.Translation(
new Vector3(BitConverter.ToSingle(vertexData, vertexIndex + 0), BitConverter.ToSingle(vertexData, vertexIndex + 4), BitConverter.ToSingle(vertexData, vertexIndex + 8)));
134 var nodeTransform = nodeTransforms[i];
135 Matrix.Multiply(ref posMatrix, ref nodeTransform, out rotatedMatrix);
137 combinedVerts.Add(rotatedMatrix.TranslationVector.X);
138 combinedVerts.Add(rotatedMatrix.TranslationVector.Y);
139 combinedVerts.Add(rotatedMatrix.TranslationVector.Z);
141 vertexIndex += stride;
144 var indexDataAsset = assetManager.Load<
BufferData>(meshData.Draw.IndexBuffer.Buffer.Location);
146 var indexData = indexDataAsset.Content;
147 var indexIndex = meshData.Draw.IndexBuffer.Offset;
148 for (var v = 0; v < meshData.Draw.IndexBuffer.Count; v++)
150 if (meshData.Draw.IndexBuffer.Is32Bit)
152 combinedIndices.Add(BitConverter.ToUInt32(indexData, indexIndex) + indexOffset);
157 combinedIndices.Add(BitConverter.ToUInt16(indexData, indexIndex) + indexOffset);
163 var decompositionDesc =
new ConvexHullMesh.DecompositionDesc
165 VertexCount = (uint)combinedVerts.Count / 3,
166 IndicesCount = (uint)combinedIndices.Count,
167 Vertexes = combinedVerts.ToArray(),
168 Indices = combinedIndices.ToArray(),
169 Depth = convexHullDesc.Depth,
170 PosSampling = convexHullDesc.PosSampling,
171 PosRefine = convexHullDesc.PosRefine,
172 AngleSampling = convexHullDesc.AngleSampling,
173 AngleRefine = convexHullDesc.AngleRefine,
174 Alpha = convexHullDesc.Alpha,
175 Threshold = convexHullDesc.Threshold,
176 SimpleHull = convexHullDesc.SimpleWrap
181 convexHullMesh =
new ConvexHullMesh();
184 convexHullMesh.Generate(decompositionDesc);
186 var
count = convexHullMesh.Count;
188 commandContext.Logger.Info(
"Node generated " + count +
" convex hulls");
190 var vertexCountHull = 0;
192 for (uint h = 0; h <
count; h++)
195 convexHullMesh.CopyPoints(h, out points);
197 var pointList =
new List<Vector3>();
199 for (var v = 0; v < points.Length; v += 3)
201 var vert =
new Vector3(points[v + 0], points[v + 1], points[v + 2]);
207 hullsList.Add(pointList);
210 convexHullMesh.CopyIndices(h, out indices);
217 var indexList =
new List<uint>(indices);
219 indicesList.Add(indexList);
224 convexHullMesh.Dispose();
225 convexHullMesh = null;
228 commandContext.Logger.Info(
"For a total of " + vertexCountHull +
" vertexes");
234 convexHullDesc.Model = null;
238 assetManager.Save(Url, asset.Data);
240 return Task.FromResult(ResultStatus.Successful);
Result of a compilation of assets when using IAssetCompiler.Compile
A command processing an Asset.
Specifies settings for AssetManager.Load{T} operations.
The context used when compiling an asset in a Package.
Data type for SiliconStudio.Paradox.Effects.Mesh.
Data type for SiliconStudio.Paradox.Effects.Model.
using SiliconStudio.Paradox. Physics
SiliconStudio.Core.Mathematics.Vector3 Vector3
Content of a GPU buffer (vertex buffer, index buffer, etc...).
Defines a normalized file path. See UPath for details. This class cannot be inherited.
Represents a 4x4 mathematical matrix.