Skip to content

Instantly share code, notes, and snippets.

@partybusiness
Last active December 31, 2020 22:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save partybusiness/3d2ca7cfa9b0b7d918c8e26b4898cc18 to your computer and use it in GitHub Desktop.
Save partybusiness/3d2ca7cfa9b0b7d918c8e26b4898cc18 to your computer and use it in GitHub Desktop.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class JokerTeleport : MonoBehaviour
{
[SerializeField]
SkinnedMeshRenderer displayMesh;
[SerializeField]
Transform jokerTransform;
[SerializeField]
Material normalMaterial;
[SerializeField]
Material teleportMaterial;
[SerializeField]
float teleportLength = 0.1f;
void Start()
{
teleportMaterial = new Material(teleportMaterial); //duplicate so changes to it don't cause trouble in editor
normalMaterial = displayMesh.sharedMaterial;
}
private void Update()
{
if (Input.GetKeyDown(KeyCode.T))
{
StartCoroutine(TeleportTo(transform.position + Vector3.forward*-100f));
}
}
IEnumerator TeleportTo(Vector3 newPosition)
{
displayMesh.sharedMaterial = teleportMaterial;
var i = 0f;
var progressID = Shader.PropertyToID("_TeleportProgress");
teleportMaterial.SetFloat(progressID, 0);
while (i <= teleportLength)
{
teleportMaterial.SetFloat(progressID, i / teleportLength);
yield return new WaitForEndOfFrame();
i += Time.deltaTime;
}
jokerTransform.position = newPosition;
while (i >= 0)
{
teleportMaterial.SetFloat(progressID, i / teleportLength);
yield return new WaitForEndOfFrame();
i -= Time.deltaTime;
}
teleportMaterial.SetFloat(progressID, 0);
yield return new WaitForEndOfFrame();
displayMesh.sharedMaterial = normalMaterial;
}
}
Shader "Unlit/JokerTeleport"
{
Properties
{
[NoScaleOffset] _MainTex ("2D Texture", 2D) = "white" {}
_DistortScale("Distort Scale", float) = 5
_DistortHeight("Distort Height", float) = 0.5
_TextureScale("Texture Scale", float) = 5
_TeleportProgress("Teleport Progress", range(0,1)) = 0.5
}
SubShader
{
ZWrite On
Pass{
Tags { "LightMode" = "Always" }
ColorMask 0
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : POSITION;
};
struct v2f
{
float4 vertex : SV_POSITION;
};
float _DistortScale;
float _DistortHeight;
float _TeleportProgress;
v2f vert(appdata v)
{
v2f o;
fixed4 worldPos = mul(unity_ObjectToWorld, v.vertex);
fixed distortionAmount = sin(worldPos.x / _DistortScale + _Time.w * 12) + sin(worldPos.z / _DistortScale);
o.vertex = UnityObjectToClipPos(v.vertex) + fixed4(0, _DistortHeight*distortionAmount*smoothstep(0.0,0.3,_TeleportProgress),0,0);
return o;
}
void frag() {}
ENDCG
}
LOD 100
//Blend SrcAlpha OneMinusSrcAlpha // Traditional transparency
Blend DstColor Zero // Multiplicative
Cull Back
ZWrite On
Tags{
"RenderType" = "Transparent"
"Queue" = "Transparent"
}
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : POSITION;
};
struct v2f
{
float4 worldPos : TEXCOORD0;
float4 vertex : SV_POSITION;
float4 screenPos : TEXCOORD1;
};
sampler2D _MainTex;
float _DistortScale;
float _DistortHeight;
float _TextureScale;
float _TeleportProgress;
v2f vert (appdata v)
{
v2f o;
fixed4 worldPos = mul(unity_ObjectToWorld, v.vertex);
o.worldPos = worldPos;
fixed distortionAmount = sin(worldPos.x/_DistortScale + _Time.w*12) + sin(worldPos.z / _DistortScale);
o.vertex = UnityObjectToClipPos(v.vertex) + fixed4(0, _DistortHeight*distortionAmount*smoothstep(0.0, 0.3, _TeleportProgress),0,0);
o.screenPos = ComputeScreenPos(o.vertex);
return o;
}
fixed4 frag (v2f i) : SV_Target
{
fixed target = _TeleportProgress / 2 + 0.6;
fixed4 col = smoothstep(target, target-0.1, tex2D(_MainTex, (i.screenPos.xy / i.screenPos.w)*_TextureScale * fixed2(3,0.01) + (round(_Time.y*12)*1548.09)%1).r);
return col;
}
ENDCG
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment