278 lines
10 KiB
Plaintext
278 lines
10 KiB
Plaintext
![]() |
// ***********************************************************************
|
|||
|
// Author : Kimch
|
|||
|
// Created : 2018-7-1
|
|||
|
// Description : 角色Shader
|
|||
|
// Last Modified By : Kimch
|
|||
|
// Last Modified On : 2019-7-1
|
|||
|
// ***********************************************************************
|
|||
|
// <copyright file= "MY_PBS_PlayerV4" company="kk"></copyright>
|
|||
|
// ***********************************************************************
|
|||
|
Shader "MY/PBS/PlayerV4"
|
|||
|
{
|
|||
|
Properties
|
|||
|
{
|
|||
|
[HDR]_Color("Color", Color) = (1,1,1,1)
|
|||
|
_MainTex("Albedo (RGB)", 2D) = "white" {}
|
|||
|
|
|||
|
[NoScaleOffset]_MetalTex("Metallic", 2D) = "black" {}
|
|||
|
_MetalColor("Metallic Color", Color) = (0,0,0,1)
|
|||
|
_GlossColor("Smoothness Color", Color) = (0,0,0,1)
|
|||
|
|
|||
|
// 立方体贴图属性
|
|||
|
_Cube("Cubemap", CUBE) = "" {}
|
|||
|
|
|||
|
[Header(Detail)]
|
|||
|
_DetailAlbedoMap("Detail Albedo x2", 2D) = "grey" {}
|
|||
|
_DetailNormalMapScale("Scale", Float) = 1.0
|
|||
|
_DetailNormalMap("Normal Map", 2D) = "bump" {}
|
|||
|
|
|||
|
[HDR]_DetailColor("花纹颜色", Color) = (0,0,0,1)
|
|||
|
_DetailPower("花纹强度", Range(-10,10)) = 1
|
|||
|
[Header(Rim)]
|
|||
|
_RimColor("边光颜色", Color) = (0,0,0,1)
|
|||
|
_RimPower("边光强度", Range(-10,10)) = 1
|
|||
|
_RimRange("边光范围", Range(1,10)) = 1.8
|
|||
|
[Header(Emission)]
|
|||
|
_EmissionColor("自发光颜色", Color) = (0,0,0)
|
|||
|
_EmissionPower("自发光强度", Range(-10,10)) = 0
|
|||
|
[Header(Scroll)]
|
|||
|
_ScrollTex("流光贴图(RGB)", 2D) = "black" {}
|
|||
|
_ScrollPower("流光强度", Range(-10, 10)) = 1
|
|||
|
_ScrollSpeedX("X流光速度", Range(-10, 10)) = 0
|
|||
|
_ScrollSpeedY("Y流光速度", Range(-10, 10)) = 0
|
|||
|
[Header(ColorMask)]
|
|||
|
[NoScaleOffset]_ColorMaskTex("换色贴图", 2D) = "white" { }
|
|||
|
|
|||
|
[HDR]_RColorMaskColor("区域一渐变色1(红色部分控制)", Color) = (0,0,0,1)
|
|||
|
[HDR]_RColorMaskColor2("区域一渐变色2(红色部分控制)", Color) = (0,0,0,1)
|
|||
|
_RColorMaskUVRange("区域一渐变色范围(R-G X=B Y=A)", Color) = (0,1,0,1)
|
|||
|
|
|||
|
[HDR]_GColorMaskColor("区域二渐变色1(绿色部分控制)", Color) = (0,0,0,1)
|
|||
|
[HDR]_GColorMaskColor2("区域二渐变色2(绿色部分控制)", Color) = (0,0,0,1)
|
|||
|
|
|||
|
[HDR]_BColorMaskColor("区域三渐变色1(蓝色部分控制)", Color) = (0,0,0,1)
|
|||
|
[HDR]_BColorMaskColor2("区域三渐变色2(蓝色部分控制)", Color) = (0,0,0,1)
|
|||
|
|
|||
|
}
|
|||
|
|
|||
|
SubShader
|
|||
|
{
|
|||
|
Tags { "RenderType" = "Opaque" }
|
|||
|
LOD 200
|
|||
|
|
|||
|
CGPROGRAM
|
|||
|
// Physically based Standard lighting model, and enable shadows on all light types
|
|||
|
#pragma surface surf Standard fullforwardshadows vertex:myvert finalcolor:mycolor exclude_path:deferred exclude_path:prepass nometa nolightmap nofog
|
|||
|
|
|||
|
// Use shader model 3.0 target, to get nicer looking lighting
|
|||
|
#pragma target 3.0
|
|||
|
|
|||
|
samplerCUBE _Cube;
|
|||
|
|
|||
|
sampler2D _MainTex;
|
|||
|
fixed4 _Color;
|
|||
|
|
|||
|
sampler2D _MetalTex;
|
|||
|
fixed4 _MetalColor;
|
|||
|
fixed4 _GlossColor;
|
|||
|
|
|||
|
fixed3 _RimColor;
|
|||
|
half _RimPower;
|
|||
|
half _RimRange;
|
|||
|
|
|||
|
#if _DETAIL
|
|||
|
sampler2D _DetailAlbedoMap;
|
|||
|
sampler2D _DetailNormalMap;
|
|||
|
half _DetailNormalMapScale;
|
|||
|
#endif
|
|||
|
fixed3 _DetailColor;
|
|||
|
half _DetailPower;
|
|||
|
|
|||
|
fixed3 _EmissionColor;
|
|||
|
half _EmissionPower;
|
|||
|
|
|||
|
// Color Mask
|
|||
|
sampler2D _ColorMaskTex;
|
|||
|
fixed4 _RColorMaskColor;
|
|||
|
fixed4 _RColorMaskColor2;
|
|||
|
fixed4 _RColorMaskUVRange;
|
|||
|
|
|||
|
fixed4 _GColorMaskColor;
|
|||
|
fixed4 _BColorMaskColor;
|
|||
|
|
|||
|
// Scroll
|
|||
|
sampler2D _ScrollTex;
|
|||
|
half _ScrollPower;
|
|||
|
half _ScrollSpeedX;
|
|||
|
half _ScrollSpeedY;
|
|||
|
|
|||
|
struct Input
|
|||
|
{
|
|||
|
float2 uv_MainTex;
|
|||
|
float2 uv_ScrollTex;
|
|||
|
float2 uv_DetailAlbedoMap;
|
|||
|
float2 uv_DetailNormalMap;
|
|||
|
float3 viewDir;
|
|||
|
float3 worldNormal;
|
|||
|
float3 worldRefl;
|
|||
|
float2 scrollAdd;
|
|||
|
INTERNAL_DATA
|
|||
|
};
|
|||
|
|
|||
|
fixed4 LerpMaskColor(fixed mask, fixed4 color1, fixed4 color2, fixed2 uv, fixed4 uvRange)
|
|||
|
{
|
|||
|
fixed t = (uv.x*uvRange.w + uv.y*uvRange.z - uvRange.x) / (uvRange.y - uvRange.x);
|
|||
|
return lerp(color1,color2,t) * mask;
|
|||
|
}
|
|||
|
|
|||
|
//#define _NORMALMAP
|
|||
|
//#ifdef _NORMALMAP
|
|||
|
// half3 NormalInTangentSpace(float4 texcoords)
|
|||
|
// {
|
|||
|
// half3 normalTangent = UnpackScaleNormal(tex2D(_BumpMap, texcoords.xy), _BumpScale);
|
|||
|
|
|||
|
//#if _DETAIL && defined(UNITY_ENABLE_DETAIL_NORMALMAP)
|
|||
|
// half mask = DetailMask(texcoords.xy);
|
|||
|
// half3 detailNormalTangent = UnpackScaleNormal(tex2D(_DetailNormalMap, texcoords.zw), _DetailNormalMapScale);
|
|||
|
//#if _DETAIL_LERP
|
|||
|
// normalTangent = lerp(
|
|||
|
// normalTangent,
|
|||
|
// detailNormalTangent,
|
|||
|
// mask);
|
|||
|
//#else
|
|||
|
// normalTangent = lerp(
|
|||
|
// normalTangent,
|
|||
|
// BlendNormals(normalTangent, detailNormalTangent),
|
|||
|
// mask);
|
|||
|
//#endif
|
|||
|
//#endif
|
|||
|
|
|||
|
// return normalTangent;
|
|||
|
// }
|
|||
|
//#endif
|
|||
|
// Add instancing support for this shader. You need to check 'Enable Instancing' on materials that use the shader.
|
|||
|
// See https://docs.unity3d.com/Manual/GPUInstancing.html for more information about instancing.
|
|||
|
// #pragma instancing_options assumeuniformscaling
|
|||
|
//UNITY_INSTANCING_BUFFER_START(Props)
|
|||
|
// put more per-instance properties here
|
|||
|
//UNITY_INSTANCING_BUFFER_END(Props)
|
|||
|
|
|||
|
|
|||
|
//struct SurfaceOutputStandardSpecular
|
|||
|
//{
|
|||
|
// fixed3 Albedo; // diffuse color
|
|||
|
// fixed3 Specular; // specular color
|
|||
|
// float3 Normal; // tangent space normal, if written
|
|||
|
// half3 Emission;
|
|||
|
// half Smoothness; // 0=rough, 1=smooth
|
|||
|
// half Occlusion; // occlusion (default 1)
|
|||
|
// fixed Alpha; // alpha for transparencies
|
|||
|
//};
|
|||
|
|
|||
|
//struct SurfaceOutputStandard
|
|||
|
//{
|
|||
|
// fixed3 Albedo; // base (diffuse or specular) color
|
|||
|
// float3 Normal; // tangent space normal, if written
|
|||
|
// half3 Emission;
|
|||
|
// half Metallic; // 0=non-metal, 1=metal
|
|||
|
// // Smoothness is the user facing name, it should be perceptual smoothness but user should not have to deal with it.
|
|||
|
// // Everywhere in the code you meet smoothness it is perceptual smoothness
|
|||
|
// half Smoothness; // 0=rough, 1=smooth
|
|||
|
// half Occlusion; // occlusion (default 1)
|
|||
|
// fixed Alpha; // alpha for transparencies
|
|||
|
//};
|
|||
|
|
|||
|
void myvert(inout appdata_full v, out Input o)
|
|||
|
{
|
|||
|
UNITY_INITIALIZE_OUTPUT(Input, o);
|
|||
|
|
|||
|
o.scrollAdd.xy = float2(_ScrollSpeedX, _ScrollSpeedY)*_Time.y;
|
|||
|
}
|
|||
|
|
|||
|
void surf(Input IN, inout SurfaceOutputStandard o)
|
|||
|
{
|
|||
|
// Albedo comes from a texture tinted by color
|
|||
|
fixed4 cm = tex2D(_ColorMaskTex, IN.uv_MainTex);
|
|||
|
fixed4 specGloss = tex2D(_MetalTex, IN.uv_MainTex);
|
|||
|
fixed4 scroll = tex2D(_ScrollTex, IN.scrollAdd + IN.uv_ScrollTex);
|
|||
|
|
|||
|
//half h = specGloss.b - 0.5;
|
|||
|
//float2 offset = ParallaxOffset(h, _Parallax, IN.viewDir);
|
|||
|
//IN.uv_MainTex += offset;
|
|||
|
|
|||
|
fixed4 c = tex2D(_MainTex, IN.uv_MainTex) * _Color;
|
|||
|
|
|||
|
fixed4 cmc = LerpMaskColor(cm.r , _RColorMaskColor , _RColorMaskColor2, IN.uv_MainTex.xy, _RColorMaskUVRange) + cm.g * _GColorMaskColor + cm.b*_BColorMaskColor;
|
|||
|
o.Albedo = ((1 - specGloss.g - specGloss.r) * cmc)* c.rgb + specGloss.g *c.rgb + specGloss.r*c.rgb * 2;
|
|||
|
|
|||
|
// Specular and smoothness come from slider variables
|
|||
|
o.Metallic = saturate(sign(specGloss.r - 0.1))*_MetalColor.a + cm.r*_MetalColor.r + cm.g*_MetalColor.g + cm.b *_MetalColor.b;
|
|||
|
o.Smoothness = _GlossColor.a*specGloss.r + _GlossColor.r*cm.r + _GlossColor.g*cm.g + _GlossColor.b*cm.b;
|
|||
|
o.Emission = /*+ _EmissionPower * _EmissionColor.rgb*/ +specGloss.a*_ScrollPower*scroll.rgb + specGloss.b*_DetailPower*_DetailColor.rgb *_DetailColor.rgb;
|
|||
|
|
|||
|
half perceptualRoughness = o.Smoothness /* perceptualRoughness */;
|
|||
|
|
|||
|
// TODO: CAUTION: remap from Morten may work only with offline convolution, see impact with runtime convolution!
|
|||
|
// For now disabled
|
|||
|
#if 0
|
|||
|
float m = PerceptualRoughnessToRoughness(perceptualRoughness); // m is the real roughness parameter
|
|||
|
const float fEps = 1.192092896e-07F; // smallest such that 1.0+FLT_EPSILON != 1.0 (+1e-4h is NOT good here. is visibly very wrong)
|
|||
|
float n = (2.0 / max(fEps, m*m)) - 2.0; // remap to spec power. See eq. 21 in --> https://dl.dropboxusercontent.com/u/55891920/papers/mm_brdf.pdf
|
|||
|
|
|||
|
n /= 4; // remap from n_dot_h formulatino to n_dot_r. See section "Pre-convolved Cube Maps vs Path Tracers" --> https://s3.amazonaws.com/docs.knaldtech.com/knald/1.0.0/lys_power_drops.html
|
|||
|
|
|||
|
perceptualRoughness = pow(2 / (n + 2), 0.25); // remap back to square root of real roughness (0.25 include both the sqrt root of the conversion and sqrt for going from roughness to perceptualRoughness)
|
|||
|
#else
|
|||
|
// MM: came up with a surprisingly close approximation to what the #if 0'ed out code above does.
|
|||
|
perceptualRoughness = perceptualRoughness * (1.7 - 0.7*perceptualRoughness);
|
|||
|
#endif
|
|||
|
|
|||
|
|
|||
|
half mip = perceptualRoughnessToMipmapLevel(perceptualRoughness);
|
|||
|
half3 R = IN.worldRefl;
|
|||
|
half4 rgbm = texCUBElod(_Cube, half4(R, mip));
|
|||
|
//o.Emission += rgbm;
|
|||
|
|
|||
|
#if _DETAIL
|
|||
|
half mask = 1 - saturate(cm.g - 0.2);
|
|||
|
half3 detailAlbedo = tex2D(_DetailAlbedoMap, IN.uv_DetailAlbedoMap).rgb;
|
|||
|
#if _DETAIL_MULX2
|
|||
|
o.Albedo *= LerpWhiteTo(detailAlbedo * unity_ColorSpaceDouble.rgb, mask);
|
|||
|
#elif _DETAIL_MUL
|
|||
|
o.Albedo *= LerpWhiteTo(detailAlbedo, mask);
|
|||
|
#elif _DETAIL_ADD
|
|||
|
o.Albedo += detailAlbedo * mask;
|
|||
|
#elif _DETAIL_LERP
|
|||
|
o.Albedo = lerp(albedo, detailAlbedo, mask);
|
|||
|
#endif
|
|||
|
half3 normalTangent = IN.worldNormal;
|
|||
|
|
|||
|
|
|||
|
half3 detailNormalTangent = UnpackScaleNormal(tex2D(_DetailNormalMap, IN.uv_DetailNormalMap), _DetailNormalMapScale);
|
|||
|
#if _DETAIL_LERP
|
|||
|
normalTangent = lerp(
|
|||
|
normalTangent,
|
|||
|
detailNormalTangent,
|
|||
|
mask);
|
|||
|
#else
|
|||
|
normalTangent = lerp(
|
|||
|
normalTangent,
|
|||
|
BlendNormals(normalTangent, detailNormalTangent),
|
|||
|
mask);
|
|||
|
#endif
|
|||
|
o.Normal = normalTangent;
|
|||
|
#endif
|
|||
|
o.Alpha = c.a;
|
|||
|
}
|
|||
|
|
|||
|
void mycolor(Input IN, SurfaceOutputStandard o, inout fixed4 color)
|
|||
|
{
|
|||
|
fixed3 rim = saturate(1 - saturate(dot(IN.worldNormal, IN.viewDir)) * _RimRange) *_RimPower * _RimColor;
|
|||
|
color.rgb += rim;
|
|||
|
}
|
|||
|
|
|||
|
ENDCG
|
|||
|
}
|
|||
|
FallBack "Diffuse"
|
|||
|
}
|