Skip to content

Instantly share code, notes, and snippets.

@eliemichel
eliemichel / StructuredAccessorFactory.js
Last active November 14, 2024 01:34
Javascript view builder to interpret raw ArrayBuffer data as structured data
// Javascript view builder to interpret raw ArrayBuffer data as structured data.
// Copyright 2024 (c) Élie Michel - MIT Licensed
// NB: This is loosly inspired by https://github.com/kainino0x/structured-accessor
const kTypedArrayConstructors = {
i8: Int8Array,
u8: Uint8Array,
i16: Int16Array,
u16: Uint16Array,
i32: Int32Array,
@eliemichel
eliemichel / packHalf2x16.py
Created December 29, 2023 01:20
An efficient numpy implementation of GLSL packHalf2x16 and unpackHalf2x16
"""
An efficient numpy implementation of GLSL packHalf2x16 and unpackHalf2x16.
Author: Elie Michel <elie.michel@exppad.com>
License: MIT - 2023
"""
def packHalf2x16(x, y):
"""
Pack two floats into a single uint32, like GLSL's packHalf2x16 function
(but vectorized for numpy arrays).
@eliemichel
eliemichel / webgpu-raii.h
Created May 14, 2023 10:58
An example of RAII wrapper for WebGPU's wgpu::Buffer
// Using https://github.com/eliemichel/WebGPU-Cpp
#include <webgpu/webgpu.hpp>
namespace raii {
class Buffer {
public:
// Whenever a RAII instance is created, we create an underlying resource
Buffer(wgpu::Device device, const wgpu::BufferDescriptor& bufferDesc)
: m_raw(device.createBuffer(bufferDesc))
{}
@eliemichel
eliemichel / save_texture.h
Last active July 2, 2023 13:46
A single-header utility file to save a given WebGPU Texture MIP level into an image
#include "stb_image_write.h"
#include <webgpu/webgpu.hpp>
#include <filesystem>
#include <string>
bool saveTexture(const std::filesystem::path path, wgpu::Device device, wgpu::Texture texture, int mipLevel) {
using namespace wgpu;
@eliemichel
eliemichel / save_texture_view.h
Last active April 22, 2023 08:47
A single-header utility file to save a WebGPU TextureView into an image
#include "stb_image_write.h"
#include <webgpu/webgpu.hpp>
#include <filesystem>h
#include <string>
std::filesystem::path resolvePath(int frame) {
std::filesystem::path base = "render/frame" + std::to_string(frame) + ".png";
create_directories(base.parent_path());
@eliemichel
eliemichel / local_import.py
Last active October 18, 2022 21:55
Import a module from another text in the same .blend file
def local_import(module_name):
exec('\n'.join([l.body for l in bpy.data.texts[module_name].lines]))
for k, v in locals().items():
globals()[k] = v
# Example
import bpy
import bmesh
import numpy as np
local_import('tiny_timer.py')
@eliemichel
eliemichel / tiny_timer.py
Created October 18, 2022 21:46
A minimal profiling module
# Credits 2020-2022 to Élie Michel <elie.michel@exppad.com>
# Released in Public Domain
#
# The Software is provided "as is", without warranty of any kind, express or
# implied, including but not limited to the warranties of merchantability,
# fitness for a particular purpose and non-infringement. In no event shall the
# authors or copyright holders be liable for any claim, damages or other
# liability, whether in an action of contract, tort or otherwise, arising
# from, out of or in connection with the software or the use or other dealings
# in the Software.
@eliemichel
eliemichel / blender_duplicate_node.py
Last active May 30, 2022 17:06
Snippet for reproducing the Shift+D in a node graph editor in Blender
import bpy
def duplicate_node(n1, node_group=None, duplicate_links=False):
"""
Duplicate a node (without duplicating its links)
@param n1 The node to duplicate
@param node_group (optional) Group in which the node must be duplicated
@param duplicate_links Whether to keep input links or not
@return the new duplicate node
"""
// [...]
/**
* Create a new Mesh representing a simple pizza (really just a disc with
* a few quads on top of it).
* olive_count is the number of olives topping the pizza
* radius is the radius of the pizza
* base_polys is filled with the range of polygons belonging to the base
* olive_polys is filled with the range of polygons representing the olives
*/
@eliemichel
eliemichel / git_resolve.py
Last active September 9, 2024 14:20
Resolve conflicted areas of a file in favor of theirs or ours and leave correctly merged areas as is
# This is an automation of https://stackoverflow.com/questions/71783590/git-merge-strategy-option-theirs-for-individual-files
# Example:
# git_resolve.py --theirs path/to/some/file
import os
import argparse
from shutil import copyfile
import subprocess
parser = argparse.ArgumentParser(description='Resolve conflicted areas of a file during a git merge')