Skip to content

Instantly share code, notes, and snippets.

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public static class MathUtilities{
public static Vector2[] LineBoxIntersect(Vector2 point1, Vector2 point2, Rect rect) {
List<Vector2> intersect = new List<Vector2>(); //if this is ever greater than length 2 we have encountered a black hole
//order is clockwise from bottom left -> left edge, top edge, right edge, bottom edge
Vector2[] rectP1 = {
new Vector2(rect.xMin, rect.yMin), //bottom left
@samloeschen
samloeschen / SmoothFKMember.cs
Last active July 27, 2017 16:22
Simple 3D forward kinematics with easing. Place on any normal Transform and set fkParent in the inspector to the Transform you want it to follow.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SmoothFKMember : MonoBehaviour {
public Transform fkParent;
public float rotationSpeed = 10f;
private Vector3 initialPositionOffset;
@samloeschen
samloeschen / BloomFilter.cs
Last active January 17, 2023 17:38
A fast and simple bloom filter for Unity using the double-kawase method
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BloomFilter : MonoBehaviour {
[Range(1, 16)]
public int iterations = 1;
[Range(0f, 1f)]
public float threshold = 0.8f;
[Range(0f, 1f)]
@samloeschen
samloeschen / BillboardExampleShader.cg
Last active December 11, 2018 17:43
Example of billboarding in a shader
Shader "Unlit/BillboardColor" {
Properties{
_Color("_Color", Color) = (1,1,1,1)
}
SubShader{
Tags{ "RenderType" = "Transparent" "Queue" = "Transparent" }
@samloeschen
samloeschen / ComputeRenderer.cs
Created July 27, 2019 22:46
ComputeRenderer for Unity. Rasterizes compute shader output to make masks and stuff
using System.IO;
using System;
using UnityEngine;
using UnityEditor;
using Debug = UnityEngine.Debug;
public class ComputeRenderer : EditorWindow {
public RenderTexture previewTexture;
@samloeschen
samloeschen / LayerMaskExtensions.cs
Last active August 25, 2019 12:48
LayerMask Extensions
using UnityEngine;
using System.Collections.Generic;
public static class LayerMaskExtensions {
// int extensions
public static int Inverse(this int mask) {
return ~mask;
}
public static int Combined(this int mask, int other) {
return mask | other;
@samloeschen
samloeschen / CrescentAnimation.shader
Last active November 15, 2019 16:25
CrescentAnimation
Shader "Custom/Unlit/CrescentAnimation"
{
Properties
{
[PerInstanceData] _Anim ("Animation", Range(0, 1)) = 0
[PerInstanceData] _Flash ("Flash", Range(0, 1)) = 0
[PerInstanceData] _Color ("Color", Color) = (1,1,1,1)
[PerInstanceData] _OffsetDir ("Offset Direction", Vector) = (0.5, 0.5, 0, 0)
_Displacement ("Displacement", Float) = 0.65
@samloeschen
samloeschen / ContiguousMap.cs
Created March 2, 2020 23:12
ContiguousMap<T>
using System;
public class ContiguousMap<T> {
int[] _idIndexMap;
int[] _idPool;
int _poolCount = 0;
int _nextId = 0;
public T[] values {
get { return _values; }
}
@samloeschen
samloeschen / sparseset.c
Last active April 2, 2020 17:44
simple implementation of a Sparse Set in C
#include <stdlib.h>
#include <stdio.h>
#include "sparseset.h"
int min(int a, int b) {
return a < b ? a : b;
}
int max(int a, int b) {
return a > b ? a : b;
}
@samloeschen
samloeschen / CenteredCollectionViewFlowLayout.cs
Created May 31, 2020 14:34
CenteredCollectionViewFlowLayout: Flow layout for doing centered carousels with a UICollectionView
//
// CenteredCollectionViewFlowLayout.swift
// ACameraAppwithBlur
//
// Created by Sam Loeschen on 5/30/20.
// Copyright © 2020 Sam Loeschen. All rights reserved.
//
import Foundation
import UIKit