1 /**
  2  * Copyright (C) 2009-2012 Klaus Reimer <k@ailis.de>
  3  * See LICENSE.txt for licensing information
  4  * 
  5  * @require threedee.js
  6  * @require threedee/Color.js
  7  */
  8 
  9 /**
 10  * Constructs a new Material with the specified properties.
 11  * 
 12  * @param {threedee.Color=} ambient
 13  *            Optional ambient color. Defaults to white.
 14  * @param {threedee.Color=} diffuse
 15  *            Optional diffuse color. Defaults to white.
 16  * @param {threedee.Color=} emissive
 17  *            Optional emissive color. Defaults to black.
 18  *            
 19  * @constructor
 20  * @class 
 21  * A material.
 22  */
 23 threedee.Material = function(ambient, diffuse, emissive)
 24 {
 25     if (ambient) this.ambient = ambient;
 26     if (diffuse) this.diffuse = diffuse;
 27     if (emissive) this.emissive = emissive;
 28     threedee.Material.counter++;
 29 };
 30 
 31 /** 
 32  * Instance counter. 
 33  * @private 
 34  * @type {number} 
 35  */
 36 threedee.Material.counter = 0; 
 37     
 38 /** 
 39  * The default material. 
 40  * @final 
 41  * @type {!threedee.Material} 
 42  */
 43 threedee.Material.DEFAULT = new threedee.Material();
 44 
 45 /** 
 46  * The ambient color. 
 47  * @private 
 48  * @type {!threedee.Color} 
 49  */
 50 threedee.Material.prototype.ambient = threedee.Color.WHITE;
 51 
 52 /** 
 53  * The emissive color. 
 54  * @private
 55  * @type {!threedee.Color} 
 56  */
 57 threedee.Material.prototype.diffuse = threedee.Color.WHITE;
 58 
 59 /** 
 60  * The diffuse color. 
 61  * @private 
 62  * @type {!threedee.Color} 
 63  */
 64 threedee.Material.prototype.emissive = threedee.Color.BLACK;
 65 
 66 /**
 67  * Returns and resets the current instance counter.
 68  * 
 69  * @return {number} The number of created instances since the last call
 70  */
 71 threedee.Material.count = function()
 72 {
 73     var value = threedee.Material.counter;
 74     threedee.Material.counter = 0;
 75     return value;
 76 };
 77 
 78 
 79 /**
 80  * Returns the ambient color.
 81  * 
 82  * @return {threedee.Color} The ambient color
 83  */
 84 
 85 threedee.Material.prototype.getAmbient = function()
 86 {
 87     return this.ambient;
 88 };
 89 
 90 
 91 /**
 92  * Returns the diffuse color.
 93  * 
 94  * @return {threedee.Color} The diffuse color
 95  */
 96 
 97 threedee.Material.prototype.getDiffuse = function()
 98 {
 99     return this.diffuse;
100 };
101 
102 
103 /**
104  * Returns the emissive color.
105  * 
106  * @return {threedee.Color} The emissive color
107  */
108 
109 threedee.Material.prototype.getEmissive = function()
110 {
111     return this.emissive;
112 };
113 
114 
115 /**
116  * Converts the material into a JSON object with keys 'a', 'd' and 'e' and
117  * returns it.
118  * 
119  * @return {Object} The material as a JSON object
120  */
121 
122 threedee.Material.prototype.toJSON = function()
123 {
124     return { "a": this.ambient.toJSON(), "d": this.diffuse.toJSON(),
125         "e": this.emissive.toJSON() };
126 };
127 
128 
129 /**
130  * Creates a new material instance with the data read from the
131  * specified JSON object (with keys 'a', 'd' and 'e'). Returns null if data
132  * was empty.
133  * 
134  * @param {Object} data
135  *            The material as JSON object
136  * @return {threedee.Material} The material object or null if data was empty
137  */
138 
139 threedee.Material.fromJSON = function(data)
140 {
141     if (!data) return null;
142     return new threedee.Material(
143         threedee.Color.fromJSON(data["a"]),
144         threedee.Color.fromJSON(data["d"]),
145         threedee.Color.fromJSON(data["e"]));
146 };
147