Unity地面反射脚本和shader


本资源是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地面反射脚本和shaderMGKJ_base_1.02.shader
     文件        6200  2014-07-07 13:27  Unity地面反射脚本和shaderMGKJ_Cubemap_1.00.shader
     文件        5583  2014-07-07 13:27  Unity地面反射脚本和shaderMGKJ_Glass_base_1.00.shader
     文件        5140  2014-07-07 13:27  Unity地面反射脚本和shaderMGKJ_Mirror_base_1.00.shader
     文件       10426  2014-07-07 13:27  Unity地面反射脚本和shaderMirrorReflection.cs
     文件     1183888  2014-07-07 13:27  Unity地面反射脚本和shaderMyCubeMap.cubemap
     文件     1048620  2014-07-07 13:27  Unity地面反射脚本和shaderNormalmap_512.tga
     文件         812  2014-07-07 13:27  Unity地面反射脚本和shaderNromal_map_flat.tga

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件举报,一经查实,本站将立刻删除。

发表评论

评论列表(条)