Pārlūkot izejas kodu

Supports custom glow materials.

sebavan 5 gadi atpakaļ
vecāks
revīzija
9af7758dc3
3 mainītis faili ar 62 papildinājumiem un 4 dzēšanām
  1. 28 1
      src/Layers/effectLayer.ts
  2. 33 2
      src/Layers/glowLayer.ts
  3. 1 1
      what's new.md

+ 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.

+ 1 - 1
what's new.md

@@ -134,7 +134,7 @@
 - Fixed renormalization of mesh weights to in cleanMatrixWeights function. ([Bolloxim](https://github.com/Bolloxim))
 - Added a validationSkin function to report out any errors on skinned meshes. ([Bolloxim](https://github.com/Bolloxim))
 - PolygonMeshBuilder can be used to generate ```VertexData``` instead of requiring a mesh to be built
-
+- Supports custom materials to generate glow through ```referenceMeshToUseItsOwnMaterial``` in the ```GlowLayer``` ([sebavan](http://www.github.com/sebavan))
 
 ### glTF Loader