Unity水效果

来源:互联网 发布:polyfit函数在c语言 编辑:程序博客网 时间:2024/06/05 19:28
由于Unity自带的水用起来实在不方便,而且效果一般,没办法只好自己重新写了一个,最终出来的效果感觉还想,如果觉得效果不好请勿喷。先上图,预览一下效果!
Unity水效果 - Util - Util

Shader:

Shader "Util's Shader/Water/Water Real"
{
Properties
{
_Color("Main Color", Color) = (1,1,1,1)
_SpecColor("Specular Color", Color) = (1,1,1,1)
_Shininess("Shininess", Range(0.01,5) ) = 0.01
_MainTex("Base (RGB) Gloss (A)", 2D) = "white" {}
_BumpMap("Normalmap", 2D) = "bump" {}
_RefractionColor("Refraction Color", Color ) = (1,1,1,1)
_RefractionDis("Refraction Distortion", Range(0,1) ) = 1
_ReflectColor("Reflection Color", Color) = (1,1,1,1)
_ReflectionDis("Reflection Distortion", Range(0,1) ) = 1
_ReflectionTex("Reflection Texture", 2D) = "black" {}
_Fresnel("Fresnel", Range(0,1) ) = 0
_WaveSpeed("Wave speed (map1 x,y; map2 x,y)", Vector) = (0.2,0.2,1,0)
}

SubShader
{
GrabPass {
Name "BASE"
Tags { "LightMode" = "Always" }
}
Tags { "Queue"="Transparent" "IgnoreProjector"="False" "RenderType"="Opaque" }
Cull Back
Lighting Off
CGPROGRAM
#pragma surface surf BlinnPhongGlass dualforward
#pragma target 3.0

inline fixed4 LightingBlinnPhongGlass ( inout SurfaceOutput s, fixed3 lightDir, half3 viewDir, fixed atten)
{
half3 h = normalize (lightDir + viewDir);
fixed diff = max (0, dot (s.Normal*2, lightDir));
float nh = max (0, dot (s.Normal, h));
float spec = pow (nh, s.Specular*128.0) * s.Gloss;
fixed4 c;
c.rgb = (s.Albedo * _LightColor0.rgb * diff + _LightColor0.rgb * _SpecColor.rgb * spec) * (atten * 2);
c.a = s.Alpha + _LightColor0.a * _SpecColor.a * spec * atten;
s.Alpha = s.Alpha + c.a + c.rgb;
return c;
}

sampler2D _GrabTexture;
fixed4 _Color;
half _Shininess;
sampler2D _MainTex;
sampler2D _BumpMap;
fixed4 _ReflectColor;
sampler2D _ReflectionTex;
half _ReflectionDis;
fixed4 _RefractionColor;
half _RefractionDis;
half _Fresnel;
float4 _WaveSpeed;

struct Input {
float2 uv_MainTex;
float2 uv_BumpMap;
float3 viewDir;
float4 screenPos;
};

float2 MoveTex (float2 uv,float2 pan){
return float2(pan.x,pan.y*0.1)*_Time + uv;
}

half4 ClampColor (half4 cc) {
return half4(clamp(cc.r,0,1),clamp(cc.g,0,1),clamp(cc.b,0,1),clamp(cc.a,0,1));
}

void surf (Input IN, inout SurfaceOutput o) {
half3 bump0 = UnpackNormal(tex2D(_BumpMap,MoveTex(IN.uv_BumpMap,_WaveSpeed.xy)));
half3 bump1 = UnpackNormal(tex2D(_BumpMap,MoveTex(IN.uv_BumpMap,_WaveSpeed.zw)));
half3 bump = (bump0 + bump1) * 0.5;

o.Normal = bump;

float2 screenPos = IN.screenPos.xy/IN.screenPos.w;
float2 refractionUV = screenPos + o.Normal.xy * _RefractionDis;
refractionUV = float2(refractionUV.x,1-refractionUV.y);
half4 refraction = tex2D(_GrabTexture,refractionUV) * _RefractionColor;

half4 tex0 = tex2D(_MainTex,MoveTex(IN.uv_MainTex,_WaveSpeed.xy));
half4 tex1 = tex2D(_MainTex,MoveTex(IN.uv_MainTex,_WaveSpeed.zw));
half4 tex = (tex0 + tex1) * 0.5;
half4 difColor = _Color - _ReflectColor;
difColor = ClampColor(difColor);
half4 diffuseCol = difColor * tex;

float fresnel = pow(abs(1.0 - dot(normalize(IN.viewDir.xyz), normalize(o.Normal))),_Fresnel);
o.Alpha = fresnel * _Color.a ;

o.Albedo = diffuseCol.rgb * o.Alpha;

o.Specular = _Shininess;
o.Gloss = 1;

float2 reflectionUV = screenPos + o.Normal.xy * _ReflectionDis;
half4 reflcol = _ReflectColor * tex2D (_ReflectionTex,reflectionUV);
o.Emission = reflcol.rgb * o.Alpha + refraction.rgb * (1-o.Alpha);

}
ENDCG
}
Fallback "Diffuse"
}

配合的代码是用的Unity自带的代码:

using UnityEngine;
using System.Collections;


[ExecuteInEditMode]
public class MyWater : MonoBehaviour
{
public enum MirrorAxis {
X,Y,Z
}
public MirrorAxis mirrorAxis = MirrorAxis.X;
public bool m_ReverseAxis = false;
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_InsideWater = false;


public void OnWillRenderObject()
{
if( !enabled || !renderer || !renderer.sharedMaterial || !renderer.enabled )
return;

Camera cam = Camera.current;
if( !cam )
return;

if( s_InsideWater )
return;
s_InsideWater = true;


Camera reflectionCamera;
CreateWaterObjects( cam, out reflectionCamera );

Vector3 pos = transform.position;
Vector3 normal = transform.up;
switch (mirrorAxis){
case MirrorAxis.X:
normal = transform.right;
break;
case MirrorAxis.Y:
normal = transform.up;
break;
case MirrorAxis.Z:
normal = transform.forward;
break;
}

if (m_ReverseAxis) normal *= -1;

int oldPixelLightCount = QualitySettings.pixelLightCount;
if( m_DisablePixelLights )
QualitySettings.pixelLightCount = 0;

UpdateCameraModes( cam, reflectionCamera );

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;


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(-euler.x, euler.y, euler.z);
reflectionCamera.Render();
reflectionCamera.transform.position = oldpos;
GL.SetRevertBackfacing (false);
renderer.sharedMaterial.SetTexture( "_ReflectionTex", m_ReflectionTexture );

if( m_DisablePixelLights )
QualitySettings.pixelLightCount = oldPixelLightCount;


s_InsideWater = false;
}


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;

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;
}
}

dest.farClipPlane = src.farClipPlane;
dest.nearClipPlane = src.nearClipPlane;
dest.orthographic = src.orthographic;
dest.fieldOfView = src.fieldOfView;
dest.aspect = src.aspect;
dest.orthographicSize = src.orthographicSize;
}


private void CreateWaterObjects( Camera currentCamera, out Camera reflectionCamera)
{

reflectionCamera = null;

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 = "__WaterReflection" + GetInstanceID();
m_ReflectionTexture.isPowerOfTwo = true;
m_ReflectionTexture.hideFlags = HideFlags.DontSave;
m_OldReflectionTextureSize = m_TextureSize;
}

reflectionCamera = m_ReflectionCameras[currentCamera] as Camera;
if( !reflectionCamera )
{
GameObject go = new GameObject( "Water 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;
}
}

private static float sgn(float a)
{
if (a > 0.0f) return 1.0f;
if (a < 0.0f) return -1.0f;
return 0.0f;
}

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) );
}

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)));

projection[2] = c.x - projection[3];
projection[6] = c.y - projection[7];
projection[10] = c.z - projection[11];
projection[14] = c.w - projection[15];
}


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;
}
}



Unity水效果 - Util - Util
 
0 0
原创粉丝点击