Paint in to Texture

In this tutorial you will solve following problem

Allow kids to use brush which paint predefined texture, something like a scratch book. You can start with blank white or another color canvas or with any texture like glitter

At the end of this page you will find a link to the package with all required scripts and scene from this tutorial

1. Create new BGPageConfig

We need to create new page config which will hold the predefined texture so let's create a new class with following content

using PaintCraft.Canvas.Configs;
using UnityEngine;
[CreateAssetMenu(menuName = "PaintCraft/BGPageConfig")]
public class BGPageConfig : PageConfig {
public Texture2D BGTexture;
public override Vector2 GetSize()
{
return new Vector2(BGTexture.width, BGTexture.height);
}
}

As you see it's just hold additional texture which will be used later.

So let's create a new page config with BGTexture

and then just assign your texture to the BGTexture property

2. Setup Brush

As you seen in another demos, there is already tool which able to use texture to draw it's a advanced eraser or pattern brush.

Let's clone advanced eraser brush in the Paintcraft->BrushPack->Advanced->Eraser folder

If you will look inside this brush (menu: Window->Paintcraft->BrushSetup) you will see that it use filter BindTileTexToStartImage

What this filter does is assign start texture from page config to the material used by the tool. And what we need to do is just assign different texture (our BGTexture) to that material.

Create a new script BindTileTexToStartImage.cs with following content

using System.Collections.Generic;
using NodeInspector;
using PaintCraft.Tools;
using UnityEngine;
[NodeMenuItem("Material/BindTileTexToBG")]
public class BindTileTexToBG : FilterWithNextNode {
#region implemented abstract members of FilterWithNextNode
public override bool FilterBody(BrushContext brushLineContext)
{
Texture2D texture = (brushLineContext.Canvas.PageConfig as BGPageConfig).BGTexture;
if (brushLineContext.IsFirstPointInLine && texture != null){
SetTextureParameters(brushLineContext.BasePoints.First,
texture);
SetTextureParameters(brushLineContext.Points.First,
texture);
}
return true;
}
#endregion
void SetTextureParameters(LinkedListNode<Point> nodePoint, Texture2D texture)
{
if (nodePoint == null)
{
return;
}
nodePoint.Value.Material.SetTexture("_TileTex", texture);
nodePoint.Value.Material.SetTextureOffset("_TileTex", Vector2.zero);
nodePoint.Value.Material.SetTextureScale ("_TileTex", Vector2.one);
}
}

Now select your brush which you cloned before. rename it to the BGBrush and select BrushSetup and replace filter BindTileTexToStartImage _to filter _BindTileTexToBG

you will get something like this (see that I just change one highlighted filter)

3. Setup scene

Let's just copy OnePlayerBasic scene and change tools and page configs.

  1. Change Buttons that they will works with proper brush 1. Brush Button must point to the BGBrush 2. EraserButton must point to the advanced eraser if you want to use start image (like glitter) or simple eraser if you want to use default color

  2. In PaintcraftCanvas select the page which you created at step 1

  3. In line config select the same BGBrush.

That's it. now if you will start painting you will have similar behaviour as on the first screenshot of this tutorial.

4. Package

If you will have any issues just download the package which contains this scene and all required scripts and configs

https://www.dropbox.com/s/ans9ci92990oqj6/BGTexture.unitypackage?dl=0