UNPKG

@babylonjs/viewer

Version:

The Babylon Viewer aims to simplify a specific but common Babylon.js use case: loading, viewing, and interacting with a 3D model.

3 lines (2 loc) 4.16 kB
import{C as t,V as e}from"./index-BPURObVq.esm.min.js";class r{static ConvertPanoramaToCubemap(e,r,a,n,o=!1,i=!0){if(!e)throw"ConvertPanoramaToCubemap: input cannot be null";let h;if(e.length!=r*a*3){if(e.length!=r*a*4)throw"ConvertPanoramaToCubemap: input size is wrong";h=4}else h=3;return{front:this.CreateCubemapTexture(n,this.FACE_FRONT,e,r,a,o,i,h),back:this.CreateCubemapTexture(n,this.FACE_BACK,e,r,a,o,i,h),left:this.CreateCubemapTexture(n,this.FACE_LEFT,e,r,a,o,i,h),right:this.CreateCubemapTexture(n,this.FACE_RIGHT,e,r,a,o,i,h),up:this.CreateCubemapTexture(n,this.FACE_UP,e,r,a,o,i,h),down:this.CreateCubemapTexture(n,this.FACE_DOWN,e,r,a,o,i,h),size:n,type:t.TEXTURETYPE_FLOAT,format:t.TEXTUREFORMAT_RGB,gammaSpace:!1}}static CreateCubemapTexture(t,e,r,a,n,o,i,h){const s=new ArrayBuffer(t*t*4*3),w=new Float32Array(s),u=o?Math.max(1,Math.round(a/4/t)):1,f=1/u,l=f*f,c=e[1].subtract(e[0]).scale(f/t),d=e[3].subtract(e[2]).scale(f/t),C=1/t;let m=0;for(let o=0;o<t;o++)for(let s=0;s<u;s++){let s=e[0],p=e[2];for(let e=0;e<t;e++)for(let f=0;f<u;f++){const u=p.subtract(s).scale(m).add(s);u.normalize();const f=this.CalcProjectionSpherical(u,r,a,n,h,i);w[o*t*3+3*e+0]+=f.r*l,w[o*t*3+3*e+1]+=f.g*l,w[o*t*3+3*e+2]+=f.b*l,s=s.add(c),p=p.add(d)}m+=C*f}return w}static CalcProjectionSpherical(t,e,r,a,n,o){let i=Math.atan2(t.z,t.x);const h=Math.acos(t.y);for(;i<-Math.PI;)i+=2*Math.PI;for(;i>Math.PI;)i-=2*Math.PI;let s=i/Math.PI;const w=h/Math.PI;s=.5*s+.5;let u=Math.round(s*r);u<0?u=0:u>=r&&(u=r-1);let f=Math.round(w*a);f<0?f=0:f>=a&&(f=a-1);const l=o?a-f-1:f;return{r:e[l*r*n+u*n+0],g:e[l*r*n+u*n+1],b:e[l*r*n+u*n+2]}}}function a(t,e,r,a,n,o){n>0?(n=function(t,e){return e>1023?t*Math.pow(2,1023)*Math.pow(2,e-1023):e<-1074?t*Math.pow(2,-1074)*Math.pow(2,e+1074):t*Math.pow(2,e)}(1,n-136),t[o+0]=e*n,t[o+1]=r*n,t[o+2]=a*n):(t[o+0]=0,t[o+1]=0,t[o+2]=0)}function n(t,e){let r,a="";for(let n=e;n<t.length-e&&(r=String.fromCharCode(t[n]),"\n"!=r);n++)a+=r;return a}function o(t){let e=n(t,0);if("#"!=e[0]||"?"!=e[1])throw"Bad HDR Format.";let r=!1,a=!1,o=0;do{o+=e.length+1,e=n(t,o),"FORMAT=32-bit_rle_rgbe"==e?a=!0:0==e.length&&(r=!0)}while(!r);if(!a)throw"HDR Bad header format, unsupported FORMAT";o+=e.length+1,e=n(t,o);const i=/^-Y (.*) \+X (.*)$/g.exec(e);if(!i||i.length<3)throw"HDR Bad header format, no size";const h=parseInt(i[2]),s=parseInt(i[1]);if(h<8||h>32767)throw"HDR Bad header format, unsupported size";return o+=e.length+1,{height:s,width:h,dataPosition:o}}function i(t,e,a=!1){const n=new Uint8Array(t),i=o(n),s=h(n,i);return r.ConvertPanoramaToCubemap(s,i.width,i.height,e,a)}function h(t,e){return function(t,e){let r=e.height;const n=e.width;let o,i,h,w,u,f,l,c,d=e.dataPosition;const C=new ArrayBuffer(4*n),m=new Uint8Array(C),p=new ArrayBuffer(e.width*e.height*4*3),A=new Float32Array(p);for(;r>0;){if(o=t[d++],i=t[d++],h=t[d++],w=t[d++],2!=o||2!=i||128&h||e.width<8||e.width>32767)return s(t,e);if((h<<8|w)!=n)throw"HDR Bad header format, wrong scan line width";for(f=0,c=0;c<4;c++)for(l=(c+1)*n;f<l;)if(o=t[d++],i=t[d++],o>128){if(u=o-128,0==u||u>l-f)throw"HDR Bad Format, bad scanline data (run)";for(;u-- >0;)m[f++]=i}else{if(u=o,0==u||u>l-f)throw"HDR Bad Format, bad scanline data (non-run)";if(m[f++]=i,--u>0)for(let e=0;e<u;e++)m[f++]=t[d++]}for(c=0;c<n;c++)o=m[c],i=m[c+n],h=m[c+2*n],w=m[c+3*n],a(A,o,i,h,w,(e.height-r)*n*3+3*c);r--}return A}(t,e)}function s(t,e){let r=e.height;const n=e.width;let o,i,h,s,w,u=e.dataPosition;const f=new ArrayBuffer(e.width*e.height*4*3),l=new Float32Array(f);for(;r>0;){for(w=0;w<e.width;w++)o=t[u++],i=t[u++],h=t[u++],s=t[u++],a(l,o,i,h,s,(e.height-r)*n*3+3*w);r--}return l}r.FACE_LEFT=[new e(-1,-1,-1),new e(1,-1,-1),new e(-1,1,-1),new e(1,1,-1)],r.FACE_RIGHT=[new e(1,-1,1),new e(-1,-1,1),new e(1,1,1),new e(-1,1,1)],r.FACE_FRONT=[new e(1,-1,-1),new e(1,-1,1),new e(1,1,-1),new e(1,1,1)],r.FACE_BACK=[new e(-1,-1,1),new e(-1,-1,-1),new e(-1,1,1),new e(-1,1,-1)],r.FACE_DOWN=[new e(1,1,-1),new e(1,1,1),new e(-1,1,-1),new e(-1,1,1)],r.FACE_UP=[new e(-1,-1,-1),new e(-1,-1,1),new e(1,-1,-1),new e(1,-1,1)];export{i as G,o as R,h as a}; //# sourceMappingURL=hdr-BJqmW-Ft.esm.min.js.map