unity3d之新手引导遮黑

来源:互联网 发布:先锋网络电视 编辑:程序博客网 时间:2024/06/10 00:08

之前做新手引导是4张黑图动态拼接,最终形成一个没被遮住的方块,这样做的缺点是黑图之间有接缝。这次选择写个shader来扣出一片空白。。。

不废话,上shader

Shader "Custom/newplayer_cover"{Properties{_MainTex ("Texture", 2D) = "white" {}_CoverColor("_CoverColor", Color)=(0,0,0,1)_CoverAlpha("_CoverAlpha", range(0,1))=0minX("minX",range(0,1))=0maxX("maxX",range(0,1))=0minY("minY",range(0,1))=0maxY("maxY",range(0,1))=0}SubShader{Tags { "RenderType"="Transparent" }LOD 100Blend SrcAlpha OneMinusSrcAlpha//Blend DstColor OneCull OffLighting OffZWrite OffPass{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 _CoverColor;fixed _CoverAlpha;fixed minX;fixed maxX;fixed minY;fixed maxY;v2f vert (appdata v){v2f o;o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);o.uv = TRANSFORM_TEX(v.uv, _MainTex);return o;}fixed4 frag (v2f i) : SV_Target{// sample the texturefixed4 texCol = tex2D(_MainTex, i.uv);fixed uvX=i.uv.x;fixed uvY=i.uv.y;fixed compareX0=1-step(uvX,minX);fixed compareX1=1-step(maxX,uvX);fixed compareY0=1-step(uvY,minY);fixed compareY1=1-step(maxY,uvY);fixed cover=(1-compareX0*compareX1*compareY0*compareY1);fixed4 col;col.rgb=texCol.rgb*(1-_CoverAlpha*cover)+_CoverColor.rgb*_CoverAlpha*cover;//col.a=1;return col;}ENDCG}}}

摄像机渲染脚本:

using UnityEngine;using System.Collections;//[RequireComponent(typeof(Camera))]public class NewplayerCover : MonoBehaviour {public Material mat;[Range(0,1)]public float minX;[Range(0,1)]public float maxX;[Range(0,1)]public float minY;[Range(0,1)]public float maxY;[Header("多次渲染")]public bool mulBlit=false;[Range(1,20),Header("图像分块除数")]public int divideNum=1;// Use this for initializationvoid Start () {}// Update is called once per framevoid Update () {}void OnRenderImage (RenderTexture source, RenderTexture destination){mat.SetFloat("minX",minX);mat.SetFloat("maxX",maxX);mat.SetFloat("minY",minY);mat.SetFloat("maxY",maxY);if(mulBlit){int rtW = source.width/divideNum;int rtH = source.height/divideNum;RenderTexture buffer = RenderTexture.GetTemporary(source.width, source.height, 0);Graphics.Blit(source,buffer,mat);Graphics.Blit(buffer,destination);RenderTexture.ReleaseTemporary(buffer);}else{Graphics.Blit(source,destination,mat);}}}

新建个材质,指定上边的shader,脚本加在camera上,最终效果:

调节红框里的4个uv值可以在game视窗里看见扣出来的方块。

技术与灵感,来源于网络共享于网络。


0 0
原创粉丝点击