#ifndef XUANXUAN_UTILITY #define XUANXUAN_UTILITY //引入自UnityCG.cginc #define UNITY_PI 3.14159265359f #define UNITY_TWO_PI 6.28318530718f #define UNITY_FOUR_PI 12.56637061436f #define UNITY_INV_PI 0.31830988618f #define UNITY_INV_TWO_PI 0.15915494309f #define UNITY_INV_FOUR_PI 0.07957747155f #define UNITY_HALF_PI 1.57079632679f #define UNITY_INV_HALF_PI 0.636619772367f #include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Color.hlsl" inline half NB_Remap(half x, half inMin, half inMax, half outMin, half outMax) { // x= clamp(x,inMin,inMax); return saturate((x - inMin) / (inMax - inMin)) * (outMax - outMin) + outMin; } inline half NB_Remap01(half x, half inMin, half inMax) { // x= clamp(x,inMin,inMax); return saturate((x - inMin) / (inMax - inMin)); } inline half NB_RemapNoClamp(half x, half inMin, half inMax, half outMin, half outMax) { // x= clamp(x,inMin,inMax); return ((x - inMin) / (inMax - inMin)) * (outMax - outMin) + outMin; } //原生SmoothStep开销很大:https://zhuanlan.zhihu.com/p/34629262 //在很多时候可以用简单线性映射代替。 half SimpleSmoothstep(half min,half max,half interp) { return saturate((interp - min)/(max-min)); } half4 TryLinearize(half4 color) { #if defined(UNITY_COLORSPACE_GAMMA) return pow(color, 2.2f); #else return color; #endif } half4 TryLinearizeWithoutAlpha(half4 color) { #if defined(UNITY_COLORSPACE_GAMMA) return half4(pow(color.rgb, 2.2f), color.a); #else return color; #endif } half3 TryLinearize(half3 color) { #if defined(UNITY_COLORSPACE_GAMMA) return pow(color, 2.2f); #else return color; #endif } half2 TryLinearize(half2 color) { #if defined(UNITY_COLORSPACE_GAMMA) return pow(color, 2.2f); #else return color; #endif } half TryLinearize(half color) { #if defined(UNITY_COLORSPACE_GAMMA) return pow(color, 2.2f); #else return color; #endif } half4 tex2D_TryLinearizeWithoutAlpha(sampler2D tex, float2 uv) { #if defined(UNITY_COLORSPACE_GAMMA) return TryLinearizeWithoutAlpha(tex2D(tex, uv)); #else return tex2D(tex, uv); #endif } inline float LinearToGammaSpaceExact (float value) { if (value <= 0.0F) return 0.0F; else if (value <= 0.0031308F) return 12.92F * value; else if (value < 1.0F) return 1.055F * pow(value, 0.4166667F) - 0.055F; else return pow(value, 0.45454545F); } // real FastSRGBToLinear(real c) // { // return c * (c * (c * 0.305306011 + 0.682171111) + 0.012522878); // } float2 Rotate_Radians_float(float2 UV, float2 Center, float Rotation) { if(Rotation == 0) { return UV; } // Rotation = Rotation / 180 * 3.14; //从角度转为弧度。 Rotation *= 0.01745329222; //从角度转为弧度。 UV -= Center; float s = sin(Rotation); float c = cos(Rotation); float2x2 rMatrix = float2x2(c, -s, s, c); rMatrix *= 0.5; rMatrix += 0.5; rMatrix = rMatrix * 2 - 1; UV.xy = mul(UV.xy, rMatrix); UV += Center; return UV; } inline half luminance(half3 color) { return dot(color, float3(0.2126f, 0.7152f, 0.0722f)); } inline half DepthFactor(float Z, float near, float far) { Z = saturate((Z-near)/(far - near)); return Z; } //抽自Unity.cginc inline half3 LinearToGammaSpace (half3 linRGB) { linRGB = max(linRGB, half3(0.h, 0.h, 0.h)); // An almost-perfect approximation from http://chilliant.blogspot.com.au/2012/08/srgb-approximations-for-hlsl.html?m=1 return max(1.055h * pow(linRGB, 0.416666667h) - 0.055h, 0.h); // Exact version, useful for debugging. //return half3(LinearToGammaSpaceExact(linRGB.r), LinearToGammaSpaceExact(linRGB.g), LinearToGammaSpaceExact(linRGB.b)); } //ASE float2 voronoihash1( float2 p ) { p = float2( dot( p, float2( 127.1, 311.7 ) ), dot( p, float2( 269.5, 183.3 ) ) ); return frac( sin( p ) *43758.5453); } //ASE float voronoi1( float2 v, float time, inout float2 id, inout float2 mr, float smoothness ) { float2 n = floor( v ); float2 f = frac( v ); float F1 = 8.0; float F2 = 8.0; float2 mg = 0; for ( int j = -1; j <= 1; j++ ) { for ( int i = -1; i <= 1; i++ ) { float2 g = float2( i, j ); float2 o = voronoihash1( n + g ); o = ( sin( time + o * 6.2831 ) * 0.5 + 0.5 ); float2 r = f - g - o; float d = 0.5 * dot( r, r ); if( d