3D残影Shader
2017-02-07 23:04
246 查看
前段时间有人说想要个残影的代码
今天给大家分享下这个是效果
下面几张是不同透明度的
好了上菜吧
Shader "Phantom" {
Properties {
_normalMap ("normal map", 2D) = "bump" {} //法线贴图
_rimColor ("Rim Color", Color) = (1, 1, 1, 1)//颜色
_power ("power", Float) = 1//明暗程度
}
SubShader {
Tags
{
"RenderType"="Transparent"
"LightMode"="ForwardBase"
"Queue"="Transparent"
}
LOD 200
Pass {
Name "ForwardBase"
Tags {
"LightMode"="ForwardBase"
}
Blend SrcAlpha OneMinusSrcAlpha
//Blend SrcAlpha One
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
//#define UNITY_PASS_FORWARDBASE
#include "UnityCG.cginc"
#include "AutoLight.cginc"
//#pragma multi_compile_fwdbase_fullshadows
//#pragma exclude_renderers xbox360 ps3 flash d3d11_9x
//#pragma target 3.0
uniform sampler2D _normalMap; uniform float4 _normalMap_ST;
uniform float4 _rimColor;
uniform float _power;
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 binormalDir : TEXCOORD4;
float3 eyeDir : TEXCOORD5;
LIGHTING_COORDS(10,11)
};
VertexOutput vert (VertexInput v) {
VertexOutput o;
o.uv0 = v.texcoord0;
o.normalDir = mul(float4(v.normal,0), _World2Object).xyz;
o.tangentDir = normalize( mul( _Object2World, float4( v.tangent.xyz, 0.0 ) ).xyz );
o.binormalDir = normalize(cross(o.normalDir, o.tangentDir) * v.tangent.w);
o.posWorld = mul(_Object2World, v.vertex);
o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
// Eye direction vector
o.eyeDir.xyz = normalize( _WorldSpaceCameraPos.xyz - mul( _Object2World, v.vertex )).xyz;
TRANSFER_VERTEX_TO_FRAGMENT(o)
return o;
}
float4 frag(VertexOutput i) : COLOR
{
float3x3 tangentTransform = float3x3( i.tangentDir, i.binormalDir, i.normalDir);
float3 viewDirection = normalize(_WorldSpaceCameraPos.xyz - i.posWorld.xyz);
/////// Normals:
float2 node_64 = i.uv0;
float3 normalLocal = UnpackNormal(tex2D(_normalMap,TRANSFORM_TEX(node_64.rg, _normalMap))).rgb;
float3 normalDirection = normalize(mul( normalLocal, tangentTransform )); // Perturbed normals
float3 lightDirection = normalize(_WorldSpaceLightPos0.xyz);
float3 halfDirection = normalize(viewDirection + lightDirection);
////// Fallout:
float normalDotEye = 1 - dot( normalDirection, i.eyeDir.xyz );
float4 retColor = float4(_rimColor.r, _rimColor.g, _rimColor.b, _rimColor.a * pow(normalDotEye, _power));
return retColor;
}
ENDCG
}
}
FallBack "Diffuse"
}![](/upload/201510/12/201510122324391263.jpg)
只要拖入一张角色的法线贴图就OK了
今天给大家分享下这个是效果
下面几张是不同透明度的
好了上菜吧
Shader "Phantom" {
Properties {
_normalMap ("normal map", 2D) = "bump" {} //法线贴图
_rimColor ("Rim Color", Color) = (1, 1, 1, 1)//颜色
_power ("power", Float) = 1//明暗程度
}
SubShader {
Tags
{
"RenderType"="Transparent"
"LightMode"="ForwardBase"
"Queue"="Transparent"
}
LOD 200
Pass {
Name "ForwardBase"
Tags {
"LightMode"="ForwardBase"
}
Blend SrcAlpha OneMinusSrcAlpha
//Blend SrcAlpha One
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
//#define UNITY_PASS_FORWARDBASE
#include "UnityCG.cginc"
#include "AutoLight.cginc"
//#pragma multi_compile_fwdbase_fullshadows
//#pragma exclude_renderers xbox360 ps3 flash d3d11_9x
//#pragma target 3.0
uniform sampler2D _normalMap; uniform float4 _normalMap_ST;
uniform float4 _rimColor;
uniform float _power;
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 binormalDir : TEXCOORD4;
float3 eyeDir : TEXCOORD5;
LIGHTING_COORDS(10,11)
};
VertexOutput vert (VertexInput v) {
VertexOutput o;
o.uv0 = v.texcoord0;
o.normalDir = mul(float4(v.normal,0), _World2Object).xyz;
o.tangentDir = normalize( mul( _Object2World, float4( v.tangent.xyz, 0.0 ) ).xyz );
o.binormalDir = normalize(cross(o.normalDir, o.tangentDir) * v.tangent.w);
o.posWorld = mul(_Object2World, v.vertex);
o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
// Eye direction vector
o.eyeDir.xyz = normalize( _WorldSpaceCameraPos.xyz - mul( _Object2World, v.vertex )).xyz;
TRANSFER_VERTEX_TO_FRAGMENT(o)
return o;
}
float4 frag(VertexOutput i) : COLOR
{
float3x3 tangentTransform = float3x3( i.tangentDir, i.binormalDir, i.normalDir);
float3 viewDirection = normalize(_WorldSpaceCameraPos.xyz - i.posWorld.xyz);
/////// Normals:
float2 node_64 = i.uv0;
float3 normalLocal = UnpackNormal(tex2D(_normalMap,TRANSFORM_TEX(node_64.rg, _normalMap))).rgb;
float3 normalDirection = normalize(mul( normalLocal, tangentTransform )); // Perturbed normals
float3 lightDirection = normalize(_WorldSpaceLightPos0.xyz);
float3 halfDirection = normalize(viewDirection + lightDirection);
////// Fallout:
float normalDotEye = 1 - dot( normalDirection, i.eyeDir.xyz );
float4 retColor = float4(_rimColor.r, _rimColor.g, _rimColor.b, _rimColor.a * pow(normalDotEye, _power));
return retColor;
}
ENDCG
}
}
FallBack "Diffuse"
}![](/upload/201510/12/201510122324391263.jpg)
只要拖入一张角色的法线贴图就OK了
相关文章推荐
- 3Dshader之球形环境映射(sphere environment mapping)
- 3D引擎:Horde3D:Shader类里的 结构体,以及渲染一个 Logo的简单流程(三)
- cocos2dx - shader实现任意动画的残影效果
- 3D打印机shader特效简单实现
- Flat Surface Shader – 超炫的 3D 模拟照明效果
- 火云开发课堂 - 《Shader从入门到精通》系列 第十五节:在Shader中对3D模型进行颜色设置
- 3DShader之阴影贴图(Shadow Mapping)
- untiy 3d ShaderLab_第 2 章Unity中Shader(着色器)的形态_1_Unity通过ShaderLab 来组织Shader
- 火云开发课堂 - 《Shader从入门到精通》系列 第十六节:在Shader中对3D模型使用纹理
- untiy 3d ShaderLab_第 2 章Unity中Shader(着色器)的形态_2_Unity中Shader的3种形态
- 火云开发课堂 - 《Shader从入门到精通》系列 第十七节:在Shader中对3D模型进行色彩与纹理色的混合
- 3DShader之移位贴图(Displacement Mapping)
- 3DShader之投影网格(Projected Grid)
- 3D引擎:Horde3D:读取Shader文件流程(二)
- Qt3D Shader
- untiy 3d ShaderLab_第 2 章Unity中Shader(着色器)的形态_3_Shader的数据接口:属性和 uniform变量
- Shader和3D图形渲染管线(Render Pipeline)
- Unity 3D 残影的研究 截取当前模型的网格材质并显示
- 火云开发课堂 - 《Shader从入门到精通》系列 第十九节:在Shader中实现3D模型的UV动画
- 提供一套Unity3D双面材质Shader