private void WriteMesh(AssimpMesh mesh, int slice) { this.position[slice].SliceCount = mesh.VerticesCount; fixed(Vector3 *vptr = &this.position[slice].Stream.Buffer[0]) { memcpy(new IntPtr(vptr), mesh.PositionPointer, mesh.VerticesCount * 12); } if (mesh.HasNormals) { this.normals[slice].SliceCount = mesh.VerticesCount; fixed(Vector3 *nptr = &this.normals[slice].Stream.Buffer[0]) { memcpy(new IntPtr(nptr), mesh.NormalsPointer, mesh.VerticesCount * 12); } } else { this.normals[slice].SliceCount = 0; } if (mesh.UvChannelCount > 0) { int chancnt = this.GetChannelCount(mesh, 0); this.uv1[slice].SliceCount = mesh.VerticesCount * chancnt; this.uvchancount[slice] = chancnt; fixed(float *uptr = &this.uv1[slice].Stream.Buffer[0]) { memcpy(new IntPtr(uptr), mesh.GetUvPointer(0), mesh.VerticesCount * 4 * chancnt); } } else { this.uv1[slice].SliceCount = 0; this.uvchancount[slice] = 0; } this.indices[slice].SliceCount = mesh.Indices.Count; var inds = mesh.Indices; var ibo = this.indices[slice].Stream.Buffer; for (int i = 0; i < mesh.Indices.Count; i++) { ibo[i] = inds[i]; } }
public void Update(IPluginIO pin, DX11RenderContext context) { if (this.FInvalidate || !this.FOutGeom[0].Contains(context)) { int vertexoffset = 0; List <Pos3Norm3Tex2InstanceVertex> vertices = new List <Pos3Norm3Tex2InstanceVertex>(); List <int> indices = new List <int>(); List <Vector2> uvs = new List <Vector2>(); int cnt = this.FinUseName[0] ? idsort.Count : this.FInScene[0].MeshCount; for (int i = 0; i < cnt; i++) { AssimpMesh assimpmesh = this.FinUseName[0] == false ? this.FInScene[0].Meshes[i] : this.FInScene[0].Meshes[idsort[i]]; List <int> inds = assimpmesh.Indices; if (inds.Count > 0 && assimpmesh.VerticesCount > 0) { var texcd = assimpmesh.GetInputElements().Where(ie => ie.SemanticName == "TEXCOORD").FirstOrDefault(); bool zuv = false; if (texcd != null) { zuv = texcd.Format == SlimDX.DXGI.Format.R32G32B32_Float; } for (int j = 0; j < inds.Count; j++) { indices.Add(inds[j] + vertexoffset); } DataStream posbuffer = new DataStream(assimpmesh.PositionPointer, assimpmesh.VerticesCount * 12, true, true); DataStream normbuffer = new DataStream(assimpmesh.NormalsPointer, assimpmesh.VerticesCount * 12, true, true); DataStream uvbuffer = null; List <DataStream> uvbuffers = new List <DataStream>(); List <int> uvcounters = new List <int>(); for (int uvc = 0; uvc < assimpmesh.UvChannelCount; uvc++) { uvbuffers.Add(new DataStream(assimpmesh.GetUvPointer(uvc), assimpmesh.VerticesCount * 12, true, true)); uvcounters.Add(this.GetUVChannelCount(assimpmesh, uvc)); } if (assimpmesh.UvChannelCount > 0) { uvbuffer = new DataStream(assimpmesh.GetUvPointer(0), assimpmesh.VerticesCount * 12, true, true); } Vector3 *pos = (Vector3 *)posbuffer.DataPointer.ToPointer(); Vector3 accum = Vector3.Zero; for (int j = 0; j < assimpmesh.VerticesCount; j++) { accum += pos[j]; } Vector3 center = accum / assimpmesh.VerticesCount; for (int j = 0; j < assimpmesh.VerticesCount; j++) { Pos3Norm3Tex2InstanceVertex vert = new Pos3Norm3Tex2InstanceVertex() { InstanceID = i, Normals = normbuffer.Read <Vector3>(), Position = posbuffer.Read <Vector3>(), Center = center, TexCoords = uvbuffer != null?uvbuffer.Read <Vector2>() : Vector2.Zero }; vertices.Add(vert); for (int k = 0; k < assimpmesh.UvChannelCount; k++) { var b = uvbuffers[k]; uvs.Add(b.Read <Vector2>()); if (uvcounters[k] == 3) { b.Read <float>(); } } if (uvbuffer != null && zuv) { uvbuffer.Read <float>(); } } vertexoffset += assimpmesh.VerticesCount; } } DataStream vS = new DataStream(vertices.ToArray(), true, true); vS.Position = 0; DataStream iS = new DataStream(indices.ToArray(), true, true); iS.Position = 0; var vbuffer = new SlimDX.Direct3D11.Buffer(context.Device, vS, new BufferDescription() { BindFlags = BindFlags.VertexBuffer, CpuAccessFlags = CpuAccessFlags.None, OptionFlags = ResourceOptionFlags.None, SizeInBytes = (int)vS.Length, Usage = ResourceUsage.Default }); DX11IndexedGeometry geom = new DX11IndexedGeometry(context); geom.VertexBuffer = vbuffer; geom.IndexBuffer = new DX11IndexBuffer(context, iS, true, false); geom.InputLayout = Pos3Norm3Tex2InstanceVertex.Layout; geom.Topology = PrimitiveTopology.TriangleList; geom.VerticesCount = vertices.Count; geom.VertexSize = Pos3Norm3Tex2InstanceVertex.VertexSize; geom.HasBoundingBox = false; vS.Position = 0; DataStream uvS = new DataStream(uvs.ToArray(), true, true); uvS.Position = 0; this.FOutGeom[0][context] = geom; this.FOutBuffer[0][context] = new DX11ImmutableStructuredBuffer(context.Device, geom.VerticesCount, geom.VertexSize, vS); this.FOutIndices[0][context] = new DX11RawBuffer(context, geom.IndexBuffer.Buffer); this.FOutUVBuffer[0][context] = new DX11ImmutableStructuredBuffer(context.Device, uvs.Count, 8, uvS); this.FInvalidate = false; } }
private void WriteMesh(AssimpMesh mesh, int slice) { this.position[slice].SliceCount = mesh.VerticesCount; fixed (Vector3* vptr = &this.position[slice].Stream.Buffer[0]) { memcpy(new IntPtr(vptr), mesh.PositionPointer, mesh.VerticesCount * 12); } if (mesh.HasNormals) { this.normals[slice].SliceCount = mesh.VerticesCount; fixed (Vector3* nptr = &this.normals[slice].Stream.Buffer[0]) { memcpy(new IntPtr(nptr), mesh.NormalsPointer, mesh.VerticesCount * 12); } } else { this.normals[slice].SliceCount = 0; } if (mesh.UvChannelCount > 0) { int chancnt = this.GetChannelCount(mesh, 0); this.uv1[slice].SliceCount = mesh.VerticesCount * chancnt; this.uvchancount[slice] = chancnt; fixed (float* uptr = &this.uv1[slice].Stream.Buffer[0]) { memcpy(new IntPtr(uptr), mesh.GetUvPointer(0), mesh.VerticesCount * 4 * chancnt); } } else { this.uv1[slice].SliceCount = 0; this.uvchancount[slice] = 0; } this.indices[slice].SliceCount = mesh.Indices.Count; var inds = mesh.Indices; var ibo = this.indices[slice].Stream.Buffer; for (int i = 0; i < mesh.Indices.Count; i++) { ibo[i] = inds[i]; } }
public void Update(IPluginIO pin, DX11RenderContext context) { if (this.FInvalidate || this.FEmpty) { for (int i = 0; i < this.scenes.Count; i++) { if (scenes[i] != null) { AssimpScene scene = scenes[i]; for (int j = 0; j < scene.MeshCount; j++) { AssimpMesh assimpmesh = scene.Meshes[j]; List <int> inds = assimpmesh.Indices; if (inds.Count > 0 && assimpmesh.VerticesCount > 0) { var indexstream = new DataStream(inds.Count * 4, true, true); indexstream.WriteRange(inds.ToArray()); indexstream.Position = 0; DX11IndexOnlyGeometry geom = new DX11IndexOnlyGeometry(context); geom.IndexBuffer = new DX11IndexBuffer(context, indexstream, false, true); geom.InputLayout = assimpmesh.GetInputElements().ToArray(); geom.Topology = PrimitiveTopology.TriangleList; geom.HasBoundingBox = true; geom.BoundingBox = assimpmesh.BoundingBox; DX11DynamicStructuredBuffer <Vector3> p = new DX11DynamicStructuredBuffer <Vector3>(context, assimpmesh.PositionPointer, assimpmesh.VerticesCount); DX11DynamicStructuredBuffer <Vector3> n = new DX11DynamicStructuredBuffer <Vector3>(context, assimpmesh.NormalsPointer, assimpmesh.VerticesCount); if (assimpmesh.UvChannelCount > 0) { DX11DynamicStructuredBuffer <Vector3> u = new DX11DynamicStructuredBuffer <Vector3>(context, assimpmesh.GetUvPointer(0), assimpmesh.VerticesCount); this.FOutUvs[i][j][context] = u; } DX11RawBuffer rb = new DX11RawBuffer(context, geom.IndexBuffer.Buffer); this.FOutPosition[i][j][context] = p; this.FOutNormals[i][j][context] = n; this.FOutGeom[i][j][context] = geom; this.FOutIndices[i][j][context] = rb; } } } } this.FInvalidate = false; this.FEmpty = false; } }