Skip to content

Instantly share code, notes, and snippets.

View donmccurdy's full-sized avatar

Don McCurdy donmccurdy

View GitHub Profile
@donmccurdy
donmccurdy / generate-points.ts
Created March 18, 2024 18:53
Generate a point cloud in glTF format, using glTF Transform.
import { Document, NodeIO, Primitive } from '@gltf-transform/core';
const document = new Document();
const buffer = document.createBuffer();
const position = document.createAccessor()
.setType('VEC3')
.setBuffer(buffer)
.setArray(new Float32Array([
0, 0, 0, // ax,ay,az
@donmccurdy
donmccurdy / vertex-count-method.ts
Created March 17, 2024 14:38
Draft enum, describing different methods for computing vertex counts.
/**
* Various methods of estimating a vertex count. For some background on why
* multiple definitions of a vertex count should exist, see [_Vertex Count
* Higher in Engine than in 3D Software_](https://shahriyarshahrabi.medium.com/vertex-count-higher-in-engine-than-in-3d-software-badc348ada66).
*
* NOTICE: Many rendering features, such as volumetric transmission, may lead
* to additional passes over some or all vertices. Such tradeoffs are
* implementation-dependent, and not considered here.
*/
export enum VertexCountMethod {
@donmccurdy
donmccurdy / scan-missing-dependencies.ts
Last active March 15, 2024 18:49
Scan a monorepo for missing or transitive dependencies in subpackages.
import {readFile} from 'node:fs/promises';
import {join} from 'node:path';
import glob from 'glob';
import builtinModules from 'builtin-modules';
const IMPORT_IDENTIFIER_REGEX = /(?:\A|\r|\n|\r\n)import[^'"]*['"]([\@\.\/\w_-]+)['"]/g;
const BUILTIN_MODULES = new Set<string>(builtinModules);
const pkgPaths = glob.sync(join('modules', '*', 'package.json'));
@donmccurdy
donmccurdy / ocio_tonemap_gen.sh
Created December 13, 2023 15:07
Applies tone mapping (display transform + look) to source OpenEXR images using Blender's AgX OCIO config.
#!/usr/bin/env bash
OCIO="/Applications/Blender 4.0.app/Contents/Resources/4.0/datafiles/colormanagement/config.ocio"
OCIO_LOOK="AgX - Base Contrast"
# Source: https://github.com/sobotka/Testing_Imagery
declare -a arr=(
"blue_bar_709"
"Matas_Alexa_Mini_sample_BT709"
"mery_lightSaber_lin_srgb"
@donmccurdy
donmccurdy / gen-torus.js
Last active August 19, 2023 13:54
Sample code for generating a torus-shaped GLB from scratch using glTF Transform.
import { Document, NodeIO } from '@gltf-transform/core';
function createTorus(radius = 1, tube = 0.4, radialSegments = 12, tubularSegments = 48, arc = Math.PI * 2) {
const indicesArray = [];
const positionArray = [];
const texcoordArray = [];
const vertex = [0, 0, 0];
// generate positions and uvs
@donmccurdy
donmccurdy / batch_process.sh
Created January 31, 2023 02:35
Example script for batch CLI processing with glTF Transform.
#!/bin/bash
FILES="./path/to/models/*.glb"
for file in $FILES; do
echo "Compressing $file..."
gltf-transform draco "${file%.glb}.glb" "${file%.glb}-draco.glb"
done
@donmccurdy
donmccurdy / EXTRACT_GEOMETRY.mjs
Last active September 27, 2022 04:22
Example Node.js script using glTF-Transform to extract geometry from a glTF document and create a geometry-only three.js scene graph.
import { NodeIO } from '@gltf-transform/core';
import { KHRONOS_EXTENSIONS } from '@gltf-transform/extensions';
import draco3d from 'draco3dgltf';
// set up I/O
const io = new NodeIO()
.registerExtensions(KHRONOS_EXTENSIONS)
.registerDependencies({'draco3d.decoder': await draco3d.createDecoderModule()});
const document = await io.read('path/to/file.glb');
@donmccurdy
donmccurdy / generate_lods.mjs
Last active October 20, 2023 01:32
Example implementation of MSFT_lod in glTF-Transform.
import { Extension, ExtensionProperty, Node, NodeIO, PropertyType } from '@gltf-transform/core';
import { weld, dedup, simplifyPrimitive } from '@gltf-transform/functions';
import { MeshoptSimplifier } from 'meshoptimizer';
/******************************************************************************
* Example implementation of MSFT_lod for glTF-Transform.
*
* ⚠️ NOTICE: This code is provided for the sake of example, and is not tested or
* maintained. For a full implementation, refer to:
* https://github.com/takahirox/glTF-Transform-lod-script
@donmccurdy
donmccurdy / esm-package.md
Created July 10, 2022 23:23 — forked from sindresorhus/esm-package.md
Pure ESM package

Pure ESM package

The package linked to from here is now pure ESM. It cannot be require()'d from CommonJS.

This means you have the following choices:

  1. Use ESM yourself. (preferred)
    Use import foo from 'foo' instead of const foo = require('foo') to import the package. You also need to put "type": "module" in your package.json and more. Follow the below guide.
  2. If the package is used in an async context, you could use await import(…) from CommonJS instead of require(…).
  3. Stay on the existing version of the package until you can move to ESM.
@donmccurdy
donmccurdy / PUBLIC_CLASS_PROPERTY_ORDER.md
Last active May 5, 2022 16:08
Initialization order and public class properties

Initialization order and public class properties

(A)

class Parent {
    constructor() {
        console.log(`${this.type}::constructor`)
    }
}