Beispiel #1
0
        public static MeshData MeshInPos(this Block block, BlockPos bpos, ICoreClientAPI api)
        {
            MeshData thismesh;
            ShapeTesselatorManager tesselatormanager = api.TesselatorManager as ShapeTesselatorManager;

            if (block.HasAlternates)
            {
                long alternateIndex = block.RandomizeAxes == EnumRandomizeAxes.XYZ ?
                                      GameMath.MurmurHash3Mod(bpos.X, bpos.Y, bpos.Z, tesselatormanager.altblockModelDatas[block.Id].Length) : GameMath.MurmurHash3Mod(bpos.X, 0, bpos.Z, tesselatormanager.altblockModelDatas[block.Id].Length);
                thismesh = tesselatormanager.altblockModelDatas[block.Id][alternateIndex];
            }
            else
            {
                thismesh = tesselatormanager.blockModelDatas[block.Id];
            }

            if (block.RandomizeRotations)
            {
                if (block.BlockMaterial == EnumBlockMaterial.Leaves)
                {
                    int index = GameMath.MurmurHash3Mod(bpos.X, bpos.Y, bpos.Z, JsonTesselator.randomRotationsLeaves.Length);
                    thismesh = thismesh.Clone().MatrixTransform(JsonTesselator.randomRotMatricesLeaves[index]);
                }
                else
                {
                    int index = GameMath.MurmurHash3Mod(bpos.X, bpos.Y, bpos.Z, JsonTesselator.randomRotations.Length);
                    thismesh = thismesh.Clone().MatrixTransform(JsonTesselator.randomRotMatrices[index]);
                }
            }
            return(thismesh.Clone());
        }
Beispiel #2
0
        public static MeshData MeshInPos(this Block block, BlockPos bpos, ICoreClientAPI api)
        {
            MeshData thismesh;
            ShapeTesselatorManager tesselatormanager = api.TesselatorManager as ShapeTesselatorManager;
            var lod0 = tesselatormanager.blockModelDatasLod0.ContainsKey(block.Id) ? tesselatormanager.blockModelDatasLod0[block.Id] : null;
            var lod1 = tesselatormanager.blockModelDatas[block.Id].Clone();

            var lod0alt = tesselatormanager.altblockModelDatasLod0[block.Id];
            var lod1alt = tesselatormanager.altblockModelDatasLod1[block.Id];

            if (block.HasAlternates && lod1alt != null)
            {
                long alternateIndex = block.RandomizeAxes == EnumRandomizeAxes.XYZ ? GameMath.MurmurHash3Mod(bpos.X, bpos.Y, bpos.Z, lod1alt.Length) : GameMath.MurmurHash3Mod(bpos.X, 0, bpos.Z, lod1alt.Length);
                thismesh = lod1alt[alternateIndex].Clone();
                var lod = lod0alt?[alternateIndex].Clone();

                if (lod != null && thismesh != lod)
                {
                    thismesh.IndicesMax = thismesh.Indices.Count();
                    lod.IndicesMax      = lod.Indices.Count();

                    thismesh.AddMeshData(lod);
                    thismesh.CompactBuffers();
                }
            }
            else
            {
                thismesh            = lod1;
                thismesh.IndicesMax = thismesh.Indices.Count();
                if (lod0 != null)
                {
                    lod0.IndicesMax = lod0.Indices.Count();
                    if (thismesh != lod0)
                    {
                        thismesh.AddMeshData(lod0);
                    }
                }
            }

            if (block.RandomizeRotations)
            {
                int index = GameMath.MurmurHash3Mod(bpos.X, bpos.Y, bpos.Z, JsonTesselator.randomRotations.Length);
                thismesh = thismesh.Clone().MatrixTransform(JsonTesselator.randomRotMatrices[index]);
            }

            return(thismesh.Clone());
        }
Beispiel #3
0
        public override void StartClientSide(ICoreClientAPI api)
        {
            this.capi = api;
            api.RegisterCommand("obj", "", "", (p, a) =>
            {
                var bs      = api.World.Player.CurrentBlockSelection;
                var es      = api.World.Player.CurrentEntitySelection;
                string word = a.PopWord("object");

                if (bs != null)
                {
                    var asset = api.World.BlockAccessor.GetBlock(bs.Position).Shape.Base;
                    api.Tesselator.TesselateShape(api.World.GetBlock(0), (api.TesselatorManager as ShapeTesselatorManager).shapes[asset], out MeshData mesh);
                    ConvertToObj(mesh, word, true, false);
                }
                else if (es != null)
                {
                    api.Tesselator.TesselateShape(api.World.GetBlock(0), es.Entity.Properties.Client.LoadedShape, out MeshData mesh);
                    ConvertToObj(mesh, word, true, false);
                }
            });
            api.RegisterCommand("meshdata", "", "", (p, a) =>
            {
                var bs      = api.World.Player.CurrentBlockSelection;
                var es      = api.World.Player.CurrentEntitySelection;
                string word = a.PopWord("object");

                if (bs != null)
                {
                    var asset = api.World.BlockAccessor.GetBlock(bs.Position).Shape.Base;
                    api.Tesselator.TesselateShape(api.World.GetBlock(0), (api.TesselatorManager as ShapeTesselatorManager).shapes[asset], out MeshData mesh);
                    using (TextWriter tw = new StreamWriter(Path.Combine(GamePaths.Binaries, word + ".json")))
                    {
                        tw.Write(JsonConvert.SerializeObject(mesh, Formatting.Indented));
                    }
                }
                else if (es != null)
                {
                    api.Tesselator.TesselateShape(api.World.GetBlock(0), es.Entity.Properties.Client.LoadedShape, out MeshData mesh);
                    using (TextWriter tw = new StreamWriter(Path.Combine(GamePaths.Binaries, word + ".json")))
                    {
                        tw.Write(JsonConvert.SerializeObject(mesh, Formatting.Indented));
                    }
                }
            });

            api.RegisterCommand("objworld", "", "", (p, a) =>
            {
                MeshData mesh      = new MeshData(1, 1);
                BlockPos playerPos = api.World.Player.Entity.Pos.AsBlockPos;
                int rad            = (int)a.PopInt(16);
                int yrad           = (int)a.PopInt(16);
                ShapeTesselatorManager tesselatormanager = api.TesselatorManager as ShapeTesselatorManager;

                api.World.BlockAccessor.WalkBlocks(playerPos.AddCopy(rad, yrad, rad), playerPos.AddCopy(-rad, -yrad, -rad), (block, bpos) =>
                {
                    if (block.Id != 0 && api.World.BlockAccessor.GetLightLevel(bpos, EnumLightLevelType.MaxLight) > 0)
                    {
                        MeshData thismesh = tesselatormanager.blockModelDatasLod0.ContainsKey(block.Id) ?
                                            tesselatormanager.blockModelDatasLod0[block.Id].Clone() : block.MeshInPos(bpos, api);

                        Vec3f translation = new Vec3f(-(playerPos.X - bpos.X), -(playerPos.Y - bpos.Y), -(playerPos.Z - bpos.Z));

                        thismesh.Translate(translation);
                        mesh.AddMeshData(thismesh);
                    }
                });
                ConvertToObj(mesh, "world", false, true);
            });
        }
Beispiel #4
0
        private static MeshRef GenerateMesh(ClientMain game, string meshId, ItemStack stack)
        {
            MeshRef         meshRef = null;
            UnloadableShape shape   = new UnloadableShape();

            if (stack.Item.Shape?.Base != null)
            {
                shape.Load(game, new AssetLocationAndSource(stack.Item.Shape.Base));
            }
            if (shape.Textures == null)
            {
                shape.Textures = new Dictionary <string, AssetLocation>();
            }
            if (shape.AttachmentPointsByCode == null)
            {
                shape.AttachmentPointsByCode = new Dictionary <string, AttachmentPoint>();
            }
            Item item = new Item();

            item.Textures = new Dictionary <string, CompositeTexture>();

            foreach (ItemstackAttribute attr in stack.Attributes.GetOrAddTreeAttribute("toolparts").Values)
            {
                ItemStack partstack = attr.GetValue() as ItemStack;
                IToolPart part      = partstack.Item as IToolPart;
                if (part != null)
                {
                    if (part.TinkerProps?.ProvidedTextures == null)
                    {
                        partstack.Item.Textures.ToList().ForEach(kp =>
                        {
                            shape.Textures[kp.Key] = kp.Value.Base;
                            item.Textures[kp.Key]  = kp.Value;
                        });
                        UnloadableShape tmp = new UnloadableShape();
                        if (!tmp.Load(game, new AssetLocationAndSource(partstack.Item.Shape.Base)))
                        {
                            continue;
                        }


                        ShapeElement slot = shape.GetElementByName(part.TinkerProps.PartType);

                        if (slot != null)
                        {
                            slot.Children = slot.Children.Concat(tmp.CloneElements()[0].Children).ToArray();
                        }

                        if (tmp.AttachmentPointsByCode != null)
                        {
                            tmp.AttachmentPointsByCode.ToList().ForEach(kp => shape.AttachmentPointsByCode[kp.Key] = kp.Value);
                        }
                    }
                    else
                    {
                        part.TinkerProps.ProvidedTextures.ToList().ForEach(kp =>
                        {
                            shape.Textures[kp.Key] = kp.Value;
                            item.Textures[kp.Key]  = new CompositeTexture()
                            {
                                Base = kp.Value.Clone()
                            };
                        });
                    }
                }
            }

            ShapeTesselatorManager  manager    = game.GetType().GetField("TesselatorManager", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public).GetValue(game) as ShapeTesselatorManager;
            ItemTextureAtlasManager blockAtlas = game.GetType().GetField("ItemAtlasManager", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public).GetValue(game) as ItemTextureAtlasManager;
            ClientPlatformAbstract  platform   = game.GetType().GetField("Platform", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public).GetValue(game) as ClientPlatformAbstract;

            if (manager != null && blockAtlas != null && platform != null)
            {
                TextureSource       source  = new TextureSource(game, blockAtlas.Size, item);
                var                 field   = manager.Tesselator.GetType().GetField("meta", BindingFlags.Instance | BindingFlags.NonPublic);
                var                 oldMeta = field.GetValue(manager.Tesselator) as TesselationMetaData;
                TesselationMetaData meta    = oldMeta.Clone();
                meta.texSource    = source;
                meta.withJointIds = false;
                MeshData meshData;
                manager.TLTesselator.Value.TesselateShape((Shape)shape, out meshData, new Vec3f(), new Vec3f(), meta);
                meshRef          = platform.UploadMesh(meshData);
                MeshRefs[meshId] = new AccessLifetime <MeshRef>(meshRef);
            }
            return(meshRef);
        }