public override void AfterGPUStroke(PlaytimePainter p, BrushConfig br, StrokeVector st, BrushType type)
 {
     if (br.IsA3Dbrush(p) && p.IsAtlased())
     {
         Shader.SetGlobalVector(PainterDataAndConfig.BRUSH_ATLAS_SECTION_AND_ROWS, new Vector4(0, 0, 1, 0));
     }
 }
        public override bool OffsetAndTileUV(RaycastHit hit, PlaytimePainter p, ref Vector2 uv)
        {
            if (p.IsAtlased())
            {
                uv.x = uv.x % 1;
                uv.y = uv.y % 1;

                var m = p.GetMesh();

                int            vert = m.triangles[hit.triangleIndex * 3];
                List <Vector4> v4l  = new List <Vector4>();
                m.GetUVs(0, v4l);
                if (v4l.Count > vert)
                {
                    inAtlasIndex = (int)v4l[vert].z;
                }

                atlasRows = Mathf.Max(atlasRows, 1);

                uv = (GetAtlasedSection() + uv) / (float)atlasRows;

                return(true);
            }
            return(false);
        }
 public override void BeforeGPUStroke(PlaytimePainter pntr, BrushConfig br, StrokeVector st, BrushType type)
 {
     if (br.IsA3Dbrush(pntr) && pntr.IsAtlased())
     {
         var ats = GetAtlasedSection();
         Shader.SetGlobalVector(PainterDataAndConfig.BRUSH_ATLAS_SECTION_AND_ROWS, new Vector4(ats.x, ats.y, atlasRows, 1));
     }
 }
        public override bool BrushConfigPEGI()
        {
            PlaytimePainter p = PlaytimePainter.inspectedPainter;

            bool changed = false;

            if (p.IsAtlased())
            {
                var m = p.GetMaterial(false);
                if (p.IsOriginalShader)
                {
                    if (m.HasProperty(PainterDataAndConfig.atlasedTexturesInARow))
                    {
                        atlasRows = m.GetInt(PainterDataAndConfig.atlasedTexturesInARow);
                    }
                }

                ("Atlased Texture " + atlasRows + "*" + atlasRows).write("Shader has _ATLASED define");
                if ("Undo".Click(40).nl())
                {
                    m.DisableKeyword(PainterDataAndConfig.UV_ATLASED);
                }

                var id = p.ImgData;

                if (id.TargetIsRenderTexture())
                {
                    pegi.writeOneTimeHint("Watch out, Render Texture Brush can change neighboring textures on the Atlas.", "rtOnAtlas");
                }

                if (!id.TargetIsTexture2D())
                {
                    pegi.writeWarning("Render Texture painting does not yet support Atlas Editing");
                    pegi.newLine();
                }
            }
            return(changed);
        }
        public bool PaintTexture2D(StrokeVector stroke, float brushAlpha, ImageData image, BrushConfig bc, PlaytimePainter pntr)
        {
            if (pntr.IsAtlased())
            {
                Vector2 uvCoords = stroke.uvFrom;

                Vector2 AtlasedSection = GetAtlasedSection();

                sectorSize = image.width / atlasRows;
                atlasSector.From(AtlasedSection * sectorSize);

                Blit_Functions.brAlpha = brushAlpha;

                Blit_Functions.half = (bc.Size(false)) / 2;
                int ihalf = Mathf.FloorToInt(Blit_Functions.half - 0.5f);

                bool smooth = bc.Type(true) != BrushTypePixel.Inst;

                if (smooth)
                {
                    Blit_Functions._alphaMode = Blit_Functions.circleAlpha;
                }
                else
                {
                    Blit_Functions._alphaMode = Blit_Functions.noAlpha;
                }

                Blit_Functions._blitMode = bc.BlitMode.BlitFunctionTex2D;

                if (smooth)
                {
                    ihalf += 1;
                }

                Blit_Functions.alpha = 1;

                Blit_Functions.r = bc.mask.GetFlag(BrushMask.R);
                Blit_Functions.g = bc.mask.GetFlag(BrushMask.G);
                Blit_Functions.b = bc.mask.GetFlag(BrushMask.B);
                Blit_Functions.a = bc.mask.GetFlag(BrushMask.A);

                Blit_Functions.csrc = bc.colorLinear.ToGamma();

                MyIntVec2 tmp = image.UvToPixelNumber(uvCoords);//new myIntVec2 (pixIndex);

                int fromx = tmp.x - ihalf;

                tmp.y -= ihalf;


                var pixels = image.Pixels;

                for (Blit_Functions.y = -ihalf; Blit_Functions.y < ihalf + 1; Blit_Functions.y++)
                {
                    tmp.x = fromx;

                    for (Blit_Functions.x = -ihalf; Blit_Functions.x < ihalf + 1; Blit_Functions.x++)
                    {
                        if (Blit_Functions._alphaMode())
                        {
                            int sx = tmp.x - atlasSector.x;
                            int sy = tmp.y - atlasSector.y;

                            sx %= sectorSize;
                            if (sx < 0)
                            {
                                sx += sectorSize;
                            }
                            sy %= sectorSize;
                            if (sy < 0)
                            {
                                sy += sectorSize;
                            }

                            Blit_Functions._blitMode(ref pixels[((atlasSector.y + sy)) * image.width + (atlasSector.x + sx)]);
                        }

                        tmp.x += 1;
                    }

                    tmp.y += 1;
                }
                return(true);
            }

            return(false);
        }
 public override void Update_Brush_Parameters_For_Preview_Shader(PlaytimePainter p)
 {
     BlitModeExtensions.SetShaderToggle(!p.IsAtlased(), PainterDataAndConfig.UV_NORMAL, PainterDataAndConfig.UV_ATLASED);
 }
 public bool IsEnabledFor(PlaytimePainter painter, TextureMeta id, Brush cfg) =>
 painter.IsAtlased() && !id.TargetIsRenderTexture();
        public bool PaintTexture2D(StrokeVector stroke, float brushAlpha, TextureMeta image, BrushConfig bc, PlaytimePainter painter)
        {
            if (!painter.IsAtlased())
            {
                return(false);
            }

            var uvCoords = stroke.uvFrom;

            var atlasedSection = GetAtlasedSection();

            sectorSize = image.width / atlasRows;
            atlasSector.From(atlasedSection * sectorSize);

            BlitFunctions.brAlpha = brushAlpha;

            BlitFunctions.half = (bc.Size(false)) / 2;
            var iHalf = Mathf.FloorToInt(BlitFunctions.half - 0.5f);

            var smooth = bc.GetBrushType(true) != BrushTypes.Pixel.Inst;

            if (smooth)
            {
                BlitFunctions.alphaMode = BlitFunctions.CircleAlpha;
            }
            else
            {
                BlitFunctions.alphaMode = BlitFunctions.NoAlpha;
            }

            BlitFunctions.blitMode = bc.GetBlitMode(true).BlitFunctionTex2D(image);

            if (smooth)
            {
                iHalf += 1;
            }

            BlitFunctions.alpha = 1;


            BlitFunctions.Set(bc.mask);

            BlitFunctions.cSrc = bc.Color;

            var tmp = image.UvToPixelNumber(uvCoords);

            var fromX = tmp.x - iHalf;

            tmp.y -= iHalf;

            var pixels = image.Pixels;

            for (BlitFunctions.y = -iHalf; BlitFunctions.y < iHalf + 1; BlitFunctions.y++)
            {
                tmp.x = fromX;

                for (BlitFunctions.x = -iHalf; BlitFunctions.x < iHalf + 1; BlitFunctions.x++)
                {
                    if (BlitFunctions.alphaMode())
                    {
                        var sx = tmp.x - atlasSector.x;
                        var sy = tmp.y - atlasSector.y;

                        sx %= sectorSize;
                        if (sx < 0)
                        {
                            sx += sectorSize;
                        }
                        sy %= sectorSize;
                        if (sy < 0)
                        {
                            sy += sectorSize;
                        }

                        BlitFunctions.blitMode(ref pixels[((atlasSector.y + sy)) * image.width + (atlasSector.x + sx)]);
                    }

                    tmp.x += 1;
                }

                tmp.y += 1;
            }
            return(true);
        }