コード例 #1
0
        public EntityModelRenderer(MinecraftGeometry geometry, PooledTexture2D texture)
        {
            Texture = texture;

            var cubes = new Dictionary <string, ModelBone>();

            var converted = geometry.Convert();

            if (converted != null)
            {
                Cache(converted, cubes);
            }
            else
            {
                Cache(geometry, cubes);
            }

            Bones = cubes;
        }
コード例 #2
0
        private void Cache(MinecraftGeometry model, Dictionary <string, ModelBone> modelBones)
        {
            List <VertexPositionNormalTexture> vertices = new List <VertexPositionNormalTexture>();

            foreach (var bone in model.Bones)
            {
                if (bone == null)
                {
                    Log.Warn("Bone null");
                    continue;
                }

                if (bone.NeverRender)
                {
                    continue;
                }

                bool partOfHead = false;

                //bone.Pivot = new Vector3(-bone.Pivot.X, bone.Pivot.Y, bone.Pivot.Z);
                List <ModelBoneCube> c = new List <ModelBoneCube>();
                ModelBone            modelBone;

                if (bone.PolyMesh != null && bone.PolyMesh.Positions != null)
                {
                    var positions = bone.PolyMesh.Positions;
                    var uvs       = bone.PolyMesh.Uvs;
                    var normals   = bone.PolyMesh.Normals;
                    var polys     = bone.PolyMesh.Polys;

                    //var verts = new VertexPositionNormalTexture[positions.Length];

                    /*	short[] indexes = new short[positions.Length];
                     *      for (int i = 0; i < bone.PolyMesh.Positions.Length; i++)
                     *      {
                     *              vertices.Add(new VertexPositionNormalTexture(positions[i], normals[i], uvs[i]));
                     *              indexes[i] = (short) ((short)startIndex + i);
                     *      }
                     */

                    List <short> indices = new List <short>();
                    for (int i = 0; i < polys.Length; i++)
                    {
                        int startIndex = vertices.Count - 1;

                        int added = 0;
                        var poly  = polys[i];
                        foreach (var p in poly)
                        {
                            var pos    = positions[p[0]];
                            var normal = normals[p[1]];
                            var uv     = uvs[p[2]];

                            vertices.Add(new VertexPositionNormalTexture(pos, normal, uv));
                            added++;
                            //indices.Add((short) (vertices.Count - 1));
                        }

                        /*int target = added == 4 ? 6 : 8;
                         *
                         * for (int indiceCounter = 0; indiceCounter < added; indiceCounter++)
                         * {
                         *      //if (indiceCounter < added)
                         *      //{
                         *              indices.Add((short) (startIndex + indiceCounter));
                         *      //}
                         *      //else
                         *      //{
                         *
                         *      //}
                         * }*/
                        indices.Add((short)startIndex);
                        indices.Add((short)(startIndex + 1));
                        indices.Add((short)(startIndex + 2));
                        indices.Add((short)startIndex);
                        indices.Add((short)(startIndex + 3));
                        indices.Add((short)(startIndex + 2));
                    }

                    //(VertexPositionNormalTexture[] vertices, short[] indexes) a = (verts, indexes);

                    var part = new ModelBoneCube(indices.ToArray(), Texture, bone.Rotation, bone.Pivot, Vector3.Zero);

                    part.Mirror = false;
                    if (partOfHead)
                    {
                        part.ApplyHeadYaw = true;
                        part.ApplyYaw     = true;
                    }
                    else
                    {
                        part.ApplyPitch   = false;
                        part.ApplyYaw     = true;
                        part.ApplyHeadYaw = false;
                    }

                    c.Add(part);
                }

                modelBone = new ModelBone(c.ToArray(), bone.Parent);
                modelBone.UpdateRotationMatrix = true;
                if (!modelBones.TryAdd(bone.Name, modelBone))
                {
                    Log.Debug($"Failed to add bone! {model.Description.Identifier}:{bone.Name}");
                }
            }

            if (vertices.Count == 0)
            {
                Log.Warn($"No vertices. {JsonConvert.SerializeObject(model,Formatting.Indented)}");
                Valid = false;
                return;
            }

            VertexBuffer = GpuResourceManager.GetBuffer(this, Alex.Instance.GraphicsDevice,
                                                        VertexPositionNormalTexture.VertexDeclaration, vertices.Count, BufferUsage.None);
            VertexBuffer.SetData(vertices.ToArray());

            Valid = true;
        }