Mesh.js 2.6 KB
Newer Older
M
Mr.doob 已提交
1 2 3 4
/**
 * @author mr.doob / http://mrdoob.com/
 */

U
unknown 已提交
5
THREE.Mesh = function ( geometry, material, normUVs ) {
6

M
Mr.doob 已提交
7
	THREE.Object3D.call( this );
M
Mr.doob 已提交
8 9

	this.geometry = geometry;
M
Mr.doob 已提交
10
	this.material = material instanceof Array ? material : [ material ];
11

M
Mr.doob 已提交
12
	this.flipSided = false;
M
Mr.doob 已提交
13 14
	this.doubleSided = false;

M
Mr.doob 已提交
15 16
	this.overdraw = false;

17 18 19 20 21
	this.materialFaceGroup = {};
	this.sortFacesByMaterial();
	if( normUVs ) this.normalizeUVs();

	this.geometry.computeBoundingBox();
U
unknown 已提交
22

M
Mr.doob 已提交
23
};
24

M
Mr.doob 已提交
25 26
THREE.Mesh.prototype = new THREE.Object3D();
THREE.Mesh.prototype.constructor = THREE.Mesh;
U
unknown 已提交
27 28

THREE.Mesh.prototype.sortFacesByMaterial = function () {
29 30 31 32 33 34

	// TODO
	// Should optimize by grouping faces with ColorFill / ColorStroke materials
	// which could then use vertex color attributes instead of each being
	// in its separate VBO

35
	var i, l, f, fl, face, material, vertices, mhash, ghash, hash_map = {};
36 37 38

	function materialHash( material ) {

39
		var hash_array = [];
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54

		for ( i = 0, l = material.length; i < l; i++ ) {

			if ( material[ i ] == undefined ) {

				hash_array.push( "undefined" );

			} else {

				hash_array.push( material[ i ].toString() );

			}

		}

55
		return hash_array.join( '_' );
56 57 58 59 60 61 62

	}

	for ( f = 0, fl = this.geometry.faces.length; f < fl; f++ ) {

		face = this.geometry.faces[ f ];
		material = face.material;
63

64
		mhash = materialHash( material );
65

66
		if ( hash_map[ mhash ] == undefined ) {
67

68
			hash_map[ mhash ] = { 'hash': mhash, 'counter': 0 };
69

70 71
		}

72
		ghash = hash_map[ mhash ].hash + '_' + hash_map[ mhash ].counter;
73

74
		if ( this.materialFaceGroup[ ghash ] == undefined ) {
75

76
			this.materialFaceGroup[ ghash ] = { 'faces': [], 'material': material, 'vertices': 0 };
77 78

		}
79

80 81 82
		vertices = face instanceof THREE.Face3 ? 3 : 4;

		if ( this.materialFaceGroup[ ghash ].vertices + vertices > 65535 ) {
83

84
			hash_map[ mhash ].counter += 1;
85 86
			ghash = hash_map[ mhash ].hash + '_' + hash_map[ mhash ].counter;

87 88 89
			if ( this.materialFaceGroup[ ghash ] == undefined ) {

				this.materialFaceGroup[ ghash ] = { 'faces': [], 'material': material, 'vertices': 0 };
90

91
			}
92

93
		}
94

95 96
		this.materialFaceGroup[ ghash ].faces.push( f );
		this.materialFaceGroup[ ghash ].vertices += vertices;
97

98 99 100

	}

101
};
102

U
unknown 已提交
103 104
THREE.Mesh.prototype.normalizeUVs = function () {

105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
	var i, il, j, jl, uvs;

	for ( i = 0, il = this.geometry.uvs.length; i < il; i++ ) {

		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 );

		}

	}

123
};