|
2 | 2 |
|
3 | 3 | import Elm.Kernel.Utils exposing (Tuple2) |
4 | 4 | import Elm.Kernel.Scheduler exposing (binding, succeed, fail) |
5 | | -import WebGL.Texture as Texture exposing (LoadError, SizeError) |
| 5 | +import WebGL.Texture as Texture exposing (LoadError, SizeError, RGBA, RGB, LUMINANCE_ALPHA, LUMINANCE, ALPHA) |
| 6 | +import Result exposing (Err, Ok) |
6 | 7 |
|
7 | 8 | */ |
8 | 9 |
|
@@ -65,3 +66,71 @@ var _Texture_load = F6(function (magnify, mininify, horizontalWrap, verticalWrap |
65 | 66 | var _Texture_size = function (texture) { |
66 | 67 | return __Utils_Tuple2(texture.__width, texture.__height); |
67 | 68 | }; |
| 69 | + |
| 70 | + |
| 71 | +//Texture Loading from Bytes |
| 72 | + |
| 73 | + |
| 74 | +function getFormat(gl, format) { |
| 75 | + return (format == __Texture_RGBA && gl.RGBA) |
| 76 | + || (format == __Texture_RGB && gl.RGB) |
| 77 | + || (format == __Texture_LUMINANCE_ALPHA && gl.LUMINANCE_ALPHA) |
| 78 | + || (format == __Texture_LUMINANCE && gl.LUMINANCE) |
| 79 | + || (format == __Texture_ALPHA && gl.ALPHA) |
| 80 | +} |
| 81 | + |
| 82 | +function getByteCount(format) { |
| 83 | + return (format == __Texture_RGBA && 4) |
| 84 | + || (format == __Texture_RGB && 3) |
| 85 | + || (format == __Texture_LUMINANCE_ALPHA && 2) |
| 86 | + || (format == __Texture_LUMINANCE && 1) |
| 87 | + || (format == __Texture_ALPHA && 1) |
| 88 | +} |
| 89 | +// eslint-disable-next-line no-unused-vars |
| 90 | +var _Texture_loadBytes = F9(function (magnify, mininify, horizontalWrap, verticalWrap, flipY, width, height, format, bytes) { |
| 91 | + var isMipmap = mininify !== 9728 && mininify !== 9729; |
| 92 | + function createTexture(gl) { |
| 93 | + var pixelFormat = getFormat(gl, format); |
| 94 | + var texture = gl.createTexture(); |
| 95 | + gl.bindTexture(gl.TEXTURE_2D, texture); |
| 96 | + gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, flipY); |
| 97 | + gl.texImage2D(gl.TEXTURE_2D, 0, pixelFormat, width, height, 0, pixelFormat, gl.UNSIGNED_BYTE, new Uint8Array(bytes.buffer)); |
| 98 | + gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, magnify); |
| 99 | + gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, mininify); |
| 100 | + gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, horizontalWrap); |
| 101 | + gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, verticalWrap); |
| 102 | + if (isMipmap) { |
| 103 | + gl.generateMipmap(gl.TEXTURE_2D); |
| 104 | + } |
| 105 | + gl.bindTexture(gl.TEXTURE_2D, null); |
| 106 | + return texture; |
| 107 | + } |
| 108 | + if (bytes.byteLength < width * height * getByteCount(format)) { |
| 109 | + return __Result_Err(A2( |
| 110 | + __Texture_SizeError, |
| 111 | + width, |
| 112 | + height |
| 113 | + )); |
| 114 | + } |
| 115 | + var widthPowerOfTwo = (width & (width - 1)) === 0; |
| 116 | + var heightPowerOfTwo = (height & (height - 1)) === 0; |
| 117 | + var isSizeValid = (widthPowerOfTwo && heightPowerOfTwo) || ( |
| 118 | + !isMipmap |
| 119 | + && horizontalWrap === 33071 // clamp to edge |
| 120 | + && verticalWrap === 33071 |
| 121 | + ); |
| 122 | + if (isSizeValid) { |
| 123 | + return __Result_Ok({ |
| 124 | + $: __0_TEXTURE, |
| 125 | + __$createTexture: createTexture, |
| 126 | + __width: width, |
| 127 | + __height: height |
| 128 | + }); |
| 129 | + } else { |
| 130 | + return __Result_Err(A2( |
| 131 | + __Texture_SizeError, |
| 132 | + width, |
| 133 | + height |
| 134 | + )); |
| 135 | + } |
| 136 | +}); |
0 commit comments