Skip to content

Instantly share code, notes, and snippets.

View slembcke's full-sized avatar
👣
Gruntled

Scott Lembcke slembcke

👣
Gruntled
View GitHub Profile
@slembcke
slembcke / CustomProjection.cs
Last active February 7, 2024 11:03
Custom projections for Unity2D games.
using UnityEngine;
using System.Collections;
// This script is meant to be attached to your main camera.
// If you want to use it on more than one camera at a time, it will require
// modifcations due to the Camera.on* delegates in OnEnable()/OnDisable().
[ExecuteInEditMode]
public class CustomProjection : MonoBehaviour {
private void OnEnable(){
@slembcke
slembcke / Projection.cs
Created January 9, 2017 20:24
Custom projection support for Unity.
using UnityEngine;
using System.Collections;
[ExecuteInEditMode]
public class TreesCamera : MonoBehaviour {
private void OnEnable(){
// Optional, only enable the callbacks when in the editor.
if(Application.isEditor){
// These callbacks are invoked for all cameras including the scene view and camera previews.
Camera.onPreCull += ScenePreCull;
@slembcke
slembcke / cull.c
Created August 1, 2023 16:26
Cull a bounding box (-1, 1) using the MVP transform.
// Check if a tile is visible onscreen using it's MVP transform.
static inline bool MapTileFrustumCull(const mat4 mvp){
// Clip space center and extents.
vec4 c = {mvp.m[12], mvp.m[13], mvp.m[14], mvp.m[15]};
vfloat ex = vabs(mvp.m[ 0]) + vabs(mvp.m[ 4]) + vabs(mvp.m[ 8]);
vfloat ey = vabs(mvp.m[ 1]) + vabs(mvp.m[ 5]) + vabs(mvp.m[ 9]);
vfloat ez = vabs(mvp.m[ 2]) + vabs(mvp.m[ 6]) + vabs(mvp.m[10]);
// Check the bounds against the clip space viewport using a conservative w-value.
vfloat w = vmax(0, c.w + vabs(mvp.m[ 3]) + vabs(mvp.m[ 7]) + vabs(mvp.m[11]));
@slembcke
slembcke / disassembler.c
Created March 9, 2019 15:22
6502 disassembler
#include <stdint.h>
#include <stdio.h>
#include "disassembler.h"
// 6502 instruction tables.
enum OPCODE {
OP_LDA, OP_LDX, OP_LDY, OP_STA, OP_STX, OP_STY, OP_ADC, OP_SBC,
OP_INC, OP_INX, OP_INY, OP_DEC, OP_DEX, OP_DEY, OP_ASL, OP_LSR,
OP_ROL, OP_ROR, OP_AND, OP_ORA, OP_EOR, OP_CMP, OP_CPX, OP_CPY,
@slembcke
slembcke / frame-timing.c
Last active June 18, 2023 19:05
Filter frame timing to try and remove scheduler jitter.
double delta_time_filtered(double dt_nanos){
// Warm start the filter by assuming 60 Hz.
static double x[] = {1.6e7, 1.6e7, 1.6e7}, y[] = {1.6e7, 1.6e7, 1.6e7};
// IIR filter coefficients. 2rd order lowpass butterworth at 1/128 the sample rate.
static const double b[] = {6.321391700454014e-5, 0.00012642783400908025, 6.321391700454014e-5};
static const double a[] = {1.0, -1.9681971279272976, 0.9684499835953156};
// Apply IIR filter coefficients.
double value = b[0]*dt_nanos;
for(uint i = 2; i > 0; i--){
@slembcke
slembcke / inverted_sdl.c
Last active December 12, 2022 20:42
SDL-like API on top of an inversion of control API.
#include <stdbool.h>
#include <math.h>
#include <stdio.h>
#include <GL/gl.h>
// A vaguely SDL-like API
void iSDL_init(void);
bool iSDL_should_exit;
void iSDL_swap_buffers(void);
@slembcke
slembcke / zoom.c
Created April 28, 2022 08:58
Fractalis zooming
static void do_zoom(AppState* app, tina* coro, int button, double direction){
if(glfwGetMouseButton(app->window, button) != GLFW_PRESS) return;
Camera* cam = &app->camera;
double speed = 0;
double t0 = glfwGetTime();
while(glfwGetMouseButton(app->window, button) == GLFW_PRESS){
double t1 = glfwGetTime();
double dt = t1 - t0;
t0 = t1;
@slembcke
slembcke / drift_physics.c
Created April 27, 2022 05:39
Project Drift physics
#include <string.h>
#include "tracy/TracyC.h"
#include "drift_game.h"
typedef struct PhysicsContext PhysicsContext;
typedef struct {
uint idx0, idx1;
// Appologies for the messy code.
// It was run through SPIRV-Cross
// I cleaned it up and renamed the variables by hand.
#version 330
layout(std140) uniform Locals
{
mat2x4 LightMatrix;
float LightRadius;
@slembcke
slembcke / NearlyNearestNeighborFiltering.glsl
Last active December 27, 2020 21:19
Anti-aliased Nearest Neighbor Filtering.
uniform sampler2D texture;
varying vec2 uv;
void main(){
vec2 size = textureSize(texture);
vec2 puv = uv*size;
vec2 hfw = 0.5*fwidth(puv);
vec2 fl = floor(puv - 0.5) + 0.5;