private void ConvertToAtlased(PlaytimePainter painter)
        {
            #if UNITY_EDITOR
            if (!_atlasedMaterial)
            {
                _atlasedMaterial = painter.InstantiateMaterial(true);
            }

            painter.SetOriginalShaderOnThis();

            painter.UpdateOrSetTexTarget(TexTarget.Texture2D);

            var mat           = painter.Material;
            var texProperties = mat.MyGetTextureProperties_Editor();

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

            foreach (var f in _fields)
            {
                if (f.enabled && f.AtlasCreator != null)
                {
                    var contains = false;

                    foreach (var p in texProperties)
                    {
                        if (p.NameForDisplayPEGI().Equals(originalTextures[f.originField].NameForDisplayPEGI()))
                        {
                            contains = true;
                            break;
                        }
                    }

                    if (!contains)
                    {
                        continue;
                    }

                    var original = originalTextures[f.originField];

                    var tex = mat.Get(original);

                    Texture2D texture = null;

                    if (!tex)
                    {
                        var note = painter.name + " no " + original + " texture. Using Color.";

                        pegi.GameView.ShowNotification(note);

                        Debug.Log(note);
                    }
                    else
                    {
                        if (tex.GetType() != typeof(Texture2D))
                        {
                            Debug.Log("Not a Texture 2D: " + original);
                            return;
                        }

                        texture = (Texture2D)tex;
                    }

                    var aTextures = f.AtlasCreator.textures;

                    var added = false;

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

                    if (added)
                    {
                        continue;
                    }

                    Debug.Log("Could not find a place for " + original);

                    return;
                }
            }

            if (passedFields.Count <= 0)
            {
                return;
            }

            var firstAtlasing = false;

            var atlPlug = painter.GetModule <TileableAtlasingComponentModule>();

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

            var mainField = passedFields[0];

            atlPlug.atlasRows = mainField.AtlasCreator.Row;

            var tiling = mat.GetTiling(originalTextures[mainField.originField]);
            var offset = mat.GetOffset(originalTextures[mainField.originField]);

            for (var i = 0; i < passedFields.Count; i++)
            {
                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.aTexture);
            }

            MeshEditorManager.Inst.EditMesh(painter, true);

            if (firstAtlasing)
            {
                atlPlug.preAtlasingSavedMesh = MeshEditorManager.editedMesh.Encode().ToString();
            }

            painter.selectedMeshProfile = _matAtlasProfile;

            if (tiling != Vector2.one || offset != Vector2.zero)
            {
                MeshEditorManager.editedMesh.TileAndOffsetUVs(offset, tiling, 0);
                Debug.Log("offsetting " + offset + " tiling " + tiling);
            }

            TriangleAtlasTool.Inst.SetAllTrianglesTextureTo(index, 0, painter.selectedSubMesh);
            MeshEditorManager.Inst.Redraw();
            MeshEditorManager.Inst.StopEditingMesh();

            _atlasedMaterial.SetFloat(PainterShaderVariables.ATLASED_TEXTURES, atlPlug.atlasRows);
            painter.Material = _atlasedMaterial;

            if (firstAtlasing)
            {
                var m = painter.GetMesh();
                m.name = m.name + "_Atlased_" + index;
            }

            _atlasedMaterial.EnableKeyword(PainterShaderVariables.UV_ATLASED);
#endif
        }
Ejemplo n.º 2
0
 public void PaintPixelsInRam(StrokeVector stroke, float brushAlpha, TextureMeta image, BrushConfig bc,
                              PlaytimePainter painter) =>
 painter.GetModule <TileableAtlasingComponentModule>()
 ?.PaintTexture2D(stroke, brushAlpha, image, bc, painter);