@@ -210,7 +210,7 @@ private:
210210 case 0 :
211211 // STAGE 1 - Advanced blending
212212
213- glDrawBuffers(3 , [GL_COLOR_ATTACHMENT0 , GL_NONE , GL_NONE ].ptr);
213+ glDrawBuffers(1 , [GL_COLOR_ATTACHMENT0 ].ptr);
214214
215215 partShaderStage1.use();
216216 partShaderStage1.setUniform(gs1offset, data.origin);
@@ -225,23 +225,23 @@ private:
225225 case 1 :
226226
227227 // STAGE 2 - Basic blending (albedo, bump)
228- glDrawBuffers(3 , [GL_NONE , GL_COLOR_ATTACHMENT1 , GL_COLOR_ATTACHMENT2 ].ptr);
228+ glDrawBuffers(2 , [GL_COLOR_ATTACHMENT1 , GL_COLOR_ATTACHMENT2 ].ptr);
229229
230230 partShaderStage2.use();
231231 partShaderStage2.setUniform(gs2offset, data.origin);
232232 partShaderStage2.setUniform(gs2mvp, inGetCamera ().matrix * puppet.transform.matrix * matrix);
233233 partShaderStage2.setUniform(gs2opacity, clamp (offsetOpacity * opacity, 0 , 1 ));
234234 partShaderStage2.setUniform(gs2EmissionStrength, emissionStrength* offsetEmissionStrength);
235235
236- partShaderStage2.setUniform(partShaderStage2.getUniformLocation(" emission" ), 1 );
237- partShaderStage2.setUniform(partShaderStage2.getUniformLocation(" bump" ), 2 );
236+ partShaderStage2.setUniform(partShaderStage2.getUniformLocation(" emission" ), 0 );
237+ partShaderStage2.setUniform(partShaderStage2.getUniformLocation(" bump" ), 1 );
238238
239239 // These can be reused from stage 2
240240 partShaderStage1.setUniform(gs2MultColor, clampedTint);
241241 partShaderStage1.setUniform(gs2ScreenColor, clampedScreen);
242242 inSetBlendMode(blendingMode, true );
243243 break ;
244- case 3 :
244+ case 2 :
245245
246246 // Basic blending
247247 glDrawBuffers(3 , [GL_COLOR_ATTACHMENT0 , GL_COLOR_ATTACHMENT1 , GL_COLOR_ATTACHMENT2 ].ptr);
@@ -274,7 +274,7 @@ private:
274274
275275 }
276276
277- void renderStage () {
277+ void renderStage (bool advanced= true )(BlendMode mode ) {
278278
279279 // Enable points array
280280 glEnableVertexAttribArray(0 );
@@ -299,8 +299,10 @@ private:
299299 glDisableVertexAttribArray(1 );
300300 glDisableVertexAttribArray(2 );
301301
302- // Blending barrier
303- inBlendModeBarrier();
302+ static if (advanced) {
303+ // Blending barrier
304+ inBlendModeBarrier(mode);
305+ }
304306 }
305307
306308 /*
@@ -342,28 +344,37 @@ private:
342344 partMaskShader.setUniform(offset, data.origin);
343345 partMaskShader.setUniform(mmvp, inGetCamera ().matrix * puppet.transform.matrix * matrix);
344346 partMaskShader.setUniform(mthreshold, clamp (offsetMaskThreshold + maskAlphaThreshold, 0 , 1 ));
347+
348+ // Make sure the equation is correct
349+ glBlendEquation(GL_FUNC_ADD );
345350 glBlendFunc(GL_ONE , GL_ONE_MINUS_SRC_ALPHA );
346351
347- renderStage( );
352+ renderStage! false (blendingMode );
348353 } else {
349354
350- if (inUseMultistageBlending()) {
355+ bool hasEmissionOrBumpmap = (textures[1 ] || textures[2 ]);
356+
357+ if (inUseMultistageBlending(blendingMode)) {
351358
352359 // TODO: Detect if this Part is NOT in a composite,
353360 // If so, we can relatively safely assume that we may skip stage 1.
354361 setupShaderStage(0 , matrix);
355- renderStage();
356- setupShaderStage(1 , matrix);
357- renderStage();
358-
362+ renderStage(blendingMode);
363+
364+ // Only do stage 2 if we have emission or bumpmap textures.
365+ if (hasEmissionOrBumpmap) {
366+ setupShaderStage(1 , matrix);
367+ renderStage! false (blendingMode);
368+ }
359369 } else {
360370 setupShaderStage(2 , matrix);
361- renderStage( );
371+ renderStage! false (blendingMode );
362372 }
363373 }
364374
365375 // Reset draw buffers
366376 glDrawBuffers(3 , [GL_COLOR_ATTACHMENT0 , GL_COLOR_ATTACHMENT1 , GL_COLOR_ATTACHMENT2 ].ptr);
377+ glBlendEquation(GL_FUNC_ADD );
367378 }
368379
369380protected :
0 commit comments