diff options
Diffstat (limited to 'js/models/materials-model.js')
-rwxr-xr-x | js/models/materials-model.js | 85 |
1 files changed, 61 insertions, 24 deletions
diff --git a/js/models/materials-model.js b/js/models/materials-model.js index d8fb4016..291f7567 100755 --- a/js/models/materials-model.js +++ b/js/models/materials-model.js | |||
@@ -112,10 +112,34 @@ exports.MaterialsModel = Montage.create(Component, { | |||
112 | value: function (materialName) { | 112 | value: function (materialName) { |
113 | var index = this.getIndexOfMaterial(materialName); | 113 | var index = this.getIndexOfMaterial(materialName); |
114 | if(index !== -1) { | 114 | if(index !== -1) { |
115 | return this._materials[index]; | 115 | return this._materials[index].dup(); |
116 | } | 116 | } |
117 | } | 117 | } |
118 | }, | 118 | }, |
119 | |||
120 | getMaterialByShader: | ||
121 | { | ||
122 | value: function( shaderName ) | ||
123 | { | ||
124 | var index = this.getIndexOfMaterialByShader( shaderName ); | ||
125 | if (index >= 0) | ||
126 | return this._materials[index].dup(); | ||
127 | } | ||
128 | }, | ||
129 | |||
130 | getIndexOfMaterialByShader: { | ||
131 | value: function (shaderName) { | ||
132 | var len = this._materials.length; | ||
133 | for(var i=0; i<len; i++) { | ||
134 | var material = this._materials[i]; | ||
135 | if(material.getShaderName() === shaderName) { | ||
136 | return i; | ||
137 | } | ||
138 | } | ||
139 | |||
140 | return -1; | ||
141 | } | ||
142 | }, | ||
119 | 143 | ||
120 | getIndexOfMaterial: { | 144 | getIndexOfMaterial: { |
121 | value: function (materialName) { | 145 | value: function (materialName) { |
@@ -143,37 +167,54 @@ exports.MaterialsModel = Montage.create(Component, { | |||
143 | } | 167 | } |
144 | }, | 168 | }, |
145 | 169 | ||
146 | exportMaterials: { | 170 | getDefaultMaterialName: { |
147 | value: function() { | 171 | value: function() { |
172 | return "FlatMaterial"; | ||
173 | } | ||
174 | }, | ||
148 | 175 | ||
149 | var exportStr = "MaterialLibrary: \n"; | 176 | exportMaterials: { |
150 | 177 | value: function() | |
178 | { | ||
179 | var matArray = []; | ||
151 | var nMats = this._materials.length; | 180 | var nMats = this._materials.length; |
152 | |||
153 | for (var i=0; i<nMats; i++) { | 181 | for (var i=0; i<nMats; i++) { |
154 | var material = this._materials[i]; | 182 | var material = this._materials[i]; |
155 | exportStr += material.export(); | 183 | var matObj = material.exportJSON(); |
184 | matArray.push( matObj ); | ||
156 | } | 185 | } |
157 | 186 | ||
158 | exportStr += "endMatLib\n"; | 187 | var jObj = |
159 | return exportStr; | 188 | { |
189 | 'materialLibrary': 1.0, | ||
190 | 'materials': matArray | ||
191 | }; | ||
192 | |||
193 | // prepend an identifiable string to aid parsing when the | ||
194 | // material model is loaded. | ||
195 | var jStr = "materialLibrary;" + JSON.stringify( jObj ); | ||
196 | |||
197 | return jStr; | ||
160 | } | 198 | } |
161 | }, | 199 | }, |
162 | 200 | ||
163 | importMaterials: { | 201 | importMaterials: { |
164 | value: function( importStr ) { | 202 | value: function( jObj ) |
203 | { | ||
204 | // make sure we have some materials to import before doing anything | ||
205 | var matArray = jObj.materials; | ||
206 | if (!matArray) return; | ||
207 | |||
165 | // we replace allmaterials, so remove anything | 208 | // we replace allmaterials, so remove anything |
166 | // that is currently there. | 209 | // that is currently there. |
167 | this.clearAllMaterials(); | 210 | this.clearAllMaterials(); |
168 | 211 | ||
169 | var pu = new MaterialParser( importStr ); | 212 | var nMats = matArray.length; |
170 | 213 | for (var i=0; i<nMats; i++) | |
171 | var type = pu.nextValue( "material: ", "\n", false ); | 214 | { |
172 | 215 | var mat = null; | |
173 | while (type) { | 216 | var jMatObj = matArray[i]; |
174 | 217 | var type = jMatObj.material; | |
175 | var mat = null; | ||
176 | |||
177 | switch (type) | 218 | switch (type) |
178 | { | 219 | { |
179 | case "flat": mat = new FlatMaterial(); break; | 220 | case "flat": mat = new FlatMaterial(); break; |
@@ -203,20 +244,16 @@ exports.MaterialsModel = Montage.create(Component, { | |||
203 | 244 | ||
204 | default: | 245 | default: |
205 | throw new Error( "Unrecognized material type: " + type ); | 246 | throw new Error( "Unrecognized material type: " + type ); |
206 | pu.advancePastToken( "endMaterial\n" ); | ||
207 | break; | 247 | break; |
208 | } | 248 | } |
209 | 249 | ||
210 | if (mat) { | 250 | if (mat) { |
211 | importStr = mat.import( importStr ); | 251 | importStr = mat.importJSON( jMatObj ); |
212 | pu._strBuffer = importStr; | ||
213 | this.addMaterial( mat ); | 252 | this.addMaterial( mat ); |
214 | } | 253 | } |
215 | |||
216 | type = pu.nextValue( "material: ", "\n", false ); | ||
217 | } | 254 | } |
218 | 255 | ||
219 | return pu._strBuffer; | 256 | return; |
220 | } | 257 | } |
221 | } | 258 | } |
222 | 259 | ||