// Methods
        internal void InternalRead(MDChunk model, BinaryReader reader)
        {
            _batchSize   = reader.ReadUInt16();
            _materialID  = reader.ReadUInt16();
            _batchOffset = reader.ReadInt32();

            reader.BaseStream.Seek((uint)model.offset + MDChunk.DATA_START_ADDRESS + _batchOffset, SeekOrigin.Begin);
            _vifPackets = VIFCodeEvaluator.EvaluateBlock(reader, _batchSize);
            _batches    = MDSubMeshVifBatch.ParseBatches(_vifPackets, UsedNodeCount);

            foreach (MDSubMeshVifBatch batch in _batches)
            {
                batch.TransformedPositions = new Vector3[batch.VertexCount];
                batch.TransformedNormals   = new Vector3[batch.VertexCount];
                for (int i = 0; i < batch.VertexCount; i++)
                {
                    batch.TransformedPositions[i] = Vector3.Transform(batch.Positions[i], UsedNodes[batch.UsedNodeArrayIndex].WorldMatrix);
                    batch.TransformedNormals[i]   = Vector3.TransformNormal(batch.Normals[i], UsedNodes[batch.UsedNodeArrayIndex].WorldMatrix);
                }
            }
        }
Exemple #2
0
        // Methods
        internal void InternalRead(MDChunk model, BinaryReader reader)
        {
            _batchSize       = reader.ReadUInt16();
            _materialID      = reader.ReadUInt16();
            _batchOffset     = reader.ReadInt32();
            _numUsedNodes    = reader.ReadInt32();
            _usedNodeIndices = reader.ReadUInt16Array(_numUsedNodes);
            _usedNodes       = new MDNode[_numUsedNodes];
            for (int i = 0; i < _numUsedNodes; i++)
            {
                _usedNodes[i] = model.Nodes[_usedNodeIndices[i]];
            }

            reader.BaseStream.Seek((int)model.offset + MDChunk.DATA_START_ADDRESS + _batchOffset, SeekOrigin.Begin);
            _vifPackets = VIFCodeEvaluator.EvaluateBlock(reader, _batchSize);
            _batches    = MDSubMeshVifBatch.ParseBatches(_vifPackets, _numUsedNodes);

            foreach (MDSubMeshVifBatch batch in _batches)
            {
                batch.TransformedPositions = new Vector3[batch.VertexCount];
                batch.TransformedNormals   = new Vector3[batch.VertexCount];
                for (int i = 0; i < batch.VertexCount; i++)
                {
                    if (batch.Weights[i] != 0)
                    {
                        batch.TransformedPositions[i] = Vector3.Transform(batch.Positions[i], UsedNodes[batch.UsedNodeArrayIndex].WorldMatrix * batch.Weights[i]);
                    }
                    else
                    {
                        batch.TransformedPositions[i] = Vector3.Zero;
                    }

                    if (batch.Weights[i] != 0)
                    {
                        batch.TransformedNormals[i] = Vector3.TransformNormal(batch.Normals[i], UsedNodes[batch.UsedNodeArrayIndex].WorldMatrix * batch.Weights[i]);
                    }
                    else
                    {
                        batch.TransformedNormals[i] = Vector3.Zero;
                    }
                }
            }

            List <MDSubMeshVifBatch> fixedBatches = new List <MDSubMeshVifBatch>();

            for (int i = 0; i < _batches.Count; i += (int)_numUsedNodes)
            {
                Vector3[] pos = new Vector3[_batches[i].VertexCount];
                Vector3[] nrm = new Vector3[_batches[i].VertexCount];
                for (int k = 0; k < _batches[i].VertexCount; k++)
                {
                    for (int j = 0; j < _numUsedNodes; j++)
                    {
                        pos[k] += _batches[i + j].TransformedPositions[k];
                        nrm[k] += _batches[i + j].TransformedNormals[k];
                    }
                }
                for (int k = 0; k < _batches[i].VertexCount; k++)
                {
                    nrm[k].Normalize();
                }

                /*
                 * SubMeshVifBatch finalBatch = _batches[(int)_numUsedNodes - 1];
                 * finalBatch.TransformedPositions = pos;
                 * finalBatch.TransformedNormals = pos;
                 * finalBatch.NodeIndices =
                 * fixedBatches.Add(finalBatch);
                 */
                for (int j = 0; j < _numUsedNodes; j++)
                {
                    _batches[i + j].TransformedPositions = pos;
                    _batches[i + j].TransformedNormals   = nrm;
                    _batches[i + j].Colors        = _batches[(int)_numUsedNodes - 1].Colors;
                    _batches[i + j].TextureCoords = _batches[(int)_numUsedNodes - 1].TextureCoords;
                }
            }
        }