Skip to content

Instantly share code, notes, and snippets.

@markusfisch
Created September 24, 2016 19:14
Show Gist options
  • Save markusfisch/c159ccaebd83850c69751adfefcd202d to your computer and use it in GitHub Desktop.
Save markusfisch/c159ccaebd83850c69751adfefcd202d to your computer and use it in GitHub Desktop.
Convert Wavefront OBJ to JavaScript arrays

Convert Wavefront OBJ to JavaScript arrays

Naive bash script to convert a Wavefront OBJ (as created by Blender) into JavaScript arrays:

$ ./obj2js < model.obj > model.js

Will print something like:

var vertices = [
1.000000, -1.000000, -1.000000,
1.000000, -1.000000, 1.000000,
-1.000000, -1.000000, 1.000000,
-1.000000, -1.000000, -1.000000,
1.000000, 1.000000, -0.999999,
0.999999, 1.000000, 1.000001,
-1.000000, 1.000000, 1.000000,
-1.000000, 1.000000, -1.000000,
], indicies = [
0, 1, 2,
4, 7, 6,
0, 4, 5,
1, 5, 6,
2, 6, 7,
4, 0, 3,
]
#/usr/bin/env bash
FACES=1
echo "var vertices = ["
while read COMMAND A B C
do
case "$COMMAND" in
v)
for N in "$A" "$B" "$C"
do
if [ "$N" == '0.000000' ] || [ $N == '-0.000000' ]
then
N=0
fi
echo -n "$N, "
done
echo
;;
f)
(( FACES )) && {
echo "], indicies = ["
FACES=0
}
for N in "$A" "$B" "$C"
do
N=${N%%/*}
echo -n "$(( N-1 )), "
done
echo
;;
esac
done
echo "]"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment