水shader效果.docx
- 文档编号:30622852
- 上传时间:2023-08-18
- 格式:DOCX
- 页数:22
- 大小:16.24KB
水shader效果.docx
《水shader效果.docx》由会员分享,可在线阅读,更多相关《水shader效果.docx(22页珍藏版)》请在冰豆网上搜索。
水shader效果
Shader"FX/Water"{
Properties{
_WaveScale("Wavescale",Range(0.02,0.15))=0.063
_ReflDistort("Reflectiondistort",Range(0,1.5))=0.44
_RefrDistort("Refractiondistort",Range(0,1.5))=0.40
_RefrColor("Refractioncolor",COLOR)=(.34,.85,.92,1)
_Fresnel("Fresnel(A)",2D)="gray"{}
_BumpMap("Bumpmap(RGB)",2D)="bump"{}
WaveSpeed("Wavespeed(map1x,y;map2x,y)",Vector)=(19,9,-16,-7)
_ReflectiveColor("Reflectivecolor(RGB)fresnel(A)",2D)=""{}
_ReflectiveColorCube("Reflectivecolorcube(RGB)fresnel(A)",Cube)=""{TexGenCubeReflect}
_HorizonColor("Simplewaterhorizoncolor",COLOR)=(.172,.463,.435,1)
_MainTex("Fallbacktexture",2D)=""{}
_ReflectionTex("InternalReflection",2D)=""{}
_RefractionTex("InternalRefraction",2D)=""{}
}
//-----------------------------------------------------------
//Fragmentprogramcards
Subshader{
Tags{"WaterMode"="Refractive""RenderType"="Opaque"}
Pass{
CGPROGRAM
#pragmavertexvert
#pragmafragmentfrag
#pragmafragmentoptionARB_precision_hint_fastest
#pragmafragmentoptionARB_fog_exp2
#pragmamulti_compileWATER_REFRACTIVEWATER_REFLECTIVEWATER_SIMPLE
#ifdefinedWATER_REFLECTIVE||definedWATER_REFRACTIVE
#defineHAS_REFLECTION1
#endif
#ifdefinedWATER_REFRACTIVE
#defineHAS_REFRACTION1
#endif
#include"UnityCG.cginc"
uniformfloat4_WaveScale4;
uniformfloat4_WaveOffset;
#ifdefHAS_REFLECTION
uniformfloat_ReflDistort;
#endif
#ifdefHAS_REFRACTION
uniformfloat_RefrDistort;
#endif
structappdata{
float4vertex:
POSITION;
float3normal:
NORMAL;
};
structv2f{
V2F_POS_FOG;
#ifdefinedHAS_REFLECTION||definedHAS_REFRACTION
float3ref;
#endif
float2bumpuv[2];
float3viewDir;
};
v2fvert(appdatav)
{
v2fo;
PositionFog(v.vertex,o.pos,o.fog);
//scrollbumpwaves
float4temp;
temp.xyzw=v.vertex.xzxz*_WaveScale4+_WaveOffset;
o.bumpuv[0]=temp.xy;
o.bumpuv[1]=temp.wz;
//objectspaceviewdirection(willnormalizeperpixel)
o.viewDir.xzy=ObjSpaceViewDir(v.vertex);
#ifdefinedHAS_REFLECTION||definedHAS_REFRACTION
//calculatethereflectionvector
float3x4mat=float3x4(
0.5,0,0,0.5,
0,0.5*_ProjectionParams.x,0,0.5,
0,0,0,1
);
o.ref=mul(mat,o.pos);
#endif
returno;
}
#ifdefinedWATER_REFLECTIVE||definedWATER_REFRACTIVE
sampler2D_ReflectionTex;
#endif
#ifdefinedWATER_REFLECTIVE||definedWATER_SIMPLE
sampler2D_ReflectiveColor;
#endif
#ifdefinedWATER_REFRACTIVE
sampler2D_Fresnel;
sampler2D_RefractionTex;
uniformfloat4_RefrColor;
#endif
#ifdefinedWATER_SIMPLE
uniformfloat4_HorizonColor;
#endif
sampler2D_BumpMap;
half4frag(v2fi):
COLOR
{
i.viewDir=normalize(i.viewDir);
//combinetwoscrollingbumpmapsintoone
half3bump1=tex2D(_BumpMap,i.bumpuv[0]).rgb;
half3bump2=tex2D(_BumpMap,i.bumpuv[1]).rgb;
half3bump=bump1+bump2-1;
//fresnelfactor
halffresnelFac=dot(i.viewDir,bump);
//perturbreflection/refractionUVsbybumpmap,andlookupcolors
#ifdefHAS_REFLECTION
float3uv1=i.ref;uv1.xy+=bump*_ReflDistort;
half4refl=tex2Dproj(_ReflectionTex,uv1);
#endif
#ifdefHAS_REFRACTION
float3uv2=i.ref;uv2.xy-=bump*_RefrDistort;
half4refr=tex2Dproj(_RefractionTex,uv2)*_RefrColor;
#endif
//finalcolorisbetweenrefractedandreflectedbasedonfresnel
half4color;
#ifdefWATER_REFRACTIVE
halffresnel=tex2D(_Fresnel,float2(fresnelFac,fresnelFac)).a;
color=lerp(refr,refl,fresnel);
#endif
#ifdefWATER_REFLECTIVE
half4water=tex2D(_ReflectiveColor,float2(fresnelFac,fresnelFac));
color.rgb=lerp(water.rgb,refl.rgb,water.a);
color.a=refl.a*water.a;
#endif
#ifdefWATER_SIMPLE
half4water=tex2D(_ReflectiveColor,float2(fresnelFac,fresnelFac));
color.rgb=lerp(water.rgb,_HorizonColor.rgb,water.a);
color.a=_HorizonColor.a;
#endif
returncolor;
}
ENDCG
}
}
//-----------------------------------------------------------
//Radeon9000cards
Subshader{
Tags{"WaterMode"="Reflective""RenderType"="Opaque"}
Pass{
CGPROGRAM
#pragmavertexvert
#include"UnityCG.cginc"
uniformfloat4_WaveScale4;
uniformfloat4_WaveOffset;
uniformfloat_ReflDistort;
structappdata{
float4vertex:
POSITION;
float3normal:
NORMAL;
};
structv2f{
V2F_POS_FOG;
float2bumpuv[2]:
TEXCOORD0;
float3viewDir:
TEXCOORD2;
float4ref:
TEXCOORD3;
};
v2fvert(appdatav)
{
v2fo;
PositionFog(v.vertex,o.pos,o.fog);
//scrollbumpwaves
float4temp;
temp.xyzw=v.vertex.xzxz*_WaveScale4+_WaveOffset;
o.bumpuv[0]=temp.xy;
o.bumpuv[1]=temp.wz;
//objectspaceviewdirection
o.viewDir.xzy=normalize(ObjSpaceViewDir(v.vertex));
//calculatethereflectionvector
float4x4mat=float4x4(
.5,0,0,.5,
0,.5*_ProjectionParams.x,0,.5,
0,0,.5,.5,
0,0,0,1
);
o.ref=mul(mat,o.pos);
returno;
} //Unity3D教程手册:
ENDCG
Program""{
SubProgram{
Keywords{"WATER_REFLECTIVE""WATER_REFRACTIVE"}
SetTexture[_BumpMap]{2D}
SetTexture[_BumpMap]{2D}
SetTexture[_ReflectiveColor]{2D}
SetTexture[_ReflectionTex]{2D}
Local0,([_ReflDistort],0,0,0)
"!
!
ATIfs1.0
StartConstants;
CONSTANTc0=program.local[0];
EndConstants;
StartPrelimPass;
PassTexCoordr3,t3.stq_dq;#reflectionvector
SampleMapr0,t0.str;#bump1
SampleMapr1,t1.str;#bump2
PassTexCoordr2,t2.str;
ADDr1.half,r0.bias,r1.bias;#bump=bump1+bump2-1
DOT3r2,r1.2x,r2; #fresnel:
dot(bump,viewer-pos)
#addlessoffsetbecauseit'spurelyscreenspace;bigoneslookbad
MADr3.rg,r1,c0.r,r3; #uv+=bump*strength;addlessbecauseit'snotperspective
EndPass;
StartOutputPass;
SampleMapr3,r3.str; #reflectioncolor
SampleMapr2,r2.str; #watercolor/fresnel
LERPr0.rgb,r2.a,r3,r2;#betweenwaterandreflectedbasedonfresnel
MULr0.a,r3.a,r2.a;
EndPass;
"
}
SubProgram{
Keywords{"WATER_SIMPLE"}
SetTexture[_BumpMap]{2D}
SetTexture[_BumpMap]{2D}
SetTexture[_ReflectiveColor]{2D}
Local0,[_HorizonColor]
"!
!
ATIfs1.0
StartConstants;
CONSTANTc0=program.local[0];
EndConstants;
StartPrelimPass;
SampleMapr0,t0.str;
SampleMapr1,t1.str;
PassTexCoordr2,t2.str;
ADDr1,r0.bias,r1.bias;#bump=bump1+bump2-1
DOT3r2,r1,r2; #fresnel:
dot(bump,viewer-pos)
EndPass;
StartOutputPass;
SampleMapr2,r2.str;
LERPr0.rgb,r2.a,c0,r2;#fadeinreflection
MOVr0.a,c0.a;
EndPass;
"
}
}
}
}
//-----------------------------------------------------------
//Oldcards
//threetexture,cubemaps
Subshader{
Tags{"WaterMode"="Simple""RenderType"="Opaque"}
Pass{
Color(0.5,0.5,0.5,0.5)
SetTexture[_MainTex]{
Matrix[_WaveMatrix]
combinetexture*primary
}
SetTexture[_MainTex]{
Matrix[_WaveMatrix2]
combinetexture*primary+previous
}
SetTexture[_ReflectiveColorCube]{
combinetexture+-previous,primary
Matrix[_Reflection]
}
} //Unity3D教程:
}
//dualtexture,cubemaps
Subshader{
Tags{"WaterMode"="Simple""RenderType"="Opaque"}
Pass{
Color(0.5,0.5,0.5,0.5)
SetTexture[_MainTex]{
Matrix[_WaveMatrix]
combinetexture
}
SetTexture[_ReflectiveColorCube]{
combinetexture+-previous,primary
Matrix[_Reflection]
}
}
}
//singletexture
Subshader{
Tags{"WaterMode"="Simple""RenderType"="Opaque"}
Pass{
Color(0.5,0.5,0.5,0)
SetTexture[_MainTex]{
Matrix[_WaveMatrix]
combinetexture,primary
}
}
}
}
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- shader 效果