unity3D中要怎样制作表现玻璃和镜面材质?
来源:学生作业帮 编辑:百度作业网作业帮 分类:综合作业 时间:2024/08/17 19:20:43
unity3D中要怎样制作表现玻璃和镜面材质?
![unity3D中要怎样制作表现玻璃和镜面材质?](/uploads/image/z/3121967-47-7.jpg?t=unity3D%E4%B8%AD%E8%A6%81%E6%80%8E%E6%A0%B7%E5%88%B6%E4%BD%9C%E8%A1%A8%E7%8E%B0%E7%8E%BB%E7%92%83%E5%92%8C%E9%95%9C%E9%9D%A2%E6%9D%90%E8%B4%A8%3F)
透明玻璃材质和镜面反射材质都是需要光影技术(Shader)配合的,属于特效材质.
转给你个车身、玻璃材质的shader:
Shader "Reflective/Glass" {
Properties {
_Color ("Main Color", Color) = (1,1,1,1)
_SpecColor ("Specular Color", Color) = (0.5, 0.5, 0.5, 1)
_Shininess ("Shininess", Range (0.01, 1)) = 0.078125
_ReflectColor ("Reflect Strength", Color) = (1,1,1,0.5)
_MainTex ("Base (RGB) Gloss (A)", 2D) = "white" {}
_Parallax ("Height", Range (0.005, 0.08)) = 0.02
_Cube ("Reflection Cubemap", Cube) = "_Skybox" { TexGen CubeReflect }
}
SubShader
{
LOD 300
Tags { "Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent"}
Blend one OneMinusDstColor
ZWrite Off
// First pass does reflection cubemap
Pass
{
Name "BASE"
Tags {"LightMode" = "Always"}
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma fragmentoption ARB_fog_exp2
#pragma fragmentoption ARB_precision_hint_fastest
#include "UnityCG.cginc"
struct v2f {
V2F_POS_FOG;
float2 uv : TEXCOORD0;
float3 I : TEXCOORD1;
};
uniform float4 _MainTex_ST;
v2f vert(appdata_tan v)
{
v2f o;
PositionFog( v.vertex, o.pos, o.fog );
o.uv = TRANSFORM_TEX(v.texcoord,_MainTex);
// calculate object space reflection vector
float3 viewDir = ObjSpaceViewDir( v.vertex );
float3 I = reflect( -viewDir, v.normal );
// transform to world space reflection vector
o.I = mul( (float3x3)_Object2World, I );
return o;
}
uniform sampler2D _MainTex;
uniform samplerCUBE _Cube;
uniform float4 _ReflectColor;
half4 frag (v2f i) : COLOR
{
half4 texcol = tex2D (_MainTex, i.uv);
half4 reflcol = texCUBE( _Cube, i.I );
reflcol *= texcol.a;
return reflcol * _ReflectColor;
}
ENDCG
}
UsePass "Parallax Specular/PPL"
}
FallBack "Reflective/VertexLit", 1
}
Shader "Reflective/Glass" {
Properties {
_Color ("Main Color", Color) = (1,1,1,1)
_SpecColor ("Specular Color", Color) = (0.5, 0.5, 0.5, 1)
_Shininess ("Shininess", Range (0.01, 1)) = 0.078125
_ReflectColor ("Reflect Strength", Color) = (1,1,1,0.5)
_MainTex ("Base (RGB) Gloss (A)", 2D) = "white" {}
_Parallax ("Height", Range (0.005, 0.08)) = 0.02
_Cube ("Reflection Cubemap", Cube) = "_Skybox" { TexGen CubeReflect }
}
SubShader
{
LOD 300
Tags { "Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent"}
Blend one OneMinusDstColor
ZWrite Off
// First pass does reflection cubemap
Pass
{
Name "BASE"
Tags {"LightMode" = "Always"}
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma fragmentoption ARB_fog_exp2
#pragma fragmentoption ARB_precision_hint_fastest
#include "UnityCG.cginc"
struct v2f {
V2F_POS_FOG;
float2 uv : TEXCOORD0;
float3 I : TEXCOORD1;
};
uniform float4 _MainTex_ST;
v2f vert(appdata_tan v)
{
v2f o;
PositionFog( v.vertex, o.pos, o.fog );
o.uv = TRANSFORM_TEX(v.texcoord,_MainTex);
// calculate object space reflection vector
float3 viewDir = ObjSpaceViewDir( v.vertex );
float3 I = reflect( -viewDir, v.normal );
// transform to world space reflection vector
o.I = mul( (float3x3)_Object2World, I );
return o;
}
uniform sampler2D _MainTex;
uniform samplerCUBE _Cube;
uniform float4 _ReflectColor;
half4 frag (v2f i) : COLOR
{
half4 texcol = tex2D (_MainTex, i.uv);
half4 reflcol = texCUBE( _Cube, i.I );
reflcol *= texcol.a;
return reflcol * _ReflectColor;
}
ENDCG
}
UsePass "Parallax Specular/PPL"
}
FallBack "Reflective/VertexLit", 1
}
车身shader用Reflective/Specular中的一种
然后加入以下脚本:
RenderToCubemap.js:
// Attach this script to an object that uses a Reflective shader.
// Realtime reflective cubemaps!
@script ExecuteInEditMode
var cubemapSize = 128;
var oneFacePerFrame = false;
private var cam : Camera;
private var rtex : RenderTexture;
function Start ()
{
// render all six faces at startup
UpdateCubemap( 63 );
}
function LateUpdate ()
{
if (oneFacePerFrame)
{
var faceToRender = Time.frameCount % 6;
var faceMask = 1
转给你个车身、玻璃材质的shader:
Shader "Reflective/Glass" {
Properties {
_Color ("Main Color", Color) = (1,1,1,1)
_SpecColor ("Specular Color", Color) = (0.5, 0.5, 0.5, 1)
_Shininess ("Shininess", Range (0.01, 1)) = 0.078125
_ReflectColor ("Reflect Strength", Color) = (1,1,1,0.5)
_MainTex ("Base (RGB) Gloss (A)", 2D) = "white" {}
_Parallax ("Height", Range (0.005, 0.08)) = 0.02
_Cube ("Reflection Cubemap", Cube) = "_Skybox" { TexGen CubeReflect }
}
SubShader
{
LOD 300
Tags { "Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent"}
Blend one OneMinusDstColor
ZWrite Off
// First pass does reflection cubemap
Pass
{
Name "BASE"
Tags {"LightMode" = "Always"}
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma fragmentoption ARB_fog_exp2
#pragma fragmentoption ARB_precision_hint_fastest
#include "UnityCG.cginc"
struct v2f {
V2F_POS_FOG;
float2 uv : TEXCOORD0;
float3 I : TEXCOORD1;
};
uniform float4 _MainTex_ST;
v2f vert(appdata_tan v)
{
v2f o;
PositionFog( v.vertex, o.pos, o.fog );
o.uv = TRANSFORM_TEX(v.texcoord,_MainTex);
// calculate object space reflection vector
float3 viewDir = ObjSpaceViewDir( v.vertex );
float3 I = reflect( -viewDir, v.normal );
// transform to world space reflection vector
o.I = mul( (float3x3)_Object2World, I );
return o;
}
uniform sampler2D _MainTex;
uniform samplerCUBE _Cube;
uniform float4 _ReflectColor;
half4 frag (v2f i) : COLOR
{
half4 texcol = tex2D (_MainTex, i.uv);
half4 reflcol = texCUBE( _Cube, i.I );
reflcol *= texcol.a;
return reflcol * _ReflectColor;
}
ENDCG
}
UsePass "Parallax Specular/PPL"
}
FallBack "Reflective/VertexLit", 1
}
Shader "Reflective/Glass" {
Properties {
_Color ("Main Color", Color) = (1,1,1,1)
_SpecColor ("Specular Color", Color) = (0.5, 0.5, 0.5, 1)
_Shininess ("Shininess", Range (0.01, 1)) = 0.078125
_ReflectColor ("Reflect Strength", Color) = (1,1,1,0.5)
_MainTex ("Base (RGB) Gloss (A)", 2D) = "white" {}
_Parallax ("Height", Range (0.005, 0.08)) = 0.02
_Cube ("Reflection Cubemap", Cube) = "_Skybox" { TexGen CubeReflect }
}
SubShader
{
LOD 300
Tags { "Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent"}
Blend one OneMinusDstColor
ZWrite Off
// First pass does reflection cubemap
Pass
{
Name "BASE"
Tags {"LightMode" = "Always"}
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma fragmentoption ARB_fog_exp2
#pragma fragmentoption ARB_precision_hint_fastest
#include "UnityCG.cginc"
struct v2f {
V2F_POS_FOG;
float2 uv : TEXCOORD0;
float3 I : TEXCOORD1;
};
uniform float4 _MainTex_ST;
v2f vert(appdata_tan v)
{
v2f o;
PositionFog( v.vertex, o.pos, o.fog );
o.uv = TRANSFORM_TEX(v.texcoord,_MainTex);
// calculate object space reflection vector
float3 viewDir = ObjSpaceViewDir( v.vertex );
float3 I = reflect( -viewDir, v.normal );
// transform to world space reflection vector
o.I = mul( (float3x3)_Object2World, I );
return o;
}
uniform sampler2D _MainTex;
uniform samplerCUBE _Cube;
uniform float4 _ReflectColor;
half4 frag (v2f i) : COLOR
{
half4 texcol = tex2D (_MainTex, i.uv);
half4 reflcol = texCUBE( _Cube, i.I );
reflcol *= texcol.a;
return reflcol * _ReflectColor;
}
ENDCG
}
UsePass "Parallax Specular/PPL"
}
FallBack "Reflective/VertexLit", 1
}
车身shader用Reflective/Specular中的一种
然后加入以下脚本:
RenderToCubemap.js:
// Attach this script to an object that uses a Reflective shader.
// Realtime reflective cubemaps!
@script ExecuteInEditMode
var cubemapSize = 128;
var oneFacePerFrame = false;
private var cam : Camera;
private var rtex : RenderTexture;
function Start ()
{
// render all six faces at startup
UpdateCubemap( 63 );
}
function LateUpdate ()
{
if (oneFacePerFrame)
{
var faceToRender = Time.frameCount % 6;
var faceMask = 1