Skip to content

Instantly share code, notes, and snippets.

View runevision's full-sized avatar

Rune Skovbo Johansen runevision

View GitHub Profile
@runevision
runevision / BurstSDFGenerator.cs
Created September 11, 2024 07:48
Signed Distance Field generator for Unity with Burst support
/*
Based on the Anti-aliased Euclidean distance transform described by Stefan Gustavson and
Robin Strand. For further information, see https://contourtextures.wikidot.com/ and
https://web.archive.org/web/20220503051209/https://weber.itn.liu.se/~stegu/edtaa/
The algorithm is an adapted version of Stefan Gustavson's code, it inherits the copyright
statement below, which applies to this file only.
The rewrite with Unity Burst support makes the execution 40 times faster by default,
and 75 times faster if the passed in textures are both of type TextureFormat.RGBAFloat.
@runevision
runevision / ProjectionViewer.cs
Created August 17, 2024 12:01
Script to create visual illusion of rotating wireframe object that tricks the eye into switching orientation and rotation direction
using UnityEngine;
// Visual effect/illusion where a rotating model alternates between
// seeming to rotate one way around and the other.
// Unlike many similar effects, the eyes are nudged/forced to see it
// in alternating ways by oscillating between regular perspective and
// inverse perspective, changing which parts of the model appear
// bigger or smaller on the screen.
// Attach this script on the GameObject with the Camera component.
@runevision
runevision / BurstMethodTester.cs
Last active September 12, 2024 10:26
Using Unity Burst directly on methods without jobs - unofficial example code
using Unity.Burst;
using Unity.Collections;
using Unity.Collections.LowLevel.Unsafe;
using Unity.Jobs;
using UnityEngine;
// This example code demonstrates using Unity Burst directly on a static method without jobs.
// Unity NativeArrays can't be used directly in Unity Burst methods (only in Burst jobs),
// so we have to pass pointers to arrays instead.
@runevision
runevision / Shadows.cginc
Last active May 3, 2024 13:32
Water Foam Particle Shader
UNITY_DECLARE_SHADOWMAP(_SunCascadedShadowMap);
float4 _SunCascadedShadowMap_TexelSize;
#define GET_CASCADE_WEIGHTS(wpos, z) getCascadeWeights_splitSpheres(wpos)
#define GET_SHADOW_FADE(wpos, z) getShadowFade_SplitSpheres(wpos)
#define GET_SHADOW_COORDINATES(wpos,cascadeWeights) getShadowCoord(wpos,cascadeWeights)
/**
* Gets the cascade weights based on the world position of the fragment and the poisitions of the split spheres for each cascade.
@runevision
runevision / Unity versions not affected by Unity Runtime Fee.md
Created September 14, 2023 09:45
Unity versions not affected by Unity Runtime Fee

Unity versions not affected by Unity Runtime Fee

This is information that has been dug up by me and others in the Unity community. It's not official information from Unity (but most of the linked resources are). It is also not legal advise. I am not a lawyer.

TLDR:

Contrary to what Unity themselves are saying, for games made with these Unity versions, developers can elect not to be affected by a newer version of the Terms of Service that introduces the Unity Runtime Fee:

  • Unity 2022.x or earlier
  • Unity 2021.x LTS or earlier
@runevision
runevision / Text2.cs
Last active August 20, 2024 06:42
Text2 extends the Unity UI Text class and makes hyphens and soft hypens work
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
// Text2 extends the Text component in Unity UI.
// It makes hyphens and soft hyphens work.
// Inserting soft hyphens in text can be tricky and confusing, given they are invisible,
// so you can instead also insert Hyphenation Point characters, which will be replaced by soft hyphens:
// https://www.compart.com/en/unicode/U+2027
public class Text2 : Text {
@runevision
runevision / ObjectExtensions.cs
Created September 16, 2019 13:11
obj.IsDestroyed() method for Unity objects
public static class ObjectExtensions {
public static bool IsDestroyed (this UnityEngine.Object obj) {
// Returns true if the object has been destroyed;
// false if it's actually null.
// (You could throw a NullReferenceException instead
// in the latter case if you wanted.)
return obj == null && !ReferenceEquals (obj, null);
}
}
@runevision
runevision / RedistributeUVsPostProcessor.cs
Created September 15, 2019 13:31
Redistribute UVs PostProcessor for Unity
// This can be used to redistribute the UV coordinates
// (either, U, V, or both) on an imported mesh which represents an
// extruded surface, such as ropes, or other shapes extruded along a
// spline. It's important for this method to work, that all vertices
// of each edge loop have the same U or V coordinate (depending on
// which is chosen for redistribution).
//
// Usage:
//
// Meshes that should be redistributed should contain "Redistribute"
@runevision
runevision / CameraTrackingRefraction.cs
Created October 22, 2017 11:09
Unity CommandBuffer replacement for GrabPass - works with multiple separate cameras.
using UnityEngine;
using UnityEngine.Rendering;
// This script is added to cameras automatically at runtime by the ObjectNeedingRefraction scripts.
public class CameraTrackingRefraction : MonoBehaviour {
[System.NonSerialized]
public int lastRenderedFrame = -1;
Camera cam;
using UnityEngine;
using System.Collections.Generic;
[System.Serializable]
public class SerializableDictionary<TKey, TValue> : Dictionary<TKey, TValue>, ISerializationCallbackReceiver {
[SerializeField]
private List<TKey> keys = new List<TKey> ();
[SerializeField]
private List<TValue> values = new List<TValue> ();