소스 검색

Improve readPixel from noiseTexture

David Catuhe 7 년 전
부모
커밋
dee332969a
3개의 변경된 파일19개의 추가작업 그리고 11개의 파일을 삭제
  1. 7 4
      src/Engine/babylon.engine.ts
  2. 6 5
      src/Materials/Textures/babylon.baseTexture.ts
  3. 6 2
      src/Particles/babylon.particleSystem.ts

+ 7 - 4
src/Engine/babylon.engine.ts

@@ -6975,7 +6975,7 @@
         }
 
         /** @hidden */
-        public _readTexturePixels(texture: InternalTexture, width: number, height: number, faceIndex = -1, level = 0): ArrayBufferView {
+        public _readTexturePixels(texture: InternalTexture, width: number, height: number, faceIndex = -1, level = 0, buffer?: ArrayBufferView): ArrayBufferView {
             let gl = this._gl;
             if (!this._dummyFramebuffer) {
                 let dummy = gl.createFramebuffer();
@@ -6995,15 +6995,18 @@
             }
 
             let readType = (texture.type !== undefined) ? this._getWebGLTextureType(texture.type) : gl.UNSIGNED_BYTE;
-            let buffer: ArrayBufferView;
 
             switch (readType) {
                 case gl.UNSIGNED_BYTE:
-                    buffer = new Uint8Array(4 * width * height);
+                    if (!buffer) {
+                        buffer = new Uint8Array(4 * width * height);
+                    }
                     readType = gl.UNSIGNED_BYTE;
                     break;
                 default:
-                    buffer = new Float32Array(4 * width * height);
+                    if (!buffer) {
+                        buffer = new Float32Array(4 * width * height);
+                    }
                     readType = gl.FLOAT;
                     break;
             }

+ 6 - 5
src/Materials/Textures/babylon.baseTexture.ts

@@ -312,11 +312,12 @@
          * Reads the pixels stored in the webgl texture and returns them as an ArrayBuffer.
          * This will returns an RGBA array buffer containing either in values (0-255) or
          * float values (0-1) depending of the underlying buffer type.
-         * @param faceIndex The face of the texture to read (in case of cube texture)
-         * @param level The LOD level of the texture to read (in case of Mip Maps)
+         * @param faceIndex defines the face of the texture to read (in case of cube texture)
+         * @param level defines the LOD level of the texture to read (in case of Mip Maps)
+         * @param buffer defines a user defined buffer to fill with data
          * @returns The Array buffer containing the pixels data.
          */
-        public readPixels(faceIndex = 0, level = 0): Nullable<ArrayBufferView> {
+        public readPixels(faceIndex = 0, level = 0, buffer?: ArrayBufferView): Nullable<ArrayBufferView> {
             if (!this._texture) {
                 return null;
             }
@@ -341,10 +342,10 @@
             }
 
             if (this._texture.isCube) {
-                return engine._readTexturePixels(this._texture, width, height, faceIndex, level);
+                return engine._readTexturePixels(this._texture, width, height, faceIndex, level, buffer);
             }
 
-            return engine._readTexturePixels(this._texture, width, height, -1, level);
+            return engine._readTexturePixels(this._texture, width, height, -1, level, buffer);
         }
 
         public releaseInternalTexture(): void {

+ 6 - 2
src/Particles/babylon.particleSystem.ts

@@ -179,13 +179,17 @@
             // Default emitter type
             this.particleEmitterType = new BoxParticleEmitter();
 
+            // Update
+            let noiseTextureData: Nullable<Uint8Array> = null;
             this.updateFunction = (particles: Particle[]): void => {
-                let noiseTextureData: Nullable<Uint8Array> = null;
                 let noiseTextureSize: Nullable<ISize> = null;
 
                 if (this.noiseTexture) { // We need to get texture data back to CPU
-                    noiseTextureData = <Nullable<Uint8Array>>(this.noiseTexture.readPixels());
                     noiseTextureSize = this.noiseTexture.getSize();
+                    if (!noiseTextureData) {
+                        noiseTextureData = new Uint8Array(4 * noiseTextureSize.width * noiseTextureSize.height); 
+                    }
+                    this.noiseTexture.readPixels(0, 0, noiseTextureData);
                 }
 
                 for (var index = 0; index < particles.length; index++) {