Skip to content

Instantly share code, notes, and snippets.

@samloeschen
samloeschen / RenderTexturePool.cs
Created October 19, 2020 18:11
RenderTexturePool
// Sometimes, RenderTexture.GetTemporary() and RenderTexture.ReleaseTemporary() leak textures.
// For those times, there is RenderTexturePool.
using System.Collections.Generic;
using Unity.Collections;
using UnityEngine;
public class RenderTexturePool {
readonly Dictionary<RenderTextureDescriptor, List<RenderTextureCounter>> _pool;
readonly int _initialListCapacity;
@samloeschen
samloeschen / NativeListExtensions.cs
Last active October 1, 2020 04:10
NativeListExtensions
public static class NativeListExtensions {
public static unsafe void UnsafeAddRange<T>(this NativeList<T> nativeList, void* ptr, int length) where T: struct {
int idx = nativeList.Length;
int newCount = idx + length;
if (nativeList.Capacity < newCount) {
nativeList.Resize(nativeList.Capacity + length, NativeArrayOptions.UninitializedMemory);
}
nativeList.Length = newCount;
int stride = UnsafeUtility.SizeOf<T>();
@samloeschen
samloeschen / AnimationCurveToLUT.cs
Last active July 29, 2020 12:01
AnimationCurveToLUT
using UnityEngine;
using Unity.Mathematics;
public static class AnimationCurveExtensions {
public static Texture2D ToLUT(this AnimationCurve curve, int width, bool apply = true) {
// create texture
Texture2D tex = new Texture2D(width, 1, TextureFormat.R8, 0, true);
float x = 0f;
float t = 0f;
@samloeschen
samloeschen / DelayedCameraCapture.cs
Last active July 9, 2020 16:19
DelayedCameraCapture
using System;
using System.IO;
using System.Collections;
using UnityEngine;
public class DelayedCaptureBehaviour: MonoBehaviour {
public new Camera camera;
public int delayFrames;
public KeyCode targetKey;
@samloeschen
samloeschen / CenteredCollectionViewFlowLayout.swift
Created June 23, 2020 16:54
CenteredCollectionViewFlowLayout
//
// CenteredCollectionViewFlowLayout.swift
// censr
//
// Created by Sam Loeschen on 5/30/20.
// Copyright © 2020 Sam Loeschen. All rights reserved.
//
import Foundation
import UIKit
@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
@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 / 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 / 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 / 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;