Last active
March 14, 2024 16:32
-
-
Save baobao/16dd0c5ee430c15f8ff0c8a8817e683b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
public class Noise2 : MonoBehaviour | |
{ | |
Shader m_shader; | |
Material m_mat; | |
[Range(0,1)] | |
public float horizonValue; | |
void OnRenderImage(RenderTexture src, RenderTexture dest) | |
{ | |
if (m_mat == null) | |
{ | |
m_shader = Shader.Find("Hidden/Noise2"); | |
m_mat = new Material(m_shader); | |
m_mat.hideFlags = HideFlags.DontSave; | |
} | |
// ランダムシード値を更新することで乱数を動かす | |
m_mat.SetInt("_Seed", Time.frameCount); | |
// 左右にずらす値をセット | |
m_mat.SetFloat("_HorizonValue", horizonValue); | |
Graphics.Blit(src, dest, m_mat); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Shader "Hidden/Noise2" | |
{ | |
Properties | |
{ | |
_MainTex ("Texture", 2D) = "white" {} | |
} | |
CGINCLUDE | |
#include "UnityCG.cginc" | |
sampler2D _MainTex; | |
// 0 ~ 1 | |
float _HorizonValue = 1; | |
// 乱数シード | |
int _Seed; | |
// 乱数生成 | |
// http://neareal.net/index.php?ComputerGraphics%2FHLSL%2FCommon%2FGenerateRandomNoiseInPixelShader | |
float rnd(float2 value, int Seed) | |
{ | |
return frac(sin(dot(value.xy, float2(12.9898, 78.233)) + Seed) * 43758.5453); | |
} | |
fixed4 frag (v2f_img i) : SV_Target | |
{ | |
float rndValue = rnd(i.uv, _Seed); | |
// -1 or 1 左右どちらにずれるかランダムにする | |
int tmp = step(rndValue, 0.5)*2 - 1; | |
// ピクセルジャンプ値 | |
float rndU = _HorizonValue * tmp * rndValue; | |
float2 uv = float2(frac(i.uv.x + rndU), i.uv.y); | |
fixed4 col = tex2D(_MainTex, uv); | |
return col; | |
} | |
ENDCG | |
SubShader | |
{ | |
Pass | |
{ | |
CGPROGRAM | |
#pragma vertex vert_img | |
#pragma fragment frag | |
ENDCG | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment