private void GetChannels(int[] version) { m_Channels = new ChannelInfo[6]; for (int i = 0; i < 6; i++) { m_Channels[i] = new ChannelInfo(); } for (var s = 0; s < m_Streams.Length; s++) { var m_Stream = m_Streams[s]; var channelMask = new BitArray(new[] { (int)m_Stream.channelMask }); byte offset = 0; for (int i = 0; i < 6; i++) { if (channelMask.Get(i)) { var m_Channel = m_Channels[i]; m_Channel.stream = (byte)s; m_Channel.offset = offset; switch (i) { case 0: //kShaderChannelVertex case 1: //kShaderChannelNormal m_Channel.format = 0; //kChannelFormatFloat m_Channel.dimension = 3; break; case 2: //kShaderChannelColor m_Channel.format = 2; //kChannelFormatColor m_Channel.dimension = 4; break; case 3: //kShaderChannelTexCoord0 case 4: //kShaderChannelTexCoord1 m_Channel.format = 0; //kChannelFormatFloat m_Channel.dimension = 2; break; case 5: //kShaderChannelTangent m_Channel.format = 0; //kChannelFormatFloat m_Channel.dimension = 4; break; } offset += (byte)(m_Channel.dimension * MeshHelper.GetFormatSize(version, m_Channel.format)); } } } }
private void GetStreams(int[] version) { var streamCount = m_Channels.Max(x => x.stream) + 1; m_Streams = new StreamInfo[streamCount]; uint offset = 0; for (int s = 0; s < streamCount; s++) { uint chnMask = 0; uint stride = 0; for (int chn = 0; chn < m_Channels.Length; chn++) { var m_Channel = m_Channels[chn]; if (m_Channel.stream == s) { if (m_Channel.dimension > 0) { chnMask |= 1u << chn; stride += m_Channel.dimension * MeshHelper.GetFormatSize(version, m_Channel.format); } } } m_Streams[s] = new StreamInfo { channelMask = chnMask, offset = offset, stride = stride, dividerOp = 0, frequency = 0 }; offset += m_VertexCount * stride; //static size_t AlignStreamSize (size_t size) { return (size + (kVertexStreamAlign-1)) & ~(kVertexStreamAlign-1); } offset = (offset + (16u - 1u)) & ~(16u - 1u); } }
private void ReadVertexData() { m_VertexCount = (int)m_VertexData.m_VertexCount; for (var chn = 0; chn < m_VertexData.m_Channels.Length; chn++) { var m_Channel = m_VertexData.m_Channels[chn]; if (m_Channel.dimension > 0) { var m_Stream = m_VertexData.m_Streams[m_Channel.stream]; var channelMask = new BitArray(new[] { (int)m_Stream.channelMask }); if (channelMask.Get(chn)) { if (version[0] < 2018 && chn == 2 && m_Channel.format == 2) { m_Channel.dimension = 4; } var componentByteSize = (int)MeshHelper.GetFormatSize(version, m_Channel.format); var componentBytes = new byte[m_VertexCount * m_Channel.dimension * componentByteSize]; for (int v = 0; v < m_VertexCount; v++) { var vertexOffset = (int)m_Stream.offset + m_Channel.offset + (int)m_Stream.stride * v; for (int d = 0; d < m_Channel.dimension; d++) { var componentOffset = vertexOffset + componentByteSize * d; Buffer.BlockCopy(m_VertexData.m_DataSize, componentOffset, componentBytes, componentByteSize * (v * m_Channel.dimension + d), componentByteSize); } } if (reader.endian == EndianType.BigEndian && componentByteSize > 1) //swap bytes { for (var i = 0; i < componentBytes.Length / componentByteSize; i++) { var buff = new byte[componentByteSize]; Buffer.BlockCopy(componentBytes, i * componentByteSize, buff, 0, componentByteSize); buff = buff.Reverse().ToArray(); Buffer.BlockCopy(buff, 0, componentBytes, i * componentByteSize, componentByteSize); } } int[] componentsIntArray = null; float[] componentsFloatArray = null; if (MeshHelper.IsIntFormat(version, m_Channel.format)) { componentsIntArray = MeshHelper.BytesToIntArray(componentBytes, componentByteSize); } else { componentsFloatArray = MeshHelper.BytesToFloatArray(componentBytes, componentByteSize); } if (version[0] >= 2018) { switch (chn) { case 0: //kShaderChannelVertex m_Vertices = componentsFloatArray; break; case 1: //kShaderChannelNormal m_Normals = componentsFloatArray; break; case 2: //kShaderChannelTangent m_Tangents = componentsFloatArray; break; case 3: //kShaderChannelColor m_Colors = componentsFloatArray; break; case 4: //kShaderChannelTexCoord0 m_UV0 = componentsFloatArray; break; case 5: //kShaderChannelTexCoord1 m_UV1 = componentsFloatArray; break; case 6: //kShaderChannelTexCoord2 m_UV2 = componentsFloatArray; break; case 7: //kShaderChannelTexCoord3 m_UV3 = componentsFloatArray; break; case 8: //kShaderChannelTexCoord4 m_UV4 = componentsFloatArray; break; case 9: //kShaderChannelTexCoord5 m_UV5 = componentsFloatArray; break; case 10: //kShaderChannelTexCoord6 m_UV6 = componentsFloatArray; break; case 11: //kShaderChannelTexCoord7 m_UV7 = componentsFloatArray; break; //2018.2 and up case 12: //kShaderChannelBlendWeight if (m_Skin == null) { InitMSkin(); } for (int i = 0; i < m_VertexCount; i++) { for (int j = 0; j < m_Channel.dimension; j++) { m_Skin[i].weight[j] = componentsFloatArray[i * m_Channel.dimension + j]; } } break; case 13: //kShaderChannelBlendIndices if (m_Skin == null) { InitMSkin(); } for (int i = 0; i < m_VertexCount; i++) { for (int j = 0; j < m_Channel.dimension; j++) { m_Skin[i].boneIndex[j] = componentsIntArray[i * m_Channel.dimension + j]; } } break; } } else { switch (chn) { case 0: //kShaderChannelVertex m_Vertices = componentsFloatArray; break; case 1: //kShaderChannelNormal m_Normals = componentsFloatArray; break; case 2: //kShaderChannelColor m_Colors = componentsFloatArray; break; case 3: //kShaderChannelTexCoord0 m_UV0 = componentsFloatArray; break; case 4: //kShaderChannelTexCoord1 m_UV1 = componentsFloatArray; break; case 5: if (version[0] >= 5) //kShaderChannelTexCoord2 { m_UV2 = componentsFloatArray; } else //kShaderChannelTangent { m_Tangents = componentsFloatArray; } break; case 6: //kShaderChannelTexCoord3 m_UV3 = componentsFloatArray; break; case 7: //kShaderChannelTangent m_Tangents = componentsFloatArray; break; } } } } } }