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 / sliding
Last active February 12, 2018 18:48
Falling down a hill.
// Gravity pulls the player down,
// but the slope of the hill pushes them in another direction.
static const float G = ...; // Acceleration due to gravity.
// First find what percentage of gravity is wasted opposing the ground normal:
float dotN = Vector3.Dot(groundNormal, -Vector3.up);
// Or simplified, just -groundNormal.y
// Subtract that from the down vector to get the percent
@slembcke
slembcke / coro.s
Created July 11, 2018 16:56
Simple coroutines for cc65.
.include "zeropage.inc"
.macpack generic
.import pusha, popa
.import pushax
.import addysp, subysp
.import _exit
.data
#ifndef CORO_H
#define CORO_H
#include <stdint.h>
typedef uintptr_t (*coro_func)(uintptr_t);
void coro_start(coro_func func);
uintptr_t coro_yield(uintptr_t);
uintptr_t coro_resume(uintptr_t);
@slembcke
slembcke / naco.h
Created July 17, 2018 19:47
libnaco: Nano coroutine library for cc65.
#ifndef NACO_H
#define NACO_H
#include <stdint.h>
#include <stdlib.h>
typedef uintptr_t (*naco_func)(uintptr_t);
void naco_init(naco_func func, void *naco_buffer, size_t buffer_size);
uintptr_t naco_yield(uintptr_t value);
@slembcke
slembcke / FlickerExample.txt
Created August 24, 2018 21:53
Code snippets for my flicker issue.
static void px_ppu_sync_off(){
px_mask &= ~PX_MASK_RENDER_ENABLE;
px_wait_nmi();
// Without these I get a few garbage lines at the top of the screen
waitvsync();
waitvsync();
}
static void px_ppu_sync_on(){
@slembcke
slembcke / decompress_lz4.s
Created October 28, 2018 07:10
GBZ80 Lz4 decompression function progress.
section "lz4-data", hram
token: db ; Most recently read token.
backref: dw
; NOTES:
; dst/src is always stored in de/hl
section "lz4-code", rom0
@slembcke
slembcke / anisotropy.hlsl
Created January 30, 2019 03:43
Anisotropic reflections
// 'coord.y' is the clip space y-coordinate of the pixel on the water.
// 'ray0.y/ray0.w' is the clip space y-coordinate of the reflected pixel.
// The basic idea is near the horizion where the reflected point is close to the water,
// you should have a strong reflection. The further it gets from the horizon the more it should be blurred vertically.
// The result is pretty good. :D
float aniso = _SSRAniso*abs(coord.y - ray0.y/ray0.w);
float2 ddy_uv = max(ddy(uv), float2(0, aniso));
...
float4 color = tex2Dgrad(_CameraColorTexture, uv, ddx(uv), ddy_uv);
@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,
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
static const size_t COUNT = 64*(1<<20);
typedef float FLOAT_T;
@slembcke
slembcke / ProjectedBounds.cs
Last active April 29, 2019 18:16
Calculate a bounded mvp matrix for an object.
using UnityEngine;
[ExecuteInEditMode]
public class ProjectedBounds : MonoBehaviour {
private Transform _transform;
private Renderer _renderer;
private Mesh _mesh;
private MaterialPropertyBlock _block;
private void Start(){