Skip to content

Instantly share code, notes, and snippets.

@oskarbraten
oskarbraten / textureUnpack1D.glsl
Last active October 9, 2022 13:27
Shader code for sampling 1D data that has been packed in a 2D texture (with mipmaps)
/*
Fetches the texel at x by mapping it to the texel-coords of the relevant mipmap level.
*/
float textureUnpack1D_texelFetch(sampler2D map, int x, int level) {
if (level > 0) {
/*
Adjusts x to the appropriate mipmap level:
For example, a sample at position 6 in level 0, will be at positions:
- level 1: 6/4¹ = 1.5 = 1
- level 2: 6/4² = 0.375 = 0
@oskarbraten
oskarbraten / packed-1d-mipmaps.ts
Last active September 30, 2022 18:26
Generates mipmaps for a single channel image by processing 4 pixels in the order they appear in the underlying buffer as opposed to visually.
interface SingleChannelImage {
width: number;
height: number;
data: Uint8ClampedArray
}
function mipmaps(source: SingleChannelImage, fn = Math.max): SingleChannelImage[] {
if (source.width === 1 && source.height === 1) {
return [source];
}
@oskarbraten
oskarbraten / kotlin_javascript_functions.js
Last active March 23, 2018 23:29
Kotlin inspired built in functions for Javascript
Object.defineProperty(
Object.prototype,
'let',
{
value: function (callback) {
return callback(this);
},
enumerable: false,
writable: false
}
@oskarbraten
oskarbraten / custom_shader_example.js
Created November 21, 2017 18:48
A three.js custom shader example
"use strict";
class MyCustomMaterial extends THREE.ShaderMaterial {
// constructor takes appropriate parameters.
// Default values using object destructuring (ES6)
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment#Object_destructuring
constructor({
color = 0xffffff,
emissive = 0x000000,