public bool InitFromMesh(CRenderContext rc, CGfxMeshPrimitives mesh) { mesh.PreUse(true); if (false == SDK_GfxMeshDataProvider_InitFromMesh(CoreObject, rc.CoreObject, mesh.CoreObject)) { return(false); } var ptr = SDK_GfxMeshDataProvider_GetIndices(CoreObject); IndexBuffer = new Support.CBlobObject(ptr); for (int i = 0; i < (int)EVertexSteamType.VST_Number; i++) { ptr = SDK_GfxMeshDataProvider_GetStream(CoreObject, (EVertexSteamType)i); if (ptr.GetPointer() == IntPtr.Zero) { continue; } VertexBuffers[i] = new Support.CBlobObject(ptr); } return(true); }
public static async System.Threading.Tasks.Task <CGfxVertexCloud> CookFromMesh(CRenderContext rc, CGfxMeshPrimitives mesh, float density = 1.0f) { mesh.PreUse(true); var result = new CGfxVertexCloud(); var posVB = mesh.GeometryMesh.GetVertexBuffer(EVertexSteamType.VST_Position); if (posVB == null) { return(null); } var uvVB = mesh.GeometryMesh.GetVertexBuffer(EVertexSteamType.VST_UV); if (uvVB == null) { return(null); } var indexBuff = mesh.GeometryMesh.GetIndexBuffer(); if (indexBuff == null) { return(null); } var posBlob = new Support.CBlobObject(); var uvBlob = new Support.CBlobObject(); var indexBlob = new Support.CBlobObject(); await CEngine.Instance.EventPoster.Post(() => { posVB.GetBufferData(rc, posBlob); uvVB.GetBufferData(rc, uvBlob); if (density != 1.0f) { indexBuff.GetBufferData(rc, indexBlob); } return(true); }, Thread.Async.EAsyncTarget.Render); //await posVB.GetBufferData(rc, posBlob); //await uvVB.GetBufferData(rc, uvBlob); if (density == 1.0f) { unsafe { UInt32 num = posBlob.Size / (UInt32)sizeof(Vector3); result.Positions = new Vector3[num]; Vector3 *src = (Vector3 *)posBlob.Data.ToPointer(); fixed(Vector3 *dst = &result.Positions[0]) { for (UInt32 i = 0; i < num; i++) { dst[i] = src[i]; } } num = uvBlob.Size / (UInt32)sizeof(Vector2); result.Datas = new Vector4[num]; Vector2 *src2 = (Vector2 *)uvBlob.Data.ToPointer(); fixed(Vector4 *dst = &result.Datas[0]) { for (UInt32 i = 0; i < num; i++) { dst[i].X = src2[i].X; dst[i].Y = src2[i].Y; } } } } else { List <TSurface> surfaces = new List <TSurface>(); unsafe { UInt32 num = posBlob.Size / (UInt32)sizeof(Vector3); UInt32 numindex = 0; Vector3 *src = (Vector3 *)posBlob.Data.ToPointer(); if (indexBuff.Desc.Type == EIndexBufferType.IBT_Int16) { numindex = indexBlob.Size / sizeof(UInt16); UInt16 *indices = (UInt16 *)indexBlob.Data.ToPointer(); for (int i = 0; i < numindex; i += 3) { surfaces.Add(CreateSurface(ref src[indices[i]], ref src[indices[i + 1]], ref src[indices[i + 2]])); } } else { numindex = indexBlob.Size / sizeof(UInt32); UInt32 *indices = (UInt32 *)indexBlob.Data.ToPointer(); for (int i = 0; i < numindex; i += 3) { surfaces.Add(CreateSurface(ref src[indices[i]], ref src[indices[i + 1]], ref src[indices[i + 2]])); } } //根据面积排序 surfaces.Sort((a, b) => { if (a.S > b.S) { return(1); } else if (a.S < b.S) { return(-1); } return(0); }); //填充点 List <Vector3> Positions = new List <Vector3>(); for (UInt32 i = 0; i < num; i++) { Positions.Add(src[i]); } int count = (int)((density - 1f) / 0.5f); for (int n = 0; n < count; n++) { int surfacecount = surfaces.Count / 2; for (int i = surfaces.Count - 1; i >= surfacecount; i--) { Positions.Add((surfaces[i].Point1 + surfaces[i].Point2 + surfaces[i].Point3) / 3); var p1 = (surfaces[i].Point1 + surfaces[i].Point2) / 2; var p2 = (surfaces[i].Point2 + surfaces[i].Point3) / 2; var p3 = (surfaces[i].Point3 + surfaces[i].Point1) / 2; var ss = surfaces[i].S; var sss = surfaces[surfaces.Count - 1 - i].S; Positions.Add(p1); Positions.Add(p2); Positions.Add(p3); surfaces.RemoveAt(surfaces.Count - 1); surfaces.Add(CreateSurface(ref p1, ref p2, ref p3)); } if (n != count - 1) { surfaces.Sort((a, b) => { if (a.S > b.S) { return(1); } else if (a.S < b.S) { return(-1); } return(0); }); } } result.Positions = new Vector3[Positions.Count]; fixed(Vector3 *dst = &result.Positions[0]) { for (int i = 0; i < Positions.Count; i++) { dst[i] = Positions[i]; } } result.Datas = new Vector4[1]; } } return(result); }