Skip to content

Instantly share code, notes, and snippets.

@tracer8
Forked from PixelScream/GravityWarp.shader
Created May 18, 2018 02:39
Show Gist options
  • Save tracer8/3650a57467d6bd415ba41903758b8e50 to your computer and use it in GitHub Desktop.
Save tracer8/3650a57467d6bd415ba41903758b8e50 to your computer and use it in GitHub Desktop.
Gravity Warp Shader
Shader "Unlit/GravityWarp"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
}
SubShader
{
Tags {
"RenderType"="Opaque"
//"DisableBatching"="True"
}
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
float3 normal : NORMAL;
};
struct v2f
{
float2 uv : TEXCOORD0;
float4 vertex : SV_POSITION;
float3 sphereMask :TEXCOORD1;
float3 posWorld : TEXCOORD2;
float3 normal : NORMAL;
};
sampler2D _MainTex;
float3 _GravityPoint;
float _GravityMulitplier; float _GravityDistance;
v2f vert (appdata v)
{
v2f o;
//float3 localGravityPoint = mul(unity_ObjectToWorld, _GravityPoint).rgb;
o.posWorld = mul(unity_ObjectToWorld, v.vertex).xyz;
float3 dir = normalize(o.posWorld - _GravityPoint);
float dist = distance(o.posWorld, _GravityPoint);
dist = saturate(1 - (dist * _GravityDistance) );
// TODO clamp to center point
v.vertex.rgb += dir * dist * _GravityMulitplier;
o.sphereMask = dist;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = v.uv;
o.normal = v.normal;
return o;
}
fixed4 frag (v2f i) : SV_Target
{
fixed4 c = tex2D (_MainTex, i.posWorld.xy) ;
c = lerp(c, tex2D (_MainTex, i.posWorld.yz), i.normal.x * i.normal.x) ;
c = lerp(c, tex2D (_MainTex, i.posWorld.xz), i.normal.y * i.normal.y) ;
c.rgb -= 0.4;
c.rgb = saturate(c.rgb - i.sphereMask * (1-_GravityMulitplier) * 0.333);
return c;
}
ENDCG
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[ExecuteInEditMode]
public class GravityWarpSetter : MonoBehaviour {
int id;
public float multiplier = 5, distance = 5;
void Start () {
id = Shader.PropertyToID("_GravityPoint");
Set();
}
void Update () {
Shader.SetGlobalVector(id, transform.position);
}
void OnValidate()
{
Set();
}
public void Set()
{
Shader.SetGlobalFloat("_GravityMulitplier", multiplier);
Shader.SetGlobalFloat("_GravityDistance", 1 / distance);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment