@react-three/lightmap
Version:
In-browser lightmap/AO baker for react-three-fiber and ThreeJS
3 lines (2 loc) • 29.5 kB
JavaScript
"use strict";function e(e){return e&&"object"==typeof e&&"default"in e?e.default:e}Object.defineProperty(exports,"__esModule",{value:!0});var r=require("react"),t=e(r),n=require("three"),a=e(require("potpack")),o=require("@react-three/fiber");function i(e,r,t,n,a,o,i){try{var u=e[o](i),s=u.value}catch(e){return void t(e)}u.done?r(s):Promise.resolve(s).then(n,a)}function u(e){return function(){var r=this,t=arguments;return new Promise((function(n,a){var o=e.apply(r,t);function u(e){i(o,n,a,u,s,"next",e)}function s(e){i(o,n,a,u,s,"throw",e)}u(void 0)}))}}function s(){return(s=Object.assign||function(e){for(var r=1;r<arguments.length;r++){var t=arguments[r];for(var n in t)Object.prototype.hasOwnProperty.call(t,n)&&(e[n]=t[n])}return e}).apply(this,arguments)}function c(e,r){(null==r||r>e.length)&&(r=e.length);for(var t=0,n=new Array(r);t<r;t++)n[t]=e[t];return n}function l(e,r){var t;if("undefined"==typeof Symbol||null==e[Symbol.iterator]){if(Array.isArray(e)||(t=function(e,r){if(e){if("string"==typeof e)return c(e,void 0);var t=Object.prototype.toString.call(e).slice(8,-1);return"Object"===t&&e.constructor&&(t=e.constructor.name),"Map"===t||"Set"===t?Array.from(e):"Arguments"===t||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t)?c(e,void 0):void 0}}(e))||r&&e&&"number"==typeof e.length){t&&(e=t);var n=0;return function(){return n>=e.length?{done:!0}:{done:!1,value:e[n++]}}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}return(t=e[Symbol.iterator]()).next.bind(t)}var f,p=(function(e){var r=function(e){var r=Object.prototype,t=r.hasOwnProperty,n="function"==typeof Symbol?Symbol:{},a=n.iterator||"@@iterator",o=n.asyncIterator||"@@asyncIterator",i=n.toStringTag||"@@toStringTag";function u(e,r,t){return Object.defineProperty(e,r,{value:t,enumerable:!0,configurable:!0,writable:!0}),e[r]}try{u({},"")}catch(e){u=function(e,r,t){return e[r]=t}}function s(e,r,t,n){var a=Object.create((r&&r.prototype instanceof f?r:f).prototype),o=new A(n||[]);return a._invoke=function(e,r,t){var n="suspendedStart";return function(a,o){if("executing"===n)throw new Error("Generator is already running");if("completed"===n){if("throw"===a)throw o;return{value:void 0,done:!0}}for(t.method=a,t.arg=o;;){var i=t.delegate;if(i){var u=x(i,t);if(u){if(u===l)continue;return u}}if("next"===t.method)t.sent=t._sent=t.arg;else if("throw"===t.method){if("suspendedStart"===n)throw n="completed",t.arg;t.dispatchException(t.arg)}else"return"===t.method&&t.abrupt("return",t.arg);n="executing";var s=c(e,r,t);if("normal"===s.type){if(n=t.done?"completed":"suspendedYield",s.arg===l)continue;return{value:s.arg,done:t.done}}"throw"===s.type&&(n="completed",t.method="throw",t.arg=s.arg)}}}(e,t,o),a}function c(e,r,t){try{return{type:"normal",arg:e.call(r,t)}}catch(e){return{type:"throw",arg:e}}}e.wrap=s;var l={};function f(){}function p(){}function h(){}var d={};d[a]=function(){return this};var v=Object.getPrototypeOf,m=v&&v(v(k([])));m&&m!==r&&t.call(m,a)&&(d=m);var g=h.prototype=f.prototype=Object.create(d);function y(e){["next","throw","return"].forEach((function(r){u(e,r,(function(e){return this._invoke(r,e)}))}))}function w(e,r){var n;this._invoke=function(a,o){function i(){return new r((function(n,i){!function n(a,o,i,u){var s=c(e[a],e,o);if("throw"!==s.type){var l=s.arg,f=l.value;return f&&"object"==typeof f&&t.call(f,"__await")?r.resolve(f.__await).then((function(e){n("next",e,i,u)}),(function(e){n("throw",e,i,u)})):r.resolve(f).then((function(e){l.value=e,i(l)}),(function(e){return n("throw",e,i,u)}))}u(s.arg)}(a,o,n,i)}))}return n=n?n.then(i,i):i()}}function x(e,r){var t=e.iterator[r.method];if(void 0===t){if(r.delegate=null,"throw"===r.method){if(e.iterator.return&&(r.method="return",r.arg=void 0,x(e,r),"throw"===r.method))return l;r.method="throw",r.arg=new TypeError("The iterator does not provide a 'throw' method")}return l}var n=c(t,e.iterator,r.arg);if("throw"===n.type)return r.method="throw",r.arg=n.arg,r.delegate=null,l;var a=n.arg;return a?a.done?(r[e.resultName]=a.value,r.next=e.nextLoc,"return"!==r.method&&(r.method="next",r.arg=void 0),r.delegate=null,l):a:(r.method="throw",r.arg=new TypeError("iterator result is not an object"),r.delegate=null,l)}function b(e){var r={tryLoc:e[0]};1 in e&&(r.catchLoc=e[1]),2 in e&&(r.finallyLoc=e[2],r.afterLoc=e[3]),this.tryEntries.push(r)}function M(e){var r=e.completion||{};r.type="normal",delete r.arg,e.completion=r}function A(e){this.tryEntries=[{tryLoc:"root"}],e.forEach(b,this),this.reset(!0)}function k(e){if(e){var r=e[a];if(r)return r.call(e);if("function"==typeof e.next)return e;if(!isNaN(e.length)){var n=-1,o=function r(){for(;++n<e.length;)if(t.call(e,n))return r.value=e[n],r.done=!1,r;return r.value=void 0,r.done=!0,r};return o.next=o}}return{next:E}}function E(){return{value:void 0,done:!0}}return p.prototype=g.constructor=h,h.constructor=p,p.displayName=u(h,i,"GeneratorFunction"),e.isGeneratorFunction=function(e){var r="function"==typeof e&&e.constructor;return!!r&&(r===p||"GeneratorFunction"===(r.displayName||r.name))},e.mark=function(e){return Object.setPrototypeOf?Object.setPrototypeOf(e,h):(e.__proto__=h,u(e,i,"GeneratorFunction")),e.prototype=Object.create(g),e},e.awrap=function(e){return{__await:e}},y(w.prototype),w.prototype[o]=function(){return this},e.AsyncIterator=w,e.async=function(r,t,n,a,o){void 0===o&&(o=Promise);var i=new w(s(r,t,n,a),o);return e.isGeneratorFunction(t)?i:i.next().then((function(e){return e.done?e.value:i.next()}))},y(g),u(g,i,"Generator"),g[a]=function(){return this},g.toString=function(){return"[object Generator]"},e.keys=function(e){var r=[];for(var t in e)r.push(t);return r.reverse(),function t(){for(;r.length;){var n=r.pop();if(n in e)return t.value=n,t.done=!1,t}return t.done=!0,t}},e.values=k,A.prototype={constructor:A,reset:function(e){if(this.prev=0,this.next=0,this.sent=this._sent=void 0,this.done=!1,this.delegate=null,this.method="next",this.arg=void 0,this.tryEntries.forEach(M),!e)for(var r in this)"t"===r.charAt(0)&&t.call(this,r)&&!isNaN(+r.slice(1))&&(this[r]=void 0)},stop:function(){this.done=!0;var e=this.tryEntries[0].completion;if("throw"===e.type)throw e.arg;return this.rval},dispatchException:function(e){if(this.done)throw e;var r=this;function n(t,n){return i.type="throw",i.arg=e,r.next=t,n&&(r.method="next",r.arg=void 0),!!n}for(var a=this.tryEntries.length-1;a>=0;--a){var o=this.tryEntries[a],i=o.completion;if("root"===o.tryLoc)return n("end");if(o.tryLoc<=this.prev){var u=t.call(o,"catchLoc"),s=t.call(o,"finallyLoc");if(u&&s){if(this.prev<o.catchLoc)return n(o.catchLoc,!0);if(this.prev<o.finallyLoc)return n(o.finallyLoc)}else if(u){if(this.prev<o.catchLoc)return n(o.catchLoc,!0)}else{if(!s)throw new Error("try statement without catch or finally");if(this.prev<o.finallyLoc)return n(o.finallyLoc)}}}},abrupt:function(e,r){for(var n=this.tryEntries.length-1;n>=0;--n){var a=this.tryEntries[n];if(a.tryLoc<=this.prev&&t.call(a,"finallyLoc")&&this.prev<a.finallyLoc){var o=a;break}}o&&("break"===e||"continue"===e)&&o.tryLoc<=r&&r<=o.finallyLoc&&(o=null);var i=o?o.completion:{};return i.type=e,i.arg=r,o?(this.method="next",this.next=o.finallyLoc,l):this.complete(i)},complete:function(e,r){if("throw"===e.type)throw e.arg;return"break"===e.type||"continue"===e.type?this.next=e.arg:"return"===e.type?(this.rval=this.arg=e.arg,this.method="return",this.next="end"):"normal"===e.type&&r&&(this.next=r),l},finish:function(e){for(var r=this.tryEntries.length-1;r>=0;--r){var t=this.tryEntries[r];if(t.finallyLoc===e)return this.complete(t.completion,t.afterLoc),M(t),l}},catch:function(e){for(var r=this.tryEntries.length-1;r>=0;--r){var t=this.tryEntries[r];if(t.tryLoc===e){var n=t.completion;if("throw"===n.type){var a=n.arg;M(t)}return a}}throw new Error("illegal catch attempt")},delegateYield:function(e,r,t){return this.delegate={iterator:k(e),resultName:r,nextLoc:t},"next"===this.method&&(this.arg=void 0),l}},e}(e.exports);try{regeneratorRuntime=r}catch(e){Function("r","regeneratorRuntime = r")(r)}}(f={exports:{}}),f.exports),h=p.mark(m),d=new n.Color("#000000");function v(e,r){var t=4*r,n=e.data[t],a=e.data[t+1],o=e.data[t+2],i=e.data[t+3];if(0===o)return null;var u=Math.round(o-1),s=Math.round(i-1);if(u<0||u>=e.items.length)throw new Error("incorrect atlas map item data: "+n+", "+a+", "+o+", "+i);var c=e.items[u];if(s<0||s>=c.faceCount)throw new Error("incorrect atlas map face data: "+n+", "+a+", "+o+", "+i);return{texelIndex:r,originalMesh:c.originalMesh,originalBuffer:c.originalBuffer,faceIndex:s,pU:n,pV:a}}function m(e,r){var t,n,a,o,i;return p.wrap((function(u){for(;;)switch(u.prev=u.next){case 0:t=e.width*e.height,n=0,a=0;case 4:if(!(n<t)){u.next=16;break}if(o=n,n+=1,(i=v(e,o))||!(a<100)){u.next=11;break}return a+=1,u.abrupt("continue",4);case 11:return a=0,u.next=14,i;case 14:u.next=4;break;case 16:r();case 17:case"end":return u.stop()}}),h)}function g(e,r,t,a){var o=function(e){for(var r,t=[],a=l(e);!(r=a()).done;){var o=r.value;if(o instanceof n.Mesh){var i=o.geometry;if(i instanceof n.BufferGeometry){var u=i.attributes.uv2;if(!u)throw new Error("expecting UV2 coordinates on writable lightmapped mesh");var s=i.index;if(!s)throw new Error("expected face index array");var c=s.array.length;if(!(u instanceof n.BufferAttribute))throw new Error("expected uv2 attribute");for(var f=t.length,p=new n.Float32BufferAttribute(3*c,3),h=new n.Float32BufferAttribute(2*c,2),d=new n.Float32BufferAttribute(4*c,4),v=s.array,m=0;m<c;m+=1){var g=m%3;h.copyAt(m,u,v[m]),d.setXYZW(m,1&g,(2&g)>>1,f+1,(m-g)/3+1)}var y=new n.BufferGeometry;y.setAttribute("position",p),y.setAttribute("uv2",h),y.setAttribute("faceInfo",d),t.push({faceCount:c/3,perFaceBuffer:y,originalMesh:o,originalBuffer:i})}}}return t}(a),i=function(e){var r=new n.Scene;r.name="Atlas mapper ortho scene";for(var t,a=l(e);!(t=a()).done;){var o=t.value,i=new n.Mesh;i.frustumCulled=!1,i.geometry=o.perFaceBuffer,i.material=new n.ShaderMaterial({side:n.DoubleSide,vertexShader:"\n attribute vec4 faceInfo;\n attribute vec2 uv2;\n\n varying vec4 vFaceInfo;\n uniform vec2 uvOffset;\n\n void main() {\n vFaceInfo = faceInfo;\n\n gl_Position = projectionMatrix * vec4(\n uv2 + uvOffset, // UV2 is the actual position on map\n 0,\n 1.0\n );\n }\n",fragmentShader:"\n varying vec4 vFaceInfo;\n\n void main() {\n // encode the face information in map\n gl_FragColor = vFaceInfo;\n }\n"}),r.add(i)}return r}(o),u=new n.WebGLRenderTarget(r,t,{type:n.FloatType,magFilter:n.NearestFilter,minFilter:n.NearestFilter,depthBuffer:!1,generateMipmaps:!1}),s=new n.OrthographicCamera(0,1,1,0,0,1),c=new Float32Array(r*t*4),f=new n.Color;e.getClearColor(f);var p=e.getClearAlpha(),h=e.autoClear;return e.setRenderTarget(u),e.setClearColor(d,0),e.autoClear=!0,e.render(i,s),e.setRenderTarget(null),e.setClearColor(f,p),e.autoClear=h,e.readRenderTargetPixels(u,0,0,r,t,c),u.dispose(),{width:r,height:t,texture:new n.DataTexture(c,r,t,n.RGBAFormat,n.FloatType),data:c,items:o.map((function(e){return{faceCount:e.faceCount,originalMesh:e.originalMesh,originalBuffer:e.originalBuffer}}))}}var y=new n.Vector3,w=new n.Vector3,x=new n.Vector3,b=new n.Vector3,M=new n.Vector3,A=new n.Vector4,k=new n.Color,E=new n.Vector3,L=new n.Color("#000000"),I=new n.Color("#ffffff"),S={targetSize:16,offset:0,near:.05,far:50};function F(e,r,t,n,a,o){var i=1-a-o;e.fromArray(r,3*t[n]),e.multiplyScalar(i),E.fromArray(r,3*t[n+1]),e.addScaledVector(E,a),E.fromArray(r,3*t[n+2]),e.addScaledVector(E,o),e.normalize()}function C(e,r,t,n,a){e.position.copy(t),e.up.copy(a),M.copy(n),M.add(t),e.lookAt(M),e.scale.set(1,1,1),e.applyMatrix4(r.matrixWorld)}function T(e,r,t,n,a,o){e.position.copy(t),e.up.copy(n),M.copy(t),M.addScaledVector(a,o),e.lookAt(M),e.scale.set(1,1,1),e.applyMatrix4(r.matrixWorld)}function V(e){for(var r=new Array(e*e),t=.5/e,n=0;n<e;n+=1)for(var a=n/e-.5+t,o=0;o<e;o+=1){var i=Math.hypot(2*(o/e-.5+t),2*a),u=Math.hypot(i,1);r[n*e+o]=1/u}return r}var P=new n.Vector4;function O(e,r){for(var t,n=0,a=0,o=0,i=0,u=l(e());!(t=u()).done;)for(var s=t.value,c=s.rgbaData,f=s.probeBox,p=s.originX,h=f.z,d=4*s.rowPixelStride,v=f.y*d+4*f.x,m=(f.y+f.w)*d,g=s.originY;v<m;){for(var y=v+4*f.z,w=p,x=v;x<y;x+=4){var b=r[g*h+w];n+=b*c[x],a+=b*c[x+1],o+=b*c[x+2],i+=b,w+=1}v+=d,g+=1}P.x=n/i,P.y=a/i,P.z=o/i}function R(e,r,t,n){return N.apply(this,arguments)}function N(){return(N=u(p.mark((function e(r,t,a,o){var i,u,s,c,l,f,h,d,v,m,g;return p.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return u=r?I:L,s=(i=a.targetSize)/2,c=4*i,l=2*i*8,f=V(i),h=new n.WebGLRenderTarget(c,l,{type:n.FloatType,magFilter:n.NearestFilter,minFilter:n.NearestFilter,generateMipmaps:!1}),d=new n.PerspectiveCamera(90,1,a.near,r?t:a.far),v=new Float32Array(c*l*4),m=new Array(8),g=p.mark((function e(r,t,o){var g,M,E,L,I,S,V,R,N,j,B,_,X,Y,G,D,U,z;return p.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:r.getClearColor(k),g=r.getClearAlpha(),M=r.autoClear,E=r.toneMapping,r.toneMapping=n.LinearToneMapping,h.scissorTest=!0,h.scissor.set(0,0,c,l),h.viewport.set(0,0,c,l),r.setRenderTarget(h),r.autoClear=!1,r.setClearColor(u,1),r.clear(!0,!0,!1),L=0;case 13:if(!(L<8)){e.next=71;break}if((I=o.next()).done||!I.value){e.next=66;break}if(V=(S=I.value).originalMesh,R=S.originalBuffer,N=S.faceIndex,j=S.pU,B=S.pV,_=L*i*2,m[L]=S.texelIndex,R.index){e.next=21;break}throw new Error("expected indexed mesh");case 21:G=R.attributes.normal.array,y.fromArray(Y=R.attributes.position.array,3*(X=R.index.array)[D=3*N]),w.fromArray(Y,3*X[D+1]),x.fromArray(Y,3*X[D+2]),w.sub(y),x.sub(y),y.addScaledVector(w,j),y.addScaledVector(x,B),F(b,G,X,D,j,B),0===b.x&&0===b.y?w.set(1,0,0):w.set(0,0,1),x.crossVectors(b,w),x.normalize(),w.crossVectors(b,x),w.normalize(),y.addScaledVector(b,a.offset),C(d,V,y,b,w),h.viewport.set(0,_+i,i,i),h.scissor.set(0,_+i,i,i),r.setRenderTarget(h),r.render(t,d),T(d,V,y,b,w,1),h.viewport.set(0,_,i,i),h.scissor.set(0,_+s,i,s),r.setRenderTarget(h),r.render(t,d),T(d,V,y,b,w,-1),h.viewport.set(i,_,i,i),h.scissor.set(i,_+s,i,s),r.setRenderTarget(h),r.render(t,d),T(d,V,y,b,x,1),h.viewport.set(2*i,_,i,i),h.scissor.set(2*i,_+s,i,s),r.setRenderTarget(h),r.render(t,d),T(d,V,y,b,x,-1),h.viewport.set(3*i,_,i,i),h.scissor.set(3*i,_+s,i,s),r.setRenderTarget(h),r.render(t,d),e.next=68;break;case 66:return m[L]=void 0,e.abrupt("break",71);case 68:L+=1,e.next=13;break;case 71:r.readRenderTargetPixels(h,0,0,c,l,v),r.setRenderTarget(null),r.setClearColor(k,g),r.autoClear=M,r.toneMapping=E,U=p.mark((function e(r){var t;return p.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(void 0!==(t=m[r])){e.next=3;break}return e.abrupt("return","break");case 3:return O(p.mark((function e(){var t,n;return p.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n={rgbaData:v,rowPixelStride:4*i,probeBox:A,originX:0,originY:0},A.set(0,(t=r*i*2)+i,i,i),n.originX=0,n.originY=0,e.next=8,n;case 8:return A.set(0,t+s,i,s),n.originX=0,n.originX=s,e.next=13,n;case 13:return A.set(i,t+s,i,s),n.originX=0,n.originX=s,e.next=18,n;case 18:return A.set(2*i,t+s,i,s),n.originX=0,n.originX=s,e.next=23,n;case 23:return A.set(3*i,t+s,i,s),n.originX=0,n.originX=s,e.next=28,n;case 28:case"end":return e.stop()}}),e)})),f),e.next=7,{texelIndex:t,rgba:P};case 7:case"end":return e.stop()}}),e)})),z=0;case 78:if(!(z<8)){e.next=86;break}return e.delegateYield(U(z),"t0",80);case 80:if("break"!==e.t0){e.next=83;break}return e.abrupt("break",86);case 83:z+=1,e.next=78;break;case 86:case"end":return e.stop()}}),e)})),e.prev=15,e.next=18,o(g,h.texture);case 18:return e.prev=18,h.dispose(),e.finish(18);case 21:case"end":return e.stop()}}),e,null,[[15,,18,21]])})))).apply(this,arguments)}var j=new n.Vector3,B=new n.Vector3,_=new n.Vector3,X=new n.Vector3,Y=new n.Vector3,G=new n.Vector3,D=new n.Vector3,U=new n.Vector2,z=new n.Vector2,q=new n.Vector2,W=new n.Vector3,Z=new n.Vector3,$=new n.Vector3;function H(e,r,t,n){W.fromArray(e,3*n),Y.fromArray(r,3*n);for(var a=t[n],o=0;o<n;o+=1){Z.fromArray(e,3*o),$.fromArray(r,3*o);var i=t[o];if(Z.equals(W)&&$.equals(Y)&&a===i)return o}return n}function J(e){for(var r,t=e.attributes.position.array,a=Math.floor(t.length/3),o=Math.floor(a/3),i=e.attributes.normal.array,u=e.groups||[],s=new Array(a),c=l(u);!(r=c()).done;){var f=r.value;s.fill(f.materialIndex,f.start,Math.min(a,f.start+f.count))}var p=new n.Uint16BufferAttribute(3*o,3);p.count=3*o;for(var h=0;h<o;h+=1){var d=3*h,v=H(t,i,s,d),m=H(t,i,s,d+1),g=H(t,i,s,d+2);p.setXYZ(h,v,m,g)}e.setIndex(p)}function K(e,r,t){for(var n=1,a=0,o=0;o<3;o+=1){j.fromArray(t,3*e[r+o]),B.fromArray(t,3*e[r+(o+2)%3]),_.fromArray(t,3*e[r+(o+1)%3]),B.sub(j),_.sub(j),B.normalize(),_.normalize();var i=Math.abs(B.dot(_));n>i&&(n=i,a=o)}return a}function Q(e){for(var r=4;r<=512;r*=2)if(e<r)return r;throw new Error("minimum lightmap dimension for auto-UV2 is "+e+" which is too large: please reduce texelsPerUnit and/or polygon count")}function ee(e,r,t,o){for(var i,u=o.texelsPerUnit,s=[],c=!1,f=l(t);!(i=f()).done;){var p=i.value;if(p instanceof n.Mesh){var h=p.geometry;if(!(h instanceof n.BufferGeometry))throw new Error("expecting buffer geometry");h.index||J(h);var d=h.index;if(!d)throw new Error("unexpected missing geometry index attr");var v=d.array,m=Math.floor(v.length/3),g=h.attributes.position.array,y=h.attributes.normal.array,w=new Array(g.length/3);if(h.attributes.uv2){if(s.length>0)throw new Error('found a mesh with "uv2" attribute in a scene with auto-calculated UV2 data: please do not mix-and-match');c=!0}else{if(c)throw new Error('found a mesh with missing "uv2" attribute in a scene with predefined UV2 data: please do not mix-and-match');var x=new n.Float32BufferAttribute(2*g.length/3,2);h.setAttribute("uv2",x);for(var b=0;b<3*m;b+=3){for(var M=void 0,A=0;A<3;A+=1){var k=w[v[b+A]];if(k)if(M&&M!==k){var E,L,I;(E=M.posIndices).push.apply(E,k.posIndices),(L=M.posLocalX).push.apply(L,k.posLocalX),(I=M.posLocalY).push.apply(I,k.posLocalY);for(var S,F=l(k.posIndices);!(S=F()).done;)w[S.value]=M;var C=s.indexOf(k);if(-1===C)throw new Error("unexpected orphaned layout box");s.splice(C,1)}else M=k}if(!M){var T=K(v,b,g),V=b+T,P=b+(T+2)%3,O=b+(T+1)%3;j.fromArray(g,3*v[V]),B.fromArray(g,3*v[P]),_.fromArray(g,3*v[O]),B.sub(j),_.sub(j),Y.fromArray(y,3*v[V]),G.crossVectors(_,Y),D.crossVectors(Y,G),G.normalize(),D.normalize(),M={x:0,y:0,w:0,h:0,uv2Attr:x,uAxis:G.clone(),vAxis:D.clone(),posArray:g,posIndices:[],posLocalX:[],posLocalY:[]},s.push(M)}for(var R=0;R<3;R+=1){var N=v[b+R];w[N]||(w[N]=M,M.posIndices.push(N),M.posLocalX.push(0),M.posLocalY.push(0))}}}}}for(var W=0,Z=s;W<Z.length;W++){var $=Z[W],H=$.uAxis,ee=$.vAxis,re=$.posArray,te=$.posIndices,ne=$.posLocalX,ae=$.posLocalY;z.set(Number.POSITIVE_INFINITY,Number.POSITIVE_INFINITY),q.set(Number.NEGATIVE_INFINITY,Number.NEGATIVE_INFINITY);for(var oe=0;oe<te.length;oe+=1)X.fromArray(re,3*te[oe]),U.set(X.dot(H),X.dot(ee)),z.min(U),q.max(U),ne[oe]=U.x,ae[oe]=U.y;var ie=q.x-z.x,ue=q.y-z.y;if(ie<0||ue<0)throw new Error("zero-point polygon?");var se=Math.ceil(ie*u),ce=Math.ceil(ue*u);$.w=se+2,$.h=ce+2;for(var le=0;le<te.length;le+=1)ne[le]=(ne[le]-z.x)/ie,ae[le]=(ae[le]-z.y)/ue}if(0===s.length)return[e||0,r||0];var fe=a(s),pe=fe.w,he=fe.h;if(e&&pe>e||r&&he>r)throw new Error("minimum lightmap size for auto-UV2 is "+pe+"x"+he+" which is too large to fit provided "+e+"x"+r+": please reduce texelsPerUnit and/or polygon count");for(var de=e||Q(pe),ve=r||Q(he),me=0,ge=s;me<ge.length;me++)for(var ye=ge[me],we=ye.uv2Attr,xe=ye.posIndices,be=ye.posLocalX,Me=ye.posLocalY,Ae=ye.x+1,ke=ye.y+1,Ee=ye.w-2,Le=ye.h-2,Ie=0;Ie<xe.length;Ie+=1)we.setXY(xe[Ie],(Ae+be[Ie]*Ee)/de,(ke+Me[Ie]*Le)/ve);return[de,ve]}var re=p.mark(ue),te=Symbol("lightmap ignore flag"),ne=Symbol("lightmap read-only flag"),ae=Object.prototype.hasOwnProperty;function oe(e,r){return ae.call(e.userData,r)}var ie=!1;function ue(e,r,t){var n,a,o,i,u,s,c;return p.wrap((function(f){for(;;)switch(f.prev=f.next){case 0:n=[e],a=[!1];case 2:if(!(n.length>0)){f.next=15;break}if(o=n.pop(),i=a.pop(),!(!o.visible||oe(o,te)||r&&oe(o,ne))){f.next=8;break}return t&&t(o),f.abrupt("continue",2);case 8:return u=i||oe(o,ne),ie=u,f.next=12,o;case 12:for(s=l(o.children);!(c=s()).done;)n.push(c.value),a.push(u);f.next=2;break;case 15:case"end":return f.stop()}}),re)}function se(e,r,t){var a=new Float32Array(e*r*4),o=new n.DataTexture(a,e,r,n.RGBAFormat,n.FloatType);return o.magFilter=t,o.minFilter=t,o.generateMipmaps=!1,[o,a]}function ce(){return new Promise((function(e){return setTimeout(e,0)}))}function le(e,r,t){return fe.apply(this,arguments)}function fe(){return(fe=u(p.mark((function e(r,t,a){var o,i,u,c,l,f,h,d,v,m,y,w,x,b,M,A,k,E;return p.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return o=t.ao,i=t.aoDistance,u=t.emissiveMultiplier,c=t.bounceMultiplier,l=t.lightMapSize,f=t.textureFilter,h=t.texelsPerUnit,d=s({},S,t.samplerSettings),m=(v=l?["number"==typeof l?l:l[0],"number"==typeof l?l:l[1]]:[void 0,void 0])[0],y=v[1],e.next=5,ce();case 5:return w=ee(m,y,ue(r,!0),{texelsPerUnit:h||2}),x=w[0]||64,b=w[1]||64,e.next=10,ce();case 10:return M=se(x,b,f||n.LinearFilter),A=M[0],k=M[1],e.next=13,a();case 13:return E=g(e.sent,x,b,ue(r,!0)),e.abrupt("return",{aoMode:!!o,aoDistance:i||3,emissiveMultiplier:void 0===u?32:u,bounceMultiplier:void 0===c?1:c,lightScene:r,atlasMap:E,irradiance:A,irradianceData:k,settings:d});case 16:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function pe(e){return e instanceof n.MeshLambertMaterial||e instanceof n.MeshPhongMaterial||e instanceof n.MeshStandardMaterial||e instanceof n.MeshPhysicalMaterial}var he=Symbol("lightmap baker: stashed original material");function de(e,r){return ve.apply(this,arguments)}function ve(){return(ve=u(p.mark((function e(r,t){var a,o,i,u,s,c,f,h,d,v,m,g,y,w;return p.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:a=r.aoMode,o=r.emissiveMultiplier,i=r.bounceMultiplier,s=r.irradiance,c=[],f=[],h=l(ue(u=r.lightScene,!1,(function(e){e.visible&&(e.visible=!1,f.push(e))})));case 4:if((d=h()).done){e.next=20;break}if(v=d.value,!(a&&v instanceof n.Light)){e.next=10;break}return v.visible=!1,f.push(v),e.abrupt("continue",18);case 10:if(v instanceof n.Mesh){e.next=12;break}return e.abrupt("continue",18);case 12:m=v,g=Array.isArray(m.material)?m.material:[m.material],y=g.map((function(e){if(!e||!pe(e))return e;if(a){if(e.aoMap&&e.aoMap!==s)throw new Error("do not set your own AO map manually on baked scene meshes")}else if(e.lightMap&&e.lightMap!==s)throw new Error("do not set your own light map manually on baked scene meshes");var r=new n.MeshPhongMaterial;return r.alphaMap=e.alphaMap,r.alphaTest=e.alphaTest,e instanceof n.MeshLambertMaterial||(r.displacementBias=e.displacementBias,r.displacementMap=e.displacementMap,r.displacementScale=e.displacementScale,r.flatShading=e.flatShading),r.morphNormals=e.morphNormals,r.morphTargets=e.morphTargets,r.opacity=e.opacity,r.premultipliedAlpha=e.premultipliedAlpha,r.side=e.side,r.skinning=e.skinning,r.transparent=e.transparent,r.visible=e.visible,a||(r.color=e.color,r.emissive=e.emissive,r.emissiveIntensity=e.emissiveIntensity*o,r.emissiveMap=e.emissiveMap,r.map=e.map,r.shadowSide=e.shadowSide,r.vertexColors=e.vertexColors),r.shininess=0,r.toneMapped=!1,ie?(r.aoMap=e.aoMap,r.aoMapIntensity=e.aoMapIntensity,a||(r.lightMap=e.lightMap,r.lightMapIntensity=e.lightMapIntensity*i)):a?(r.aoMap=s,e.aoMap=s):(r.lightMapIntensity=i,r.lightMap=s,e.lightMap=s,r.aoMap=e.aoMap,r.aoMapIntensity=e.aoMapIntensity),r})),m.userData[he]=m.material,m.material=Array.isArray(m.material)?y:y[0],c.push(m);case 18:e.next=4;break;case 20:return w=null,a&&(w=new n.AmbientLight("#ffffff"),u.add(w)),e.prev=22,e.next=25,t();case 25:return e.prev=25,w&&u.remove(w),f.forEach((function(e){e.visible=!0})),c.forEach((function(e){var r=e.userData,t=r[he];delete r[he],t?e.material=t:console.error("lightmap baker: missing original material",e)})),e.finish(25);case 30:case"end":return e.stop()}}),e,null,[[22,,25,30]])})))).apply(this,arguments)}var me=[1,1,0,-1,-1,-1,0,1],ge=[0,1,1,1,0,-1,-1,-1];function ye(e,r,t,n,a,o){var i=4*n;a.w=1,a.toArray(o,i);for(var u=n%r,s=n-u,c=0;c<8;c+=1){var l=me[c],f=ge[c],p=4*((t+s+f*r)%t+(r+u+l)%r);0===e[p+2]&&(0===l||0===f||0===o[p+3])&&a.toArray(o,p)}}function we(e,r){return xe.apply(this,arguments)}function xe(){return(xe=u(p.mark((function e(r,t){return p.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,R(r.aoMode,r.aoDistance,r.settings,function(){var e=u(p.mark((function e(n){var a,o,i,u,s,c,f,h,d,v,g,y;return p.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:o=r.irradiance,i=r.irradianceData,s=(u=(a=r.atlasMap).width)*a.height,c=new Float32Array(4*s),f=0;case 5:if(!(f<2)){e.next=21;break}c.fill(0),h=!1,d=m(a,(function(){h=!0}));case 9:if(h){e.next=16;break}return e.next=12,t();case 12:for(v=l(n(e.sent,r.lightScene,d));!(g=v()).done;)ye(a.data,u,s,(y=g.value).texelIndex,y.rgba,c);e.next=9;break;case 16:i.set(c),o.needsUpdate=!0;case 18:f+=1,e.next=5;break;case 21:case"end":return e.stop()}}),e)})));return function(r){return e.apply(this,arguments)}}());case 2:case"end":return e.stop()}}),e)})))).apply(this,arguments)}var be=t.createContext(null),Me=function(e){},Ae=function(e){},ke=function(e){var n=e.workPerFrame,a=e.children,i=o.useThree().gl,s=Math.max(1,n||2),c=r.useRef(s);c.current=s;var f=r.useRef(!1),h=r.useRef([]),d=r.useRef(!1);r.useEffect((function(){return function(){d.current=!0;var e=[].concat(h.current);h.current.length=0;for(var r,t=l(e);!(r=t()).done;){var n=r.value;try{n.reject(new Error("work manager was unmounted while waiting for RAF"))}catch(e){}}}}),[]);var v=r.useCallback((function(){if(d.current)throw new Error("work manager is no longer available");if(!f.current){var e=function(){var r=u(p.mark((function r(){var t,n,a;return p.wrap((function(r){for(;;)switch(r.prev=r.next){case 0:t=0;case 1:if(!(t<c.current)){r.next=16;break}if(0!==h.current.length){r.next=5;break}return f.current=!1,r.abrupt("return");case 5:return n=Math.floor(Math.random()*h.current.length),a=h.current[n],h.current.splice(n,1),a.resolve(i),r.next=11,a.promise;case 11:return r.next=13,a.promise;case 13:t+=1,r.next=1;break;case 16:requestAnimationFrame(e);case 17:case"end":return r.stop()}}),r)})));return function(){return r.apply(this,arguments)}}();f.current=!0,requestAnimationFrame(e)}var r=Me,t=Ae,n=new Promise((function(e,n){r=e,t=n}));return h.current.push({resolve:r,reject:t,promise:n}),n}),[i]);return t.createElement(t.Fragment,null,t.createElement(be.Provider,{value:v},a))},Ee=t.createContext(null);function Le(){return(Le=u(p.mark((function e(r,t,n,a){var o;return p.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,le(r,t,n);case 2:return a(o=e.sent),e.next=6,de(o,u(p.mark((function e(){return p.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,we(o,n);case 2:case"end":return e.stop()}}),e)}))));case 6:case"end":return e.stop()}}),e)})))).apply(this,arguments)}var Ie=function(e){throw e.promise},Se=function(e){var t=e.onStarted,n=r.useRef(t);return n.current=t,r.useLayoutEffect((function(){var e=function(){},r=new Promise((function(r){e=r}));return n.current(r),function(){e()}}),[]),null},Fe=function(e){var a=r.useRef(e),o=r.useRef(!!e.legacySuspense),i=function(){var e=r.useContext(be);if(!e)throw new Error("must be inside work manager");var t=r.useRef((function(){}));return r.useEffect((function(){return function(){t.current()}}),[]),r.useMemo((function(){var r=new Promise((function(e){t.current=e})).then((function(){throw new Error("work requester was unmounted")}));return r.catch((function(){})),function(){return Promise.race([r,e()])}}),[e])}(),u=r.useRef(!e.disabled);u.current=u.current||!e.disabled;var s=u.current,c=r.useState(null),l=c[0],f=c[1],p=r.useState(null),h=p[0],d=p[1],v=r.useRef(null),m=r.useRef();r.useLayoutEffect((function(){if(s){var e=(v.current||Promise.resolve()).then((function(){var e=m.current;if(!(e&&e instanceof n.Scene))throw new Error("expecting lightmap scene");return function(e,r,t,n){return Le.apply(this,arguments)}(e,a.current,i,(function(e){f(e)}))})).then((function(){d({promise:e,isComplete:!0})}));d({promise:e,isComplete:!1})}}),[s,i]);var g=r.useMemo((function(){return l?{atlasTexture:l.atlasMap.texture,outputTexture:l.irradiance}:null}),[l]),y=t.createElement("group",{name:"Lightmap Scene Wrapper"},t.cloneElement(e.children,{ref:m}));return t.createElement(Ee.Provider,{value:g},h&&!h.isComplete?t.createElement(Ie,{promise:h.promise}):null,o.current?t.createElement(t.Suspense,{fallback:t.createElement(Se,{onStarted:function(e){v.current=e}})},y):y)},Ce=t.forwardRef((function(e,r){var n=e.workPerFrame,a=e.children,o=function(e,r){if(null==e)return{};var t,n,a={},o=Object.keys(e);for(n=0;n<o.length;n++)r.indexOf(t=o[n])>=0||(a[t]=e[t]);return a}(e,["workPerFrame","children"]);return t.createElement(ke,{workPerFrame:n},t.createElement(Fe,Object.assign({},o),t.createElement("scene",{name:"Lightmap Scene",ref:r},a)))}));exports.DebugContext=Ee,exports.Lightmap=Ce,exports.LightmapIgnore=function(e){var r,n=e.children;return t.createElement("group",{name:"Lightmap opt-out wrapper",userData:(r={},r[te]=!0,r)},n)},exports.LightmapReadOnly=function(e){var r,n=e.children;return t.createElement("group",{name:"Lightmap read-only wrapper",userData:(r={},r[ne]=!0,r)},n)};
//# sourceMappingURL=lightmap.cjs.production.min.js.map