Exemplo n.º 1
0
        public override void Register(Block block)
        {
            var index         = MaterialProvider.AllocateBlock();
            var textureName   = "Stone2";
            var textureNormal = "NormalMap";
            var uv            = MaterialProvider.Replace(index, GetTexture(textureName), TextureType.Main);

            MaterialProvider.Replace(index, GetTexture(textureNormal), TextureType.Normal);

            var pp = new BlockPart[6];

            void addPart(Facing facing)
            {
                pp[(int)facing] = Geometry.GetBlockPart(facing, uv);
            }

            addPart(Facing.North);
            addPart(Facing.East);
            addPart(Facing.South);
            addPart(Facing.West);
            addPart(Facing.Up);
            addPart(Facing.Down);

            parts[(int)block.id] = pp;
        }
Exemplo n.º 2
0
        BlockParts RegisterBlock(ModelInfo modelInfo)
        {
            if (textures.TryGetValue(modelInfo.texture, out var blockParts))
            {
                return(blockParts);
            }

            var render = RenderTexture.active;

            var texture = GetTexture(modelInfo.texture);
            var _normal = GetTexture(modelInfo.normal);

            if (_normal == null)
            {
                Debug.LogError("not found normal texture for model " + modelInfo.name);
            }
            var src = MaterialProvider.CreateTexture2D(texture.width, texture.height);

            Graphics.ConvertTexture(texture, src);
            var normal = MaterialProvider.CreateTexture2D(texture.width, texture.height);

            Graphics.ConvertTexture(_normal, normal);
            var tileSize   = new Vector2Int(src.width / tileGridSize.x, src.height / tileGridSize.y);
            var tmpTxt     = MaterialProvider.CreateTexture2D(tileSize.x * 2, tileSize.y * 2);
            var normalTemp = MaterialProvider.CreateTexture2D(tileSize.x * 2, tileSize.y * 2);

            var corners = new Vec2i[4];

            corners[(int)CornerEnum.TL] = new Vec2i(0, tileSize.x);
            corners[(int)CornerEnum.TR] = new Vec2i(tileSize.x, tileSize.y);
            corners[(int)CornerEnum.BL] = new Vec2i(0, 0);
            corners[(int)CornerEnum.BR] = new Vec2i(tileSize.x, 0);
            foreach (var sample in samples.Values)
            {
                for (int i = 0; i < 4; i++)
                {
                    var x = sample.corners[i].x * tileSize.x;
                    var y = sample.corners[i].y * tileSize.y;
                    Graphics.CopyTexture(src, 0, 0, x, y, tileSize.x, tileSize.y, tmpTxt, 0, 0, corners[i].x, corners[i].y);
                    Graphics.CopyTexture(normal, 0, 0, x, y, tileSize.x, tileSize.y, normalTemp, 0, 0, corners[i].x, corners[i].y);
                }
                var index = MaterialProvider.AllocateBlock();
                sample.uv = MaterialProvider.Replace(index, tmpTxt, TextureType.Main);
                MaterialProvider.Replace(index, normalTemp, TextureType.Normal);
            }
            RenderTexture.active = render;
            UnityEngine.Object.Destroy(tmpTxt);

            void addPart(BlockPart[] pp, Facing facing, Rect uvRect)
            {
                pp[(int)facing] = Geometry.GetBlockPart(facing, uvRect);
            }

            blockParts       = new BlockParts();
            blockParts.parts = new BlockPart[256][];
            for (int sides = 0; sides <= 0xFF; sides++)
            {
                var sample = indexes[sides];

                var pp = new BlockPart[6];
                addPart(pp, Facing.North, sample.uv);
                addPart(pp, Facing.East, sample.uv);
                addPart(pp, Facing.South, sample.uv);
                addPart(pp, Facing.West, sample.uv);
                addPart(pp, Facing.Up, sample.uv);
                addPart(pp, Facing.Down, sample.uv);
                blockParts.parts[sides] = pp;
            }

            textures.Add(modelInfo.texture, blockParts);
            return(blockParts);
        }