1 /** 2 * Copyright (C) 2009-2012 Klaus Reimer <k@ailis.de> 3 * See LICENSE.txt for licensing information 4 * 5 * @require threedee.js 6 */ 7 8 /** 9 * Constructs a new polygon 10 * 11 * @param {!Array.<number>} vertices 12 * The referenced vertices. 13 * @param {?threedee.Material=} material 14 * Optional polygon-specific material. If not set then the polygon 15 * uses the material of the model. 16 * @constructor 17 * @class 18 * A polygon. 19 */ 20 21 threedee.Polygon = function(vertices, material) 22 { 23 this.vertices = vertices; 24 if (material) this.material = material; 25 threedee.Polygon.counter++; 26 }; 27 28 /** 29 * Instance counter. 30 * @private 31 * @type {number} 32 */ 33 threedee.Polygon.counter = 0; 34 35 /** 36 * The referenced vertices. 37 * @private 38 * @type {!Array.<number>} 39 */ 40 threedee.Polygon.prototype.vertices; 41 42 /** 43 * The material used by this polygon. 44 * @private 45 * @type {?threedee.Material} 46 */ 47 threedee.Polygon.prototype.material = null; 48 49 /** 50 * Returns and resets the current instance counter. 51 * 52 * @return {number} The number of created instances since the last call 53 */ 54 threedee.Polygon.count = function() 55 { 56 var value = threedee.Polygon.counter; 57 threedee.Polygon.counter = 0; 58 return value; 59 }; 60 61 /** 62 * Returns the number of referenced vertices. 63 * 64 * @return {number} The number of references vertices 65 */ 66 threedee.Polygon.prototype.countVertices = function() 67 { 68 return this.vertices.length; 69 }; 70 71 /** 72 * Returns the vertex with the specified index. 73 * 74 * @param {number} index 75 * The index 76 * @return {number} The vertex 77 */ 78 threedee.Polygon.prototype.getVertex = function(index) 79 { 80 return this.vertices[index]; 81 }; 82 83 /** 84 * Returns the material. Returns null if this polygon is not a 85 * polygon-specific material and uses the one of the model instead. 86 * 87 * @return {threedee.Material} The material 88 */ 89 threedee.Polygon.prototype.getMaterial = function() 90 { 91 return this.material; 92 }; 93 94 /** 95 * Sets the material. Set it to null to remove the polygon-specific 96 * material. The model material is used then. 97 * 98 * @param {threedee.Material} material 99 * The material to set 100 */ 101 threedee.Polygon.prototype.setMaterial = function(material) 102 { 103 this.material = material; 104 }; 105 106 /** 107 * Converts the polygon into a JSON object with keys 'v' (Array with vertex 108 * indices) and optionally 'm' (The polygon-specific material). 109 * 110 * @return {Object} The polygon as a JSON object 111 */ 112 threedee.Polygon.prototype.toJSON = function() 113 { 114 var data; 115 116 data = { "v": this.vertices }; 117 if (this.material) data.m = this.material.toJSON(); 118 return data; 119 }; 120 121 /** 122 * Creates a new polygon instance with the data read from the 123 * specified JSON object (with keys 'v' and 'm'). Returns null if data 124 * was empty. 125 * 126 * @param {Object} data 127 * The polygon as JSON object 128 * @return {threedee.Polygon} 129 * The polygon object or null if data was empty. 130 */ 131 threedee.Polygon.fromJSON = function(data) 132 { 133 if (!data) return null; 134 return new threedee.Polygon( 135 (/** @type {!Array.<number>} */ data["v"]), 136 threedee.Material.fromJSON(data["m"])); 137 }; 138