Sfoglia il codice sorgente

support flipY for textures

Alexander Rose 5 anni fa
parent
commit
7cba9cda0c
2 ha cambiato i file con 16 aggiunte e 8 eliminazioni
  1. 1 0
      src/mol-gl/renderable/util.ts
  2. 15 8
      src/mol-gl/webgl/texture.ts

+ 1 - 0
src/mol-gl/renderable/util.ts

@@ -20,6 +20,7 @@ export interface TextureImage<T extends Uint8Array | Float32Array> {
     readonly array: T
     readonly width: number
     readonly height: number
+    readonly flipY?: boolean
 }
 
 export interface TextureVolume<T extends Uint8Array | Float32Array> {

+ 15 - 8
src/mol-gl/webgl/texture.ts

@@ -118,6 +118,14 @@ export function getAttachment(gl: GLRenderingContext, extensions: WebGLExtension
     throw new Error('unknown texture attachment');
 }
 
+function isTexture2d(x: TextureImage<any> | TextureVolume<any>, target: number, gl: GLRenderingContext): x is TextureImage<any> {
+    return target === gl.TEXTURE_2D;
+}
+
+function isTexture3d(x: TextureImage<any> | TextureVolume<any>, target: number, gl: WebGL2RenderingContext): x is TextureImage<any> {
+    return target === gl.TEXTURE_3D;
+}
+
 export interface Texture {
     readonly id: number
     readonly target: number
@@ -214,14 +222,13 @@ export function createTexture(gl: GLRenderingContext, extensions: WebGLExtension
         gl.pixelStorei(gl.UNPACK_ALIGNMENT, 1);
         gl.pixelStorei(gl.UNPACK_COLORSPACE_CONVERSION_WEBGL, gl.NONE);
         gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, 0);
-        if (target === gl.TEXTURE_2D) {
-            const { array, width: _width, height: _height } = data as TextureImage<any>;
-            width = _width, height = _height;
-            gl.texImage2D(target, 0, internalFormat, width, height, 0, format, type, array);
-        } else if (isWebGL2(gl) && target === gl.TEXTURE_3D) {
-            const { array, width: _width, height: _height, depth: _depth } = data as TextureVolume<any>;
-            width = _width, height = _height, depth = _depth;
-            gl.texImage3D(target, 0, internalFormat, width, height, depth, 0, format, type, array);
+        if (isTexture2d(data, target, gl)) {
+            gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, !!data.flipY);
+            width = data.width, height = data.height;
+            gl.texImage2D(target, 0, internalFormat, width, height, 0, format, type, data.array);
+        } else if (isWebGL2(gl) && isTexture3d(data, target, gl)) {
+            width = data.width, height = data.height, depth = data.depth;
+            gl.texImage3D(target, 0, internalFormat, width, height, depth, 0, format, type, data.array);
         } else {
             throw new Error('unknown texture target');
         }