瀏覽代碼

Update use of texture

Guide 5 年之前
父節點
當前提交
b24cc880f9
共有 2 個文件被更改,包括 30 次插入35 次删除
  1. 5 0
      src/Particles/cloudPoint.ts
  2. 25 35
      src/Particles/pointsCloudSystem.ts

+ 5 - 0
src/Particles/cloudPoint.ts

@@ -228,6 +228,11 @@ export class PointsGroup {
      * @hidden
      * @hidden
      */
      */
     public _groupDensity: number[];
     public _groupDensity: number[];
+    /**
+     * Only when points are colored by texture carries pointer to texture list array
+     * @hidden
+     */
+    public _textureNb: number;
 
 
     /**
     /**
      * Creates a points group object. This is an internal reference to produce particles for the PCS.
      * Creates a points group object. This is an internal reference to produce particles for the PCS.

+ 25 - 35
src/Particles/pointsCloudSystem.ts

@@ -13,6 +13,7 @@ import { Ray } from "../Culling/ray";
 import { PickingInfo } from "../Collisions/pickingInfo";
 import { PickingInfo } from "../Collisions/pickingInfo";
 import { StandardMaterial } from "../Materials/standardMaterial";
 import { StandardMaterial } from "../Materials/standardMaterial";
 import { Texture } from "./../Materials/Textures/texture";
 import { Texture } from "./../Materials/Textures/texture";
+import { BaseTexture } from "./../Materials/Textures/baseTexture";
 import { Scalar } from "../Maths/math.scalar";
 import { Scalar } from "../Maths/math.scalar";
 
 
 /** Defines the 4 color options */
 /** Defines the 4 color options */
@@ -459,45 +460,28 @@ export class PointsCloudSystem implements IDisposable {
             return;
             return;
         }
         }
 
 
-        var mat = <StandardMaterial>mesh.material;
-        if (mat.diffuseTexture === null && mat.emissiveTexture === null) {
-            Logger.Warn(mesh.name + "has no useable texture.");
-            pointsGroup._groupImageData = null;
-            this._setPointsColorOrUV(mesh, pointsGroup, isVolume, true, false);
-            return;
-        }
-        if (mat.diffuseTexture!._texture === null && mat.emissiveTexture!._texture === null) {
+        var mat = mesh.material;
+        let textureList: BaseTexture[] = mat.getActiveTextures();
+        if (textureList.length === 0) {
             Logger.Warn(mesh.name + "has no useable texture.");
             Logger.Warn(mesh.name + "has no useable texture.");
             pointsGroup._groupImageData = null;
             pointsGroup._groupImageData = null;
             this._setPointsColorOrUV(mesh, pointsGroup, isVolume, true, false);
             this._setPointsColorOrUV(mesh, pointsGroup, isVolume, true, false);
             return;
             return;
         }
         }
+
         var clone = <Mesh>mesh.clone();
         var clone = <Mesh>mesh.clone();
         clone.setEnabled(false);
         clone.setEnabled(false);
-        if (mat.diffuseTexture !== null) {
-            this._promises.push(new Promise((resolve) => {
-                (<Texture>mat.diffuseTexture).onLoadObservable.add(() => {
-                    pointsGroup._groupImageData = mat.diffuseTexture!.readPixels();
-                    pointsGroup._groupImgWidth = mat.diffuseTexture!._texture!.width;
-                    pointsGroup._groupImgHeight = mat.diffuseTexture!._texture!.height;
-                    this._setPointsColorOrUV(clone, pointsGroup, isVolume, true, true);
-                    clone.dispose();
-                    resolve();
-                });
-            }));
-        }
-        else {
-            this._promises.push(new Promise((resolve) => {
-                (<Texture>mat.emissiveTexture).onLoadObservable.add(() => {
-                    pointsGroup._groupImageData = mat.emissiveTexture!.readPixels();
-                    pointsGroup._groupImgWidth = mat.emissiveTexture!._texture!.width;
-                    pointsGroup._groupImgHeight = mat.emissiveTexture!._texture!.height;
-                    this._setPointsColorOrUV(clone, pointsGroup, isVolume, true);
-                    clone.dispose();
-                    resolve();
-                });
-            }));
-        }
+        this._promises.push(new Promise((resolve) => {
+            BaseTexture.WhenAllReady(textureList, () => {
+                let n = pointsGroup._textureNb;
+                pointsGroup._groupImageData = textureList[n].readPixels();
+                pointsGroup._groupImgWidth = textureList[n].getSize().width;
+                pointsGroup._groupImgHeight = textureList[this.nbParticles].getSize().height;
+                this._setPointsColorOrUV(clone, pointsGroup, isVolume, true, true);
+                clone.dispose();
+                return resolve();
+            });
+        }));
     }
     }
 
 
     // calculates the point density per facet of a mesh for surface points
     // calculates the point density per facet of a mesh for surface points
@@ -622,12 +606,12 @@ export class PointsCloudSystem implements IDisposable {
      * @param range (number from 0 to 1) to determine the variation in shape and tone for a stated color
      * @param range (number from 0 to 1) to determine the variation in shape and tone for a stated color
      * @returns the number of groups in the system
      * @returns the number of groups in the system
      */
      */
-    public addSurfacePoints(mesh: Mesh, nb: number, colorWith?: number, color?: Color4, range?: number): number {
+    public addSurfacePoints(mesh: Mesh, nb: number, colorWith?: number, color?: Color4 | number, range?: number): number {
         var colored = colorWith ? colorWith : PointColor.Random;
         var colored = colorWith ? colorWith : PointColor.Random;
         if (isNaN(colored) ||  colored < 0 || colored > 3) {
         if (isNaN(colored) ||  colored < 0 || colored > 3) {
             colored = PointColor.Random ;
             colored = PointColor.Random ;
         }
         }
-        color = color ? color : new Color4(1, 1, 1, 1);
+        
         var meshPos = <FloatArray>mesh.getVerticesData(VertexBuffer.PositionKind);
         var meshPos = <FloatArray>mesh.getVerticesData(VertexBuffer.PositionKind);
         var meshInd = <IndicesArray>mesh.getIndices();
         var meshInd = <IndicesArray>mesh.getIndices();
 
 
@@ -635,6 +619,12 @@ export class PointsCloudSystem implements IDisposable {
         var pointsGroup = new PointsGroup(this._groupCounter, null);
         var pointsGroup = new PointsGroup(this._groupCounter, null);
 
 
         pointsGroup._groupDensity = this._calculateDensity(nb, meshPos, meshInd);
         pointsGroup._groupDensity = this._calculateDensity(nb, meshPos, meshInd);
+        if (colored === PointColor.Color) {
+            pointsGroup._textureNb = <number>color ? <number>color : 0;
+        }
+        else {
+            color = <Color4>color ? <Color4>color : new Color4(1, 1, 1, 1);
+        }
         switch (colored) {
         switch (colored) {
             case PointColor.Color:
             case PointColor.Color:
                 this._colorFromTexture(mesh, pointsGroup, false);
                 this._colorFromTexture(mesh, pointsGroup, false);
@@ -646,7 +636,7 @@ export class PointsCloudSystem implements IDisposable {
                 this._setPointsColorOrUV(mesh, pointsGroup, false);
                 this._setPointsColorOrUV(mesh, pointsGroup, false);
                 break;
                 break;
             case PointColor.Stated:
             case PointColor.Stated:
-                this._setPointsColorOrUV(mesh, pointsGroup, false, undefined, undefined, color, range);
+                this._setPointsColorOrUV(mesh, pointsGroup, false, undefined, undefined, <Color4>color, range);
                 break;
                 break;
         }
         }
         this.nbParticles += nb;
         this.nbParticles += nb;