本文主要是介绍类崩坏三NPR渲染分析(一),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
类崩坏三NPR渲染分析(一)
- 使用MatCap实现空之律者的翅膀效果
- 什么是MatCap
- 实现组成
- 描边效果
- MatCap Shader
- 完整代码
使用MatCap实现空之律者的翅膀效果
什么是MatCap
Material Capture(材质捕获),通常被简称为MatCap,在Zbrush、Sculptris、Mudbox等3D软件中有比较多的使用。
MatCap Shader的基本思路是,使用某特定材质球的贴图,作为当前材质的视图空间环境贴图(view-space environment map),来实现具有均匀表面着色的反射材质物体的显示。考虑到物体的所有法线的投影的范围在x(-1,1),y(-1,1),构成了一个圆形,所以MatCap 贴图中存储光照信息的区域是一个圆形。
基于MatCap思想的Shader,可以无需提供任何光照,只需提供一张或多张合适的MatCap贴图作为光照结果的“指导”即可。
实现组成
1.首先我们需要实现的描边效果
2.在描边的基础上我们加上MatCap效果来模拟PBR
3.在此基础之上再加上扭曲效果、菲涅尔反射和高光
描边效果
我们采用的是法线外扩的方法来实现描边效果
加了蓝色的描边效果
SubShader {Tags {"RenderType"="Opaque"}Pass {Name "Outline"Tags {}Cull FrontCGPROGRAM#pragma vertex vert#pragma fragment frag#include "UnityCG.cginc"#pragma fragmentoption ARB_precision_hint_fastest#pragma multi_compile_shadowcaster#pragma multi_compile_fog#pragma only_renderers d3d9 d3d11 glcore gles #pragma target 3.0uniform float _LineWidth;uniform float4 _LineColor;struct VertexInput {float4 vertex : POSITION;float3 normal : NORMAL;};struct VertexOutput {float4 pos : SV_POSITION;UNITY_FOG_COORDS(0)};VertexOutput vert (VertexInput v) {VertexOutput o = (VertexOutput)0;o.pos = UnityObjectToClipPos( float4(v.vertex.xyz + v.normal*_LineWidth,1) );UNITY_TRANSFER_FOG(o,o.pos);return o;}float4 frag(VertexOutput i, float facing : VFACE) : COLOR {float isFrontFace = ( facing >= 0 ? 1 : 0 );float faceSign = ( facing >= 0 ? 1 : -1 );return fixed4(_LineColor.rgb,0);}ENDCG}
MatCap Shader
Pass {Name "FORWARD"Tags {"LightMode"="ForwardBase"}Cull OffCGPROGRAM#pragma vertex vert#pragma fragment frag#define UNITY_PASS_FORWARDBASE#include "UnityCG.cginc"#pragma multi_compile_fwdbase_fullshadows#pragma multi_compile_fog#pragma only_renderers d3d9 d3d11 glcore gles #pragma target 3.0uniform sampler2D _MainTex; uniform float4 _MainTex_ST;uniform float _FresnelPower;uniform float4 _FresnelColor;uniform float4 _MainColor;uniform sampler2D _MatCap; uniform float4 _MatCap_ST;uniform sampler2D _BumpMap; uniform float4 _BumpMap_ST;struct VertexInput {float4 vertex : POSITION;float3 normal : NORMAL;float4 tangent : TANGENT;float2 texcoord0 : TEXCOORD0;};struct VertexOutput {float4 pos : SV_POSITION;float2 uv0 : TEXCOORD0;float4 posWorld : TEXCOORD1;float3 normalDir : TEXCOORD2;float3 tangentDir : TEXCOORD3;float3 bitangentDir : TEXCOORD4;UNITY_FOG_COORDS(4)};VertexOutput vert (VertexInput v) {VertexOutput o = (VertexOutput)0;o.uv0 = v.texcoord0;o.normalDir = UnityObjectToWorldNormal(v.normal);o.pos = UnityObjectToClipPos( v.vertex );o.tangentDir = normalize( mul( unity_ObjectToWorld, float4( v.tangent.xyz, 0.0 ) ).xyz );o.bitangentDir = normalize(cross(o.normalDir, o.tangentDir) * v.tangent.w);o.posWorld = mul(unity_ObjectToWorld, v.vertex);UNITY_TRANSFER_FOG(o,o.pos);return o;}float4 frag(VertexOutput i, float facing : VFACE) : COLOR {float isFrontFace = ( facing >= 0 ? 1 : 0 );float faceSign = ( facing >= 0 ? 1 : -1 );i.normalDir = normalize(i.normalDir);i.normalDir *= faceSign;float3 viewDirection = normalize(_WorldSpaceCameraPos.xyz - i.posWorld.xyz);float3 normalDirection = i.normalDir;float4 _MainTex_var = tex2D(_MainTex,TRANSFORM_TEX(i.uv0, _MainTex));float3x3 tangentTransform = float3x3( i.tangentDir, i.bitangentDir, i.normalDir);float4 _BumpMap_var = tex2D(_BumpMap,TRANSFORM_TEX(i.uv0, _BumpMap));float3 normalLocal = _BumpMap_var.rgb;float3 normalDir = normalize(mul(normalLocal, tangentTransform )); // Perturbed normalsfloat2 _MatCaprg = (mul( UNITY_MATRIX_V, float4(normalDir,0) ).xyz.rgb*0.5+0.5).rg;float4 _MatCap_var = tex2D(_MatCap,TRANSFORM_TEX(_MatCaprg, _MatCap));float3 finalColor = ((_MainColor.rgb*_MainTex_var.rgb)+(pow(1.0-max(0,dot(normalDirection, viewDirection)),_FresnelPower)*_FresnelColor.rgb));finalColor = finalColor + _MatCap_var * _MainColor;fixed4 finalRGBA = fixed4(finalColor,1);UNITY_APPLY_FOG(i.fogCoord, finalRGBA);return finalRGBA;}ENDCG}
完整代码
Shader "LightWings" {Properties {_LineWidth ("LineWidth", Range(0, 0.1)) = 0[HDR]_LineColor ("LineColor", Color) = (0.0895329,0.2127204,1,1)_MainTex ("MainTex", 2D) = "white" {}[HDR]_MainColor ("MainColor", Color) = (0.007352948,0.4660244,1,1)_FresnelPower ("FresnelPower", Range(0.1, 10)) = 1.201405[HDR]_FresnelColor ("FresnelColor", Color) = (0.6617647,0.944016,1,1)_MatCap ("MatCap", 2D) = "white" {}_BumpMap ("BumpMap", 2D) = "white" {}}SubShader {Tags {"RenderType"="Opaque"}Pass {Name "Outline"Tags {}Cull FrontCGPROGRAM#pragma vertex vert#pragma fragment frag#include "UnityCG.cginc"#pragma fragmentoption ARB_precision_hint_fastest#pragma multi_compile_shadowcaster#pragma multi_compile_fog#pragma only_renderers d3d9 d3d11 glcore gles #pragma target 3.0uniform float _LineWidth;uniform float4 _LineColor;struct VertexInput {float4 vertex : POSITION;float3 normal : NORMAL;};struct VertexOutput {float4 pos : SV_POSITION;UNITY_FOG_COORDS(0)};VertexOutput vert (VertexInput v) {VertexOutput o = (VertexOutput)0;o.pos = UnityObjectToClipPos( float4(v.vertex.xyz + v.normal*_LineWidth,1) );UNITY_TRANSFER_FOG(o,o.pos);return o;}float4 frag(VertexOutput i, float facing : VFACE) : COLOR {float isFrontFace = ( facing >= 0 ? 1 : 0 );float faceSign = ( facing >= 0 ? 1 : -1 );return fixed4(_LineColor.rgb,0);}ENDCG}Pass {Name "FORWARD"Tags {"LightMode"="ForwardBase"}Cull OffCGPROGRAM#pragma vertex vert#pragma fragment frag#define UNITY_PASS_FORWARDBASE#include "UnityCG.cginc"#pragma multi_compile_fwdbase_fullshadows#pragma multi_compile_fog#pragma only_renderers d3d9 d3d11 glcore gles #pragma target 3.0uniform sampler2D _MainTex; uniform float4 _MainTex_ST;uniform float _FresnelPower;uniform float4 _FresnelColor;uniform float4 _MainColor;uniform sampler2D _MatCap; uniform float4 _MatCap_ST;uniform sampler2D _BumpMap; uniform float4 _BumpMap_ST;struct VertexInput {float4 vertex : POSITION;float3 normal : NORMAL;float4 tangent : TANGENT;float2 texcoord0 : TEXCOORD0;};struct VertexOutput {float4 pos : SV_POSITION;float2 uv0 : TEXCOORD0;float4 posWorld : TEXCOORD1;float3 normalDir : TEXCOORD2;float3 tangentDir : TEXCOORD3;float3 bitangentDir : TEXCOORD4;UNITY_FOG_COORDS(4)};VertexOutput vert (VertexInput v) {VertexOutput o = (VertexOutput)0;o.uv0 = v.texcoord0;o.normalDir = UnityObjectToWorldNormal(v.normal);o.pos = UnityObjectToClipPos( v.vertex );o.tangentDir = normalize( mul( unity_ObjectToWorld, float4( v.tangent.xyz, 0.0 ) ).xyz );o.bitangentDir = normalize(cross(o.normalDir, o.tangentDir) * v.tangent.w);o.posWorld = mul(unity_ObjectToWorld, v.vertex);UNITY_TRANSFER_FOG(o,o.pos);return o;}float4 frag(VertexOutput i, float facing : VFACE) : COLOR {float isFrontFace = ( facing >= 0 ? 1 : 0 );float faceSign = ( facing >= 0 ? 1 : -1 );i.normalDir = normalize(i.normalDir);i.normalDir *= faceSign;float3 viewDirection = normalize(_WorldSpaceCameraPos.xyz - i.posWorld.xyz);float3 normalDirection = i.normalDir;float4 _MainTex_var = tex2D(_MainTex,TRANSFORM_TEX(i.uv0, _MainTex));float3x3 tangentTransform = float3x3( i.tangentDir, i.bitangentDir, i.normalDir);float4 _BumpMap_var = tex2D(_BumpMap,TRANSFORM_TEX(i.uv0, _BumpMap));float3 normalLocal = _BumpMap_var.rgb;float3 normalDir = normalize(mul(normalLocal, tangentTransform )); // Perturbed normalsfloat2 _MatCaprg = (mul( UNITY_MATRIX_V, float4(normalDir,0) ).xyz.rgb*0.5+0.5).rg;float4 _MatCap_var = tex2D(_MatCap,TRANSFORM_TEX(_MatCaprg, _MatCap));float3 finalColor = ((_MainColor.rgb*_MainTex_var.rgb)+(pow(1.0-max(0,dot(normalDirection, viewDirection)),_FresnelPower)*_FresnelColor.rgb));finalColor = finalColor + _MatCap_var * _MainColor;fixed4 finalRGBA = fixed4(finalColor,1);UNITY_APPLY_FOG(i.fogCoord, finalRGBA);return finalRGBA;}ENDCG}Pass {Name "ShadowCaster"Tags {"LightMode"="ShadowCaster"}Offset 1, 1Cull OffCGPROGRAM#pragma vertex vert#pragma fragment frag#define UNITY_PASS_SHADOWCASTER#include "UnityCG.cginc"#include "Lighting.cginc"#pragma fragmentoption ARB_precision_hint_fastest#pragma multi_compile_shadowcaster#pragma multi_compile_fog#pragma only_renderers d3d9 d3d11 glcore gles #pragma target 3.0struct VertexInput {float4 vertex : POSITION;};struct VertexOutput {V2F_SHADOW_CASTER;};VertexOutput vert (VertexInput v) {VertexOutput o = (VertexOutput)0;o.pos = UnityObjectToClipPos( v.vertex );TRANSFER_SHADOW_CASTER(o)return o;}float4 frag(VertexOutput i, float facing : VFACE) : COLOR {float isFrontFace = ( facing >= 0 ? 1 : 0 );float faceSign = ( facing >= 0 ? 1 : -1 );SHADOW_CASTER_FRAGMENT(i)}ENDCG}}FallBack "Diffuse"
}
这篇关于类崩坏三NPR渲染分析(一)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!