u3d 镜面反射的效果
建议用ps新建一个rbg为106,106,107的淡黑色图作为这个东东的材质,透明度为40%
Shader "Custom / newMirror " {
Properties {
_MainTex ("Base (RGB)", 2D) = "white" {}
_ReflectionTex ("Reflection", 2D) = "white" { TexGen ObjectLinear }
}
// two texture cards: full thing
Subshader {
Pass {
SetTexture[_MainTex] { combine texture }
SetTexture[_ReflectionTex] { matrix [_ProjMatrix] combine texture * previous }
}
}
// fallback: just main texture
Subshader {
Pass {
SetTexture [_MainTex] { combine texture }
}
}
}
、、、、、、、然后新建一个cs文件 命名为newMirror.cs
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 bool m_DisablePixelLights = true;
public int m_TextureSize = 256;
public float m_ClipPlaneOffset = 0.07f;
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
Vector3 pos = transform.position;
Vector3 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;
CalculateObliqueMatrix (ref projection, clipPlane);
reflectionCamera.projectionMatrix = projection;
reflectionCamera.cullingMask = ~(1<<4) & m_ReflectLayers.value; // never render water layer
reflectionCamera.targetTexture = m_ReflectionTexture;
GL.SetRevertBackfacing (true);
reflectionCamera.transform.position = newpos;
Vector3 euler = cam.transform.eulerAngles;
reflectionCamera.transform.eulerAngles = new Vector3(0, euler.y, euler.z);
reflectionCamera.Render();
reflectionCamera.transform.position = oldpos;
GL.SetRevertBackfacing (false);
Material[] materials = renderer.sharedMaterials;
foreach( Material mat in materials ) {
if( mat.HasProperty("_ReflectionTex") )
mat.SetTexture( "_ReflectionTex", m_ReflectionTexture );
}
// Set matrix on the shader that transforms UVs from object space into screen
// space. We want to just project reflection texture on screen.
Matrix4x4 scaleOffset = Matrix4x4.TRS(
new Vector3(0.5f,0.5f,0.5f), Quaternion.identity, new Vector3(0.5f,0.5f,0.5f) );
Vector3 scale = transform.lossyScale;
Matrix4x4 mtx = transform.localToWorldMatrix * Matrix4x4.Scale( new Vector3(1.0f/scale.x, 1.0f/scale.y, 1.0f/scale.z) );
mtx = scaleOffset * cam.projectionMatrix * cam.worldToCameraMatrix * mtx;
foreach( Material mat in materials ) {
mat.SetMatrix( "_ProjMatrix", mtx );
}
// Restore pixel light count
if( m_DisablePixelLights )
QualitySettings.pixelLightCount = oldPixelLightCount;
s_InsideRendering = false;
}
// Cleanup all the objects we possibly have created
void OnDisable()
{
if( m_ReflectionTexture ) {
DestroyImmediate( m_ReflectionTexture );
m_ReflectionTexture = null;
}
foreach( DictionaryEntry kvp in m_ReflectionCameras )
DestroyImmediate( ((Camera)kvp.Value).gameObject );
m_ReflectionCameras.Clear();
}
private void UpdateCameraModes( Camera src, Camera dest )
{
if( dest == null )
return;
// set camera to clear the same way as current camera
dest.clearFlags = src.clearFlags;
dest.backgroundColor = src.backgroundColor;
if( src.clearFlags == CameraClearFlags.Skybox )
{
Skybox sky = src.GetComponent(typeof(Skybox)) as Skybox;
Skybox mysky = dest.GetComponent(typeof(Skybox)) as Skybox;
if( !sky || !sky.material )
{
mysky.enabled = false;
}
else
{
mysky.enabled = true;
mysky.material = sky.material;
}
}
// update other values to match current camera.
// even if we are supplying custom camera&projection matrices,
// some of values are used elsewhere (e.g. skybox uses far plane)
dest.farClipPlane = src.farClipPlane;
dest.nearClipPlane = src.nearClipPlane;
dest.orthographic = src.orthographic;
dest.fieldOfView = src.fieldOfView;
dest.aspect = src.aspect;
dest.orthographicSize = src.orthographicSize;
}
// On-demand create any objects we need
private void CreateMirrorObjects( Camera currentCamera, out Camera reflectionCamera )
{
reflectionCamera = null;
// Reflection render texture
if( !m_ReflectionTexture || m_OldReflectionTextureSize != m_TextureSize )
{
if( m_ReflectionTexture )
DestroyImmediate( m_ReflectionTexture );
m_ReflectionTexture = new RenderTexture( m_TextureSize, m_TextureSize, 16 );
m_ReflectionTexture.name = "__MirrorReflection" + GetInstanceID();
m_ReflectionTexture.isPowerOfTwo = true;
m_ReflectionTexture.hideFlags = HideFlags.DontSave;
m_OldReflectionTextureSize = m_TextureSize;
}
// Camera for reflection
reflectionCamera = m_ReflectionCameras[currentCamera] as Camera;
if( !reflectionCamera ) // catch both not-in-dictionary and in-dictionary-but-deleted-GO
{
GameObject go = new GameObject( "Mirror Refl Camera id" + GetInstanceID() + " for " + currentCamera.GetInstanceID(), typeof(Camera), typeof(Skybox) );
reflectionCamera = go.camera;
reflectionCamera.enabled = false;
reflectionCamera.transform.position = transform.position;
reflectionCamera.transform.rotation = transform.rotation;
reflectionCamera.gameObject.AddComponent("FlareLayer");
go.hideFlags = HideFlags.HideAndDontSave;
m_ReflectionCameras[currentCamera] = reflectionCamera;
}
}
// Extended sign: returns -1, 0 or 1 based on sign of a
private static float sgn(float a)
{
if (a > 0.0f) return 1.0f;
if (a < 0.0f) return -1.0f;
return 0.0f;
}
// Given position/normal of the plane, calculates plane in camera space.
private Vector4 CameraSpacePlane (Camera cam, Vector3 pos, Vector3 normal, float sideSign)
{
Vector3 offsetPos = pos + normal * m_ClipPlaneOffset;
Matrix4x4 m = cam.worldToCameraMatrix;
Vector3 cpos = m.MultiplyPoint( offsetPos );
Vector3 cnormal = m.MultiplyVector( normal ).normalized * sideSign;
return new Vector4( cnormal.x, cnormal.y, cnormal.z, -Vector3.Dot(cpos,cnormal) );
}
// Adjusts the given projection matrix so that near plane is the given clipPlane
// clipPlane is given in camera space. See article in Game Programming Gems 5.
private static void CalculateObliqueMatrix (ref Matrix4x4 projection, Vector4 clipPlane)
{
Vector4 q = projection.inverse * new Vector4(
sgn(clipPlane.x),
sgn(clipPlane.y),
1.0f,
1.0f
);
Vector4 c = clipPlane * (2.0F / (Vector4.Dot (clipPlane, q)));
// third row = clip plane - fourth row
projection[2] = c.x - projection[3];
projection[6] = c.y - projection[7];
projection[10] = c.z - projection[11];
projection[14] = c.w - projection[15];
}
// Calculates reflection matrix around the given plane
private static void CalculateReflectionMatrix (ref Matrix4x4 reflectionMat, Vector4 plane)
{
reflectionMat.m00 = (1F - 2F*plane[0]*plane[0]);
reflectionMat.m01 = ( - 2F*plane[0]*plane[1]);
reflectionMat.m02 = ( - 2F*plane[0]*plane[2]);
reflectionMat.m03 = ( - 2F*plane[3]*plane[0]);
reflectionMat.m10 = ( - 2F*plane[1]*plane[0]);
reflectionMat.m11 = (1F - 2F*plane[1]*plane[1]);
reflectionMat.m12 = ( - 2F*plane[1]*plane[2]);
reflectionMat.m13 = ( - 2F*plane[3]*plane[1]);
reflectionMat.m20 = ( - 2F*plane[2]*plane[0]);
reflectionMat.m21 = ( - 2F*plane[2]*plane[1]);
reflectionMat.m22 = (1F - 2F*plane[2]*plane[2]);
reflectionMat.m23 = ( - 2F*plane[3]*plane[2]);
reflectionMat.m30 = 0F;
reflectionMat.m31 = 0F;
reflectionMat.m32 = 0F;
reflectionMat.m33 = 1F;
}
}
u3d 镜面反射的效果相关推荐
- U3D人物名称跟随效果
1:游戏中常有人物名称跟随这种效果如下图所示是我在某游戏中截的图 如上如所示玩家的名称和称号会随着人物移动一起移动,那么这种效果如何用Unity实现呢 我这里提供一种思路 在人物预设中添加一个标记位这 ...
- 镜面反射与Phong模型
在许多情形中,与景物的内部距离相比,光源与景物之间的距离大得多,例如太阳光照射一个城市的情形.在这种情形中,可以假定从光源发出的照明作用在整个景物上是恒定的,可以完全不考虑由于距离所引起的变化. 下面 ...
- Unity_ShaderGraph初体验_简易的漫反射+镜面反射
^ ^ 准备 开始 漫反射 镜面反射 叠加 https://www.bilibili.com/video/BV1TW411e74C 感谢傅老师 准备 第一次使用.(Unity 2019.3.15 1. ...
- Jade ( Translucency / Subsurface Scattering ) Shader——玉石效果——SSS深度图实现
试验效果1: 下面是最简化的shader: Shader "RE/Jade" {Properties {_MainColor( "MainColor", Col ...
- 主板没有rgb接口怎么接灯_纯白信仰打造RGB主机,四件套提升100%性能-Thermaltake...
没有RGB就没有信仰是当代主机的普遍看法,为了满足用户的需求,不论是主板还是显卡亦或者机箱本身,支持ARGB灯光同步的产品向来最受欢迎. 以这个需求出发,一期机箱改造计划就这样开始了.主板.CPU散热 ...
- DirectX11 With Windows SDK--12 深度/模板状态、平面镜反射绘制
前言 深度/模板测试使用的是与后备缓冲区同等分辨率大小的缓冲区,每个元素的一部分连续位用于深度测试,其余的则用作模板测试.两个测试的目的都是为了能够根据深度/模板状态需求的设置来选择需要绘制的像素. ...
- 【LearnOpenGL】-PBR材质
PBR,或者用更通俗一些的称呼是指基于物理的渲染(Physically Based Rendering),它指的是一些在不同程度上都基于与现实世界的物理原理更相符的基本理论所构成的渲染技术的集合.正因 ...
- Three.js基础探寻七——Lamber材质与Phong材质
材质(Material)是独立于物体顶点信息之外的与渲染效果相关的属性.通过设置材质可以改变物体的颜色.纹理贴图.光照模式等. 本篇将介绍基本材质以及两种基于光照模型的材质(Lamber与Phong) ...
- Avoiding 16 Common OpenGL Pitfalls(避免 16 个常见的 OpenGL 陷阱)
前言 避免 16 个常见的 OpenGL 陷阱原文 Avoiding 16 Common OpenGL Pitfalls Copyright 1998, 1999 by Mark J. Kilgard ...
最新文章
- 华为云服务器安装win10系统,云服务器可以安装win10吗
- JavaScript 正则表达式(RegExp对象、属性、方法、String支持)
- LeetCode 1976. 到达目的地的方案数(迪杰斯特拉 Python 优先队列)
- android顶部导航高度,Android特效——————底部/顶部导航条(Fragment+ViewPaper+XTabLayout)...
- 利用unittest+ddt进行接口测试(二):使用yaml文件管理测试数据
- 上三角矩阵法Matlab,在MATLAB中重塑/变换上三角矩阵
- k易语言html导入超级列表框,易语言超级列表框导入TXT内容的方法
- 【Latex】一、TeX Live和TeXstudio安装及使用教程
- 氚云CRM产品的详细介绍
- 山东理工大学oj打字速度测试
- python 打开网页并截图_python 使用默认浏览器打开,截图内容,并识别内容
- android 人脸 动画表情包,天呐 原来动画角色的面部表情是这样做出来的
- ARToolKit在visual studio2013(win10)的环境配置
- python 内置函数 和 匿名函数
- 用js给自己照相并修图
- AK消防 注册消防工程师 要点汇总 建筑消防性能化防火设计
- AppWidget(桌面小部件)
- 20145339顿珠达杰 《信息安全系统设计基础》第0周学习总结
- rancher坏了或删除,继续使用k8s集群
- linux开发stm32
热门文章
- 莫比乌斯反演学习笔记
- 使用windows自带虚拟机---Hyper-V 管理器
- Androi--内容提供器
- xshell最多支持4个_3分钟苹果发布会:iPhone12支持5G、3个尺寸4种型号
- 通过对抗网络实现具有身份和姿态鲁棒性的表情识别
- 玩转AWS CloudWatch微信告警
- 【哈密顿图】算法分析
- 爱快虚拟机安装Windows
- win10清理_小学生都会:win10设置自动清理缓存和垃圾文件
- ImageMagick将多张图片拼接成一张图片_批量裁剪图片、拼接长图、添加水印不想打开ps,你可以试试这款软件...