Пример #1
0
        private MeshData[] GenContentMeshes(ITesselatorAPI tesselator)
        {
            ItemStack content = GetContents();

            if (content == null)
            {
                return(null);
            }

            Dictionary <string, MeshData[]> meshes = ObjectCacheUtil.GetOrCreate(Api, "plantContainerContentMeshes", () =>
            {
                return(new Dictionary <string, MeshData[]>());
            });

            MeshData[] meshwithVariants;
            string     containersize = this.ContainerSize;
            string     key           = GetContents()?.ToString() + "-" + containersize;

            if (meshes.TryGetValue(key, out meshwithVariants))
            {
                return(meshwithVariants);
            }

            curContProps = PlantContProps;
            if (curContProps == null)
            {
                return(null);
            }

            CompositeShape compoShape = curContProps.Shape;

            if (compoShape == null)
            {
                compoShape = content.Class == EnumItemClass.Block ? content.Block.Shape : content.Item.Shape;
            }
            ModelTransform transform = curContProps.Transform;

            if (transform == null)
            {
                transform = new ModelTransform().EnsureDefaultValues();
                transform.Translation.Y = Block.Attributes["fillHeight"].AsFloat(0.4f);
            }

            contentTexSource = content.Class == EnumItemClass.Block ? capi.Tesselator.GetTexSource(content.Block) : capi.Tesselator.GetTextureSource(content.Item);
            List <IAsset> assets;

            if (compoShape.Base.Path.EndsWith("*"))
            {
                assets = Api.Assets.GetMany(compoShape.Base.WithPathPrefixOnce("shapes/").Path.Substring(0, compoShape.Base.Path.Length - 1), compoShape.Base.Domain);
            }
            else
            {
                assets = new List <IAsset>();
                assets.Add(Api.Assets.TryGet(compoShape.Base.WithPathPrefixOnce("shapes/").WithPathAppendixOnce(".json")));
            }

            if (assets != null && assets.Count > 0)
            {
                meshwithVariants = new MeshData[assets.Count];

                for (int i = 0; i < assets.Count; i++)
                {
                    IAsset asset = assets[i];
                    Shape  shape = asset.ToObject <Shape>();
                    shapeTextures = shape.Textures;
                    MeshData mesh;

                    tesselator.TesselateShape("plant container content shape", shape, out mesh, this);

                    for (int j = 0; j < mesh.RenderPassCount; j++)
                    {
                        mesh.RenderPasses[j] = (int)EnumChunkRenderPass.OpaqueNoCull;
                    }

                    Block       block = GetContents()?.Block;
                    VertexFlags flags = block?.VertexFlags;
                    if (flags != null && flags.GrassWindWave)
                    {
                        int grassWave = VertexFlags.FoliageWindWaveBitMask | VertexFlags.WeakWaveBitMask;

                        for (int vertexNum = 0; vertexNum < mesh.GetVerticesCount(); vertexNum++)
                        {
                            float y = mesh.xyz[vertexNum * 3 + 1];
                            if (y > block.WaveFlagMinY)
                            {
                                mesh.Flags[vertexNum] |= grassWave;
                            }
                        }
                    }

                    mesh.ModelTransform(transform);
                    meshwithVariants[i] = mesh;
                }
            }
            else
            {
                Api.World.Logger.Error("Plant container, content asset {0} not found,", compoShape.Base.WithPathPrefixOnce("shapes/").WithPathAppendixOnce(".json"));
            }

            return(meshes[key] = meshwithVariants);
        }
        private MeshData[] GenContentMeshes(ITesselatorAPI tesselator)
        {
            ItemStack content = GetContents();

            if (content == null)
            {
                return(null);
            }

            Dictionary <string, MeshData[]> meshes = ObjectCacheUtil.GetOrCreate(Api, "plantContainerContentMeshes", () =>
            {
                return(new Dictionary <string, MeshData[]>());
            });

            MeshData[] meshwithVariants;
            string     containersize = this.ContainerSize;
            string     key           = content?.ToString() + "-" + containersize;

            if (meshes.TryGetValue(key, out meshwithVariants))
            {
                return(meshwithVariants);
            }

            curContProps = PlantContProps;
            if (curContProps == null)
            {
                return(null);
            }

            CompositeShape compoShape = curContProps.Shape;

            if (compoShape == null)
            {
                compoShape = content.Class == EnumItemClass.Block ? content.Block.Shape : content.Item.Shape;
            }
            ModelTransform transform = curContProps.Transform;

            if (transform == null)
            {
                transform = new ModelTransform().EnsureDefaultValues();
                transform.Translation.Y = Block.Attributes["fillHeight"].AsFloat(0.4f);
            }

            contentTexSource = content.Class == EnumItemClass.Block ? capi.Tesselator.GetTexSource(content.Block) : capi.Tesselator.GetTextureSource(content.Item);
            List <IAsset> assets;

            if (compoShape.Base.Path.EndsWith("*"))
            {
                assets = Api.Assets.GetMany(compoShape.Base.WithPathPrefixOnce("shapes/").Path.Substring(0, compoShape.Base.Path.Length - 1), compoShape.Base.Domain);
            }
            else
            {
                assets = new List <IAsset>();
                assets.Add(Api.Assets.TryGet(compoShape.Base.WithPathPrefixOnce("shapes/").WithPathAppendixOnce(".json")));
            }

            if (assets != null && assets.Count > 0)
            {
                meshwithVariants = new MeshData[assets.Count];

                for (int i = 0; i < assets.Count; i++)
                {
                    IAsset asset = assets[i];
                    Shape  shape = asset.ToObject <Shape>();
                    shapeTextures = shape.Textures;
                    MeshData mesh;

                    try
                    {
                        byte climateColorMapId = content.Block?.ClimateColorMapResolved == null ? (byte)0 : (byte)(content.Block.ClimateColorMapResolved.RectIndex + 1);
                        byte seasonColorMapId  = content.Block?.SeasonColorMapResolved == null ? (byte)0 : (byte)(content.Block.SeasonColorMapResolved.RectIndex + 1);

                        tesselator.TesselateShape("plant container content shape", shape, out mesh, this, null, 0, climateColorMapId, seasonColorMapId);
                    }
                    catch (Exception e)
                    {
                        Api.Logger.Error(e.Message + " (when tesselating " + compoShape.Base.WithPathPrefixOnce("shapes/") + ")"); meshwithVariants = null; break;
                    }

                    // WTF is this here for? It breaks mycelium overlay

                    /*for (int j = 0; j < mesh.RenderPassCount; j++)
                     * {
                     *  mesh.RenderPassesAndExtraBits[j] = (int)EnumChunkRenderPass.OpaqueNoCull;
                     * }*/

                    //Block block = GetContents()?.Block;

                    /*int clearBitMask = VertexFlags.ClearWindModeBitsMask;
                     * for (int vertexNum = 0; vertexNum < mesh.GetVerticesCount(); vertexNum++)
                     * {
                     *  float y = mesh.xyz[vertexNum * 3 + 1];
                     *  if (y <= block.WaveFlagMinY)
                     *  {
                     *      mesh.Flags[vertexNum] &= clearBitMask;
                     *  }
                     * }*/

                    mesh.ModelTransform(transform);
                    meshwithVariants[i] = mesh;
                }
            }
            else
            {
                Api.World.Logger.Error("Plant container, content asset {0} not found,", compoShape.Base.WithPathPrefixOnce("shapes/").WithPathAppendixOnce(".json"));
            }

            return(meshes[key] = meshwithVariants);
        }