Skip to content

Instantly share code, notes, and snippets.

@khadzhynov
Created October 21, 2021 19:50
Show Gist options
  • Save khadzhynov/0effdd2aad87f29f09ba5cfc2b084a50 to your computer and use it in GitHub Desktop.
Save khadzhynov/0effdd2aad87f29f09ba5cfc2b084a50 to your computer and use it in GitHub Desktop.
Psychodelic Sky Exercise
Shader "GG/Skybox/CubemapPsychodelic"
{
Properties
{
_Tint("Tint Color", Color) = (.5, .5, .5, .5)
_Color1 ("Color1", Color) = (1, .5, .5, .5)
_Color2 ("Color2", Color) = (.5, .5, 1, .5)
[Gamma] _Exposure("Exposure", Range(0, 8)) = 1.0
_Rotation("Rotation", Range(0, 360)) = 0
[NoScaleOffset] _Tex("Cubemap (HDR)", Cube) = "grey" {}
_DistortionMap("Distortion Map", 2D) = "grey" {}
_VOffset("V Offset", Range(-1, 1)) = 0
_VScale("V Scale", Range(-4, 4)) = 1
_DistortionAmount("DistortionAMount", Range(-4, 4)) = 0
_ShiftHue("Shift hue", Range(0, 360)) = 0
_ShiftSaturation("Shift saturation", Range(0, 5)) = 1
_ShiftValue("Shift value", Range(0, 5)) = 1
}
SubShader
{
Tags { "Queue" = "Background" "RenderType" = "Background" "PreviewType" = "Skybox" }
Cull Off ZWrite Off
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma target 2.0
#include "UnityCG.cginc"
samplerCUBE _Tex;
sampler2D _DistortionMap;
half _DistortionAmount;
half4 _Tex_HDR;
half4 _Tint;
half _Exposure;
float _Rotation;
fixed _VOffset;
fixed _VScale;
fixed4 _Color1;
fixed4 _Color2;
half _ShiftHue;
half _ShiftSaturation;
half _ShiftValue;
float3 RotateAroundYInDegrees(float3 vertex, float degrees)
{
float alpha = degrees * UNITY_PI / 180.0;
float sina, cosa;
sincos(alpha, sina, cosa);
float2x2 m = float2x2(cosa, -sina, sina, cosa);
return float3(mul(m, vertex.xz), vertex.y).xzy;
}
struct appdata_t {
float4 vertex : POSITION;
float2 texcoord : TEXCOORD0;
UNITY_VERTEX_INPUT_INSTANCE_ID
};
struct v2f {
float4 vertex : SV_POSITION;
float3 texcoord : TEXCOORD0;
float2 texcoordMap : TEXCOORD1;
UNITY_VERTEX_OUTPUT_STEREO
};
float2 Random2(float2 p)
{
return frac(sin(float2(dot(p,float2(117.12,341.7)),dot(p,float2(269.5,123.3))))*43458.5453);
}
float3 TransformHSV(float3 col, fixed dynamicShift)
{
float uAngle = _ShiftHue * 3.14 / 180 * _SinTime.z;
float wAngle = _ShiftHue * 3.14 / 180 * _CosTime.z;
float uMultipler = _ShiftValue * _ShiftSaturation * cos(uAngle + dynamicShift);
float wMultipler = _ShiftValue * _ShiftSaturation * sin(wAngle + dynamicShift);
float3 ret = col;
ret.r = (0.299 * _ShiftValue + 0.701 * uMultipler + 0.168 * wMultipler) * col.r
+ (0.587 * _ShiftValue - 0.587 * uMultipler + 0.330 * wMultipler) * col.g
+ (0.114 * _ShiftValue - 0.114 * uMultipler - 0.497 * wMultipler) * col.b;
ret.g = (0.299 * _ShiftValue - 0.299 * uMultipler - 0.328 * wMultipler) * col.r
+ (0.587 * _ShiftValue + 0.413 * uMultipler + 0.035 * wMultipler) * col.g
+ (0.114 * _ShiftValue - 0.114 * uMultipler + 0.292 * wMultipler) * col.b;
ret.b = (0.299 * _ShiftValue - 0.3 * uMultipler + 1.25 * wMultipler) * col.r
+ (0.587 * _ShiftValue - 0.588 * uMultipler - 1.05 * wMultipler) * col.g
+ (0.114 * _ShiftValue + 0.886 * uMultipler - 0.203 * wMultipler) * col.b;
return ret;
}
v2f vert(appdata_t v)
{
v2f o;
UNITY_SETUP_INSTANCE_ID(v);
UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);
float3 rotated = RotateAroundYInDegrees(v.vertex, _Rotation + _Time.x);
o.vertex = UnityObjectToClipPos(rotated);
o.texcoord = v.vertex.xyz;
o.texcoordMap = v.texcoord;
return o;
}
fixed4 frag(v2f i) : SV_Target
{
half2 random = Random2(i.texcoordMap.xy) * 0.01;
half scaler = lerp(2.5, 2, (abs(_SinTime.x / 3) + abs(_CosTime.x / 3)) / (2 + random.x));
half4 distortion = tex2D (_DistortionMap, fixed2(i.texcoordMap.x + _Time.x, i.texcoordMap.y + _Time.x) / scaler);
half4 distortion2 = tex2D (_DistortionMap, fixed2(i.texcoordMap.y - _Time.x + distortion.x +random.y, i.texcoordMap.x - _Time.x + distortion.y + random.x) / scaler);
fixed3 uv = fixed3(
i.texcoord.x,
i.texcoord.y * _VScale + _VOffset,
i.texcoord.z);
uv.x += distortion.r * _DistortionAmount;
uv.y += distortion.b * _DistortionAmount;
half4 tex = texCUBE(_Tex, uv);
half3 c = DecodeHDR(tex, _Tex_HDR);
c = lerp(_Color1, _Color2, uv.x) + lerp(_Color1, _Color2, uv.y);
c = c * _Tint.rgb * unity_ColorSpaceDouble.rgb;
c *= _Exposure;
fixed3 first = TransformHSV(c.rgb, distortion.b + random.y);
fixed3 second = TransformHSV(c.rgb, -distortion2.r + random.x);
c.rgb = first + second;
//c.rgb = TransformHSV(c.rgb, distortion.b + distortion2.r * 0.5);
return half4(c, 1);
}
ENDCG
}
}
Fallback Off
}
@khadzhynov
Copy link
Author

khadzhynov commented Oct 21, 2021

Please note: this code is a "quick-and-dirty" exercise, made just for fun. It's not performant and not polished.

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