private ModelBone ProcessBone(EntityModelBone bone, List <VertexPositionNormalTexture> vertices, Vector2 uvScale, Vector2 textureSize, Dictionary <string, ModelBone> modelBones) { List <ModelBoneCube> cubes = new List <ModelBoneCube>(); ModelBone modelBone; if (bone.Cubes != null) { foreach (var cube in bone.Cubes) { if (cube == null) { Log.Warn("Cube was null!"); continue; } var size = cube.Size; var origin = cube.Origin; var pivot = cube.Pivot; var rotation = cube.Rotation; origin = new Vector3(-(origin.X + size.X), origin.Y, origin.Z); //VertexPositionNormalTexture[] vertices; Cube built = new Cube(size * (float)cube.Inflate, textureSize); built.Mirrored = bone.Mirror; built.BuildCube(cube.Uv * uvScale); vertices = ModifyCubeIndexes(vertices, ref built.Front); vertices = ModifyCubeIndexes(vertices, ref built.Back); vertices = ModifyCubeIndexes(vertices, ref built.Top); vertices = ModifyCubeIndexes(vertices, ref built.Bottom); vertices = ModifyCubeIndexes(vertices, ref built.Left); vertices = ModifyCubeIndexes(vertices, ref built.Right); var part = new ModelBoneCube(built.Front.indexes .Concat(built.Back.indexes) .Concat(built.Top.indexes) .Concat(built.Bottom.indexes) .Concat(built.Left.indexes) .Concat(built.Right.indexes) .ToArray(), Texture, rotation, pivot, origin); part.Mirror = bone.Mirror; cubes.Add(part); } } modelBone = new ModelBone(cubes.ToArray(), bone.Parent, bone); modelBone.UpdateRotationMatrix = !bone.NeverRender; if (!modelBones.TryAdd(bone.Name, modelBone)) { Log.Debug($"Failed to add bone! {bone.Name}"); } return(modelBone); }
private void Cache(Dictionary <string, ModelBone> modelBones) { List <EntityModelBone> headBones = new List <EntityModelBone>(); var headBone = Model.Bones.FirstOrDefault(x => x.Name.Contains("head", StringComparison.InvariantCultureIgnoreCase)); if (headBone != null) { headBones.Add(headBone); foreach (var bone in Model.Bones) { if (bone == headBone) { continue; } if (bone.Parent.Equals(headBone.Name)) { headBones.Add(bone); } } foreach (var bone in Model.Bones.Where(x => !headBones.Any(hb => hb.Name.Equals(x.Name, StringComparison.InvariantCultureIgnoreCase)))) { if (headBones.Any(x => x.Name.Equals(bone.Name, StringComparison.InvariantCultureIgnoreCase))) { headBones.Add(bone); } } } List <VertexPositionNormalTexture> vertices = new List <VertexPositionNormalTexture>(); foreach (var bone in Model.Bones) { if (bone == null) { continue; } // if (bone.NeverRender) continue; bool partOfHead = headBones.Contains(bone); //bone.Pivot = new Vector3(-bone.Pivot.X, bone.Pivot.Y, bone.Pivot.Z); List <ModelBoneCube> c = new List <ModelBoneCube>(); ModelBone modelBone; if (bone.Cubes != null) { foreach (var cube in bone.Cubes) { if (cube == null) { Log.Warn("Cube was null!"); continue; } var size = cube.Size; var origin = cube.Origin; var pivot = bone.Pivot; var rotation = bone.Rotation; //VertexPositionNormalTexture[] vertices; Cube built = new Cube(size, new Vector2(Texture.Width, Texture.Height)); built.Mirrored = bone.Mirror; built.BuildCube(cube.Uv); vertices = ModifyCubeIndexes(vertices, ref built.Front, origin); vertices = ModifyCubeIndexes(vertices, ref built.Back, origin); vertices = ModifyCubeIndexes(vertices, ref built.Top, origin); vertices = ModifyCubeIndexes(vertices, ref built.Bottom, origin); vertices = ModifyCubeIndexes(vertices, ref built.Left, origin); vertices = ModifyCubeIndexes(vertices, ref built.Right, origin); var part = new ModelBoneCube(built.Front.indexes .Concat(built.Back.indexes) .Concat(built.Top.indexes) .Concat(built.Bottom.indexes) .Concat(built.Left.indexes) .Concat(built.Right.indexes) .ToArray(), Texture, rotation, pivot, origin); part.Mirror = bone.Mirror; 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); modelBone.UpdateRotationMatrix = !bone.NeverRender; if (!modelBones.TryAdd(bone.Name, modelBone)) { Log.Warn($"Failed to add bone! {Model.Name}:{bone.Name}"); } } VertexBuffer = GpuResourceManager.GetBuffer(this, Alex.Instance.GraphicsDevice, VertexPositionNormalTexture.VertexDeclaration, vertices.Count, BufferUsage.None); VertexBuffer.SetData(vertices.ToArray()); }
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; }