Skip to content

Instantly share code, notes, and snippets.

View StagPoint's full-sized avatar

StagPoint Software StagPoint

  • StagPoint Software
  • Seattle, WA
View GitHub Profile
@StagPoint
StagPoint / Morton2D.cs
Last active May 6, 2024 15:45
Represents 2D Morton Codes by interleaving two 16-bit unsigned integer values into a 32-bit unsigned integer.
// **************************************************
// EXAMPLE USAGE:
//
// // Returns a single value with arguments x and y interleaved
// var code = MortonCode2D.Interleave( 123, 456 );
//
// // Extracts the interleaved values (123 and 456) into integer variables x and y
// MortonCode2D.Deinterleave( code, out int x, out int y )
//
// **************************************************
@StagPoint
StagPoint / QuaternionCompression.cs
Last active May 3, 2024 22:23
C# - Use "smallest three" compression for transmitting Quaternion rotations in Unity's UNET networking, from 16 bytes to 7 bytes.
// Copyright (c) 2016 StagPoint Software
namespace StagPoint.Networking
{
using System;
using UnityEngine;
using UnityEngine.Networking;
/// <summary>
/// Provides some commonly-used functions for transferring compressed data over the network using
@StagPoint
StagPoint / OuterGlow.cs
Last active April 25, 2024 20:05
Custom VisualElement for Unity's UI Toolkit which is useful for creating an "outer glow" effect for buttons, or a "drop shadow" effect for popups, windows, and dialogs.
// Created 2024 StagPoint. Released to the public domain.
using System;
using System.Runtime.CompilerServices;
using Unity.Collections;
using UnityEngine;
using UnityEngine.Scripting;
using UnityEngine.UIElements;
@StagPoint
StagPoint / NativePriorityQueue.cs
Last active March 7, 2024 20:34
Priority queue implementation for Unity that uses Native Containers for data storage. This version relies on the stored type implementing the IComparable<T> interface.
// Copyright 2017-2021 StagPoint Software
namespace StagPoint.Collections
{
using System;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using Unity.Collections;
using Unity.Collections.LowLevel.Unsafe;
@StagPoint
StagPoint / DoPlanesIntersectAtSinglePoint.cs
Created September 25, 2018 17:57
Find intersection point of three planes in C# for Unity
private bool planesIntersectAtSinglePoint( Plane p0, Plane p1, Plane p2, out Vector3 intersectionPoint )
{
const float EPSILON = 1e-4f;
var det = Vector3.Dot( Vector3.Cross( p0.normal, p1.normal ), p2.normal );
if( det < EPSILON )
{
intersectionPoint = Vector3.zero;
return false;
}
@StagPoint
StagPoint / AddAnimationClipsToControllerAsset.cs
Created December 5, 2021 18:50
Unity editor script that adds existing AnimationClip assets to the AnimationController asset that uses them, cutting down on clutter and making it easier to manage your controllers and their associated animation clips.
namespace StagPoint.EditorUtils
{
using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using UnityEditor.Animations;
@StagPoint
StagPoint / SimplePathSmoother.cs
Last active June 17, 2022 10:59
Implements a simplistic 3D curve smoother for Unity. Depending on the intended use case, best results might be achieved by preprocessing the curve to have equally-spaced points, or using the Ramer-Douglas-Peucker algorithm to remove unnecessary points. It wasn't needed for my use case, so I didn't include that code here. YMMV.
namespace StagPoint.Math
{
using Unity.Collections;
using Unity.Mathematics;
/// <summary>
/// Implements a simplistic 3D curve smoother
/// </summary>
public static class CurveSmoother
{
@StagPoint
StagPoint / CameraMath.cs
Created December 2, 2020 23:20
Small collection of utility functions for calculating screen size of objects, size of camera frustum at a distance, etc.
using System;
using System.Runtime.InteropServices;
using System.Runtime.CompilerServices;
using Unity.Burst;
using Unity.Mathematics;
public static class CameraMath
{
/// <summary>
@StagPoint
StagPoint / FloatConversion.cs
Last active May 19, 2021 17:49
Convert between floating point and integral types in C# / Unity
// Copyright (c) 2016 StagPoint Software
// NOTE: C#-style unions (structs using FieldOffset to cause multiple fields to 'overlap' in memory) may not be allowed on
// some IL2CPP platforms? If you get a compile error related to using FieldOffset on your target platform, comment the
// following #define to force this code to use another method.
//
// The UnityEngine.Networking code on BitBucket uses (#if !INCLUDE_IL2CPP) to control whether to use unions, but that code
// may be out of date (Unity has so far failed to respond), but testing confirms that this method does work on at least one
// of the IL2CPP target platforms, so we've chosen a more explicit #define instead.
#define USE_UNION_FOR_CONVERSION
@StagPoint
StagPoint / TriangleBoxIntersect.cs
Last active February 23, 2021 19:37
Triangle/AABB Intersection Test in C#
public static bool IntersectsBox( Vector3 a, Vector3 b, Vector3 c, Vector3 boxCenter, Vector3 boxExtents )
{
// From the book "Real-Time Collision Detection" by Christer Ericson, page 169
// See also the published Errata at http://realtimecollisiondetection.net/books/rtcd/errata/
// Translate triangle as conceptually moving AABB to origin
var v0 = ( a - boxCenter );
var v1 = ( b - boxCenter );
var v2 = ( c - boxCenter );