色阶

作者:追风剑情 发布于:2016-1-3 16:08 分类:Shader

色阶Shader

Shader "Custom/LevelsShader" {
	Properties {
		_MainTex ("Base (RGB)", 2D) = "white" {}
		_inBlack ("Input Black", Range(0, 255)) = 0
		_inGamma ("Input Gamma", Range(0, 255)) = 1.61
		_inWhite ("Input White", Range(0, 255)) = 255
		_outWhite ("Output White", Range(0, 255)) = 255
		_outBlack ("Output Black", Range(0, 255)) = 0
	}
	SubShader {
		Tags { "RenderType"="Opaque" }
		LOD 200
		
		CGPROGRAM
		#pragma surface surf Lambert

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

		struct Input {
			float2 uv_MainTex;
		};
		
		//自定义函数 (色阶计算公式)
		float GetPixelLevel(float pixelColor)
		{
			float pixelResult;
			//把值的范围从0.0~1.0扩大至0.0~255.0
			pixelResult = (pixelColor * 255.0);
			//减去_inBlack值,使像素变得更暗。
			pixelResult = max(0, pixelResult - _inBlack);
			//计算新的白色值,即调节像素亮度。
			pixelResult = saturate(pow(pixelResult / (_inWhite - _inBlack), _inGamma));
			//对最小像素值以及最大像素值有一个最终的全局控制,除以255使其重新恢复到0.0~1.0的范围。
			pixelResult = (pixelResult * (_outWhite - _outBlack) + _outBlack)/255.0;
			return pixelResult;
		}
		
		void surf (Input IN, inout SurfaceOutput o) {
			half4 c = tex2D (_MainTex, IN.uv_MainTex);
			o.Albedo = GetPixelLevel(c);
			o.Alpha = c.a;
		}
		ENDCG
	} 
	FallBack "Diffuse"
}

调节不同的参数值,得到的几种效果截图。

r1.pngr2.png

r3.pngr4.png

标签: Shader

Powered by emlog  蜀ICP备18021003号-1   sitemap

川公网安备 51019002001593号