protected override CpuAnimatedVertexBuffer Read(ContentReader input, CpuAnimatedVertexBuffer buffer) { IGraphicsDeviceService graphicsDeviceService = (IGraphicsDeviceService)input.ContentManager.ServiceProvider.GetService(typeof(IGraphicsDeviceService)); var device = graphicsDeviceService.GraphicsDevice; // read standard VertexBuffer var declaration = input.ReadRawObject <VertexDeclaration>(); var vertexCount = (int)input.ReadUInt32(); // int dataSize = vertexCount * declaration.VertexStride; //byte[] data = new byte[dataSize]; //input.Read(data, 0, dataSize); //read data var channels = declaration.GetVertexElements(); var cpuVertices = new VertexIndicesWeightsPositionNormal[vertexCount]; var gpuVertices = new VertexPositionNormalTexture[vertexCount]; for (int i = 0; i < vertexCount; i++) { foreach (var channel in channels) { switch (channel.VertexElementUsage) { case VertexElementUsage.Position: System.Diagnostics.Debug.Assert(channel.VertexElementFormat == VertexElementFormat.Vector3); var pos = input.ReadVector3(); cpuVertices[i].Position = pos; gpuVertices[i].Position = pos; break; case VertexElementUsage.Normal: System.Diagnostics.Debug.Assert(channel.VertexElementFormat == VertexElementFormat.Vector3); var nor = input.ReadVector3(); cpuVertices[i].Normal = nor; gpuVertices[i].Normal = nor; break; case VertexElementUsage.TextureCoordinate: System.Diagnostics.Debug.Assert(channel.VertexElementFormat == VertexElementFormat.Vector2); var tex = input.ReadVector2(); gpuVertices[i].TextureCoordinate = tex; break; case VertexElementUsage.BlendWeight: System.Diagnostics.Debug.Assert(channel.VertexElementFormat == VertexElementFormat.Vector4); var wei = input.ReadVector4(); cpuVertices[i].BlendWeights = wei; break; case VertexElementUsage.BlendIndices: System.Diagnostics.Debug.Assert(channel.VertexElementFormat == VertexElementFormat.Byte4); var i0 = input.ReadByte(); var i1 = input.ReadByte(); var i2 = input.ReadByte(); var i3 = input.ReadByte(); cpuVertices[i].BlendIndex0 = i0; cpuVertices[i].BlendIndex1 = i1; cpuVertices[i].BlendIndex2 = i2; cpuVertices[i].BlendIndex3 = i3; break; default: throw new Exception(); } } } // read extras bool IsWriteOnly = input.ReadBoolean(); if (buffer == null) { BufferUsage usage = (IsWriteOnly) ? BufferUsage.WriteOnly : BufferUsage.None; buffer = new CpuAnimatedVertexBuffer(device, VertexPositionNormalTexture.VertexDeclaration, vertexCount, usage); } buffer.SetData(gpuVertices, 0, vertexCount); buffer.SetGpuVertices(gpuVertices); buffer.SetCpuVertices(cpuVertices); return(buffer); }
internal static Native.Animation.VertexTypes.VertexIndicesWeightsPositionNormal ToNativeCpuVertex(this VertexIndicesWeightsPositionNormal source) { Native.Animation.VertexTypes.VertexIndicesWeightsPositionNormal result; //result.BlendIndices = source.BlendIndices.ToInt4Data(); result.BlendIndices = new Native.Animation.Data.Int4Data() { X = source.BlendIndex0, Y = source.BlendIndex1, Z = source.BlendIndex2, W = source.BlendIndex3, }; result.BlendWeights = source.BlendWeights.ToNativeVector4Data(); result.Position = source.Position.ToNativeVector3Data(); result.Normal = source.Normal.ToNativeVector3Data(); return(result); }