在unity中使用Matcap实现玻璃材质

编程入门 行业动态 更新时间:2024-10-19 08:52:31

在unity中使用Matcap实现玻璃<a href=https://www.elefans.com/category/jswz/34/1765866.html style=材质"/>

在unity中使用Matcap实现玻璃材质

关于Matcap 

Matcap(Material Capture)是一种渲染技术,用于在渲染过程中模拟具有真实光照效果的材质。Matcap贴图通常为一张包含着光照等材质信息的球形纹理,以模型的的法线作为uv采样这张纹理,即可把材质映射到模型中,通常能够提供较优的性能效率。但由于这张纹理是基于预先捕获的光照信息,因此并不适用动态关照场景。

玻璃材质

核心思想就是通过Matcap贴图模拟玻璃的高光和折射

Shader "Custom/GlassOutside"
{Properties{_BaseColor("Base Color",Color) = (1,1,1,1)_SpecularColor("Specular Color",Color) = (1,1,1,1)_matCap("matCap Texture",2d) = "white"{}_ThicknessTex("Thickness Texture",2d) = "white"{}_edgeThickness("edge Thickness",float) = 1.0_refMatCapTexture("refraction matCap Texture",2d) = "white"{}_refintensity("refraction matCap intensity",float) = 1.0_normalTexture("Normal Texture",2d) = "white"{}}SubShader{Tags{"RenderPipeline" = "UniversalPipeline""Queue" = "Transparent""RenderType" = "Transparent""IgnoreProjector" = "True"}Blend SrcAlpha OneMinusSrcAlphaZWrite OffHLSLINCLUDE#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl"#include "Packages/com.unity.render-pipelines.core/ShaderLibrary/SpaceTransforms.hlsl"CBUFFER_START(UnityPerMaterial)half _edgeThickness;half _refintensity;half4 _BaseColor;half4 _SpecularColor;CBUFFER_ENDENDHLSLPass{Tags{"LightMode" = "UniversalForward"}HLSLPROGRAM#pragma vertex vert#pragma fragment fragTexture2D _matCap;SAMPLER(sampler_matCap);Texture2D _ThicknessTex;SAMPLER(sampler_ThicknessTex);Texture2D _refMatCapTexture;SAMPLER(sampler_refMatCapTexture);Texture2D _normalTexture;SAMPLER(sampler_normalTexture);struct Attributes{float4 positionOS : POSITION;float3 normalOS : NORMAL;float4 tangentOS : TANGENT;float2 uv : TEXCOORD0;};struct Varings{float4 positionCS : SV_POSITION;float3 positionWS : TEXCOORD3;float3 normalWS : TEXCOORD2;float3 tangentWS : TEXCOORD4;float3 BtangentWS : TEXCOORD5;float3 normalVS : TEXCOORD0;float2 uv : TEXCOORD1;};Varings vert(Attributes IN){Varings OUT;OUT.positionCS = TransformObjectToHClip(IN.positionOS.xyz);float3 normalWS = TransformObjectToWorldDir(IN.normalOS);OUT.normalVS = normalize(TransformWorldToViewDir(normalWS));OUT.normalWS = normalize(normalWS);OUT.tangentWS = normalize(TransformObjectToWorldDir(IN.tangentOS.xyz));OUT.BtangentWS = normalize(cross(OUT.normalWS,OUT.tangentWS.xyz)*IN.tangentOS.w);OUT.positionWS = TransformObjectToWorld(IN.positionOS.xyz);OUT.uv = IN.uv;return OUT;}float4 frag(Varings IN):SV_TARGET{//glass patternfloat3 normalTS = UnpackNormal(SAMPLE_TEXTURE2D(_normalTexture,sampler_normalTexture,IN.uv));//TBNfloat3x3 TBN = float3x3(IN.tangentWS,IN.BtangentWS,IN.normalWS);float3 bump = normalize(mul(normalTS,TBN));float3 viewBump = normalize(TransformWorldToViewDir(bump));//sepcularfloat2 matCapuv = IN.normalVS.xy;//float2 matCapuv = viewBump.xy;matCapuv = matCapuv*0.490+0.5;half3 matCapColor = SAMPLE_TEXTURE2D(_matCap,sampler_matCap,matCapuv).rgb;//thicknessfloat thickness = SAMPLE_TEXTURE2D(_ThicknessTex,sampler_ThicknessTex,IN.uv).r;float3 viewDir = normalize(IN.positionWS.xyz - GetCameraPositionWS().xyz);float fresnel = pow(saturate(1.0-dot(-viewDir,IN.normalWS.xyz)),_edgeThickness);thickness += fresnel;//refractionfloat Refintensity = thickness * _refintensity;float3 refColor = SAMPLE_TEXTURE2D(_refMatCapTexture,sampler_refMatCapTexture,matCapuv + Refintensity).rgb;float3 c1 = _BaseColor.rgb*0.5;float3 c2 = refColor*_BaseColor.rgb;float3 finalCol = lerp(c1,c2,thickness);return float4(lerp(matCapColor.r * _SpecularColor.rgb,finalCol,thickness), matCapColor.r + thickness);}ENDHLSL}}
}

参考:

[1]如何在游戏中表现玻璃的质感?这个方法效率超高

[2]【玉兔 | 图形学与游戏开发】高性能的玻璃杯渲染Shader | 折射+反射+厚度+材质纹路 | 零基础也能学_哔哩哔哩_bilibili

贴图来源:/

更多推荐

在unity中使用Matcap实现玻璃材质

本文发布于:2024-02-25 14:14:47,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1699240.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:材质   玻璃   unity   Matcap

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!