Ejemplo n.º 1
0
        public static bool PaintTexture2D(StrokeVector stroke, float brushAlpha, ImageData image, BrushConfig bc, PlaytimePainter pntr)
        {
            var pl = pntr.GetPlugin <TileableAtlasingPainterPlugin>();

            if (pl != null)
            {
                return(pl.PaintTexture2D(stroke, brushAlpha, image, bc, pntr));
            }
            else
            {
                return(false);
            }
        }
        public void ConvertToAtlased(PlaytimePainter painter)
        {
#if UNITY_EDITOR
            if (AtlasedMaterial == null)
            {
                AtlasedMaterial = painter.InstantiateMaterial(true);
            }

            painter.SetOriginalShaderOnThis();

            painter.UpdateOrSetTexTarget(TexTarget.Texture2D);

            Material      mat     = painter.GetMaterial(false);
            List <string> tfields = mat.GetTextures();

            int index = 0;
            List <FieldAtlas> passedFields   = new List <FieldAtlas>();
            List <Texture2D>  passedTextures = new List <Texture2D>();
            List <Color>      passedColors   = new List <Color>();

            foreach (var f in fields)
            {
                if ((f.enabled) && (f.AtlasCreator != null) && (tfields.Contains(originalTextures[f.originField])))
                {
                    string original = originalTextures[f.originField];

                    Texture tex = mat.GetTexture(original);

                    Texture2D texture = null;

                    if (tex == null)
                    {
                        var note = painter.name + " no " + original + " texture. Using Color.";
#if PEGI
                        note.showNotification();
#endif
                        Debug.Log(note);
                    }
                    else
                    {
                        if (tex.GetType() != typeof(Texture2D))
                        {
                            Debug.Log("Not a Texture 2D: " + original);
                            return;
                        }

                        texture = (Texture2D)tex;
                    }

                    var aTexes = f.AtlasCreator.textures;

                    bool added = false;

                    for (int i = index; i < aTexes.Count; i++)
                    {
                        if ((aTexes[i] == null) || (!aTexes[i].used) || (aTexes[i].texture == texture))
                        {
                            index = i;
                            passedFields.Add(f);
                            passedTextures.Add(texture);
                            passedColors.Add(f.col);
                            added = true;
                            break;
                        }
                    }

                    if (!added)
                    {
                        Debug.Log("Could not find a place for " + original);
                        return;
                    }
                }
            }

            if (passedFields.Count > 0)
            {
                bool firstAtlasing = false;

                var atlPlug = painter.GetPlugin <TileableAtlasingPainterPlugin>();

                if (atlPlug.preAtlasingMaterials == null)
                {
                    atlPlug.preAtlasingMaterials = painter.GetMaterials();
                    atlPlug.preAtlasingMesh      = painter.GetMesh();
                    firstAtlasing = true;
                }

                var MainField = passedFields[0];

                atlPlug.atlasRows = MainField.AtlasCreator.Row;

                Vector2 tyling = mat.GetTextureScale(originalTextures[MainField.originField]);
                Vector2 offset = mat.GetTextureOffset(originalTextures[MainField.originField]);

                for (int i = 0; i < passedFields.Count; i++)
                {// var f in passedFields){
                    var f  = passedFields[i];
                    var ac = f.AtlasCreator;

                    ac.textures[index] = new AtlasTextureField(passedTextures[i], passedColors[i]);

                    ac.AddTargets(f, originalTextures[f.originField]);
                    ac.ReconstructAsset();
                    AtlasedMaterial.SetTexture(f.atlasedField, ac.a_texture);
                }

                MeshManager.Inst.EditMesh(painter, true);

                if (firstAtlasing)
                {
                    atlPlug.preAtlasingSavedMesh = MeshManager.Inst.edMesh.Encode().ToString();
                }

                painter.selectedMeshProfile = matAtlasProfile;

                if ((tyling != Vector2.one) || (offset != Vector2.zero))
                {
                    MeshManager.Inst.edMesh.TileAndOffsetUVs(offset, tyling, 0);
                    Debug.Log("offsetting " + offset + " tyling " + tyling);
                }

                TriangleAtlasTool.Inst.SetAllTrianglesTextureTo(index, 0, painter.selectedSubmesh);
                MeshManager.Inst.Redraw();
                MeshManager.Inst.DisconnectMesh();

                AtlasedMaterial.SetFloat(PainterDataAndConfig.atlasedTexturesInARow, atlPlug.atlasRows);
                painter.Material = AtlasedMaterial;

                if (firstAtlasing)
                {
                    var m = painter.GetMesh();
                    m.name = m.name + "_Atlased_" + index;
                }
                //painter.getMaterial(false).SetTextureOffset(1,Vector2.zero);

                AtlasedMaterial.EnableKeyword(PainterDataAndConfig.UV_ATLASED);
            }
#endif
        }