Skip to content

Instantly share code, notes, and snippets.

@hapticdata
Created January 17, 2013 05:06
Show Gist options
  • Save hapticdata/4553801 to your computer and use it in GitHub Desktop.
Save hapticdata/4553801 to your computer and use it in GitHub Desktop.
convert a toxiclibs.js trianglemesh into a three.js BufferGeometry, needs testing
define(function( require ){
var THREE = require('three');
/**
* take a toxiclibs.js mesh and turn it into a THREE.BufferGeometry
* @param {toxi.geom.mesh.TriangleMesh} mesh
* @type {THREE.BufferGeometry}
*/
return function( mesh ){
var geometry = new THREE.BufferGeometry();
//3 points per face, 3 axis per point
var totalFloats = mesh.faces.length * 3 * 3;
var vertices = new Float32Array( totalFloats );
var normals = new Float32Array( totalFloats );
//start at 0, stride with 3
mesh.getMeshAsVertexArray( vertices, 0, 3 );
mesh.getFaceNormalsAsArray( normals, 0, 3 );
geometry.attributes = {
position: {
itemSize: 3,
array: vertices,
numItems: totalFloats
},
normal: {
itemSize: 3,
array: normals,
numItems: totalFloats
}
};
return geometry;
};
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment