Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
[Unity] 水面反射エフェクト2D
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityStandardAssets;
// Unity5.5.2f1
namespace TSKT
{
[RequireComponent(typeof(Camera))]
public class WaterMirror2D : UnityStandardAssets.ImageEffects.ImageEffectBase
{
Camera camera;
Camera Camera
{
get
{
return camera ?? (camera = GetComponent<Camera>());
}
}
[SerializeField]
Vector3 surfacePosition;
public Vector3 SurfacePosition
{
get
{
return surfacePosition;
}
set
{
surfacePosition = value;
}
}
[SerializeField]
[Range(0f, 1f)]
float alpha = 0f;
[SerializeField]
[Range(-10f, 10f)]
float gradation = 0.5f;
[SerializeField]
Color overlay = Color.clear;
[SerializeField]
[Range(0.01f, 10f)]
float compress = 1.5f;
int _OverlayPropertyId;
int _ParameterPropertyId;
void Awake()
{
_OverlayPropertyId = Shader.PropertyToID("_Overlay");
_ParameterPropertyId = Shader.PropertyToID("_Parameter");
}
void OnRenderImage(RenderTexture source, RenderTexture destination)
{
var pos = Camera.WorldToViewportPoint(surfacePosition);
material.SetColor(_OverlayPropertyId, overlay);
material.SetVector(_ParameterPropertyId, new Vector4(
pos.y,
alpha,
gradation,
compress
));
Graphics.Blit(source, destination, material);
}
}
}
Shader "TSKT/Hidden/WaterMirror2D"
{
Properties {
_MainTex ("Base (RGB)", 2D) = "white" {}
_Overlay("Overlay", Color) = (0,0,0,0)
// Height, Alpha, Gradation, Compress
_Parameter("Parameter", Vector) = (0.5, 0.0, 0.5, 1.5)
}
SubShader {
Pass {
ZTest Always Cull Off ZWrite Off
CGPROGRAM
#pragma vertex vert_img
#pragma fragment frag
#include "UnityCG.cginc"
uniform sampler2D _MainTex;
fixed4 _Overlay;
fixed4 _Parameter;
fixed4 frag (v2f_img i) : SV_Target
{
fixed _Height = _Parameter.x;
fixed _Alpha = _Parameter.y;
fixed _Gradation = _Parameter.z;
fixed _Compress = _Parameter.w;
fixed4 mainColor = tex2D(_MainTex, i.uv);
#if UNITY_UV_STARTS_AT_TOP
fixed y = 1.0 - i.uv.y;
#else
fixed y = i.uv.y;
#endif
if (y < _Height)
{
fixed distance = _Height - y;
fixed alpha = saturate(distance * _Gradation + _Alpha);
fixed wave = sin(distance * 25 - _Time.y) * 0.005;
fixed u = i.uv.x + wave;
fixed v = _Height + distance * _Compress;
#if UNITY_UV_STARTS_AT_TOP
v = 1.0 - v;
#endif
fixed4 color = tex2D(_MainTex, fixed2(u, v));
mainColor = lerp(mainColor, color, alpha);
mainColor.rgb = lerp(
mainColor.rgb,
_Overlay.rgb,
_Overlay.a);
}
return mainColor;
}
ENDCG
}
}
Fallback off
}
Owner

enue commented Feb 20, 2017

unity-5 5 0p4-64bit-world-1-1 unity-game22-pc_-mac-_-linux-standalone-dx11-2017_01_25-6_37_41

© UTJ/UCL

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment