Skip to content

Instantly share code, notes, and snippets.

@SabinT
SabinT / Homography.cs
Created March 25, 2024 06:29
Square to Quad homography (credits: hecomi)
using UnityEngine;
/// <summary>
/// Utility to map points inside a unit square (0,0)-(1,1) to a quad defined by 4 points.
/// Uses math from:
/// https://github.com/hecomi/uHomography/blob/master/Assets/uHomography/Runtime/Scripts/Homography.cs
/// Instructions to use for projection mapping:
/// * Gather screen coordinates of four corner points, and call UpdateMatrix with them.
/// * The Transform method now will map a point inside a unit square to an appropriate point on the screen quad.
@SabinT
SabinT / CameraQuad.cs
Created March 11, 2023 21:35
CameraQuad.cs
private void CalculateCorners()
{
float aspectRatio = Screen.width / (float)Screen.height;
if (this.Camera == null)
{
this.Camera = Camera.main;
}
// IMPORTANT: Do not use Screen.width here, use Camera.pixelWidth!!!
@SabinT
SabinT / SerializableMap.cs
Last active August 4, 2022 04:57
Serializable string to generic map in Unity
using System;
using System.Collections.Generic;
using UnityEngine;
namespace Lumic.Utils
{
/// <summary>
/// Assumes string keys.
/// Kind of works, use the +/- buttons on the "Keys". +/- Buttons on values list does't work.
/// Example usage:
@SabinT
SabinT / p5jsImprovedSlider.js
Last active May 19, 2022 05:22
p5js createSlider with label and values
/**
* Example:
* let param1 = 5;
* createSlider(0, 10, param1, 0.01, "Param 1", (val) => { param1 = val; });
*/
export function createSlider (min, max, value, step, text, valueChangeHandler) {
const container = document.createElement('div');
const label = document.createElement('div');
const elt = document.createElement('input');
label.setAttribute('style', "background: #214288; color: white; font-family: monospace; font-size: large");
@SabinT
SabinT / ObjectPool.cs
Created March 7, 2022 07:47
Generic Object Pooling in Unity 3D
using System;
using System.Collections.Generic;
using UnityEngine;
namespace Lumic.Structure.Procedural
{
public class MeshPool : ObjectPool<Mesh>
{
protected override Mesh CreateNew()
{
@SabinT
SabinT / .gitattributes
Created January 6, 2022 08:13 — forked from nemotoo/.gitattributes
.gitattributes for Unity3D with git-lfs
## Unity ##
*.cs diff=csharp text
*.cginc text
*.shader text
*.mat merge=unityyamlmerge eol=lf
*.anim merge=unityyamlmerge eol=lf
*.unity merge=unityyamlmerge eol=lf
*.prefab merge=unityyamlmerge eol=lf
@SabinT
SabinT / CameraGridSetter.cs
Created October 22, 2021 05:55
Tiled camera rendering helper in Unity
using UnityEngine;
using EasyButtons;
using UnityEngine.Rendering;
namespace Lumic.Utils
{
/// <summary>
/// TODO support orthographic cameras
/// </summary>
[RequireComponent(typeof(Camera))]
@SabinT
SabinT / UnitySetup.bat
Last active January 6, 2022 08:39
Common Unity project first time setup
git init
curl -o .gitignore https://github.com/github/gitignore/raw/main/Unity.gitignore
curl -o .gitattributes https://gist.github.com/nemotoo/b8a1c3a0f1225bb9231979f389fd4f3f/raw/dc3e8cab80fc62d1c60db70c761b1ffa636aa796/.gitattributes
git add .gitignore
git add .gitattributes
git commit -m "Initialize with gitignore/git LFS attributes"
# Optional: add useful packages
@SabinT
SabinT / GenericComputeShaderDriver.cs
Last active February 7, 2023 22:34
Unity3D: Generic template to pass options to a compute shader and dispatch; avoids common boilerplate of Shader.PropertyToID and shader.SetInt etc
using EasyButtons;
namespace Lumic.Compute
{
using System;
using System.Linq;
using System.Reflection;
using System.Collections.Generic;
using UnityEngine;
@SabinT
SabinT / Triangle.obj
Created July 9, 2021 17:26
Unit equilateral triangle on XY plane, pointy side towards +ve Y axis
# Assuming triangle side, x = 1 unit
# Height of triangle, H = sqrt(x) * x / 2 = 0.866025403784438
# Height of centroid h = x / (2 * sqrt(3)) = 0.288675134594812; 2h = 0.577350269189624
# Lower Right Vertex: (x/2, -h)
v 0.5 -0.288675134594812 0
# Top Vertex: (0, 2h)
v 0 0.577350269189624 0
# Lower Left Vertex (-x/2, -h)
v -0.5 -0.288675134594812 0
# Note: OBJ format uses 1-based indexing, not 0-based