Skip to content

Instantly share code, notes, and snippets.

View pema99's full-sized avatar

Pema Malling pema99

View GitHub Profile
@pema99
pema99 / CompareMipSelection
Created May 9, 2025 19:28
Compares SW and HW mipmap selection
Shader "Unlit/CompareMipSelection"
{
Properties
{
_MainTex ("Main color texture", 2D) = "white" {}
_MipTexture ("Texture with different color mips", 2D) = "white" {}
_AnisoLevel ("Aniso Level (must set manually to match texture)", Range(0, 16)) = 0
[ToggleUI] _VisualizeMipLevels ("Visualize Mip Levels", Integer) = 0
}
SubShader
@pema99
pema99 / AnisotropicFilteringMipSelection.hlsl
Created May 9, 2025 18:48
Mipmap level selection accounting for anisotropic filtering.
void EllipsoidTransformDerivatives(inout float2 dx, inout float2 dy)
{
bool anyZero = length(dx) == 0 || length(dy) == 0;
bool parallel = (dx.x * dy.y - dx.y * dy.x) == 0;
bool perpendicular = dot(dx, dy) == 0;
bool nonFinite = isinf(dx) || isinf(dy) || isnan(dx) || isnan(dy);
if (!anyZero && !parallel && !perpendicular && !nonFinite)
{
float A = dx.y*dx.y + dy.y*dy.y;
float B = -2.0 * (dx.x * dx.y + dy.x * dy.y);
@pema99
pema99 / MakeWeirdMips.cs
Created May 7, 2025 23:52
Creates a texture where each mip level is a different color.
public class MakeWeirdMips : MonoBehaviour
{
[MenuItem("Pema/MakeWeirdMips")]
public static void MakeWeirdMipsAsset()
{
Texture2D tex = new Texture2D(1024, 1024, GraphicsFormat.R32G32B32A32_SFloat, TextureCreationFlags.MipChain);
FillMip(tex, Color.magenta, 0); // 1024
FillMip(tex, Color.green, 1); // 512
FillMip(tex, Color.blue, 2); // 256
FillMip(tex, Color.red, 3); // 128
@pema99
pema99 / MediocreSSR.shader
Created May 4, 2025 20:22
Mediocre SSR implementation, demonstrating coarse tracing followed by binary search to refine the position.
Shader "pema99/SSR"
{
Properties
{
_MaxDistance("Max Distance", Range(0, 100)) = 10
_Sparsity("Ray Sparsity", Range(0, 1)) = 1.0
_Thickness("Thickness", Range(0, 1)) = 0.1
_EdgeFade("Edge Fade", Range(0, 1)) = 0.1
_Steps("Refinement Steps", Range(1, 20)) = 5
}
@pema99
pema99 / TerribleSSR.shader
Created May 4, 2025 19:38
A bad implementation of screenspace reflection showing the absolute bare minimum of how to raymarch in screenspace.
Shader "pema99/SSR"
{
Properties
{
_MaxDistance("Max Distance", Range(0, 100)) = 100
_Steps("Steps", Range(1, 1000)) = 1
_Thickness("Thickness", Range(0, 1)) = 0.1
_EdgeFade("Edge Fade", Range(0, 1)) = 0.1
}
SubShader
@pema99
pema99 / ZH3.cginc
Last active November 23, 2024 23:53
ZH3 - Quadratic Zonal Harmonics, i3D 2024 - Unity shadercode
// Paper: ZH3: Quadratic Zonal Harmonics, i3D 2024. https://torust.me/ZH3.pdf
// Code based on paper and demo https://www.shadertoy.com/view/Xfj3RK
#include "UnityCG.cginc"
// L0 radiance = L0 irradiance * PI / Y_0 / AHat_0
// PI / (sqrt(1 / PI) / 2) / PI = 2 * sqrt(PI)
const static float L0IrradianceToRadiance = 2 * sqrt(UNITY_PI);
// L1 radiance = L1 irradiance * PI / Y_1 / AHat_1
@pema99
pema99 / SlangMarcher.shader
Created May 5, 2024 22:31
Slang Raymarcher
Shader "pema99/SlangMarcher"
{
Properties
{
_Iterations ("Max iterations", Float) = 128
_MaxDist ("Max distance", Float) = 50
_MinDist ("Min distance", Float) = 0.001
}
SubShader
{
@pema99
pema99 / QuadLightEmitter.cs
Created March 21, 2024 23:04
Analytical polygonal irradiance
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[ExecuteAlways]
public class QuadLightEmitter : MonoBehaviour
{
[ColorUsage(false, true)]
public Color color = Color.white;
public Transform[] verts;
@pema99
pema99 / BufferMap.cs
Last active October 20, 2023 20:31
BufferMap - A dense, associative map from handle to value, with O(1) amortized complexity for all operations.
// SPDX-License-Identifier: MIT
// Author: pema99
// This file contains an implementation of a data structure I've named a BufferMap.
// It works as a map from 'handles' to values, where handles are just integers.
// You insert values into the map, and are returned an automatically generated handle.
// You can then use this handle to retrieve the value, or remove it from the map.
// Handles are never invalidated until they are explicitly removed.
// Amortized time complexities for adding, removing and getting a value from
// the map are all O(1). Additionally, the data structure has the property that
@pema99
pema99 / QuadIntrinsics.cginc
Last active July 3, 2024 09:48
Emulated Quad and Wave intrinsics for basic fragment shaders
// SPDX-License-Identifier: MIT
// Author: pema99
// This file contains functions that simulate Quad and Wave Intrinsics without access to either.
// For more information on those, see: https://github.com/Microsoft/DirectXShaderCompiler/wiki/Wave-Intrinsics
// To use the functions, you must call SETUP_QUAD_INTRINSICS(pos) at the start of your fragment shader,
// where 'pos' is the pixel position, ie. the fragment input variable with the SV_Position semantic.
// Note that some functions will require SM 5.0, ie. #pragma target 5.0.