資源簡介
本資源是Unity技術應用,腳本直接應用在地面模型上,加上相對應的shader能達到即時地面反射效果,可調節精度。

代碼片段和文件信息
using?UnityEngine;
using?System.Collections;
//?This?is?in?fact?just?the?Water?script?from?Pro?Standard?Assets
//?just?with?refraction?stuff?removed.
[ExecuteInEditMode]?//?Make?mirror?live-update?even?when?not?in?play?mode
public?class?MirrorReflection?:?MonoBehaviour
{
public?enum?NormalAxes?{?X?=?0?Y=?1?Z=?2?}
public?NormalAxes?mirroraxes?=?NormalAxes.Z;
public?bool?m_DisablePixelLights?=?true;
????public?int?m_TextureSize?=?256;
????public?float?m_ClipPlaneOffset?=?0.01f;
????public?Matrix4x4?abc;
????public?layerMask?m_Reflectlayers?=?-1;
????????
????private?Hashtable?m_ReflectionCameras?=?new?Hashtable();?//?Camera?->?Camera?table
????
????private?RenderTexture?m_ReflectionTexture?=?null;
????private?int?m_OldReflectionTextureSize?=?0;
????
????private?static?bool?s_InsideRendering?=?false;
????//?This?is?called?when?it‘s?known?that?the?object?will?be?rendered?by?some
????//?camera.?We?render?reflections?and?do?other?updates?here.
????//?Because?the?script?executes?in?edit?mode?reflections?for?the?scene?view
????//?camera?will?just?work!
????public?void?OnWillRenderobject()
????{
????????if(?!enabled?||?!renderer?||?!renderer.sharedMaterial?||?!renderer.enabled?)
????????????return;
????????????
????????Camera?cam?=?Camera.current;
????????if(?!cam?)
????????????return;
????
????????//?Safeguard?from?recursive?reflections.????????
????????if(?s_InsideRendering?)
????????????return;
????????s_InsideRendering?=?true;
????????
????????Camera?reflectionCamera;
????????CreateMirrorobjects(?cam?out?reflectionCamera?);
????????
????????//?find?out?the?reflection?plane:?position?and?normal?in?world?space
//?鏡面法線,X軸=right,Y軸=up,Z軸=forward
????????Vector3?pos?=?transform.position;
Vector3?normal?=?transform.forward;
if(mirroraxes==NormalAxes.X)
normal?=?transform.right;
if(mirroraxes==NormalAxes.Y)
normal?=?transform.up;
????????//?Optionally?disable?pixel?lights?for?reflection
????????int?oldPixelLightCount?=?QualitySettings.pixelLightCount;
????????if(?m_DisablePixelLights?)
????????????QualitySettings.pixelLightCount?=?0;
????????
????????UpdateCameraModes(?cam?reflectionCamera?);
????????
????????//?Render?reflection
????????//?Reflect?camera?around?reflection?plane
????????float?d?=?-Vector3.Dot?(normal?pos)?-?m_ClipPlaneOffset;
????????Vector4?reflectionPlane?=?new?Vector4?(normal.x?normal.y?normal.z?d);
????
????????Matrix4x4?reflection?=?Matrix4x4.zero;
????????CalculateReflectionMatrix?(ref?reflection?reflectionPlane);
????????Vector3?oldpos?=?cam.transform.position;
????????Vector3?newpos?=?reflection.MultiplyPoint(?oldpos?);
????????reflectionCamera.worldToCameraMatrix?=?cam.worldToCameraMatrix?*?reflection;
????
????????//?Setup?oblique?projection?matrix?so?that?near?plane?is?our?reflection
????????//?plane.?This?way?we?clip?everything?below/above?it?for?free.
????????Vector4?clipPlane?=?CameraSpacePlane(?reflectionCamera?pos?normal?1.0f?);
????????Matrix4x4?projection?=?cam.projectionMatrix;
?????
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????目錄???????????0??2014-07-07?13:27??Unity地面反射腳本和shader\
?????文件????????4545??2014-07-07?13:27??Unity地面反射腳本和shader\MGKJ_ba
?????文件????????6200??2014-07-07?13:27??Unity地面反射腳本和shader\MGKJ_Cubemap_1.00.shader
?????文件????????5583??2014-07-07?13:27??Unity地面反射腳本和shader\MGKJ_Glass_ba
?????文件????????5140??2014-07-07?13:27??Unity地面反射腳本和shader\MGKJ_Mirror_ba
?????文件???????10426??2014-07-07?13:27??Unity地面反射腳本和shader\MirrorReflection.cs
?????文件?????1183888??2014-07-07?13:27??Unity地面反射腳本和shader\MyCubeMap.cubemap
?????文件?????1048620??2014-07-07?13:27??Unity地面反射腳本和shader\Normalmap_512.tga
?????文件?????????812??2014-07-07?13:27??Unity地面反射腳本和shader\Nromal_map_flat.tga
- 上一篇:硬件工程師 經典筆試面試題
- 下一篇:伺服電機的設計和特性
評論
共有 條評論