Skip to content

Instantly share code, notes, and snippets.

👣
Gruntled

Scott Lembcke slembcke

👣
Gruntled
View GitHub Profile
@slembcke
slembcke / NearlyNearestNeighborFiltering.glsl
Last active Jul 11, 2020
Anti-aliased Nearest Neighbor Filtering.
View NearlyNearestNeighborFiltering.glsl
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;
@slembcke
slembcke / CustomProjection.cs
Last active May 9, 2020
Custom projections for Unity2D games.
View CustomProjection.cs
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 / FourierLights.hlsl
Created Jan 21, 2020
Fourier lighting code
View FourierLights.hlsl
struct FragOutput {
half4 a0 : SV_TARGET0;
half4 a1 : SV_TARGET1;
half4 b1 : SV_TARGET2;
half4 a2 : SV_TARGET3;
half4 b2 : SV_TARGET4;
};
// Double angle formula derived from the trig identity.
float2 DoubleAngle(float2 n){
@slembcke
slembcke / mulx.c
Last active Oct 30, 2019
Signed 16.16 fixed point multiply.
View mulx.c
static inline fixed mulx_fast(fixed x, fixed y){return (fixed){(x.asint >> 8) * (y.asint >> 8)};}
static inline fixed mulx(fixed x, fixed y){
bool neg = (x.asint ^ y.asint) < 0;
union {
u32 asint;
struct {u16 hi, lo;};
} _x, _y;
if(x.asint >= 0) _x.asint = x.asint; else _x.asint = -x.asint;
View genesis-boot.s
00000204 <start>:
204: 1039 00a1 0001 moveb a10001 <VDP_REG_DMA_SRC2+0xa06901>,%d0
20a: 0200 000f andib #15,%d0
20e: 6700 000c beqw 21c <start+0x18>
212: 23fc 04fd 1e0d movel #83697165,a14000 <VDP_REG_DMA_SRC2+0xa0a900>
218: 00a1 4000
21c: 4a79 00c0 0004 tstw c00004 <VDP_CTRL>
222: 41f9 00c0 0004 lea c00004 <VDP_CTRL>,%a0
228: 30bc 8004 movew #-32764,%a0@
22c: 30bc 8144 movew #-32444,%a0@
@slembcke
slembcke / screenspace.shader
Last active Sep 3, 2019
Screenspace coordinates
View screenspace.shader
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 Apr 29, 2019
Calculate a bounded mvp matrix for an object.
View ProjectedBounds.cs
using UnityEngine;
[ExecuteInEditMode]
public class ProjectedBounds : MonoBehaviour {
private Transform _transform;
private Renderer _renderer;
private Mesh _mesh;
private MaterialPropertyBlock _block;
private void Start(){
View timing.c
#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;
View disassembler.c
#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 Jan 30, 2019
Anisotropic reflections
View anisotropy.hlsl
// '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);
You can’t perform that action at this time.