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 / screenspace.shader
Last active September 3, 2019 15:29
Screenspace coordinates
CGPROGRAM
// Pass a clean copy of the VP matrix.
// Shader.SetGlobalMatrix("_MyMatrixVP", Camera.current.projectionMatrix*Camera.current.worldToCameraMatrix);
// Unity applies poorly documented magic to UNITY_MATRIX_VP that you don't want.
float4x4 _MyMatrixVP;
struct VertexOutput {
// Pass the screen space coordinate as a new varying.
// Don't re-use SV_POSITION in your frag shader, it only works on non-DirectX platforms IIRC.
float4 screenSpace : TEXCOORD1;
@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(){
#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 / 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 / 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 / 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 / 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 / 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);
#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 / 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