Files
three.js/src/objects/Mesh.js
T
unknown 60c2354424 Added OBJ -> Three.js converter.
Added OBJ converter test example.

Modified Three.js to handle converted models:

 - extended WebGL renderer to use texturing
   - broke down model into multiple VBOs according to materials
   - textures are lazy created when images get loaded
     (converter takes care of resizing images to nearest power of 2
      dimensions using 2d canvas)

 - changed material array semantics in Mesh object
    - before: multiple materials were applied to all faces (broken in WebGL, needs multitexturing shader)
    - now: there is only single material per face, but one mesh can have faces with different materials

 - added per vertex normals (to get smooth shading in WebGL)
2010-10-18 11:33:32 +02:00

65 lines
1.5 KiB
JavaScript

/**
* @author mr.doob / http://mrdoob.com/
*/
THREE.Mesh = function ( geometry, material, normUVs ) {
THREE.Object3D.call( this );
this.geometry = geometry;
this.material = material instanceof Array ? material : [ material ];
this.flipSided = false;
this.doubleSided = false;
this.overdraw = false;
this.materialFaces = {};
this.sortFacesByMaterial();
if( normUVs ) this.normalizeUVs();
this.geometry.computeBoundingBox();
};
THREE.Mesh.prototype = new THREE.Object3D();
THREE.Mesh.prototype.constructor = THREE.Mesh;
THREE.Mesh.prototype.sortFacesByMaterial = function () {
var f, fl, face, material;
for ( f = 0, fl = this.geometry.faces.length; f < fl; f++ ) {
face = this.geometry.faces[ f ];
material = face.material;
if ( this.materialFaces[material] == undefined )
this.materialFaces[material] = { 'faces': [] };
this.materialFaces[material].faces.push( f );
}
}
THREE.Mesh.prototype.normalizeUVs = function () {
var i,j;
for ( i = 0, l = this.geometry.uvs.length; i < l; i++ ) {
var uvs = this.geometry.uvs[i];
for ( j = 0, jl = uvs.length; j < jl; j++ ) {
// texture repeat
// (WebGL does this by default but canvas renderer needs to do it explicitly)
if( uvs[j].u != 1.0 ) uvs[j].u = uvs[j].u - Math.floor(uvs[j].u);
if( uvs[j].v != 1.0 ) uvs[j].v = uvs[j].v - Math.floor(uvs[j].v);
}
}
}