Skip to content

Instantly share code, notes, and snippets.

View greggman's full-sized avatar
😁

Greggman greggman

😁
View GitHub Profile
<script>
(function() {
var insert = function(e) {
var elem = document.createElement("pre");
elem.appendChild(document.createTextNode(Array.prototype.join.call(arguments, " ")));
document.body.insertBefore(elem, document.body.firstChild);
};
window.addEventListener('error', insert);
window.console.log = function(origFn) {
return function() {
var gl = document.createElement("canvas").getContext("webgl");
var tex = gl.createTexture();
gl.bindTexture(gl.TEXTURE_2D, tex);
var fb = gl.createFramebuffer();
gl.bindFramebuffer(gl.FRAMEBUFFER, fb);
gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, tex, 0);
[
"ALPHA",
"LUMINANCE",
"LUMINANCE_ALPHA",
@greggman
greggman / test.jslib
Last active February 4, 2016 10:51
unity js issue
var test = {
useClosure: (function() {
var callCount_ = 0;
return function() {
console.log("been called: ", ++callCount_);
};
}()),
};
@greggman
greggman / modal dialog
Created May 11, 2016 09:47
Simple modal dialog for Unity / Unity3D
using UnityEngine;
using System;
// example:
// HFTDialog.MessageBox("error", "Sorry but you're S.O.L", () => { Application.Quit() });
public class HFTDialog : MonoBehaviour {
Rect m_windowRect;
Action m_action;
@greggman
greggman / Importer.md
Created August 6, 2016 09:32
A simple importer for Unity

Assume you have a custom file you want to create an importer for. It could be an .xls file or whatever. In this case we're going to use a JSON file because it's easy but we're going to pick a custom extension to make it easy to tell which files are ours?

Let's assume the format of the JSON file is

{
  "someValue": 123,
  "someOtherValue": 456.297,
  "someBoolValue": true,
  "someStringValue": "this is a string",

}

function PseudoRandom() {
var randomSeed_ = 0;
var RANDOM_RANGE_ = Math.pow(2, 32);
/**
* Returns a deterministic pseudorandom number between 0 and 1
* @return {number} a random number between 0 and 1
*/
this.random = function() {
@greggman
greggman / debug.js
Last active December 13, 2016 09:16
A colorful JavaScript logger - similar to npm/debug
// example of making something like npm's debug module inside electron
const makeLogFunc = require('./logger');
const s_debugRE = function(debug) {
debug = debug || '-----';
const patterns = debug.split(',').map(pattern => {
return pattern.replace(/\*/, '.*?') || '----';
});
const pattern = '^(' + patterns.join('|') + ')$';
@greggman
greggman / sed-in-place.sh
Created December 14, 2016 14:38
osx sed in place with newline of files matching grep
grep -l -R --include="*.js" --include="*.html" webglLessonsHelper.setupSlider . | xargs -n 1 sed -E -i '' $'s/<script src="([^"]+)\\/webgl-utils.js"/<script src="\\1\\/webgl-lessons-ui.js"><\\/script>\\\n<script src="\\1\\/webgl-utils.js"/'
@greggman
greggman / half.js
Created January 4, 2017 08:19
Convert to from half to number in JavaScript
// from http://stackoverflow.com/questions/6162651/half-precision-floating-point-in-java/6162687#6162687
var toHalf = (function() {
var floatView = new Float32Array(1);
var int32View = new Int32Array(floatView.buffer);
return function toHalf(fval) {
floatView[0] = fval;
var fbits = int32View[0];
var sign = (fbits >> 16) & 0x8000; // sign only
@greggman
greggman / example.js
Created August 3, 2017 09:32
mod your shader in js
function getShaderSource(needsBGRA) {
const twizzle = needsBGRA ? '.bgra' : '';
return `
...
imageColor = texture2D(u_image, v_image_coordinates)${twizzle};
...
`;
}