Browse Source

Merge pull request #6974 from sebavan/master

Supports custom glow materials.
David Catuhe 5 years ago
parent
commit
4483aef4df
3 changed files with 62 additions and 3 deletions
  1. 1 0
      dist/preview release/what's new.md
  2. 28 1
      src/Layers/effectLayer.ts
  3. 33 2
      src/Layers/glowLayer.ts

+ 1 - 0
dist/preview release/what's new.md

@@ -39,6 +39,7 @@
 - Added support for 180 VR videos in `VideoDome` ([RaananW](https://github.com/RaananW/))
 - Added optional parameter to use Euler angles in planeRotationGizmo ([CedricGuillemet](https://github.com/CedricGuillemet))
 - Added `AnimationGroup.onAnimationGroupLoopObservable` ([Deltakosh](https://github.com/deltakosh/))
+- Supports custom materials to generate glow through ```referenceMeshToUseItsOwnMaterial``` in the ```GlowLayer``` ([sebavan](http://www.github.com/sebavan))
 
 ### Engine
 

+ 28 - 1
src/Layers/effectLayer.ts

@@ -141,6 +141,16 @@ export abstract class EffectLayer {
     public onBeforeComposeObservable = new Observable<EffectLayer>();
 
     /**
+     * An event triggered when the mesh is rendered into the effect render target.
+     */
+    public onBeforeRenderMeshToEffect = new Observable<AbstractMesh>();
+
+    /**
+     * An event triggered after the mesh has been rendered into the effect render target.
+     */
+    public onAfterRenderMeshToEffect = new Observable<AbstractMesh>();
+
+    /**
      * An event triggered when the generated texture has been merged in the scene.
      */
     public onAfterComposeObservable = new Observable<EffectLayer>();
@@ -677,7 +687,12 @@ export abstract class EffectLayer {
 
         this._setEmissiveTextureAndColor(mesh, subMesh, material);
 
-        if (this._isReady(subMesh, hardwareInstancedRendering, this._emissiveTextureAndColor.texture)) {
+        this.onBeforeRenderMeshToEffect.notifyObservers(mesh);
+
+        if (this._useMeshMaterial(mesh)) {
+            mesh.render(subMesh, hardwareInstancedRendering);
+        }
+        else if (this._isReady(subMesh, hardwareInstancedRendering, this._emissiveTextureAndColor.texture)) {
             engine.enableEffect(this._effectLayerMapGenerationEffect);
             mesh._bind(subMesh, this._effectLayerMapGenerationEffect, Material.TriangleFillMode);
 
@@ -752,6 +767,16 @@ export abstract class EffectLayer {
             // Need to reset refresh rate of the main map
             this._mainTexture.resetRefreshCounter();
         }
+
+        this.onAfterRenderMeshToEffect.notifyObservers(mesh);
+    }
+
+    /**
+     * Defines wether the current material of the mesh should be use to render the effect.
+     * @param mesh defines the current mesh to render
+     */
+    protected _useMeshMaterial(mesh: AbstractMesh): boolean {
+        return false;
     }
 
     /**
@@ -819,6 +844,8 @@ export abstract class EffectLayer {
         this.onDisposeObservable.clear();
         this.onBeforeRenderMainTextureObservable.clear();
         this.onBeforeComposeObservable.clear();
+        this.onBeforeRenderMeshToEffect.clear();
+        this.onAfterRenderMeshToEffect.clear();
         this.onAfterComposeObservable.clear();
         this.onSizeChangedObservable.clear();
     }

+ 33 - 2
src/Layers/glowLayer.ts

@@ -85,8 +85,7 @@ export interface IGlowLayerOptions {
 /**
  * The glow layer Helps adding a glow effect around the emissive parts of a mesh.
  *
- * Once instantiated in a scene, simply use the addMesh or removeMesh method to add or remove
- * glowy meshes to your scene.
+ * Once instantiated in a scene, by default, all the emissive meshes will glow.
  *
  * Documentation: https://doc.babylonjs.com/how_to/glow_layer
  */
@@ -154,6 +153,7 @@ export class GlowLayer extends EffectLayer {
 
     private _includedOnlyMeshes: number[] = [];
     private _excludedMeshes: number[] = [];
+    private _meshesUsingTheirOwnMaterials: number[] = [];
 
     /**
      * Callback used to let the user override the color selection on a per mesh basis
@@ -512,6 +512,37 @@ export class GlowLayer extends EffectLayer {
     }
 
     /**
+     * Defines wether the current material of the mesh should be use to render the effect.
+     * @param mesh defines the current mesh to render
+     */
+    protected _useMeshMaterial(mesh: AbstractMesh): boolean {
+        if (this._meshesUsingTheirOwnMaterials.length == 0) {
+            return false;
+        }
+        return this._meshesUsingTheirOwnMaterials.indexOf(mesh.uniqueId) > -1;
+    }
+
+    /**
+     * Add a mesh to be rendered through its own material and not with emissive only.
+     * @param mesh The mesh for which we need to use its material
+     */
+    public referenceMeshToUseItsOwnMaterial(mesh: AbstractMesh): void {
+        this._meshesUsingTheirOwnMaterials.push(mesh.uniqueId);
+    }
+
+    /**
+     * Remove a mesh from being rendered through its own material and not with emissive only.
+     * @param mesh The mesh for which we need to not use its material
+     */
+    public unReferenceMeshFromUsingItsOwnMaterial(mesh: AbstractMesh): void {
+        let index = this._meshesUsingTheirOwnMaterials.indexOf(mesh.uniqueId);
+        while (index > 0) {
+            this._meshesUsingTheirOwnMaterials.slice(index, index + 1);
+            index = this._meshesUsingTheirOwnMaterials.indexOf(mesh.uniqueId);
+        }
+    }
+
+    /**
      * Free any resources and references associated to a mesh.
      * Internal use
      * @param mesh The mesh to free.