Skip to content

Instantly share code, notes, and snippets.

View jimfleming's full-sized avatar

Jim Fleming jimfleming

View GitHub Profile
@jimfleming
jimfleming / UnityDiffuseLightmap.shader
Last active June 21, 2023 00:44
Example depicting applying Unity lightmapping data to a simple diffuse shader.
Shader "Diffuse Lightmap" {
Properties {
_MainTex ("Texture 1", 2D) = "white" {}
}
SubShader {
Tags { "RenderType" = "Opaque" }
Pass {
@jimfleming
jimfleming / sciblur.py
Created July 3, 2013 17:08
Gaussian blur using scipy
from math import ceil
import pygtk
pygtk.require('2.0')
import gtk
import numpy as np
from scipy import ndimage
@jimfleming
jimfleming / blur.py
Created July 3, 2013 17:08
Naive blur implementation
from math import sqrt, exp, pi
import pygtk
pygtk.require('2.0')
import gtk
import numpy as np
def make_kernel(radius):
width = radius * 2 + 1
@jimfleming
jimfleming / bounds.cs
Created June 25, 2013 04:18
Sizes a bounding box to its children.
BoxCollider collider = gameObject.AddComponent<BoxCollider>();
Bounds parentBounds = new Bounds(Vector3.zero, Vector3.zero);
Bounds childBounds = new Bounds(Vector3.zero, Vector3.zero);
MeshFilter[] meshFilters = transform.GetComponentsInChildren<MeshFilter>();
foreach (MeshFilter meshFilter in meshFilters) {
childBounds.center = transform.InverseTransformPoint(meshFilter.renderer.bounds.center);
childBounds.size = meshFilter.sharedMesh.bounds.size;
parentBounds.Encapsulate(temp);
}
@jimfleming
jimfleming / approx_tanh.go
Last active December 17, 2015 18:18
Approximate hyperbolic tangent function (-15x-, nevermind, post Go 1.1 only 2x faster than `math.tanh`)
func ApproxTanh(x float64) float64 {
if x > 21 {
return 1
}
if x < -21 {
return -1
}
if x == +0 {
return +0
}
@jimfleming
jimfleming / genome.coffee
Created April 8, 2013 04:37
Reads a 23andme genome download into redis
fs = require 'fs'
path = require 'path'
redis = require('redis').createClient()
multi = redis.multi()
console.log 'Reading...'
fileName = process.argv.slice(-1)[0]
filePath = path.join process.cwd, fileName
fileStream = fs.createReadStream filePath
@jimfleming
jimfleming / set.go
Created April 5, 2013 05:13
A simple set data structure in go.
package set
type Set map[interface{}]struct{}
func (this *Set) Contains(m *interface{}) (exists bool) {
s := *this
_, exists = s[m]
return
}
@jimfleming
jimfleming / levenshtein.coffee
Created August 15, 2012 15:44
Fuzzy string comparison
# Based on: http://seatgeek.com/blog/dev/fuzzywuzzy-fuzzy-string-matching-in-python
_ = require 'underscore'
token_regex = /\w+/g
trim_regex = /^\s+|\s+$/g
levenshtein_dist = (a, b) ->
throw new TypeError('a is empty') unless _.isString(a)
throw new TypeError('b is empty') unless _.isString(b)
@jimfleming
jimfleming / rand_data.coffee
Created January 23, 2012 04:31
Generates random data for a redis database
#!/usr/bin/env coffee
guid = (length = 32) ->
chars = '0123456789abcdef'
result = ''
while length--
result += chars[Math.floor(Math.random() * chars.length)]
return result
@jimfleming
jimfleming / _.missing
Created April 20, 2011 01:48
returns an object describing any missing attributes nested within an object
_.mixin({
missing: function(obj, requirements) {
var missing = {};
_.each(requirements, function(requirement, field) {
if (_.isNull(obj[field]) || _.isUndefined(obj[field])) {
missing[field] = false;
return;
}
if (typeof obj[field] === 'object') {
potential = _.missing(obj[field], requirement);