unity3D中要怎样制作表现玻璃和unity 镜面反射材质材质

5129人阅读
unity(26)
经验(23)
首先着色器来自这里:/forum/read.php?tid=3162
没有账号的童鞋可以到这里下载这个着色器和脚本:/share/link?shareid=149713&uk=
具体使用方法看上面的发的地址。
如果你已经开始使用了这个着色器,就会发现一个严重的问题,那就是在用自己的模型上就会出现各种奇怪的问题,而且蛋疼的是在编辑模式下没问题,运行游戏就有问题!原帖的回帖中也有不少人提到这个问题。
具体我这里的表现是:1、材质变成了蓝色。2、镜面反射完全不正确。
我在大概两周的时间里我都没有找到解决办法,后来还到国外翻到了一个叫MirrorReflection2的着色器,后来发现也是一样,而且这个着色器根本就是有问题的,想知道有什么问题的童鞋就自己去研究吧。
搞技术的就是要解决问题。首先想到的办法是学习着色器自己修改,后来发现这是个坑,看了一些教程后发现都不知道从何开始。后来还是打算慢慢研究为什么在自己的模型上会出错,说实在,我到最后都没搞清楚为什么,可能是顶点法线什么的计算不对吧,不过没关系,最后还是找到了解决办法,倒不如说是偶然发现。
1、材质变成了蓝色。这个是在一个单独的场景里研究发现的,变成蓝色是因为摄像机的背景颜色就是蓝色的!把摄像机背景颜色改成黑色就没问题。
2、反射不正确。具体发现过程略过,其实只是一个很简单的道理。如果说内置Plane能正常反射,那么用内置Plane来反射不就好了。等等,我们之所以使用自己的模型就是因为镜面不是矩形的,用Plane该怎么弄?其实之前想过类型Mask之类的东西不过明显不现实,而其实简单的办法是共享材质。所谓镜面归根到底不过是对材质的修改,那么让Plane接收正确的反射,然后使用了相同材质的模型就得到了正确的反射,不过这个Plane该放哪?我们不希望在场景中看到它。放到镜子后面去不就好了,这里谁都看不到,而且还不会影响镜面效果。
需要注意的是,如果镜面模型很大,而场景进入的时候摄像机看到了模型而看不到Plane,那么模型的材质就是白色的,所以尽量把Plane拉伸到跟模型差不多才能解决这个问题,这大概算是这个方法的弊端,所以如果知道怎么完美的解决方案是最好不过,本文只是抛砖引玉之用。
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:252659次
积分:2956
积分:2956
排名:第10504名
原创:61篇
转载:13篇
评论:79条
(1)(1)(2)(2)(3)(1)(1)(1)(3)(1)(1)(1)(3)(1)(4)(1)(2)(8)(5)(7)(4)(17)(5)unity3D中怎么表现玻璃和镜面材质
透明玻璃材质和镜面反射材质都是需要光影技术(Shader)配合的,属于特效材质。
转给你个车身、玻璃材质的shader:
Shader "Reflective/Glass"
&&& Properties
_Color ("Main Color", Color) =
(1,1,1,1)&&
_SpecColor ("Specular Color", Color) = (0.5, 0.5, 0.5,
_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)) =
_Cube ("Reflection Cubemap", Cube) = "_Skybox" { TexGen CubeReflect
SubShader&&&
Tags { "Queue"="Transparent" "IgnoreProjector"="True"
"RenderType"="Transparent"}&&
OneMinusDstColor&&
ZWrite Off&&
&&&&&&&&&&
// First pass does reflection
&&&&&&&&&&&
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)&&
&&& PositionFog(
v.vertex, o.pos, o.fog );&&
&&& o.uv =
TRANSFORM_TEX(v.texcoord,_MainTex);&&
&&& // calculate
object space reflection
&&& float3
viewDir = ObjSpaceViewDir( v.vertex
&&& float3 I =
reflect( -viewDir, v.normal
&&& // transform
to world space reflection
&&& o.I = mul(
(float3x3)_Object2World, I
&&& return
uniform sampler2D
uniform samplerCUBE
uniform float4
_ReflectC&&
half4 frag (v2f i) :
&&& half4 texcol
= tex2D (_MainTex, i.uv);&&
reflcol = texCUBE( _Cube, i.I
&&& reflcol *=
texcol.a;&&
&&& return
reflcol * _ReflectC&&
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
Tags { "Queue"="Transparent" "IgnoreProjector"="True"
"RenderType"="Transparent"}
Blend one OneMinusDstColor
ZWrite Off
// First pass does reflection cubemap
&&&&&&&&&&&
Name "BASE"
&&&&&&&&&&&
Tags {"LightMode" = "Always"}
#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)
&&& PositionFog(
v.vertex, o.pos, o.fog );
&&& o.uv =
TRANSFORM_TEX(v.texcoord,_MainTex);
&&& // calculate
object space reflection
&&& float3
viewDir = ObjSpaceViewDir( v.vertex );
&&& float3 I =
reflect( -viewDir, v.normal );
&&& // transform
to world space reflection vector
&&& o.I = mul(
(float3x3)_Object2World, I );
&&& return
uniform sampler2D _MainT
uniform samplerCUBE _C
uniform float4 _ReflectC
half4 frag (v2f i) : COLOR
&&& half4 texcol
= tex2D (_MainTex, i.uv);
reflcol = texCUBE( _Cube, i.I );
&&& reflcol *=
&&& return
reflcol * _ReflectC
UsePass "Parallax Specular/PPL"
&&& FallBack
"Reflective/VertexLit", 1
车身shader用Reflective/Specular中的一种
然后加入以下脚本:
RenderToCubemap.js:
// Attach this script to an object that uses a Reflective
// Realtime reflective
cubemaps!&&
@script ExecuteInEditMode&&
var cubemapSize = 128;&&
var oneFacePerFrame =
private var cam : C&&
private var rtex :
function Start
&&& // render
all six faces at startup&&
UpdateCubemap( 63 );&&
function LateUpdate
(oneFacePerFrame)&&&
var faceToRender = Time.frameCount %
var faceMask = 1 &&
UpdateCubemap (faceMask);&&
UpdateCubemap (63); // all six
function UpdateCubemap (faceMask :
var go = new GameObject ("CubemapCamera",
Camera);&&
go.hideFlags =
HideFlags.HideAndDontS&&
go.transform.position =
transform.&&
go.transform.rotation =
Quaternion.&&
cam = go.&&
cam.farClipPlane = 100; // don't render very far into
cam.enabled =&&
(!rtex)&&&
rtex = new RenderTexture (cubemapSize, cubemapSize,
rtex.isPowerOfTwo =&&
rtex.isCubemap =&&
rtex.hideFlags =
HideFlags.HideAndDontS&&
renderer.sharedMaterial.SetTexture ("_Cube",
cam.transform.position =
transform.&&
cam.RenderToCubemap (rtex,
faceMask);&&
function OnDisable
DestroyImmediate (cam);&&
DestroyImmediate (rtex);&&
已投稿到:
以上网友发言只代表其个人观点,不代表新浪网的观点或立场。Unity3d&Shader(三)&Pass(Blending)带贴图的透明玻璃等
Blending is used to make transparent
混合被用于制作透明物体。
<img ALT="Blend" src="/blog7style/images/common/sg_trans.gif" real_src ="/Components/Images/SL-Blend-0.jpg" NAME="image_operate_26169"
TITLE="Unity3d&Shader(三)&Pass(Blending)带贴图的透明玻璃等" />
When graphics are rendered, after all shaders have executed and
all textures have been applied, the pixels are written to the
screen. How they are combined with what is already there is
controlled by the Blend command.
当图像被渲染时,所有着色器被执行以后,所有贴图被应用后,像素将被写到屏幕。他们是如何通过Blend命令的控制和已有的图像合并呢?
Syntax 语法
Turn off blending 关闭混合
Blend SrcFactor DstFactor
Configure & enable blending. The generated
color is multiplied by the SrcFactor. The color
already on screen is multiplied by DstFactor and
the two are added together.
配置并启动混合。产生的颜色被乘以SrcFactor.
已存在于屏幕的颜色乘以DstFactor,并且两者将被叠加在一起。
Blend SrcFactor DstFactor, SrcFactorA
DstFactorA
Same as above, but use different factors for blending the alpha
同上,但是使用不同的要素来混合alpha通道
BlendOp Min | Max | Sub | RevSub
Instead of adding blended colors together, do a different
operation on them.
不是添加混合颜色在一起,而是对它们做不同的操作。
Properties 属性
All following properties are valid for both SrcFactor
& DstFactor. Source refers to the
calculated color, Destination is the color already
on the screen.
以下所有属性对SrcFactor或DstFactor都可用。Source指的是被计算的颜色,Destination是已经在屏幕上的颜色。
The value of one - use this to let either the
source or the destination color come through fully.
值为1,使用此设置来让源或是目标颜色完全的通过。
The value zero - use this to remove either the
source or the destination values.
值为0,使用此设置来删除源或目标值。
The value of this stage is multiplied by the
source color value.
此阶段的值是乘以源颜色的值。
The value of this stage is multiplied by the
source alpha value.
此阶段的值是乘以源alpha的值。
The value of this stage is multiplied by frame
buffer source color value.
此阶段的值是乘以帧缓冲区源颜色的值。
The value of this stage is multiplied by frame
buffer source alpha value.
此阶段的值是乘以帧缓冲区源alpha的值。
OneMinusSrcColor
The value of this stage is multiplied by (1 -
source color).
此阶段的值是乘以(1 - source
OneMinusSrcAlpha
The value of this stage is multiplied by (1 -
source alpha).
此阶段的值是乘以(1 - source
OneMinusDstColor
The value of this stage is multiplied by (1 -
destination color).
此阶段的值是乘以(1 - destination
OneMinusDstAlpha
The value of this stage is multiplied by (1 -
destination alpha).
此阶段的值是乘以(1 - destination
Details 细节
Below are the most common blend types:
以下是最常见的混合类型:
Blend SrcAlpha OneMinusSrcAlpha
// Alpha blending
Blend One One
// Additive
Blend One OneMinusDstColor
// Soft Additive
Blend DstColor Zero
// Multiplicative
Blend DstColor SrcColor
// 2x Multiplicative
Example 示例
Here is a small example shader that adds a texture to whatever
is on the screen already:
这里是一个着色器的小例子,添加一个纹理,无论是否已在屏幕上:
Shader "Simple Additive" {
&&& Properties
_MainTex ("Texture to blend", 2D) = "black" {}
&&& SubShader
Tags { "Queue" = "Transparent" }
&&&&&&&&&&&
Blend One One
&&&&&&&&&&&
SetTexture [_MainTex] { combine texture }
And a more complex one, Glass. This is a two-pass shader:
更复杂的一个例子,玻璃。这是一个two-pass着色器:
The first pass renders a lit, alpha-blended texture on to the
screen. The alpha channel decides the transparency.
第一个pass渲染光照、alpha混合纹理到屏幕上。Alpha通道决定的透明度。
The second pass renders a reflection cubemap on top of the
alpha-blended window, using additive transparency.
第二个pass渲染在alpha混合窗口顶部一个反射立方体贴图,使用附加透明度。
Shader "Custom/e9" {
&// 此效果可用于玻璃上面的附加文字,图案,比如透明显示器 //
&Properties{
&&_Color ("Main Color", Color) =
&&_MainTex ("Base (RGB)
Transparency(A)", 2D) = "white" {}
&&_Reflections ("Base (RGB)
Gloss(A)", Cube) = "skybox" {TexGen CubeReflect}
&SubShader{
&&Tags {"Queue" =
"Transparent"}
&&&// 混合透明,让物体透明
SrcAlpha OneMinusSrcAlpha
&&&// 主要颜色
&&&Material{
&&&&Diffuse
&&&Lighting
&&&// 材质
&&&SetTexture
[_MainTex] {
&&&&combine
texture * primary double, texture * primary
&&&// 镜面反射透明
&&&Blend One
&&&// 主颜色可不加
&&&Material{
&&&&Diffuse
&&&Lighting
&&&// 反射天空色
&&&SetTexture
[_Reflections]{
&&&&combine
&&&&Matrix
[_Reflection]
已投稿到:
以上网友发言只代表其个人观点,不代表新浪网的观点或立场。

我要回帖

更多关于 黑色镜面玻璃材质参数 的文章

 

随机推荐