Example #1
0
        public void LoadBuffers(MyModelData modelData, string assetName = null)
        {
            System.Diagnostics.Debug.Assert(modelData.Sections.Count > 0, "Invalid object");
            if (modelData.Sections.Count == 0)
                return;

            // create index buffer
            {
                m_trianglesCount = modelData.Indices.Count / 3;
                m_Indices_16bit = new ushort[modelData.Indices.Count];

                for (int i = 0; i < modelData.Indices.Count; ++i)
                    m_Indices_16bit[i] = (ushort)modelData.Indices[i];

                m_indexBuffer = new IndexBuffer(MyRender.GraphicsDevice, m_Indices_16bit.Length * sizeof(short), Usage.WriteOnly, Pool.Default, true);
                m_indexBuffer.SetData(m_Indices_16bit);
                m_indexBuffer.Tag = this;
                m_indexBufferSize = m_Indices_16bit.Length * sizeof(short);

                SignResource(m_indexBuffer);
            }

            // create vertex buffer
            {
                m_verticesCount = modelData.Positions.Count;
                m_vertices      = new MyCompressedVertexNormal[m_verticesCount];
                var vertexArray = new MyVertexFormatPositionNormalTextureTangent[m_verticesCount];

                for (int i = 0; i < modelData.Positions.Count; ++i)
                {
                    vertexArray[i].Position = modelData.Positions[i];
                    vertexArray[i].Normal   = modelData.Normals[i];
                    vertexArray[i].Tangent  = modelData.Tangents[i];
                    vertexArray[i].TexCoord = modelData.TexCoords[i];

                    m_vertices[i] = new MyCompressedVertexNormal()
                    {
                        Position = vertexArray[i].PositionPacked,
                        Normal   = vertexArray[i].NormalPacked
                    };
                }

                m_vertexDeclaration = MyVertexFormatPositionNormalTextureTangent.VertexDeclaration;
                m_vertexStride      = MyVertexFormatPositionNormalTextureTangent.Stride;
                m_vertexBufferSize  = vertexArray.Length * m_vertexStride;
                m_vertexBuffer      = new VertexBuffer(MyRender.GraphicsDevice, m_vertexBufferSize, Usage.WriteOnly, VertexFormat.None, Pool.Default);
                m_vertexBuffer.SetData(vertexArray);
                m_vertexBuffer.Tag = this;

                SignResource(m_vertexBuffer);
            }

            m_meshContainer.Clear();

            // apply materials here
            for (int s = 0; s < modelData.Sections.Count; ++s)
            {
                var mpi            = new MyMeshPartInfo();
                mpi.Technique      = m_drawTechnique;
                
                // Disabled, because it assert always when models are loaded before materials
                //System.Diagnostics.Debug.Assert(MyRenderModels.Materials.ContainsKey(modelData.Sections[s].MaterialName), "Mesh material not present!");
                
                if (MyRenderModels.Materials.ContainsKey(modelData.Sections[s].MaterialName))
                    mpi.m_MaterialDesc = MyRenderModels.Materials[modelData.Sections[s].MaterialName];

                var start = modelData.Sections[s].IndexStart;
                var end   = start + modelData.Sections[s].TriCount*3;

                for (int i = start; i < end; ++i)
                    mpi.m_indices.Add(modelData.Indices[i]);

                m_meshContainer.Add(new MyRenderMesh(mpi, null)
                {
                    IndexStart = modelData.Sections[s].IndexStart,
                    TriCount   = modelData.Sections[s].TriCount
                });
            }

            // store properties of this model
            {
                BoundingBox    = modelData.AABB;
                BoundingSphere = new BoundingSphere(modelData.AABB.Center, modelData.AABB.HalfExtents.Length());

                BoundingBoxSize     = BoundingBox.Size;
                BoundingBoxSizeHalf = BoundingBox.HalfExtents;

                ModelInfo = new MyModelInfo(m_trianglesCount, m_verticesCount, BoundingBoxSize);

                PreloadTextures(LoadingMode.Immediate);
                LoadState = Textures.LoadState.Loaded;

                m_loadedContent = true;
                m_loadedData    = true;
            }
        }
Example #2
0
        void CreateVertexBuffer()
        {
            //  Create vertex buffer - vertex format type depends on draw technique

            switch (m_drawTechnique)
            {
                case MyMeshDrawTechnique.MESH:
                case MyMeshDrawTechnique.DECAL:
                case MyMeshDrawTechnique.HOLO:
                case MyMeshDrawTechnique.ALPHA_MASKED:
                case MyMeshDrawTechnique.SKINNED:
                    {
                        if (m_forLoadingTexCoords0 == null) throw new Exception("Model '" + m_assetName + "' doesn't have texture channel 0 specified, but this shader requires it");

                        if (m_forLoadingTexCoords0.Length == 0)
                        {
                            MyVertexFormatPosition[] vertexArray = new MyVertexFormatPosition[GetVerticesCount()];
                            for (int i = 0; i < GetVerticesCount(); i++)
                            {
                                vertexArray[i].Position = m_vertices[i].Position;
                            }

                            m_vertexDeclaration = MyVertexFormatPosition.VertexDeclaration;
                            m_vertexStride = MyVertexFormatPosition.Stride;
                            m_vertexBufferSize = vertexArray.Length * m_vertexStride;
                            m_vertexBuffer = new VertexBuffer(MyRender.GraphicsDevice, m_vertexBufferSize, Usage.WriteOnly, VertexFormat.None, Pool.Default);
                            m_vertexBuffer.SetData(vertexArray);
                            m_vertexBuffer.Tag = this;
                        }
                        else
                            if (MyRenderConstants.RenderQualityProfile.UseNormals)
                            {
                                if (m_forLoadingTangents == null)
                                    throw new Exception("Model '" + m_assetName + "' doesn't have tangent vectors calculated, but this shader requires them");

                                if (BoneIndices.Length > 0)
                                {
                                    MyVertexFormatPositionNormalTextureTangentSkinned[] vertexArray = new MyVertexFormatPositionNormalTextureTangentSkinned[GetVerticesCount()];
                                    for (int i = 0; i < GetVerticesCount(); i++)
                                    {
                                        vertexArray[i].PositionPacked = m_vertices[i].Position;
                                        vertexArray[i].NormalPacked = m_vertices[i].Normal;
                                        vertexArray[i].TexCoordPacked = m_forLoadingTexCoords0[i];
                                        vertexArray[i].TangentPacked = m_forLoadingTangents[i];

                                        vertexArray[i].BoneIndices = new Byte4(BoneIndices[i].X, BoneIndices[i].Y, BoneIndices[i].Z, BoneIndices[i].W);
                                        vertexArray[i].BoneWeights = BoneWeights[i];

                                    }

                                    m_vertexDeclaration = MyVertexFormatPositionNormalTextureTangentSkinned.VertexDeclaration;
                                    m_vertexStride = MyVertexFormatPositionNormalTextureTangentSkinned.Stride;
                                    m_vertexBufferSize = vertexArray.Length * m_vertexStride;
                                    m_vertexBuffer = new VertexBuffer(MyRender.GraphicsDevice, m_vertexBufferSize, Usage.WriteOnly, VertexFormat.None, Pool.Default);
                                    m_vertexBuffer.SetData(vertexArray);
                                    m_vertexBuffer.Tag = this;
                                }
                                else
                                {

                                    MyVertexFormatPositionNormalTextureTangent[] vertexArray = new MyVertexFormatPositionNormalTextureTangent[GetVerticesCount()];
                                    for (int i = 0; i < GetVerticesCount(); i++)
                                    {
                                        vertexArray[i].PositionPacked = m_vertices[i].Position;
                                        vertexArray[i].NormalPacked = m_vertices[i].Normal;
                                        vertexArray[i].TexCoordPacked = m_forLoadingTexCoords0[i];
                                        vertexArray[i].TangentPacked = m_forLoadingTangents[i];
                                    }

                                    m_vertexDeclaration = MyVertexFormatPositionNormalTextureTangent.VertexDeclaration;
                                    m_vertexStride = MyVertexFormatPositionNormalTextureTangent.Stride;
                                    m_vertexBufferSize = vertexArray.Length * m_vertexStride;
                                    m_vertexBuffer = new VertexBuffer(MyRender.GraphicsDevice, m_vertexBufferSize, Usage.WriteOnly, VertexFormat.None, Pool.Default);
                                    m_vertexBuffer.SetData(vertexArray);
                                    m_vertexBuffer.Tag = this;
                                }

                            }
                            else
                            {

                                MyVertexFormatPositionNormalTexture[] vertexArray = new MyVertexFormatPositionNormalTexture[GetVerticesCount()];
                                for (int i = 0; i < GetVerticesCount(); i++)
                                {
                                    vertexArray[i].Position = GetVertexInt(i);
                                    vertexArray[i].Normal = GetVertexNormal(i);
                                    vertexArray[i].TexCoord = m_forLoadingTexCoords0[i].ToVector2();
                                }

                                m_vertexDeclaration = MyVertexFormatPositionNormalTexture.VertexDeclaration;
                                m_vertexStride = MyVertexFormatPositionNormalTexture.Stride;
                                m_vertexBufferSize = vertexArray.Length * m_vertexStride;
                                m_vertexBuffer = new VertexBuffer(MyRender.GraphicsDevice, m_vertexBufferSize, Usage.WriteOnly, VertexFormat.None, Pool.Default);
                                m_vertexBuffer.SetData(vertexArray);
                                m_vertexBuffer.Tag = this;

                            }
                    }
                    break;

                case MyMeshDrawTechnique.VOXELS_DEBRIS:
                    {
                        MyVertexFormatPositionNormal[] vertexArray = new MyVertexFormatPositionNormal[GetVerticesCount()];
                        for (int i = 0; i < GetVerticesCount(); i++)
                        {
                            vertexArray[i].Position = GetVertexInt(i);
                            vertexArray[i].Normal = GetVertexNormal(i);
                        }

                        m_vertexDeclaration = MyVertexFormatPositionNormal.VertexDeclaration;
                        m_vertexStride = MyVertexFormatPositionNormal.Stride;
                        m_vertexBufferSize = vertexArray.Length * m_vertexStride;
                        m_vertexBuffer = new VertexBuffer(MyRender.GraphicsDevice, m_vertexBufferSize, Usage.WriteOnly, VertexFormat.None, Pool.Default);
                        m_vertexBuffer.SetData(vertexArray);
                        m_vertexBuffer.Tag = this;
                    }
                    break;
                default:
                    {
                        throw new InvalidBranchException();
                    }
            }

            MyPerformanceCounter.PerAppLifetime.ModelVertexBuffersSize += m_vertexBufferSize;

            SignResource(m_vertexBuffer);
        }
 private void AddVertex(ref int vertexCounter, MyVertexFormatPositionNormalTextureTangent vertexData)
 {
     m_vertices[vertexCounter++] = vertexData;
 }