Skip to content

Commit edb04e1

Browse files
committed
Add support for flatShading
Closes #292
1 parent 83b6d19 commit edb04e1

1 file changed

Lines changed: 103 additions & 18 deletions

File tree

‎src/core/OverrideMaterialManager.js‎

Lines changed: 103 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ export class OverrideMaterialManager {
4040
this.originalMaterials = new Map();
4141

4242
/**
43-
* The override material.
43+
* The main override material.
4444
*
4545
* @type {Material}
4646
* @private
@@ -49,7 +49,7 @@ export class OverrideMaterialManager {
4949
this.material = null;
5050

5151
/**
52-
* Override materials for meshes that use standard front side triangles.
52+
* Override materials for meshes with front side triangles.
5353
*
5454
* @type {Material[]}
5555
* @private
@@ -58,7 +58,7 @@ export class OverrideMaterialManager {
5858
this.materials = null;
5959

6060
/**
61-
* Override materials for meshes that use back side triangles.
61+
* Override materials for meshes with back side triangles.
6262
*
6363
* @type {Material[]}
6464
* @private
@@ -67,14 +67,41 @@ export class OverrideMaterialManager {
6767
this.materialsBackSide = null;
6868

6969
/**
70-
* Override materials for meshes that use double sided triangles.
70+
* Override materials for meshes with double sided triangles.
7171
*
7272
* @type {Material[]}
7373
* @private
7474
*/
7575

7676
this.materialsDoubleSide = null;
7777

78+
/**
79+
* Override materials for flat shaded meshes with front side triangles.
80+
*
81+
* @type {Material[]}
82+
* @private
83+
*/
84+
85+
this.materialsFlatShaded = null;
86+
87+
/**
88+
* Override materials for flat shaded meshes with back side triangles.
89+
*
90+
* @type {Material[]}
91+
* @private
92+
*/
93+
94+
this.materialsFlatShadedBackSide = null;
95+
96+
/**
97+
* Override materials for flat shaded meshes with double sided triangles.
98+
*
99+
* @type {Material[]}
100+
* @private
101+
*/
102+
103+
this.materialsFlatShadedDoubleSide = null;
104+
78105
this.setMaterial(material);
79106

80107
/**
@@ -99,19 +126,41 @@ export class OverrideMaterialManager {
99126

100127
let materials;
101128

102-
switch(node.material.side) {
129+
if(node.material.flatShading) {
103130

104-
case DoubleSide:
105-
materials = this.materialsDoubleSide;
106-
break;
131+
switch(node.material.side) {
132+
133+
case DoubleSide:
134+
materials = this.materialsFlatShadedDoubleSide;
135+
break;
136+
137+
case BackSide:
138+
materials = this.materialsFlatShadedBackSide;
139+
break;
140+
141+
default:
142+
materials = this.materialsFlatShaded;
143+
break;
144+
145+
}
146+
147+
} else {
107148

108-
case BackSide:
109-
materials = this.materialsBackSide;
110-
break;
149+
switch(node.material.side) {
111150

112-
default:
113-
materials = this.materials;
114-
break;
151+
case DoubleSide:
152+
materials = this.materialsDoubleSide;
153+
break;
154+
155+
case BackSide:
156+
materials = this.materialsBackSide;
157+
break;
158+
159+
default:
160+
materials = this.materials;
161+
break;
162+
163+
}
115164

116165
}
117166

@@ -152,13 +201,14 @@ export class OverrideMaterialManager {
152201

153202
if(material !== null) {
154203

155-
// Create materials for simple, skinned and instanced meshes.
204+
// Create materials for simple, instanced and skinned meshes.
156205
const materials = this.materials = [
157206
material.clone(),
158207
material.clone(),
159208
material.clone()
160209
];
161210

211+
// FrontSide
162212
for(const m of materials) {
163213

164214
m.uniforms = Object.assign({}, material.uniforms);
@@ -168,7 +218,7 @@ export class OverrideMaterialManager {
168218

169219
materials[2].skinning = true;
170220

171-
// Create additional materials for meshes that use BackSide.
221+
// BackSide
172222
this.materialsBackSide = materials.map((m) => {
173223

174224
const c = m.clone();
@@ -178,7 +228,7 @@ export class OverrideMaterialManager {
178228

179229
});
180230

181-
// Create additional materials for meshes that use DoubleSide.
231+
// DoubleSide
182232
this.materialsDoubleSide = materials.map((m) => {
183233

184234
const c = m.clone();
@@ -188,6 +238,38 @@ export class OverrideMaterialManager {
188238

189239
});
190240

241+
// FrontSide & flatShading
242+
this.materialsFlatShaded = materials.map((m) => {
243+
244+
const c = m.clone();
245+
c.uniforms = Object.assign({}, material.uniforms);
246+
c.flatShading = true;
247+
return c;
248+
249+
});
250+
251+
// BackSide & flatShading
252+
this.materialsFlatShadedBackSide = materials.map((m) => {
253+
254+
const c = m.clone();
255+
c.uniforms = Object.assign({}, material.uniforms);
256+
c.flatShading = true;
257+
c.side = BackSide;
258+
return c;
259+
260+
});
261+
262+
// DoubleSide & flatShading
263+
this.materialsFlatShadedDoubleSide = materials.map((m) => {
264+
265+
const c = m.clone();
266+
c.uniforms = Object.assign({}, material.uniforms);
267+
c.flatShading = true;
268+
c.side = DoubleSide;
269+
return c;
270+
271+
});
272+
191273
}
192274

193275
}
@@ -252,7 +334,10 @@ export class OverrideMaterialManager {
252334

253335
const materials = this.materials
254336
.concat(this.materialsBackSide)
255-
.concat(this.materialsDoubleSide);
337+
.concat(this.materialsDoubleSide)
338+
.concat(this.materialsFlatShaded)
339+
.concat(this.materialsFlatShadedBackSide)
340+
.concat(this.materialsFlatShadedDoubleSide);
256341

257342
for(const m of materials) {
258343

0 commit comments

Comments
 (0)