Просмотр исходного кода

Fixed Texture Type and Highlight layer

Sebastien Vandenberghe 7 лет назад
Родитель
Сommit
c0a5b90b05

+ 42 - 39
src/Engine/babylon.engine.ts

@@ -446,10 +446,8 @@
         private static _TEXTUREFORMAT_LUMINANCE_ALPHA = 2;
         private static _TEXTUREFORMAT_RGB = 4;
         private static _TEXTUREFORMAT_RGBA = 5;
-        private static _TEXTUREFORMAT_R32F = 6;
-        private static _TEXTUREFORMAT_RG32F = 7;
-        private static _TEXTUREFORMAT_RGB32F = 8;
-        private static _TEXTUREFORMAT_RGBA32F = 9;
+        private static _TEXTUREFORMAT_R = 6;
+        private static _TEXTUREFORMAT_RG = 7;
 
         private static _TEXTURETYPE_UNSIGNED_INT = 0;
         private static _TEXTURETYPE_FLOAT = 1;
@@ -645,32 +643,18 @@
         }
 
         /**
-         * R32F
+         * R
          */
-        public static get TEXTUREFORMAT_R32F(): number {
-            return Engine._TEXTUREFORMAT_R32F;
+        public static get TEXTUREFORMAT_R(): number {
+            return Engine._TEXTUREFORMAT_R;
         }       
         
         /**
-         * RG32F
+         * RG
          */
-        public static get TEXTUREFORMAT_RG32F(): number {
-            return Engine._TEXTUREFORMAT_RG32F;
-        }       
-        
-        /**
-         * RGB32F
-         */
-        public static get TEXTUREFORMAT_RGB32F(): number {
-            return Engine._TEXTUREFORMAT_RGB32F;
-        }       
-        
-        /**
-         * RGBA32F
-         */
-        public static get TEXTUREFORMAT_RGBA32F(): number {
-            return Engine._TEXTUREFORMAT_RGBA32F;
-        }             
+        public static get TEXTUREFORMAT_RG(): number {
+            return Engine._TEXTUREFORMAT_RG;
+        }
 
         /** LUMINANCE_ALPHA */
         public static get TEXTUREFORMAT_LUMINANCE_ALPHA(): number {
@@ -5903,9 +5887,13 @@
          * @param format defines the data format
          * @param invertY defines if data must be stored with Y axis inverted
          * @param compression defines the used compression (can be null)
+         * @param textureType defines the texture Type (Engine.TEXTURETYPE_UNSIGNED_INT, Engine.TEXTURETYPE_FLOAT...)
          */
-        public updateRawTexture3D(texture: InternalTexture, data: Nullable<ArrayBufferView>, format: number, invertY: boolean, compression: Nullable<string> = null): void {
+        public updateRawTexture3D(texture: InternalTexture, data: Nullable<ArrayBufferView>, format: number, invertY: boolean, compression: Nullable<string> = null, textureType = Engine.TEXTURETYPE_UNSIGNED_INT): void {
+            var internalType = this._getWebGLTextureType(textureType);
             var internalFormat = this._getInternalFormat(format);
+            var internalSizedFomat = this._getRGBABufferInternalSizedFormat(textureType, format);
+
             this._bindTextureDirectly(this._gl.TEXTURE_3D, texture, true);
             this._gl.pixelStorei(this._gl.UNPACK_FLIP_Y_WEBGL, invertY === undefined ? 1 : (invertY ? 1 : 0));
 
@@ -5923,7 +5911,7 @@
             if (compression && data) {
                 this._gl.compressedTexImage3D(this._gl.TEXTURE_3D, 0, (<any>this.getCaps().s3tc)[compression], texture.width, texture.height, texture.depth, 0, data);
             } else {
-                this._gl.texImage3D(this._gl.TEXTURE_3D, 0, internalFormat, texture.width, texture.height, texture.depth, 0, internalFormat, this._gl.UNSIGNED_BYTE, data);
+                this._gl.texImage3D(this._gl.TEXTURE_3D, 0, internalSizedFomat, texture.width, texture.height, texture.depth, 0, internalFormat, internalType, data);
             }
 
             if (texture.generateMipMaps) {
@@ -5945,9 +5933,10 @@
          * @param invertY defines if data must be stored with Y axis inverted
          * @param samplingMode defines the required sampling mode (like BABYLON.Texture.NEAREST_SAMPLINGMODE)
          * @param compression defines the compressed used (can be null)
+         * @param textureType defines the compressed used (can be null)
          * @returns a new raw 3D texture (stored in an InternalTexture)
          */
-        public createRawTexture3D(data: Nullable<ArrayBufferView>, width: number, height: number, depth: number, format: number, generateMipMaps: boolean, invertY: boolean, samplingMode: number, compression: Nullable<string> = null): InternalTexture {
+        public createRawTexture3D(data: Nullable<ArrayBufferView>, width: number, height: number, depth: number, format: number, generateMipMaps: boolean, invertY: boolean, samplingMode: number, compression: Nullable<string> = null, textureType = Engine.TEXTURETYPE_UNSIGNED_INT): InternalTexture {
             var texture = new InternalTexture(this, InternalTexture.DATASOURCE_RAW3D);
             texture.baseWidth = width;
             texture.baseHeight = height;
@@ -5956,6 +5945,7 @@
             texture.height = height;
             texture.depth = depth;
             texture.format = format;
+            texture.type = textureType;
             texture.generateMipMaps = generateMipMaps;
             texture.samplingMode = samplingMode;
             texture.is3D = true;
@@ -5964,7 +5954,7 @@
                 texture._bufferView = data;
             }
 
-            this.updateRawTexture3D(texture, data, format, invertY, compression);
+            this.updateRawTexture3D(texture, data, format, invertY, compression, textureType);
             this._bindTextureDirectly(this._gl.TEXTURE_3D, texture, true);
 
             // Filters
@@ -7078,17 +7068,15 @@
                     internalFormat = this._gl.LUMINANCE_ALPHA;
                     break;
                 case Engine.TEXTUREFORMAT_RGB:
-                case Engine.TEXTUREFORMAT_RGB32F:
                     internalFormat = this._gl.RGB;
                     break;
                 case Engine.TEXTUREFORMAT_RGBA:
-                case Engine.TEXTUREFORMAT_RGBA32F:
                     internalFormat = this._gl.RGBA;
                     break;
-                case Engine.TEXTUREFORMAT_R32F:
+                case Engine.TEXTUREFORMAT_R:
                     internalFormat = this._gl.RED;
                     break;       
-                case Engine.TEXTUREFORMAT_RG32F:
+                case Engine.TEXTUREFORMAT_RG:
                     internalFormat = this._gl.RG;
                     break;                                    
             }
@@ -7111,17 +7099,28 @@
             if (type === Engine.TEXTURETYPE_FLOAT) {
                 if (format) {
                     switch(format) {
-                        case Engine.TEXTUREFORMAT_R32F:
+                        case Engine.TEXTUREFORMAT_R:
                             return this._gl.R32F;
-                        case Engine.TEXTUREFORMAT_RG32F:
+                        case Engine.TEXTUREFORMAT_RG:
                             return this._gl.RG32F;
-                            case Engine.TEXTUREFORMAT_RGB32F:
-                            return this._gl.RGB32F;                            
+                            case Engine.TEXTUREFORMAT_RGB:
+                            return this._gl.RGB32F;
                     }                    
                 }
                 return this._gl.RGBA32F;
             }
-            else if (type === Engine.TEXTURETYPE_HALF_FLOAT) {
+
+            if (type === Engine.TEXTURETYPE_HALF_FLOAT) {
+                if (format) {
+                    switch(format) {
+                        case Engine.TEXTUREFORMAT_R:
+                            return this._gl.R16F;
+                        case Engine.TEXTUREFORMAT_RG:
+                            return this._gl.RG16F;
+                            case Engine.TEXTUREFORMAT_RGB:
+                            return this._gl.RGB16F;
+                    }                    
+                }
                 return this._gl.RGBA16F;
             }
 
@@ -7130,7 +7129,11 @@
                     case Engine.TEXTUREFORMAT_LUMINANCE:
                         return this._gl.LUMINANCE;
                     case Engine.TEXTUREFORMAT_RGB:
-                        return this._gl.RGB;                        
+                        return this._gl.RGB;
+                    case Engine.TEXTUREFORMAT_R:
+                        return this._gl.R8;
+                    case Engine.TEXTUREFORMAT_RG:
+                        return this._gl.RG8;
                 }                    
             }
             return this._gl.RGBA;

+ 11 - 3
src/Layer/babylon.highlightLayer.ts

@@ -271,6 +271,14 @@
             blurTextureWidth = this._engine.needPOTTextures ? Tools.GetExponentOfTwo(blurTextureWidth, this._maxSize) : blurTextureWidth;
             blurTextureHeight = this._engine.needPOTTextures ? Tools.GetExponentOfTwo(blurTextureHeight, this._maxSize) : blurTextureHeight;
 
+            var textureType = 0;
+            if (this._engine.getCaps().textureHalfFloatRender) {
+                textureType = Engine.TEXTURETYPE_HALF_FLOAT;
+            }
+            else {
+                textureType = Engine.TEXTURETYPE_UNSIGNED_INT;
+            }
+
             this._blurTexture = new RenderTargetTexture("HighlightLayerBlurRTT",
                 {
                     width: blurTextureWidth,
@@ -279,7 +287,7 @@
                 this._scene,
                 false,
                 true,
-                Engine.TEXTURETYPE_HALF_FLOAT);
+                textureType);
             this._blurTexture.wrapU = Texture.CLAMP_ADDRESSMODE;
             this._blurTexture.wrapV = Texture.CLAMP_ADDRESSMODE;
             this._blurTexture.anisotropicFilteringLevel = 16;
@@ -315,7 +323,7 @@
                         width:  blurTextureWidth,
                         height: blurTextureHeight
                     },
-                    null, Texture.BILINEAR_SAMPLINGMODE, this._scene.getEngine(), false, Engine.TEXTURETYPE_HALF_FLOAT);
+                    null, Texture.BILINEAR_SAMPLINGMODE, this._scene.getEngine(), false, textureType);
                 this._horizontalBlurPostprocess.width = blurTextureWidth;
                 this._horizontalBlurPostprocess.height = blurTextureHeight;
                 this._horizontalBlurPostprocess.onApplyObservable.add(effect => {
@@ -326,7 +334,7 @@
                         width:  blurTextureWidth,
                         height: blurTextureHeight
                     },
-                    null, Texture.BILINEAR_SAMPLINGMODE, this._scene.getEngine(), false, Engine.TEXTURETYPE_HALF_FLOAT);
+                    null, Texture.BILINEAR_SAMPLINGMODE, this._scene.getEngine(), false, textureType);
 
                 this._postProcesses = [this._horizontalBlurPostprocess, this._verticalBlurPostprocess];
             }

+ 10 - 4
src/Materials/Textures/babylon.rawTexture3D.ts

@@ -16,11 +16,15 @@
          * @param generateMipMaps defines a boolean indicating if mip levels should be generated (true by default)
          * @param invertY defines if texture must be stored with Y axis inverted
          * @param samplingMode defines the sampling mode to use (BABYLON.Texture.TRILINEAR_SAMPLINGMODE by default)
+         * @param textureType defines the texture Type (Engine.TEXTURETYPE_UNSIGNED_INT, Engine.TEXTURETYPE_FLOAT...)
          */
         constructor(data: ArrayBufferView, width: number, height: number, depth: number, 
-                    /** Gets or sets the texture format to use*/
+                    /** Gets or sets the texture format to use */
                     public format: number, scene: Scene, 
-                    generateMipMaps: boolean = true, invertY: boolean = false, samplingMode: number = Texture.TRILINEAR_SAMPLINGMODE) {
+                    generateMipMaps: boolean = true, 
+                    invertY: boolean = false, 
+                    samplingMode: number = Texture.TRILINEAR_SAMPLINGMODE,
+                    textureType = Engine.TEXTURETYPE_UNSIGNED_INT) {
             super(null, scene, !generateMipMaps, invertY);
             this._engine = scene.getEngine();
 
@@ -32,7 +36,9 @@
                 format,
                 generateMipMaps,
                 invertY,
-                samplingMode
+                samplingMode,
+                undefined,
+                textureType
             )
     
             this.is3D = true;
@@ -46,7 +52,7 @@
             if (!this._texture) {
                 return;
             }
-            this._engine.updateRawTexture3D(this._texture, data, this._texture.format, this._texture!.invertY);
+            this._engine.updateRawTexture3D(this._texture, data, this._texture.format, this._texture!.invertY, undefined, this._texture.type);
         }
     }
 }

+ 1 - 1
src/Particles/babylon.gpuParticleSystem.ts

@@ -379,7 +379,7 @@
                 d.push(Math.random());
                 d.push(Math.random());
             }
-            this._randomTexture = new RawTexture(new Float32Array(d), maxTextureSize, 1, Engine.TEXTUREFORMAT_RGBA32F, this._scene, false, false, Texture.NEAREST_SAMPLINGMODE, Engine.TEXTURETYPE_FLOAT)
+            this._randomTexture = new RawTexture(new Float32Array(d), maxTextureSize, 1, Engine.TEXTUREFORMAT_RGBA, this._scene, false, false, Texture.NEAREST_SAMPLINGMODE, Engine.TEXTURETYPE_FLOAT)
             this._randomTexture.wrapU = Texture.WRAP_ADDRESSMODE;
             this._randomTexture.wrapV = Texture.WRAP_ADDRESSMODE;
 

+ 2 - 2
src/Shaders/default.fragment.fx

@@ -86,7 +86,7 @@ varying vec4 vColor;
 	#else
 		varying vec2 vEmissiveUV;
 	#endif
-	uniform sampler2D emissiveSampler;
+	uniform highp sampler3D emissiveSampler;
 #endif
 
 #ifdef LIGHTMAP
@@ -366,7 +366,7 @@ vec3 reflectionColor = vec3(0., 0., 0.);
 	// Emissive
 	vec3 emissiveColor = vEmissiveColor;
 #ifdef EMISSIVE
-	emissiveColor += texture2D(emissiveSampler, vEmissiveUV + uvOffset).rgb * vEmissiveInfos.y;
+	emissiveColor += texture(emissiveSampler, vec3(vEmissiveUV + uvOffset, 0.)).rgb * vEmissiveInfos.y;
 #endif
 
 #ifdef EMISSIVEFRESNEL

+ 5 - 0
src/babylon.mixins.ts

@@ -57,8 +57,13 @@ interface WebGLRenderingContext {
     R32F: number;
     RG32F: number;
     RGB32F: number;
+    R16F: number;
+    RG16F: number;
+    RGB16F: number;
     RED: number;
     RG: number;
+    R8: number;
+    RG8: number;
 
     UNSIGNED_INT_24_8: number;
     DEPTH24_STENCIL8: number;