zoukankan      html  css  js  c++  java
  • unity3d ppsspp模拟器中的post processing shader在unity中使用

    这个位置可以看到ppsspp的特殊处理

    文件位置

    来看看这些特效

    用来测试的未加特效图片

    ppsspp:

    传说系列一生爱---英杰传说

    最后的战士


    aacolor

    是关于饱和度,亮度,对比度,色调的调节,
    ppsspp中的默认参数为饱和度加强1.2倍,对比度增强1.25倍,在unity中我们可以设为外部变量自己调节

    关键代码:

    		float4 frag(v2f i) :COLOR
    		{
    			float size = 1 / _Size;
    
    			float3 c10 = tex2D(_MainTex, i.uv_MainTex + float2(0, -1)*size).rgb;
    			float3 c01 = tex2D(_MainTex, i.uv_MainTex + float2(-1, 0)*size).rgb;
    			float3 c11 = tex2D(_MainTex, i.uv_MainTex).rgb;
    			float3 c21 = tex2D(_MainTex, i.uv_MainTex + float2(1, 0)*size).rgb;
    			float3 c12 = tex2D(_MainTex, i.uv_MainTex + float2(0, 1)*size).rgb;
    
    			float3 dt = float3(1.0, 1.0, 1.0);
    			float k1 = dot(abs(c01 - c21), dt);
    			float k2 = dot(abs(c10 - c12), dt);
    
    			float3 color = (k1*(c10 + c12) + k2*(c01 + c21) + 0.001*c11) / (2.0*(k1 + k2) + 0.001);
    
    			float x = sqrt(dot(color, color));
    
    			color.r = pow(color.r + 0.001, _A);
    			color.g = pow(color.g + 0.001, _A);
    			color.b = pow(color.b + 0.001, _A);
    
    			//饱和度,亮度,对比度,色调映射
    			return float4(contrast4(x)*normalize(color*_C_ch)*_B,1);
    
    		}




    ppsspp中实测


    unity中实测



    bloom

    辉光效果
    ppsspp中的辉光在unity种效果并不好,模糊处理不够,亮度过量,采样方式为grid采样
    之前写过一篇详细博文关于HDR(与bloom相似)

    关键代码:

    	float4 frag(v2f i) :COLOR
    			{
    				float size = 1 / _Size;
    				float2 uv = i.uv_MainTex;
    
    
    				float3 color = tex2D(_MainTex, i.uv_MainTex);
    				float4 sum = 0;
    				float3 bloom;
    
    
    				for (int i = -3; i < 3; i++)
    				{
    					sum += tex2D(_MainTex, uv + float2(-1, i)*size) * _Amount;
    					sum += tex2D(_MainTex, uv + float2(0, i)*size) * _Amount;
    					sum += tex2D(_MainTex, uv + float2(1, i)*size) * _Amount;
    				}
    
    				if (color.r < 0.3 && color.g < 0.3 && color.b < 0.3)
    				{
    					bloom = sum.rgb*sum.rgb*0.012 + color;
    				}
    				else
    				{
    					if (color.r < 0.5 && color.g < 0.5 && color.b < 0.5)
    					{
    						bloom = sum.xyz*sum.xyz*0.009 + color;
    					}
    					else
    					{
    						bloom = sum.xyz*sum.xyz*0.0075 + color;
    					}
    				}
    
    				bloom = mix(color, bloom, _Power);
    
    				return float4(bloom, 1);
    			}




    ppsspp中实测


    unity中实测



    cartoon

    卡通效果的post processing
    颜色总共分为四层,把颜色灰度每层段数值再加上减到最少的小数部分,产生一些过渡效果
    再用得出的灰度乘上原色值,保留了原来的颜色只是明暗分为四层,层之间有过度
    通过边缘检测描边,着色道理与之前写过的一篇详细博文像似,但不完全相同,

    关键代码:

    	float4 frag(v2f i) :COLOR
    			{
    				float size = 1 / _Size;
    
    				float3 c00 = tex2D(_MainTex, i.uv_MainTex + float2(-1, -1)*size).rgb;
    				float3 c10 = tex2D(_MainTex, i.uv_MainTex + float2(0, -1)*size).rgb;
    				float3 c20 = tex2D(_MainTex, i.uv_MainTex + float2(1, -1)*size).rgb;
    				float3 c01 = tex2D(_MainTex, i.uv_MainTex + float2(-1, 0)*size).rgb;
    				float3 c11 = tex2D(_MainTex, i.uv_MainTex).rgb;
    				float3 c21 = tex2D(_MainTex, i.uv_MainTex + float2(1, 0)*size).rgb;
    				float3 c02 = tex2D(_MainTex, i.uv_MainTex + float2(-1, 1)*size).rgb;
    				float3 c12 = tex2D(_MainTex, i.uv_MainTex + float2(0, 1)*size).rgb;
    				float3 c22 = tex2D(_MainTex, i.uv_MainTex + float2(1, 1)*size).rgb;
    				float3 dt = float3(1.0, 1.0, 1.0);
    
    				float d1 = dot(abs(c00 - c22), dt);
    				float d2 = dot(abs(c20 - c02), dt);
    				float hl = dot(abs(c01 - c21), dt);
    				float vl = dot(abs(c10 - c12), dt);
    				float d = _Bb*(d1 + d2 + hl + vl) / (dot(c11, dt) + 0.15);
    	
    				float lc = 4.0*length(c11);
    			
    				float f = frac(lc); 
    				f *= f;
    				lc = 0.25*(floor(lc) + f*f) + 0.05;
    				//颜色总共分为四层,把颜色灰度每层段数值再加上减到最少的小数部分,产生一些过渡效果
    			
    				c11 = 4.0*normalize(c11);
    				float3 frct = frac(c11); 
    				frct *= frct;
    				c11 = floor(c11) + 0.05*dt + frct*frct;
    				return float4(0.25*lc*(1.1 - d*sqrt(d))*c11,1);
    				//再用得出的灰度乘上原色值,保留了原来的颜色只是明暗分为四层,层之间有过度
    				//通过边缘检测描边,着色道理与之前的一篇文章像似,但不完全相同,
    
    			}




    ppsspp中实测


    unity中实测



    CRT

    CRT是模拟以前大脑袋电脑的阴极射线管(Cathode Ray Tube)的显示器的特殊效果,频闪之类的特效,用过大脑袋的都懂。

    关键代码:

    		float4 frag(v2f i) :COLOR
    			{
    
    				// scanlines
    				float vPos = float((i.uv_MainTex.y + _Time.z * 0.5) * 272.0);
    				float j = 2;
    				float line_intensity = modf(float(vPos), j);
    
    				// color shift
    				float off = line_intensity *0.00001;
    				float2 shift = float2(off, 0);
    
    				// shift R and G channels to simulate NTSC color bleed
    				float2 colorShift = float2(0.001, 0);
    				float r = tex2D(_MainTex, i.uv_MainTex + colorShift + shift).x;
    				float g = tex2D(_MainTex, i.uv_MainTex - colorShift + shift).y;
    				float b = tex2D(_MainTex, i.uv_MainTex).z;
    
    				float4 c = float4(r, g * 0.99, b, 1) * clamp(line_intensity, 0.85, 1);
    
    				float rollbar = sin((i.uv_MainTex.y + _Time.z) * 30);
    
    				return c + (rollbar * 0.02);
    			}




    ppsspp中实测

    unity中实测



    fxaa

    ppsspp的fxaa抗锯齿效果在unity上异常的好,耗费很少,
    四个采样点来进行边缘检测(边缘检测的很粗糙),边缘处两个点之间模糊

    关键代码:

    	float4 frag(v2f i) :COLOR
    			{
    
    				float	u_texelDelta = 1 / _Size;
    
    
    
    				float FXAA_SPAN_MAX = 8.0;
    				float FXAA_REDUCE_MUL = 1.0 / 8.0;
    				float FXAA_REDUCE_MIN = (1.0 / 128.0);
    
    				float3 rgbNW = tex2D(_MainTex, i.uv_MainTex + (float2(-1.0, -1.0) * u_texelDelta)).xyz;
    				float3 rgbNE = tex2D(_MainTex, i.uv_MainTex + (float2(+1.0, -1.0) * u_texelDelta)).xyz;
    				float3 rgbSW = tex2D(_MainTex, i.uv_MainTex + (float2(-1.0, +1.0) * u_texelDelta)).xyz;
    				float3 rgbSE = tex2D(_MainTex, i.uv_MainTex + (float2(+1.0, +1.0) * u_texelDelta)).xyz;
    				float3 rgbM = tex2D(_MainTex, i.uv_MainTex).xyz;
    
    				float3 luma = float3(0.299, 0.587, 0.114);
    				float lumaNW = dot(rgbNW, luma);
    				float lumaNE = dot(rgbNE, luma);
    				float lumaSW = dot(rgbSW, luma);
    				float lumaSE = dot(rgbSE, luma);
    				float lumaM = dot(rgbM, luma);
    
    				float lumaMin = min(lumaM, min(min(lumaNW, lumaNE), min(lumaSW, lumaSE)));
    				float lumaMax = max(lumaM, max(max(lumaNW, lumaNE), max(lumaSW, lumaSE)));
    
    				float2 dir;
    				dir.x = -((lumaNW + lumaNE) - (lumaSW + lumaSE));
    				dir.y = ((lumaNW + lumaSW) - (lumaNE + lumaSE));
    
    				float dirReduce = max((lumaNW + lumaNE + lumaSW + lumaSE) * (0.25 * FXAA_REDUCE_MUL), FXAA_REDUCE_MIN);
    				
    				float rcpDirMin = 1.0 / (min(abs(dir.x), abs(dir.y)) + dirReduce);
    			
    				dir = min(float2(FXAA_SPAN_MAX, FXAA_SPAN_MAX),
    					max(float2(-FXAA_SPAN_MAX, -FXAA_SPAN_MAX), dir * rcpDirMin)) * u_texelDelta;
    		
    				float3 rgbA = (1.0 / 2.0) * (
    					tex2D(_MainTex, i.uv_MainTex + dir * (1.0 / 3.0 - 0.5)).xyz +
    					tex2D(_MainTex, i.uv_MainTex + dir * (2.0 / 3.0 - 0.5)).xyz);
    				float3 rgbB = rgbA * (1.0 / 2.0) + (1.0 / 4.0) * (
    					tex2D(_MainTex, i.uv_MainTex + dir * (0.0 / 3.0 - 0.5)).xyz +
    					tex2D(_MainTex, i.uv_MainTex + dir * (3.0 / 3.0 - 0.5)).xyz);
    
    				float lumaB = dot(rgbB, luma);
    
    				if ((lumaB < lumaMin) || (lumaB > lumaMax)){
    					return float4( rgbA,1);
    				}
    				else {
    					return float4(rgbB, 1);
    				}
    				//整体上是一个边缘检测,在边缘处进行采样模糊
    	
    			}




    ppsspp中实测


    unity中实测



    grayscale

    灰度
    白光的亮度用Y表示,它和红绿蓝三色光的关系:
    Y = 0.299R+0.587G+0.114B    NTSC美制电视制式亮度公式
    Y = 0.222R+0.707G+0.071B    PAL(相位逐行交变)电视制式
    ppsspp使用的是NTSC美制,Unity中的Luminance函数使用的是PAL制式

    关键代码:

    float4 frag(v2f i) :COLOR
    			{
    
    				float3 rgb = tex2D(_MainTex, i.uv_MainTex).rgb;
    				float luma = dot(rgb, float3(0.299, 0.587, 0.114));
    
    				return luma;
    
    			}

    ppsspp中实测


    unity中实测




    inversecolors

    反色

    关键代码:

    		float4 frag(v2f i) :COLOR
    			{
    
    				float3 rgb = tex2D(_MainTex, i.uv_MainTex).rgb;
    				float luma = dot(rgb, float3(0.299, 0.587, 0.114));
    			//	luma = Luminance(rgb);
    				float3 gray = float3(luma, luma, luma) - 0.5;
    				rgb -= float3(0.5, 0.5, 0.5);
    
    				return float4(mix(rgb, gray, 2.0) + 0.5, 1);
    
    			}




    ppsspp中实测

    unity中实测



    natural

    使颜色变得自然
    把颜色从RGB转到YIQ色彩空间在进行变换

    关键代码:

    	float4 frag(v2f i) :COLOR
    			{
    				float3 val00 = float3(1.2, 1.2, 1.2);
    				float3x3 RGBtoYIQ = float3x3(0.299, 0.596, 0.212,
    				0.587, -0.275, -0.523,
    				0.114, -0.321, 0.311);
    
    				float3x3 YIQtoRGB = float3x3(1.0, 1.0, 1.0,
    					0.95568806036115671171, -0.27158179694405859326, -1.1081773266826619523,
    					0.61985809445637075388, -0.64687381613840131330, 1.7050645599191817149);
    
    				float4 c = tex2D(_MainTex, i.uv_MainTex);
    				float3 c1 =mul( RGBtoYIQ,c.rgb);
    
    				c1 = float3(pow(c1.x, val00.x), c1.yz*val00.yz);
    				//转换到YIQ色彩空间再加强GB颜色1.2倍
    				return float4(mul(YIQtoRGB,c1), 1);
    			}




    ppsspp中实测

    unity中实测



    scanlines

    屏幕上的线的效果

    关键代码:

    	float4 frag(v2f i) :COLOR
    			{
    
    				float pos0 = ((i.uv_MainTex.y + 1.0) * 170.0*_Amount);
    				float pos1 = cos((frac(pos0) - 0.5)*3.1415926*_Inten)*1.5;
    				float4 rgb = tex2D(_MainTex, i.uv_MainTex);
    
    				// slight contrast curve
    				float4 color = rgb*0.5 + 0.5*rgb*rgb*1.2;
    
    				// color tint
    				color *= float4(0.9, 1.0, 0.7, 0.0);
    
    				// vignette
    				color *= 1.1 - 0.6 * (dot(i.uv_MainTex - 0.5, i.uv_MainTex - 0.5) * 2.0);
    
    				return mix(float4(0, 0, 0, 0), color, pos1);
    			}




    ppsspp中实测

    unity中实测



    sharpen

    锐化
    取上下两个采样点,采样点之间的颜色差越大(边缘,色差大等处),sharp越明显

    关键代码:

    		float4 frag(v2f i) :COLOR
    			{
    				float4 c = tex2D(_MainTex, i.uv_MainTex);
    				c -= tex2D(_MainTex, i.uv_MainTex + _Size)*7.0*_Inten;
    				c += tex2D(_MainTex, i.uv_MainTex - _Size)*7.0*_Inten;
    				//采样点之间的颜色差越大(边缘,色差大等处),sharp越明显
    				return c;
    			}




    ppsspp中实测

    unity中实测



    vignette

    晕影效果

    关键代码:

    			float4 frag(v2f i) :COLOR
    			{
    
    				float vignette = 1.1 - 0.6 * (dot(i.uv_MainTex - 0.5, i.uv_MainTex - 0.5) * 2.0);
    				float3 rgb = tex2D(_MainTex, i.uv_MainTex).rgb;
    				return float4(vignette * rgb, 1);
    
    			}




    ppsspp中实测

    unity中实测



    4xhqglsl

    平滑效果

    ppsspp中实测

    upscale_spline36

    缩放滤镜
    基于样条线的缩放(Spline based resizers)分别有spline16 spline36 spline64这三个缩放滤镜基于样条插值算法。样条差值算法在放大时尽可能的锐化图像,减少失真
    该算法原作者为Panorama tools的开发者
    详细算法的介绍请看forum.doom9.org/showthread.php?t=147117

    ppsspp中实测

    用一张不同的图做测试,在比例2X的情况下全屏拉伸,图像会变模糊,使用缩放滤镜进行放大,效果就像4X一样清楚

    未开启

    开启

     



    全部代码已上传至GitHub

                                        --------by wolf96


  • 相关阅读:
    重构第四天 : 用多态替换条件语句(if else & switch)
    MSBuild 教程(2)
    为什么Nhibernate中属性和方法必须Virtual的
    重构第三天:提升方法&下移方法
    重构第二天:移动方法
    重构第一天:封装集合
    MSbuild 教程
    工程经验总结之吹水"管理大境界"
    呕心沥血之作:完美解决Informix的中文乱码问题
    万事开头难——我的蛮荒时代
  • 原文地址:https://www.cnblogs.com/zhanlang96/p/4691553.html
Copyright © 2011-2022 走看看