Skip to content

Instantly share code, notes, and snippets.

View hariedo's full-sized avatar

Ed Halley hariedo

View GitHub Profile
#
# Object > Set Mesh Origin to Bottom
# A python script compatible with Blender 2.90 API.
#
# Takes the currently selected mesh-type objects,
# and moves the object origin location to the "bottom" of the mesh.
# The bottom is calculated as the average X and Y and the minimum Z.
#
# This is useful in 3D Printing, for example, when importing published
# STL geometry files. Typically the bottom of the shape is flat and
@hariedo
hariedo / ReplaceMaterials.cs
Last active November 4, 2023 15:44
Unity component to replace materials whenever enabled
// ReplaceMaterial.cs
//
using System.Collections.Generic;
using UnityEngine;
namespace Screenplay
{
//
// When enabled, all materials (or all materials matching given original)
// in this object are replaced with another material. When disabled, the
@hariedo
hariedo / QuitAppOnEscape.cs
Created November 5, 2023 00:06
A development-time component to watch for Esc and quit playing a Unity game
// QuitAppOnEscape.cs
//
using UnityEngine;
#if ENABLE_INPUT_SYSTEM
using UnityEngine.InputSystem;
#endif
namespace Screenplay.Workbench
{
//
// assembly == the gameobject root to move
// feature == the specific place on the assembly to align with station
// station == the reference to which the assembly feature will align
// flip == an additional rotation applied to the assembly pivoting
// around the feature (e.g., 180º Y flip)
//
public static void AlignAtFeature(
Transform assembly, Transform feature, Transform station,
Quaternion flip)
{
// Thing.cs
// Automatically indexed MonoBehaviour for searching by type.
//
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace Things
{
// Given a float f, return next possible representation.
// This method will have issues around +/-maxfloat, +/-infinity, NaN.
//
public static float NextULP(float f, int ulp=1)
{
int bits = System.BitConverter.SingleToInt32Bits(f);
return System.BitConverter.Int32BitsToSingle(bits + ulp);
}
public static Color HexRGB(uint hexcode)
{
// e.g., 0xFF0033
float b = (hexcode & 0xFF) / 255f;
float g = ((hexcode >> 8) & 0xFF) / 255f;
float r = ((hexcode >> 16) & 0xFF) / 255f;
return new Color(r, g, b);
}
public static Quaternion YawPitchRoll(float yaw, float pitch, float roll)
{
Quaternion heading = Quaternion.AngleAxis(yaw, Vector3.up);
Quaternion attitude = Quaternion.AngleAxis(pitch, Vector3.right);
Quaternion bank = Quaternion.AngleAxis(roll, Vector3.forward);
return heading * attitude * bank;
// transform.rotation *= YawPitchRoll(yaw, pitch, roll);
///// transform.rotation *= Quaternion.Euler(pitch, yaw, roll);
public static Vector3[] DistributePointsOnUnitSphere(int count)
{
Assert.IsTrue(count > 0);
Vector3[] points = new Vector3[count];
// The Fibonacci Sphere algorithm.
float phi = Mathf.PI * (Mathf.Sqrt(5f) - 1f);
float descent = 2f / (float)(count-1);
public static void DrawPolygon(Vector3 center, Vector3 axis, float radius,
int sides, Color color=default, float duration=0f, bool depthTest=true)
{
//duration = Mathf.Max(0.001f, duration);
Vector3 start = Limb(axis);
Vector3 prior = start * radius;
if (sides < 3) sides = 3;
float step = 360f/sides;
for (float f = step; f <= 360f; f += step)