Unity Shaders and Effects Cookbook中的Photoshop levels effect

这个shader写完了之后就是这样的。

Shader "Custom/2_6Photoshop levels effect" {
	Properties {
		_MainTex ("Base (RGB)", 2D) = "white" {}
		
		_inBlack("Input Black",Range(0,255)) = 0
		_inGamma("Input Gamma", Range(0, 2)) = 1.61
		_inWhite("Input White",Range(0,255)) = 255
		
		_outBlack("Output Black",Range(0,255)) = 0
		_outWhite("Output White",Range(0,255)) = 255
		
	}
	SubShader {
		Tags { "RenderType"="Opaque" }
		LOD 200
		
		CGPROGRAM
		// Physically based Standard lighting model, and enable shadows on all light types
		#pragma surface surf Standard fullforwardshadows

		// Use shader model 3.0 target, to get nicer looking lighting
		#pragma target 3.0

		sampler2D _MainTex;
		float _inBlack;
		float _inGamma;
		float _inWhite;
		float _outWhite;
		float _outBlack;

		struct Input {
			float2 uv_MainTex;
		};

		inline float GetPixelLevel(float pixelColor)
		{
			float outRPixel;
			outRPixel = (pixelColor * 255.0);
			outRPixel = max(0, outRPixel - _inBlack);
			outRPixel = saturate(pow(outRPixel / (_inWhite - _inBlack), _inGamma));
			outRPixel = (outRPixel * (_outWhite - _outBlack) + _outBlack) / 255.0;
			return outRPixel;
		}

		void surf (Input IN, inout SurfaceOutputStandard o) {
			
			fixed4 c = tex2D(_MainTex, IN.uv_MainTex);
			//Create a Variable to store a pixel channel from our MainTex texture
			
			
			c.r = GetPixelLevel(c.r);
			c.g = GetPixelLevel(c.g);
			c.b = GetPixelLevel(c.b);

			o.Albedo = c.rgb;
		}
		ENDCG
	}
	FallBack "Diffuse"
}


这个shader写完了感觉懵逼懵逼的,好像都懂了,实则一点都不明白,好好地看了一下,发现其实这个shader起到的作用就是ps里的色阶。

熟悉ps的人肯定都知道,ps里的色阶其实就是有这四个参数_inBlack,_inWhite,_outWhite,_outBlack,还有一个Gamma。

下面是调整ps里色阶这几个参数得到的效果:


(没有调整的图片)



这个编辑器真的太难用了……

上图分别是调整了_inBlack,_inWhite,Gamma,_outWhite,_outBlack值后的效果,最后在unity中做出来的效果也是这样的,这里就不再截图了。

怎么理解这个色阶呢,从一个调色师的角度来说,一个比较直观的理解就是,inWhite和iBlack是一个输入的阈值,在inWhite和inBalck之间的颜色正常输入,大于等于inWhite的灰度被规定为白色,小于等于inBlack的就是黑色。Gamma就是一个控制亮度的,gamma越小(越靠近直方图黑色方向)图就越黑,gamma越大(越靠近直方图白色方向),图就越亮。输出色阶的话就是输出是什么样的,outBlack就是黑色输出的时候是什么颜色,0——黑色就是黑色,125——黑色变成灰色,255——黑色也映射成白色;outWhite同理。

说到这里估计大家都对色阶这个东西直观上的作用有所了解了,从公式上来也不难,无非就是一个线性向量到另一个线性向量映射的过程。

inline float GetPixelLevel(float pixelColor)
		{
			float outRPixel;
			outRPixel = (pixelColor * 255.0);
			outRPixel = max(0, outRPixel - _inBlack);
			outRPixel = saturate(pow(outRPixel / (_inWhite - _inBlack), _inGamma));
			outRPixel = (outRPixel * (_outWhite - _outBlack) + _outBlack) / 255.0;
			return outRPixel;
		}

在这里我们先忽略gamma,其实gamma就是一个指数,指数越大,最后得到的值越大,就越亮;指数越小,得到的结果值越小,图越黑。就暂时这么简单粗暴地理解吧,如果有人能看到这篇文章并且对这个解释有什么意见或建议的话欢迎告诉我。但我估计没有。下面就是纯色阶处理的东西了,从网上随便截了张图。


怎么样,过程是不是惊人的相似。哎,其实就是这样的。以上。



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值