Unity 画图效果

原创 2015年11月18日 16:19:20
1.PS做一个带透明通道圆作为笔刷2.通过设置颜色把这个笔刷纹理渲染到RenderTexture上
using UnityEngine;
using System.Collections;
using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;

public class Painting : MonoBehaviour {

	private RenderTexture texRender;
	public Material mat;
	public Texture  brushTypeTexture;
	private enum BrushColor
	{
		red,
		green,
		blue,
		pink,
		yellow,
		gray,
		black,
		white,
		count,
	}
	private float brushScale = 0.5f;
	private BrushColor brushColorType = BrushColor.black;
	private Color [] brushColor = new Color[(int)BrushColor.count] {Color.red,Color.green,Color.blue,new Color(255,0,255),Color.yellow,Color.gray,Color.black,Color.white};

	void Start () {
		texRender = new RenderTexture (Screen.width, Screen.height,24,RenderTextureFormat.ARGB32);
		Clear (texRender);
	}

	Vector3 startPosition = Vector3.zero;
	Vector3 endPosition = Vector3.zero;
	void Update () {

		if(Input.GetMouseButton(0))
		{
			OnMouseMove(new Vector3(Input.mousePosition.x,Input.mousePosition.y,0));
		}
		if(Input.GetMouseButtonUp(0))
		{
			OnMouseUp();
		}
	}

	void OnMouseUp()
	{
		startPosition = Vector3.zero;
	}

	void OnMouseMove(Vector3 pos)
	{
		endPosition = pos;
		DrawBrush(texRender,(int)endPosition.x,(int)endPosition.y,brushTypeTexture,brushColor[(int)brushColorType],brushScale);
		
		if(startPosition.Equals(Vector3.zero))
		{
			startPosition = endPosition;
			return;
		}

		float distance = Vector3.Distance(startPosition,endPosition);
		if (distance > 1)
		{
			int d = (int)distance;
			for (int i = 0; i < d; i++)
			{
				float difx = endPosition.x - startPosition.x;
				float dify = endPosition.y - startPosition.y;
				float delta = (float)i / distance;
				DrawBrush(texRender,new Vector2(startPosition.x + (difx * delta), startPosition.y + (dify * delta)),brushTypeTexture,brushColor[(int)brushColorType],brushScale);
			}
		}
		startPosition = endPosition;
	}

	void Clear(RenderTexture destTexture)
	{
		Graphics.SetRenderTarget (destTexture);		
		GL.PushMatrix();
		GL.Clear(true,true,Color.white);
		GL.PopMatrix();
	}

	void DrawBrush(RenderTexture destTexture,Vector2 pos,Texture sourceTexture,Color color,float scale)
	{
		DrawBrush (destTexture,(int)pos.x, (int)pos.y, sourceTexture,color,scale);			
	}

	void DrawBrush(RenderTexture destTexture,int x,int y,Texture sourceTexture,Color color,float scale)
	{
		DrawBrush (destTexture, new Rect (x, y, sourceTexture.width, sourceTexture.height), sourceTexture,color,scale);
	}

	void DrawBrush(RenderTexture destTexture,Rect destRect,Texture sourceTexture,Color color,float scale)
	{	
		float left = destRect.left - destRect.width*scale / 2.0f;
		float right = destRect.left + destRect.width*scale / 2.0f;
		float top = destRect.top - destRect.height*scale / 2.0f;
		float bottom = destRect.top + destRect.height*scale / 2.0f;

		Graphics.SetRenderTarget (destTexture);

		GL.PushMatrix();
		GL.LoadOrtho();

		mat.SetTexture("_MainTex",brushTypeTexture);
		mat.SetColor ("_Color",color);
		mat.SetPass(0);	

		GL.Begin(GL.QUADS);

		GL.TexCoord2(0.0f, 0.0f); GL.Vertex3(left/Screen.width, top/Screen.height, 0);
		GL.TexCoord2(1.0f, 0.0f); GL.Vertex3(right/Screen.width, top/Screen.height, 0);    
		GL.TexCoord2(1.0f, 1.0f); GL.Vertex3(right/Screen.width, bottom/Screen.height, 0);
		GL.TexCoord2(0.0f, 1.0f); GL.Vertex3(left/Screen.width, bottom/Screen.height, 0);

		GL.End();	
		GL.PopMatrix();
	}

	bool bshow = true;
	void OnGUI() {	

		if(bshow)
		{
			GUI.DrawTexture(new Rect(0,0,Screen.width,Screen.height),texRender,ScaleMode.StretchToFill);
		}

		if(GUI.Button(new Rect(0,150,100,30),"clear"))
		{
			Clear (texRender);
		}

		if(GUI.Button(new Rect(100,150,100,30),"hide"))
		{
			bshow = !bshow;
		}

		int width = Screen.width / (int)BrushColor.count;

		for(int i=0; i<(int)BrushColor.count; i++)
		{
			if(GUI.Button(new Rect(i*width,0,width,30),Enum.GetName(typeof(BrushColor),i)))
			{
				brushColorType = (BrushColor)i;
			}
		}

		GUI.Label (new Rect (0, 200, 300, 30), "brushScale : " + brushScale.ToString("F2"));
		brushScale = (int)GUI.HorizontalSlider (new Rect (120, 205, 200, 30), brushScale*10.0f, 1, 50) / 10.0f;
		if(brushScale < 0.1f)
			brushScale = 0.1f;
	}
}
Shader "Painting"
{
	Properties
	{
		_MainTex ("MainTex (RGB) Trans (A)", 2D) = "white" {}
		_Color ("Color", Color) = (1,1,1,1)
	}

	SubShader
	{
		Tags
		{ 
			"Queue"="Transparent" 
			"IgnoreProjector"="True" 
			"RenderType"="Transparent" 
			"PreviewType"="Plane"
			"CanUseSpriteAtlas"="True"
		}

		Cull Off
		Lighting Off
		ZWrite Off
		Fog { Mode Off }
		Blend One OneMinusSrcAlpha

		Pass
		{
			CGPROGRAM
			#pragma vertex vert
			#pragma fragment frag
			#include "UnityCG.cginc"

			struct v2f
			{
				float4 vertex : SV_POSITION;
				half2 texcoord : TEXCOORD0;
			};

			fixed4 _Color;

			v2f vert(appdata_base IN)
			{
				v2f OUT;
				OUT.vertex = mul(UNITY_MATRIX_MVP, IN.vertex);
				OUT.texcoord = IN.texcoord;
				return OUT;
			}

			sampler2D _MainTex;

			fixed4 frag(v2f IN) : SV_Target
			{
				float4 col = _Color * tex2D(_MainTex, IN.texcoord);
				col.rgb *= col.a;
				return col;
			}
			ENDCG
		}
	}
}




                    

相关文章推荐

浅谈下Unity实现像素画画的功能

我现在做的一个项目是像素游戏,那么就包含了像素人物角色的换装 不同的部位实现换装的步骤很麻烦 因此相当在Unity中画画这种,查阅了大量的资料,发现这种方法是行不通的 改变单个图...

Unity3d实现画笔

本文介绍如何在unity3d中实现画笔的功能 首先画笔需要一个材质 Material,之前在网上找了很多画笔,但是都没说材质怎么生成,导致画不出线来。 创建材质: Material rectMa...

unity--鼠标画线、画图实现

http://blog.csdn.net/chenggong2dm/article/details/24488469 首先让我们来看一下效果: 实现方法: 1...

将Gif图转换成Unity Texture2D

最近项目里要从网上拉取玩家的头像,可玩家的头像可以用gif图但unity不支持gif图,这样就会出现拉取失败会显示问号的图,那是Unity内转换Texture2D图失败是给的默认图。于是在网上查找解决...
  • hyf2713
  • hyf2713
  • 2016年09月06日 19:37
  • 3254

Unity&Shader基础篇-绘图2D图形

一、闲言碎语唠叨两句 原文转载请注明出处点击打开链接 有了前面的几章的基础,接下来我们就可以编写一些案例来训练和强化Shader编程。本章和接下来的几章都会是在屏幕上绘制2D的图像,因此需要建立一个绘...

unity--鼠标画线、画图实现

在untiy有时候需要用到画线,比如说在做签名或者画图的时候,主要用的组件就是LineRenderer实现代码 using UnityEngine; using System.Collections...

在unity里绘图板 制作一个画板的几种思路和原理

写这个的原因是绘画方面的内容在unity里还是很弱的,没有opengl 或者以前flash那样直观又容易操作, 当然unity里其他很好用,当如如果使用unity里GL底层来控制,虽然可以但是缺乏与其...

Delphi7高级应用开发随书源码

  • 2003年04月30日 00:00
  • 676KB
  • 下载

Unity 画图效果

1.PS做一个带透明通道圆作为笔刷2.通过设置颜色把这个笔刷纹理渲染到RenderTexture上 using UnityEngine; using System.Collections; using...

【Unity】开发日志1——画笔 The Printer

我每天都想去学习一些东西,但是每次做一半就坚持不下去,一方面的原因是:我的的确确遇到很多困难;另一方面的确是无从下手,不知道如何去学习; 我现在就是这样不断的摸索摸索,所以就写下我摸索的过程。一方面希...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Unity 画图效果
举报原因:
原因补充:

(最多只允许输入30个字)