进阶版笔刷的原理还是RT叠加,不过用的是GL的方法,用起来更灵活,可以生成一张小号的图叠加到大图上。
Shader "MyShader/SS_18"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
_Color ("Color", Color) =(0,0,0,0)
_Row ("Row", float) = 0
_Column ("Column",float) = 0
_Size("Size",float)=4//图片被拆分为4*4
}
SubShader
{
Blend SrcAlpha OneMinusSrcAlpha
Tags { "RenderType"="Opaque" }
LOD 100
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};
struct v2f
{
float2 uv : TEXCOORD0;
float4 vertex : SV_POSITION;
};
sampler2D _MainTex;
float4 _MainTex_ST;
fixed4 _Color;
fixed _Row;
fixed _Column;
fixed _Size;
v2f vert (appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = TRANSFORM_TEX(v.uv, _MainTex)/_Size;
o.uv=fixed2(o.uv.x+_Row/_Size,o.uv.y+_Column/_Size);
return o;
}
fixed4 frag (v2f i) : SV_Target
{
fixed4 col = tex2D(_MainTex, i.uv);
return col*_Color;
}
ENDCG
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SS_18 : MonoBehaviour
{
public RenderTexture destTexture;
Material mat;
public Texture2D sourceTexture;
public Texture2D orgTexture;
public Vector2 pos;//pos为0-7(左下角(0,0),右上角(7,7))
public float cellSize =0.125f;//cellSize=1/cellCount;
public float cellCount= 8;
public float scale = 1.5f;
public Color color=Color.red;
float edge;
public Material Mat
{
get
{
if (mat == null)
{
mat = new Material(Shader.Find("MyShader/SS_18"));
}
return mat;
}
set
{
mat = value;
}
}
void Start()
{
cellSize = 1 / cellCount;
edge = (scale - 1) * cellSize;
}
[ContextMenu("Draw")]
void Draw()
{
DrawByPos(pos);
}
//根据UV进行绘制
public void DrawByUV(Vector2 _pos)
{
_pos.x -= 0.5f;
_pos.y -= 0.5f;
Graphics.SetRenderTarget(destTexture);
var left = _pos.x * cellSize - edge;
var right = _pos.x * cellSize + cellSize + edge;
var top = _pos.y * cellSize + cellSize + edge;
var bottom = _pos.y * cellSize - edge;// - cellSize * 0.5f;
Mat.SetTexture("_MainTex", sourceTexture);//笔刷的图片
Mat.SetFloat("_Row", Random.Range(0, 4));
Mat.SetFloat("_Column", Random.Range(0, 4));
Mat.SetColor("_Color", color);
Mat.SetPass(0);
GL.PushMatrix();
GL.LoadOrtho();
GL.Begin(GL.QUADS);
GL.TexCoord2(1, 1);//按顺时针绘制,起点随意
GL.Vertex3(left, bottom, 0);
GL.TexCoord2(1, 0);
GL.Vertex3(left, top, 0);
GL.TexCoord2(0, 0);
GL.Vertex3(right, top, 0);
GL.TexCoord2(0, 1);
GL.Vertex3(right, bottom, 0);
GL.End();
GL.PopMatrix();
}
//根据坐标进行绘制
public void DrawByPos(Vector2 _pos)
{
DrawByUV(new Vector2(cellCount - pos.x-0.5f, cellCount - pos.y-0.5f));//因为转入的图的UV坐标问题,需要翻转
}
[ContextMenu("Reset")]
public void ResetRT()
{
Graphics.Blit(orgTexture,destTexture);
}
private void Update()
{
if (Input.GetMouseButtonDown(0))
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
var _hit =new RaycastHit();
if (Physics.Raycast(ray, out _hit, 100f))
{
DrawByUV(_hit.textureCoord* cellCount);//_hit.textureCoord获取UV值,需要翻转才能与世界坐标对应
Debug.Log(_hit.textureCoord);
}
}
}
}
返回目录:https://blog.csdn.net/yzy1987523/article/details/106676451