@spiffcommerce/preview 2.1.6 → 2.1.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/main.js +1 -1
- package/dist/types.d.ts +4 -0
- package/package.json +4 -2
package/dist/main.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
var e=require("@babylonjs/core/Actions/actionManager"),t=require("@babylonjs/core/Actions/directActions"),i=require("@babylonjs/core/Engines/engine"),a=require("@babylonjs/core/Engines/nullEngine"),r=require("@babylonjs/core/Layers/glowLayer"),n=require("@babylonjs/core/Layers/highlightLayer"),s=require("@babylonjs/core/Materials/PBR/pbrMaterial"),o=require("@babylonjs/core/Materials/Textures/cubeTexture"),c=require("@babylonjs/core/Materials/Textures/dynamicTexture"),l=require("@babylonjs/core/Maths/math.color"),h=require("@babylonjs/core/Maths/math.vector"),u=require("@babylonjs/core/Meshes/Compression/dracoCompression"),d=require("@babylonjs/core/Misc/observable"),m=require("@babylonjs/core/Misc/tools"),g=require("@babylonjs/core/scene"),p=require("@babylonjs/core/Loading/sceneLoader"),b=require("@babylonjs/loaders/glTF");require("@babylonjs/core/Engines/Extensions/engine.views"),require("@babylonjs/core/Materials/Textures/Loaders/ddsTextureLoader"),require("@babylonjs/core/Materials/Textures/Loaders/envTextureLoader"),require("@babylonjs/core/Materials/Textures/Loaders/ktxTextureLoader"),require("@babylonjs/core/Misc/screenshotTools"),require("@babylonjs/core/Rendering/boundingBoxRenderer"),require("@babylonjs/loaders/glTF/2.0/Extensions");var f=require("@babylonjs/core/Materials/Textures/texture"),y=require("@babylonjs/core/Materials/Textures/mirrorTexture"),v=require("@babylonjs/core/Maths/math"),w=require("@babylonjs/core/Probes/reflectionProbe"),x=require("@babylonjs/core/Animations/animation"),M=require("@babylonjs/core/Animations/easing"),T=require("@babylonjs/core/Cameras/arcRotateCamera"),C=require("@babylonjs/core/Misc/assetsManager");function A(e,t){return Object.keys(t).forEach((function(i){"default"===i||"__esModule"===i||e.hasOwnProperty(i)||Object.defineProperty(e,i,{enumerable:!0,get:function(){return t[i]}})})),e}function L(e,t,i,a){Object.defineProperty(e,t,{get:i,set:a,enumerable:!0,configurable:!0})}var R={};let P;var O;let E;var S;L(R,"ProductCameraRig",(()=>P)),L(R,"MaterialEffectMode",(()=>E)),(O=P||(P={}))[O.Orbit=0]="Orbit",O[O.Pan=1]="Pan",(S=E||(E={})).None="None",S.RemoveWhenSelected="RemoveWhenSelected",S.ApplyWhenSelected="ApplyWhenSelected";var I={};L(I,"SpiffCommerce3DPreviewService",(()=>ae)),L(I,"createBaseModel",(()=>re));const B=new Map;async function F(e,t,i){return new Promise(((a,r)=>{const n=B.get(e);if(n&&n.scene.uid===t.uid)return a(n);p.SceneLoader.LoadAssetContainerAsync(e,void 0,t,i).then((t=>{B.set(e,t),a(t)})).catch(r)}))}function q(e,t,i){return t in e?Object.defineProperty(e,t,{value:i,enumerable:!0,configurable:!0,writable:!0}):e[t]=i,e}class j{constructor(e){q(this,"customOptions",void 0),q(this,"getSceneClearColor",(()=>{const e=this.customOptions?.backgroundImage?0:1;if(this.customOptions&&this.customOptions.backgroundImage)return new(0,l.Color4)(0,0,0,e).toLinearSpace();if(this.customOptions&&this.customOptions.backgroundColor){const t=l.Color3.FromHexString(this.customOptions.backgroundColor);return new(0,l.Color4)(t.r,t.g,t.b,e).toLinearSpace()}return new(0,l.Color4)(.98,.98,.98,e).toLinearSpace()})),q(this,"highlightColorFromConfig",(()=>this.customOptions&&this.customOptions.highlightColor?this.hexToColor4(this.customOptions.highlightColor):new(0,l.Color4)(.98,.98,.98,1).toLinearSpace())),q(this,"hexToColor4",((e,t=1)=>{const i=l.Color3.FromHexString(e);return new(0,l.Color4)(i.r,i.g,i.b,t).toLinearSpace()})),this.customOptions=e}get options(){return this.customOptions}get scene(){return{clearColor:this.getSceneClearColor(),transparentBackground:this.customOptions?.backgroundImage,environmentFile:this.customOptions?.environmentFile??"assets/model-viewer/default.env"}}get camera(){return{autoOrientation:this.customOptions?.disableAutomaticOrientation??!0,autoRotation:{enabled:this.customOptions?.autoRotation??!1,idleTimeMs:this.customOptions?.idleTimeBeforeRotation??5e3},limits:{min:{alpha:this.customOptions?.lowerAlphaLimitDeg?this.customOptions?.lowerAlphaLimitDeg*Math.PI/180:void 0,beta:this.customOptions?.lowerBetaLimitDeg?this.customOptions?.lowerBetaLimitDeg*Math.PI/180:void 0,radius:this.customOptions?.minZoomOverride},max:{alpha:this.customOptions?.upperAlphaLimitDeg?this.customOptions?.upperAlphaLimitDeg*Math.PI/180:void 0,beta:this.customOptions?.upperBetaLimitDeg?this.customOptions?.upperBetaLimitDeg*Math.PI/180:void 0,radius:this.customOptions?.maxZoomOverride}}}}get highlights(){return{enabled:this.customOptions?.highlightOnMaterialHover??!1,color:this.highlightColorFromConfig()}}}var N={};L(N,"RenderingConfiguration",(()=>k)),L(N,"REFLECTION_PROBE_RESOLUTION",(()=>D));class k{static getDynamicTextureResolution(){return this.getIsMobile()||!k.offscreenRenderingSupported()?{width:1024,height:1024}:{width:2048,height:2048}}static shouldMipMap(){return!0}static offscreenRenderingSupported(){return!navigator.userAgent.includes("SamsungBrowser")&&(!!window.Worker&&!!window.OffscreenCanvas)}static getMirrorTextureResolution(){return this.getIsMobile()?512:1024}static getIsMobile(){return window.innerWidth<=480}}const D=128;function V(e,t,a){t.forEach((t=>{const r=t.getID(),n=t.getName(),s=k.getDynamicTextureResolution();e.materials.filter((e=>e.name===n)).forEach((o=>{const l=a.get(r);if(l)_(o,l),l.update(false);else{const l=function(e,t,a,r){const n=new(0,c.DynamicTexture)(e,{width:a,height:r},t,k.shouldMipMap(),f.Texture.TRILINEAR_SAMPLINGMODE,i.Engine.TEXTUREFORMAT_RGBA),s=n.getContext();s&&(s.fillStyle="#f5f5f5",s.fillRect(0,0,a,r),n.update());return n}(n,e,s.width,s.height);a.set(r,l),t.setStaticContext(l.getContext()),_(o,l),l.onLoadObservable.addOnce((()=>{l.update(false)}))}}))}))}function _(e,t){if(e instanceof s.PBRMaterial){const i=e,a=i.albedoTexture;a?(t.wrapU=a.wrapU,t.wrapV=a.wrapV):(t.wrapU=1,t.wrapV=1),i.albedoTexture=t}else{const i=e,a=i.diffuseTexture;a&&(t.wrapU=a.wrapU,t.wrapV=a.wrapV),i.diffuseTexture=t}}function z(e,t,i){return t in e?Object.defineProperty(e,t,{value:i,enumerable:!0,configurable:!0,writable:!0}):e[t]=i,e}class U{constructor(e){z(this,"name",void 0),z(this,"enabled",void 0),z(this,"loader",void 0),this.name="glbPostProcessor",this.enabled=!0,this.loader=e}onReady(){this.applyReflections(this.loader.babylonScene)}loadNodeAsync(e,t,i){return this.loader.loadNodeAsync(e,t,(function(e){t.extras&&Object.keys(t.extras).forEach((i=>{const a=t.extras[i];e.metadata[i]=a})),i(e)}))}loadMaterialPropertiesAsync(e,t,i){return this.enableMaterialExtrasIfRequired(t,i),this.loader.loadMaterialPropertiesAsync(e,t,i)}dispose(){}enableMaterialExtrasIfRequired(e,t){if(e.extras&&t instanceof s.PBRMaterial){if(e.extras.sheen){const i=t;i.sheen.isEnabled=!0,i.sheen.intensity=e.extras.sheen}if(e.extras.translucency){const i=t;i.subSurface.isTranslucencyEnabled=!0,i.subSurface.translucencyIntensity=e.extras.translucency,e.extras.translucencyR&&e.extras.translucencyG&&e.extras.translucencyB&&(i.subSurface.tintColor=new(0,v.Color3)(e.extras.translucencyR,e.extras.translucencyG,e.extras.translucencyB))}if(e.extras.useDepthPrePass){const e=t;e.needDepthPrePass=!0,e.forceIrradianceInFragment=!0}if(e.extras.useParallax){const i=t;i.useParallax=!0,i.useParallaxOcclusion=!0,i.parallaxScaleBias=e.extras.useParallax}}}applyReflections(e){function t(e){const t=[];return e.transformNodes.forEach((e=>{e.metadata&&e.metadata.reflective&&t.push(...e.getChildMeshes())})),e.meshes.forEach((e=>{e.metadata&&e.metadata.reflective&&!t.includes(e)&&t.push(e)})),t}e.meshes.forEach((i=>{const a=i.metadata;a&&(a.mirrorTexture&&function(i,a=1){const r=i.material;if(!r)return;const n=new(0,y.MirrorTexture)("mirror",k.getMirrorTextureResolution(),e,!0);n.renderList=t(e);const s=i.getVerticesData("normal");if(!s)throw new Error("Mirror attribute specified on: "+i.name+"But no normals exist to generate a mirror from!");i.computeWorldMatrix(!0);const o=i.getWorldMatrix(),c=v.Vector3.TransformNormal(new(0,v.Vector3)(s[0],s[1],s[2]),o).normalize(),l=v.Plane.FromPositionAndNormal(i.position,c.scale(-1));n.mirrorPlane=l,n.level=a,r.reflectionTexture=n}(i,a.mirrorTexture),a.reflectionProbe&&function(i){const a=i.material,r=new(0,w.ReflectionProbe)("probe-"+a.name,D,e);r.attachToMesh(i),r.renderList&&r.renderList.push(...t(e)),a.reflectionTexture=r.cubeTexture}(i))}))}}function G(e,t,i,a){const r=e.animationGroups,n=e=>e.targetedAnimations.map((e=>e.animation.framePerSecond)).reduce(((e,t)=>e+t),0)/e.targetedAnimations.length||0;void 0===a||void 0===i||a!==i?r.forEach((e=>{e.stop();const r=n(e);e.start(t,1,a*r,i*r)})):r.forEach((e=>{e.stop();const i=n(e),r=a*i;e.start(t,1,r,r)}))}function H(e){e.animationGroups.forEach((e=>{e.stop()}))}function W(e,t,i){var a,r,n;e.stopAnimation(t),t.animations=[],Math.abs(t.alpha)>2*Math.PI&&(t.alpha=(a=t.alpha,r=0,n=2*Math.PI,a<r?n-(r-a)%(n-r):r+(a-r)%(n-r)));const s=[],o=i.target,c=o?1:0;if(i.target&&s.push(Z("cameraTargetLerp","target",(new(0,h.Vector3)).copyFrom(t.target),new(0,h.Vector3)(i.target.x,i.target.y,i.target.z),x.Animation.ANIMATIONTYPE_VECTOR3,0)),s.push(Z("cameraAlphaLerp","alpha",t.alpha,K(i.lonDeg),x.Animation.ANIMATIONTYPE_FLOAT,c)),s.push(Z("cameraBetaLerp","beta",t.beta,K(i.latDeg),x.Animation.ANIMATIONTYPE_FLOAT,c)),void 0!==i.radius){const e=Math.max(.01,i.radius);s.push(Z("cameraRadiusLerp","radius",t.radius,e,x.Animation.ANIMATIONTYPE_FLOAT,c))}t.animations.push(...s);const l=t.useAutoRotationBehavior;t.disableAutoRotationBehavior(),e.beginAnimation(t,0,o?120:60,!1,1,(()=>{t.animations=[],l&&t.enableAutoRotationBehavior()}))}function K(e){return e*Math.PI/180}function Z(e,t,i,a,r,n=0,s=x.Animation.ANIMATIONLOOPMODE_CONSTANT){const o=new(0,M.QuadraticEase);o.setEasingMode(M.EasingFunction.EASINGMODE_EASEINOUT);const c=new(0,x.Animation)(e,t,60,r,s),l=[];return n>0&&l.push({frame:0,value:i}),l.push({frame:60*n,value:i}),l.push({frame:60*(n+1),value:a}),c.setKeys(l),c.setEasingFunction(o),c}class Y extends T.ArcRotateCamera{constructor(e,t,i,a,r,n,s,o){var c,l,u;super(e,t,i,a,r,n,o),c=this,l="lastFocus",u=new(0,h.Vector3)(0,0,0),l in c?Object.defineProperty(c,l,{value:u,enumerable:!0,configurable:!0,writable:!0}):c[l]=u,this.minZ=.01,this.maxZ=1e3*this.radius,this.lowerRadiusLimit=.01*this.radius,this.enableFramingBehavior(),this.upperRadiusLimit=1.5*this.radius,this.wheelPrecision=100/this.radius,this.pinchPrecision=300/this.radius,this.wheelDeltaPercentage=.01,this.pinchDeltaPercentage=.005,this.useNaturalPinchZoom=!0,s.camera.autoOrientation&&(this.alpha+=Math.PI),s&&(s.camera.limits.min.beta&&(this.lowerBetaLimit=s.camera.limits.min.beta),s.camera.limits.max.beta&&(this.upperBetaLimit=s.camera.limits.max.beta),s.camera.limits.min.alpha&&(this.lowerAlphaLimit=s.camera.limits.min.alpha),s.camera.limits.max.alpha&&(this.upperAlphaLimit=s.camera.limits.max.alpha),s.camera.limits.min.radius&&(this.lowerRadiusLimit=s.camera.limits.min.radius),s.camera.limits.max.radius&&(this.upperRadiusLimit=s.camera.limits.max.radius),s.camera.autoRotation.enabled&&this.enableAutoRotationBehavior(s.camera.autoRotation.idleTimeMs))}getFramingBehavior(){return this.getBehaviorByName("Framing")}getAutoRotationBehavior(){const e=this.getBehaviorByName("AutoRotation");return e||void 0}enableFramingBehavior(){this.useFramingBehavior=!0;const e=this.getFramingBehavior();e.attach(this),e.framingTime=0,e.elevationReturnTime=-1,e.zoomStopsAnimation=!1,this.lowerRadiusLimit=null;const t=Q(this._scene);return e.zoomOnBoundingInfo(t.min,t.max),this.wheelPrecision=100/this.radius,null===this.lowerRadiusLimit&&(this.lowerRadiusLimit=.1),this.lastFocus.copyFrom(this.target),e}rerunFramingBehavior(e){const t=this.getFramingBehavior();t.framingTime=800;const i=Q(this._scene);t.zoomOnBoundingInfo(i.min,i.max,void 0,(()=>{e()})),t.framingTime=0}enableAutoRotationBehavior(e=5e3){this.useAutoRotationBehavior=!0;const t=this.getAutoRotationBehavior();t&&(t.idleRotationWaitTime=e)}disableAutoRotationBehavior(){this.useAutoRotationBehavior=!1}static create(e,t){e.activeCamera&&(e.activeCamera.dispose(),e.activeCamera=null);const i=Q(e),a=i.max.subtract(i.min),r=i.min.add(a.scale(.5)),n=new Y("ProductCamera",-Math.PI/2,Math.PI/2,1.5*a.length(),r,e,t);return n.panningInertia=0,n.panningOriginTarget.copyFrom(r),n.onAfterCheckInputsObservable.add((()=>{n.panningSensibility=1e3/a.length()})),e.activeCamera=n,n}}function Q(e){const t=e.meshes.filter((e=>e.name.toLowerCase().endsWith("_t")||e.name.toLowerCase().includes("_t_")));return e.getWorldExtends((e=>e.isVisible&&e.isEnabled()&&(0===t.length||t.includes(e))))}const X={albedoTexture:"albedoMapKey",bumpTexture:"normalMapKey",ambientTexture:"ambientMapKey",emissiveTexture:"emissionMapKey",opacityTexture:"alphaMapKey",metallicTexture:"metallicMapKey",refractionTexture:"refractionMapKey"};function $(e,t,i,a){["albedoTexture","bumpTexture","ambientTexture","emissiveTexture","opacityTexture","metallicTexture","refractionTexture"].forEach((r=>{!function(e,t,i,a,r){const n=X[e];if(!n)throw new Error("Unexpected texture name encountered.");const s=t[n];s?a.addTextureTask(e,s,!1,!1):r&&i[e]&&(i[e]&&i[e].dispose(),i[e]=null,function(e,t){"opacityTexture"===e&&(t.useAlphaFromAlbedoTexture=!0);"metallicTexture"===e&&(t.useRoughnessFromMetallicTextureAlpha=!1,t.useRoughnessFromMetallicTextureGreen=!1,t.useMetallnessFromMetallicTextureBlue=!1);"refractionTexture"===e&&(t.subSurface.isRefractionEnabled=!1,t.subSurface.refractionIntensity=1)}(e,i))}(r,e,t,i,a)})),function(e,t){if(!e.clearCoat)return;e.clearCoat===E.RemoveWhenSelected?(t.clearCoat.isEnabled=!1,t.clearCoat.indexOfRefraction=1.5):e.clearCoat===E.ApplyWhenSelected&&(t.clearCoat.isEnabled=!0,t.clearCoat.indexOfRefraction=e.clearCoatIOR||t.clearCoat.indexOfRefraction)}(e,t)}function J(e,t,i,a){"opacityTexture"===e&&(t.useAlphaFromAlbedoTexture=!1),"metallicTexture"===e&&(t.useRoughnessFromMetallicTextureAlpha=!1,t.useRoughnessFromMetallicTextureGreen=!0,t.useMetallnessFromMetallicTextureBlue=!0),"refractionTexture"===e&&(t.subSurface.isRefractionEnabled=!0,t.subSurface.refractionIntensity=i.refractionIntensity||1),t[e]=a}function ee(e,t,i){return t in e?Object.defineProperty(e,t,{value:i,enumerable:!0,configurable:!0,writable:!0}):e[t]=i,e}class te{constructor(){ee(this,"materialVariantMap",new Map),ee(this,"keysThatRemovedBaseModel",[]),ee(this,"loadedContainerForKey",new Map)}async applyMaterial(e,t,i,a,r){return new Promise((n=>{const s=e.materials.filter((e=>e.name===t)),o=this.materialVariantMap.get(t);if(this.materialVariantMap.set(t,{...o,...i}),0===s.length)return void n(void 0);const c=new(0,C.AssetsManager)(e);c.useDefaultLoadingScreen=!1,s.forEach((e=>$(i,e,c,r))),c.onProgress=(e,t,i)=>{a&&a(e/t*100,100,i.name)},c.onFinish=e=>{e.forEach((e=>{const t=e;a&&a(100,100,e.name),s.forEach((a=>J(e.name,a,i,t.texture)))})),n(void 0)},c.loadAsync()}))}async applyModel(e,t,i,a,r){if(a&&i&&!this.keysThatRemovedBaseModel.includes(t)&&this.keysThatRemovedBaseModel.push(t),!a){const i=this.keysThatRemovedBaseModel.includes(t);return this.loadedContainerForKey.get(t)?.removeAllFromScene(),e&&i&&await re(e.metadata.baseModel,e),Promise.resolve()}const n=await F(a,e,r);if(this.loadedContainerForKey.has(t)&&this.loadedContainerForKey.get(t)?.removeAllFromScene(),i){(await F(e.metadata.baseModel,e)).removeAllFromScene()}n.addAllToScene(),this.loadedContainerForKey.set(t,n);const s=[];this.materialVariantMap.forEach((async(t,i)=>{s.push(this.applyMaterial(e,i,t))})),await Promise.all(s)}}function ie(e,t,i){return t in e?Object.defineProperty(e,t,{value:i,enumerable:!0,configurable:!0,writable:!0}):e[t]=i,e}b.GLTF2.GLTFLoader.RegisterExtension("glbPostProcessor",(function(e){return new U(e)})),p.SceneLoader.OnPluginActivatedObservable.add((e=>{if("gltf"===e.name){const t=e;t.useRangeRequests=!0,t.transparencyAsCoverage=!0}}));class ae{constructor(e,t){ie(this,"engine",void 0),ie(this,"scene",void 0),ie(this,"configuration",void 0),ie(this,"loadProgress",new Map([["initialScene",0]])),ie(this,"materialReadyToLoadCallbacks",new Map),ie(this,"modelReadyToLoadCallbacks",new Map),ie(this,"queuedModelAnimation",void 0),ie(this,"queuedCameraAnimation",void 0),ie(this,"focusLostNotified",!1),ie(this,"loadObservable",new(0,d.Observable)),ie(this,"focusLostObservable",new(0,d.Observable)),ie(this,"dynamicTextures",new Map),ie(this,"highlightLayer",void 0),ie(this,"variantManager",new te),this.configuration=new j(t);u.DracoCompression.Configuration={decoder:{wasmUrl:"https://www.gstatic.com/draco/versioned/decoders/1.5.3/draco_wasm_wrapper_gltf.js",wasmBinaryUrl:"https://www.gstatic.com/draco/versioned/decoders/1.5.3/draco_decoder_gltf.wasm",fallbackUrl:"https://www.gstatic.com/draco/versioned/decoders/1.5.3/draco_decoder_gltf.js"}},e&&(e.getContext("webgl2")||e.getContext("webgl"));const r=console.log;console.log=()=>{};const n=e?new(0,i.Engine)(e,!0,{premultipliedAlpha:!1,preserveDrawingBuffer:!!t?.backgroundImage,audioEngine:!1,stencil:this.configuration.highlights.enabled,forceSRGBBufferSupportState:!0}):new(0,a.NullEngine);console.log=r,n.hideLoadingUI(),window.addEventListener("resize",this.fireResizeEvent.bind(this)),this.engine=n,this.scene=new(0,g.Scene)(n)}registerFocusLostListener(e){this.focusLostObservable.add(e)}unregisterFocusLostListener(e){this.focusLostObservable.removeCallback(e)}registerLoadProgressListener(e){this.loadObservable.add(e),e(this.getLoadListenerEvent())}unregisterLoadProgressListener(e){this.loadObservable.removeCallback(e)}registerView(e){const t=e.height,i=e.width;this.engine.registerView(e),e.setAttribute("height",t.toString()),e.setAttribute("width",i.toString()),this.orbitEnabled()||this.setCameraState(P.Pan)}getNumViewports(){return this.engine.views?.length||0}unregisterView(e){this.engine.unRegisterView(e),this.engine.inputElement=null}shutdown(){this.engine&&this.engine.dispose(),window.removeEventListener("resize",this.fireResizeEvent)}getSceneInitializationProgress(){return this.loadProgress.get("initialScene")}async initialize(i,a){if(this.scene.clearColor=this.configuration.scene.clearColor,this.scene.metadata={baseModel:i},this.scene.environmentTexture=o.CubeTexture.CreateFromPrefilteredData(this.configuration.scene.environmentFile,this.scene),i){const e=99;await re(i,this.scene,(t=>{this.loadProgress.set("initialScene",t.loaded*e/t.total),this.notifyLoadHandlers()}))}this.configuration.highlights.enabled&&this.scene.meshes.forEach((i=>{"targetcube_t"!==i.name&&"backgroundShell"!==i.name&&(i.isPickable=!0,i.actionManager||(i.actionManager=new(0,e.ActionManager)(this.scene)),i.actionManager.registerAction(new(0,t.ExecuteCodeAction)(e.ActionManager.OnPointerOutTrigger,(()=>{this.setHighlights([])}))),i.actionManager.registerAction(new(0,t.ExecuteCodeAction)(e.ActionManager.OnPointerOverTrigger,(e=>{if(e.meshUnderPointer){const t=e.meshUnderPointer.material;t&&this.setHighlights([t])}}))))}));const n=Y.create(this.scene,this.configuration),s=a?.getAll()||new Map;V(this.scene,s,this.dynamicTextures);if(this.scene.materials.some((e=>"emissiveTexture"in e&&null!==e.emissiveTexture))){new(0,r.GlowLayer)("GlowLayer",this.scene).intensity=.5}H(this.scene),this.engine.runRenderLoop((()=>{if(!this.engine.views)return;const e=this.engine.views[0],t=this.engine.inputElement;(!t||e&&t&&t.id!==e.target.id)&&this.reattachControls(e.target),s.forEach(((e,t)=>{const i=this.dynamicTextures.get(t);i&&e.getStaticContextDirty()&&i.isReady()&&(i.update(!1),e.setStaticContextDirty(!1))})),this.configuration.scene.transparentBackground&&this.engine.views.forEach((e=>{const t=this.engine.getRenderingCanvas();e.target.getContext("2d").clearRect(0,0,t.width,t.height)})),this.scene.render(),100!==this.getSceneInitializationProgress()&&(this.loadProgress.set("initialScene",100),this.materialReadyToLoadCallbacks.forEach(((e,t)=>{e.forEach(((e,i)=>{this.applyMaterialVariant(t,i,e)}))})),this.materialReadyToLoadCallbacks.clear(),this.modelReadyToLoadCallbacks.forEach((e=>{e(this.scene)})),this.modelReadyToLoadCallbacks.clear(),this.queuedModelAnimation&&(this.executeModelAnimation(this.queuedModelAnimation),this.queuedModelAnimation=void 0),this.queuedCameraAnimation&&(this.executeCameraAnimation(this.queuedCameraAnimation),this.queuedCameraAnimation=void 0),this.notifyLoadHandlers()),n.target.equalsWithEpsilon(n.lastFocus,.1)||this.focusLostNotified||(this.focusLostObservable.notifyObservers(void 0),this.focusLostNotified=!0)}))}executeModelAnimation(e){100===this.getSceneInitializationProgress()?G(this.scene,e.loop,e.to,e.from):this.queuedModelAnimation=e}executeCameraAnimation(e){100===this.getSceneInitializationProgress()&&this.scene.activeCamera?W(this.scene,this.scene.activeCamera,e):this.queuedCameraAnimation=e}getCameraPose(){if(this.scene){const e=this.scene.activeCamera;if(e)return{lonDeg:Math.round(180*e.alpha/Math.PI),latDeg:Math.round(180*e.beta/Math.PI),radius:Math.round(1e4*(e.radius+Number.EPSILON))/1e4,target:{x:e.target.x,y:e.target.y,z:e.target.z}}}}setCameraPose(e){if(this.scene){const t=this.scene.activeCamera;t&&(t.target=new(0,h.Vector3)(e.target.x,e.target.y,e.target.z),t.radius=e.radius,t.alpha=e.latDeg,t.beta=e.lonDeg)}}async applyMaterialVariant(e,t,i,a){if(100===this.getSceneInitializationProgress())await this.variantManager.applyMaterial(this.scene,e,i,((e,t,i)=>{this.loadProgress.set(`key_${i}`,e/t*100),this.notifyLoadHandlers()}),a);else if(this.materialReadyToLoadCallbacks.has(e)){this.materialReadyToLoadCallbacks.get(e)?.set(t,i)}else{this.materialReadyToLoadCallbacks.set(e,new Map);this.materialReadyToLoadCallbacks.get(e)?.set(t,i)}}async applyModelVariant(e,t,i){100===this.getSceneInitializationProgress()?(await this.variantManager.applyModel(this.scene,e,i||!1,t.model,(t=>{this.loadProgress.set(e,100*t.loaded/t.total),this.notifyLoadHandlers()})),this.scene.materials.forEach((e=>{if(e&&0===e.getBindedMeshes().length){const t=e instanceof s.PBRMaterial&&!(e.albedoTexture instanceof c.DynamicTexture);e.dispose(!1,t)}})),V(this.scene,t.contextService.getAll(),this.dynamicTextures),H(this.scene)):this.modelReadyToLoadCallbacks.set(e,(()=>{this.applyModelVariant(e,t,i)}))}setCameraState(e){if(!this.engine?.views||!this.engine?.views[0])throw new Error("No views attached, camera state requires a view to attach controls onto.");e===P.Orbit?this.reattachControls(this.engine.views[0].target,2):this.reattachControls(this.engine.views[0].target,0)}animateToLastCameraFocus(){return new Promise((e=>{const t=this.scene.activeCamera,i=this.configuration;t.rerunFramingBehavior((()=>{this.focusLostNotified=!1,i.camera.limits.min.radius&&(t.lowerRadiusLimit=i.camera.limits.min.radius),i.camera.limits.max.radius&&(t.upperRadiusLimit=i.camera.limits.max.radius),e()}))}))}setAutoRotation(e){const t=this.scene.activeCamera;this.configuration.camera.autoRotation.enabled&&t&&(e?t.enableAutoRotationBehavior(this.configuration.camera.autoRotation.idleTimeMs):t.disableAutoRotationBehavior())}getCurrentConfiguration(){return this.configuration.options}async renderSceneScreenshot(e,t){const i=this.scene.activeCamera;if(!i)throw new Error("Missing product camera, cannot render screenshot!");var a;(a=i).getScene().stopAnimation(a),a.animations=[];const r=i.alpha,n=i.beta,s=i.radius,o=t.latDeg*Math.PI/180,c=t.lonDeg*Math.PI/180;i.alpha=c,i.beta=o,i.radius=t.radius||i.radius;const l=await m.Tools.CreateScreenshotUsingRenderTargetAsync(this.engine,i,e,"image/png",2,!0);return i.alpha=r,i.beta=n,i.radius=s,l}orbitEnabled(){const e=this.configuration;if(!e)return!0;const t=e.camera.limits.min.alpha,i=e.camera.limits.max.alpha,a=e.camera.limits.min.beta,r=e.camera.limits.max.beta;if(void 0===t||void 0===i||void 0===a||void 0===r)return!0;const n=[a,r],s=[t,i].every((e=>e===t)),o=n.every((e=>e===a));return!s&&!o}fireResizeEvent(){this.getNumViewports()>0&&this.engine.resize()}onMaterialSelected(i){this.scene.meshes.forEach((a=>{"targetcube_t"!==a.name&&"backgroundShell"!==a.name&&(a.actionManager||(a.actionManager=new(0,e.ActionManager)(this.scene)),a.actionManager.registerAction(new(0,t.ExecuteCodeAction)(e.ActionManager.OnPickDownTrigger,(e=>{if(e.meshUnderPointer){const t=e.meshUnderPointer.material;t&&i({id:t.id,name:t.name})}}))))}))}listMaterials(){const e=this.scene?.materials;return e?e.map((e=>({id:e.id,name:e.name}))):[]}setHighlights(e,t){0===e.length&&(this.highlightLayer?.dispose(),this.highlightLayer=void 0),this.highlightLayer||(this.highlightLayer=new(0,n.HighlightLayer)("highlights",this.scene,{isStroke:!0,blurVerticalSize:.85,blurHorizontalSize:.85}),this.highlightLayer.innerGlow=!0,this.highlightLayer.outerGlow=!1),this.highlightLayer.removeAllMeshes();const i=t?new(0,l.Color3)(t[0],t[1],t[2]).toLinearSpace():void 0;e.forEach((e=>{const t=this.scene.materials.find((t=>t.name===e.name&&t.id===e.id));t&&t.getBindedMeshes().forEach((e=>this.highlightLayer?.addMesh(e,i||l.Color3.FromHexString("#fcba03"))))}))}reattachControls(e,t=2){this.scene.detachControl(),this.engine.inputElement=e;const i=this.scene.activeCamera;if(i){i.attachControl(!0,!1,t);i.inputs.attached.pointers.multiTouchPanning=!1}this.scene.attachControl(!0,!0,!0)}getLoadListenerEvent(){const e=Array.from(this.loadProgress.values()).filter((e=>e<100)),t=e.reduce(((e,t)=>e+t),0)/e.length||0;return{loadValue:0===e.length?100:t,sceneInitialized:100===this.getSceneInitializationProgress()}}notifyLoadHandlers(){this.loadObservable.notifyObservers(this.getLoadListenerEvent())}}async function re(e,t,i){(await F(e,t,i)).addAllToScene()}A(module.exports,R),A(module.exports,I),A(module.exports,N);
|
|
1
|
+
var e=require("@babylonjs/core/Actions/actionManager"),t=require("@babylonjs/core/Actions/directActions"),i=require("@babylonjs/core/Engines/engine"),a=require("@babylonjs/core/Engines/nullEngine"),r=require("@babylonjs/core/Layers/glowLayer"),n=require("@babylonjs/core/Layers/highlightLayer"),s=require("@babylonjs/core/Loading/sceneLoader"),o=require("@babylonjs/core/Materials/PBR/pbrMaterial"),c=require("@babylonjs/core/Materials/Textures/cubeTexture"),l=require("@babylonjs/core/Materials/Textures/dynamicTexture"),h=require("@babylonjs/core/Maths/math.color"),u=require("@babylonjs/core/Maths/math.vector"),d=require("@babylonjs/core/Meshes/Compression/dracoCompression"),m=require("@babylonjs/core/Misc/observable"),g=require("@babylonjs/core/Misc/tools"),p=require("@babylonjs/core/scene"),b=require("@babylonjs/loaders/glTF");require("@babylonjs/core/Engines/Extensions/engine.views"),require("@babylonjs/core/Materials/Textures/Loaders/ddsTextureLoader"),require("@babylonjs/core/Materials/Textures/Loaders/envTextureLoader"),require("@babylonjs/core/Materials/Textures/Loaders/ktxTextureLoader"),require("@babylonjs/core/Animations/animatable"),require("@babylonjs/core/Misc/screenshotTools"),require("@babylonjs/core/Rendering/boundingBoxRenderer"),require("@babylonjs/loaders/glTF/2.0/Extensions");var f=require("@babylonjs/core/Materials/Textures/texture"),y=require("@babylonjs/core/Materials/Textures/mirrorTexture"),v=require("@babylonjs/core/Maths/math"),w=require("@babylonjs/core/Probes/reflectionProbe"),x=require("@babylonjs/core/Animations/animation"),M=require("@babylonjs/core/Animations/easing"),T=require("@babylonjs/core/Cameras/arcRotateCamera"),C=require("@babylonjs/core/Misc/assetsManager");function A(e,t){return Object.keys(t).forEach((function(i){"default"===i||"__esModule"===i||e.hasOwnProperty(i)||Object.defineProperty(e,i,{enumerable:!0,get:function(){return t[i]}})})),e}function L(e,t,i,a){Object.defineProperty(e,t,{get:i,set:a,enumerable:!0,configurable:!0})}var R={};let P;var O;let E;var S;L(R,"ProductCameraRig",(()=>P)),L(R,"MaterialEffectMode",(()=>E)),(O=P||(P={}))[O.Orbit=0]="Orbit",O[O.Pan=1]="Pan",(S=E||(E={})).None="None",S.RemoveWhenSelected="RemoveWhenSelected",S.ApplyWhenSelected="ApplyWhenSelected";var I={};L(I,"SpiffCommerce3DPreviewService",(()=>ae)),L(I,"createBaseModel",(()=>re));const B=new Map;async function F(e,t,i){return new Promise(((a,r)=>{const n=B.get(e);if(n&&n.scene.uid===t.uid)return a(n);s.SceneLoader.LoadAssetContainerAsync(e,void 0,t,i).then((t=>{B.set(e,t),a(t)})).catch(r)}))}function q(e,t,i){return t in e?Object.defineProperty(e,t,{value:i,enumerable:!0,configurable:!0,writable:!0}):e[t]=i,e}class j{constructor(e){q(this,"customOptions",void 0),q(this,"getSceneClearColor",(()=>{const e=this.customOptions?.backgroundImage?0:1;if(this.customOptions&&this.customOptions.backgroundImage)return new(0,h.Color4)(0,0,0,e).toLinearSpace();if(this.customOptions&&this.customOptions.backgroundColor){const t=h.Color3.FromHexString(this.customOptions.backgroundColor);return new(0,h.Color4)(t.r,t.g,t.b,e).toLinearSpace()}return new(0,h.Color4)(.98,.98,.98,e).toLinearSpace()})),q(this,"highlightColorFromConfig",(()=>this.customOptions&&this.customOptions.highlightColor?this.hexToColor4(this.customOptions.highlightColor):new(0,h.Color4)(.98,.98,.98,1).toLinearSpace())),q(this,"hexToColor4",((e,t=1)=>{const i=h.Color3.FromHexString(e);return new(0,h.Color4)(i.r,i.g,i.b,t).toLinearSpace()})),this.customOptions=e}get options(){return this.customOptions}get scene(){return{clearColor:this.getSceneClearColor(),transparentBackground:this.customOptions?.backgroundImage,environmentFile:this.customOptions?.environmentFile??"assets/model-viewer/default.env"}}get camera(){return{autoOrientation:this.customOptions?.disableAutomaticOrientation??!0,autoRotation:{enabled:this.customOptions?.autoRotation??!1,idleTimeMs:this.customOptions?.idleTimeBeforeRotation??5e3},limits:{min:{alpha:this.customOptions?.lowerAlphaLimitDeg?this.customOptions?.lowerAlphaLimitDeg*Math.PI/180:void 0,beta:this.customOptions?.lowerBetaLimitDeg?this.customOptions?.lowerBetaLimitDeg*Math.PI/180:void 0,radius:this.customOptions?.minZoomOverride},max:{alpha:this.customOptions?.upperAlphaLimitDeg?this.customOptions?.upperAlphaLimitDeg*Math.PI/180:void 0,beta:this.customOptions?.upperBetaLimitDeg?this.customOptions?.upperBetaLimitDeg*Math.PI/180:void 0,radius:this.customOptions?.maxZoomOverride}}}}get highlights(){return{enabled:this.customOptions?.highlightOnMaterialHover??!1,color:this.highlightColorFromConfig()}}}var N={};L(N,"RenderingConfiguration",(()=>k)),L(N,"REFLECTION_PROBE_RESOLUTION",(()=>D));class k{static getDynamicTextureResolution(){return this.getIsMobile()||!k.offscreenRenderingSupported()?{width:1024,height:1024}:{width:2048,height:2048}}static shouldMipMap(){return!0}static offscreenRenderingSupported(){return!navigator.userAgent.includes("SamsungBrowser")&&(!!window.Worker&&!!window.OffscreenCanvas)}static getMirrorTextureResolution(){return this.getIsMobile()?512:1024}static getIsMobile(){return window.innerWidth<=480}}const D=128;function V(e,t,a){t.forEach((t=>{const r=t.getID(),n=t.getName(),s=k.getDynamicTextureResolution();e.materials.filter((e=>e.name===n)).forEach((o=>{const c=a.get(r);if(c)_(o,c),c.update(false);else{const c=function(e,t,a,r){const n=new(0,l.DynamicTexture)(e,{width:a,height:r},t,k.shouldMipMap(),f.Texture.TRILINEAR_SAMPLINGMODE,i.Engine.TEXTUREFORMAT_RGBA),s=n.getContext();s&&(s.fillStyle="#f5f5f5",s.fillRect(0,0,a,r),n.update());return n}(n,e,s.width,s.height);a.set(r,c),t.setStaticContext(c.getContext()),_(o,c),c.onLoadObservable.addOnce((()=>{c.update(false)}))}}))}))}function _(e,t){if(e instanceof o.PBRMaterial){const i=e,a=i.albedoTexture;a?(t.wrapU=a.wrapU,t.wrapV=a.wrapV):(t.wrapU=1,t.wrapV=1),i.albedoTexture=t}else{const i=e,a=i.diffuseTexture;a&&(t.wrapU=a.wrapU,t.wrapV=a.wrapV),i.diffuseTexture=t}}function z(e,t,i){return t in e?Object.defineProperty(e,t,{value:i,enumerable:!0,configurable:!0,writable:!0}):e[t]=i,e}class U{constructor(e){z(this,"name",void 0),z(this,"enabled",void 0),z(this,"loader",void 0),this.name="glbPostProcessor",this.enabled=!0,this.loader=e}onReady(){this.applyReflections(this.loader.babylonScene)}loadNodeAsync(e,t,i){return this.loader.loadNodeAsync(e,t,(function(e){t.extras&&Object.keys(t.extras).forEach((i=>{const a=t.extras[i];e.metadata[i]=a})),i(e)}))}loadMaterialPropertiesAsync(e,t,i){return this.enableMaterialExtrasIfRequired(t,i),this.loader.loadMaterialPropertiesAsync(e,t,i)}dispose(){}enableMaterialExtrasIfRequired(e,t){if(e.extras&&t instanceof o.PBRMaterial){if(e.extras.sheen){const i=t;i.sheen.isEnabled=!0,i.sheen.intensity=e.extras.sheen}if(e.extras.translucency){const i=t;i.subSurface.isTranslucencyEnabled=!0,i.subSurface.translucencyIntensity=e.extras.translucency,e.extras.translucencyR&&e.extras.translucencyG&&e.extras.translucencyB&&(i.subSurface.tintColor=new(0,v.Color3)(e.extras.translucencyR,e.extras.translucencyG,e.extras.translucencyB))}if(e.extras.useDepthPrePass){const e=t;e.needDepthPrePass=!0,e.forceIrradianceInFragment=!0}if(e.extras.useParallax){const i=t;i.useParallax=!0,i.useParallaxOcclusion=!0,i.parallaxScaleBias=e.extras.useParallax}}}applyReflections(e){function t(e){const t=[];return e.transformNodes.forEach((e=>{e.metadata&&e.metadata.reflective&&t.push(...e.getChildMeshes())})),e.meshes.forEach((e=>{e.metadata&&e.metadata.reflective&&!t.includes(e)&&t.push(e)})),t}e.meshes.forEach((i=>{const a=i.metadata;a&&(a.mirrorTexture&&function(i,a=1){const r=i.material;if(!r)return;const n=new(0,y.MirrorTexture)("mirror",k.getMirrorTextureResolution(),e,!0);n.renderList=t(e);const s=i.getVerticesData("normal");if(!s)throw new Error("Mirror attribute specified on: "+i.name+"But no normals exist to generate a mirror from!");i.computeWorldMatrix(!0);const o=i.getWorldMatrix(),c=v.Vector3.TransformNormal(new(0,v.Vector3)(s[0],s[1],s[2]),o).normalize(),l=v.Plane.FromPositionAndNormal(i.position,c.scale(-1));n.mirrorPlane=l,n.level=a,r.reflectionTexture=n}(i,a.mirrorTexture),a.reflectionProbe&&function(i){const a=i.material,r=new(0,w.ReflectionProbe)("probe-"+a.name,D,e);r.attachToMesh(i),r.renderList&&r.renderList.push(...t(e)),a.reflectionTexture=r.cubeTexture}(i))}))}}function G(e,t,i,a){const r=e.animationGroups,n=e=>e.targetedAnimations.map((e=>e.animation.framePerSecond)).reduce(((e,t)=>e+t),0)/e.targetedAnimations.length||0;void 0===a||void 0===i||a!==i?r.forEach((e=>{e.stop();const r=n(e);e.start(t,1,a*r,i*r)})):r.forEach((e=>{e.stop();const i=n(e),r=a*i;e.start(t,1,r,r)}))}function H(e){e.animationGroups.forEach((e=>{e.stop()}))}function W(e,t,i){var a,r,n;e.stopAnimation(t),t.animations=[],Math.abs(t.alpha)>2*Math.PI&&(t.alpha=(a=t.alpha,r=0,n=2*Math.PI,a<r?n-(r-a)%(n-r):r+(a-r)%(n-r)));const s=[],o=i.target,c=o?1:0;if(i.target&&s.push(Z("cameraTargetLerp","target",(new(0,u.Vector3)).copyFrom(t.target),new(0,u.Vector3)(i.target.x,i.target.y,i.target.z),x.Animation.ANIMATIONTYPE_VECTOR3,0)),s.push(Z("cameraAlphaLerp","alpha",t.alpha,K(i.lonDeg),x.Animation.ANIMATIONTYPE_FLOAT,c)),s.push(Z("cameraBetaLerp","beta",t.beta,K(i.latDeg),x.Animation.ANIMATIONTYPE_FLOAT,c)),void 0!==i.radius){const e=Math.max(.01,i.radius);s.push(Z("cameraRadiusLerp","radius",t.radius,e,x.Animation.ANIMATIONTYPE_FLOAT,c))}t.animations.push(...s);const l=t.useAutoRotationBehavior;t.disableAutoRotationBehavior(),e.beginAnimation(t,0,o?120:60,!1,1,(()=>{t.animations=[],l&&t.enableAutoRotationBehavior()}))}function K(e){return e*Math.PI/180}function Z(e,t,i,a,r,n=0,s=x.Animation.ANIMATIONLOOPMODE_CONSTANT){const o=new(0,M.QuadraticEase);o.setEasingMode(M.EasingFunction.EASINGMODE_EASEINOUT);const c=new(0,x.Animation)(e,t,60,r,s),l=[];return n>0&&l.push({frame:0,value:i}),l.push({frame:60*n,value:i}),l.push({frame:60*(n+1),value:a}),c.setKeys(l),c.setEasingFunction(o),c}class Y extends T.ArcRotateCamera{constructor(e,t,i,a,r,n,s,o){var c,l,h;super(e,t,i,a,r,n,o),c=this,l="lastFocus",h=new(0,u.Vector3)(0,0,0),l in c?Object.defineProperty(c,l,{value:h,enumerable:!0,configurable:!0,writable:!0}):c[l]=h,this.minZ=.01,this.maxZ=1e3*this.radius,this.lowerRadiusLimit=.01*this.radius,this.enableFramingBehavior(),this.upperRadiusLimit=1.5*this.radius,this.wheelPrecision=100/this.radius,this.pinchPrecision=300/this.radius,this.wheelDeltaPercentage=.01,this.pinchDeltaPercentage=.005,this.useNaturalPinchZoom=!0,s.camera.autoOrientation&&(this.alpha+=Math.PI),s&&(s.camera.limits.min.beta&&(this.lowerBetaLimit=s.camera.limits.min.beta),s.camera.limits.max.beta&&(this.upperBetaLimit=s.camera.limits.max.beta),s.camera.limits.min.alpha&&(this.lowerAlphaLimit=s.camera.limits.min.alpha),s.camera.limits.max.alpha&&(this.upperAlphaLimit=s.camera.limits.max.alpha),s.camera.limits.min.radius&&(this.lowerRadiusLimit=s.camera.limits.min.radius),s.camera.limits.max.radius&&(this.upperRadiusLimit=s.camera.limits.max.radius),s.camera.autoRotation.enabled&&this.enableAutoRotationBehavior(s.camera.autoRotation.idleTimeMs))}getFramingBehavior(){return this.getBehaviorByName("Framing")}getAutoRotationBehavior(){const e=this.getBehaviorByName("AutoRotation");return e||void 0}enableFramingBehavior(){this.useFramingBehavior=!0;const e=this.getFramingBehavior();e.attach(this),e.framingTime=0,e.elevationReturnTime=-1,e.zoomStopsAnimation=!1,this.lowerRadiusLimit=null;const t=Q(this._scene);return e.zoomOnBoundingInfo(t.min,t.max),this.wheelPrecision=100/this.radius,null===this.lowerRadiusLimit&&(this.lowerRadiusLimit=.1),this.lastFocus.copyFrom(this.target),e}rerunFramingBehavior(e){const t=this.getFramingBehavior();t.framingTime=800;const i=Q(this._scene);t.zoomOnBoundingInfo(i.min,i.max,void 0,(()=>{e()})),t.framingTime=0}enableAutoRotationBehavior(e=5e3){this.useAutoRotationBehavior=!0;const t=this.getAutoRotationBehavior();t&&(t.idleRotationWaitTime=e)}disableAutoRotationBehavior(){this.useAutoRotationBehavior=!1}static create(e,t){e.activeCamera&&(e.activeCamera.dispose(),e.activeCamera=null);const i=Q(e),a=i.max.subtract(i.min),r=i.min.add(a.scale(.5)),n=new Y("ProductCamera",-Math.PI/2,Math.PI/2,1.5*a.length(),r,e,t);return n.panningInertia=0,n.panningOriginTarget.copyFrom(r),n.onAfterCheckInputsObservable.add((()=>{n.panningSensibility=1e3/a.length()})),e.activeCamera=n,n}}function Q(e){const t=e.meshes.filter((e=>e.name.toLowerCase().endsWith("_t")||e.name.toLowerCase().includes("_t_")));return e.getWorldExtends((e=>e.isVisible&&e.isEnabled()&&(0===t.length||t.includes(e))))}const X={albedoTexture:"albedoMapKey",bumpTexture:"normalMapKey",ambientTexture:"ambientMapKey",emissiveTexture:"emissionMapKey",opacityTexture:"alphaMapKey",metallicTexture:"metallicMapKey",refractionTexture:"refractionMapKey"};function $(e,t,i,a){["albedoTexture","bumpTexture","ambientTexture","emissiveTexture","opacityTexture","metallicTexture","refractionTexture"].forEach((r=>{!function(e,t,i,a,r){const n=X[e];if(!n)throw new Error("Unexpected texture name encountered.");const s=t[n];s?a.addTextureTask(e,s,!1,!1):r&&i[e]&&(i[e]&&i[e].dispose(),i[e]=null,function(e,t){"opacityTexture"===e&&(t.useAlphaFromAlbedoTexture=!0);"metallicTexture"===e&&(t.useRoughnessFromMetallicTextureAlpha=!1,t.useRoughnessFromMetallicTextureGreen=!1,t.useMetallnessFromMetallicTextureBlue=!1);"refractionTexture"===e&&(t.subSurface.isRefractionEnabled=!1,t.subSurface.refractionIntensity=1)}(e,i))}(r,e,t,i,a)})),function(e,t){if(!e.clearCoat)return;e.clearCoat===E.RemoveWhenSelected?(t.clearCoat.isEnabled=!1,t.clearCoat.indexOfRefraction=1.5):e.clearCoat===E.ApplyWhenSelected&&(t.clearCoat.isEnabled=!0,t.clearCoat.indexOfRefraction=e.clearCoatIOR||t.clearCoat.indexOfRefraction)}(e,t)}function J(e,t,i,a){"opacityTexture"===e&&(t.useAlphaFromAlbedoTexture=!1),"metallicTexture"===e&&(t.useRoughnessFromMetallicTextureAlpha=!1,t.useRoughnessFromMetallicTextureGreen=!0,t.useMetallnessFromMetallicTextureBlue=!0),"refractionTexture"===e&&(t.subSurface.isRefractionEnabled=!0,t.subSurface.refractionIntensity=i.refractionIntensity||1),t[e]=a}function ee(e,t,i){return t in e?Object.defineProperty(e,t,{value:i,enumerable:!0,configurable:!0,writable:!0}):e[t]=i,e}class te{constructor(){ee(this,"materialVariantMap",new Map),ee(this,"keysThatRemovedBaseModel",[]),ee(this,"loadedContainerForKey",new Map)}async applyMaterial(e,t,i,a,r){return new Promise((n=>{const s=e.materials.filter((e=>e.name===t)),o=this.materialVariantMap.get(t);if(this.materialVariantMap.set(t,{...o,...i}),0===s.length)return void n(void 0);const c=new(0,C.AssetsManager)(e);c.useDefaultLoadingScreen=!1,s.forEach((e=>$(i,e,c,r))),c.onProgress=(e,t,i)=>{a&&a(e/t*100,100,i.name)},c.onFinish=e=>{e.forEach((e=>{const t=e;a&&a(100,100,e.name),s.forEach((a=>J(e.name,a,i,t.texture)))})),n(void 0)},c.loadAsync()}))}async applyModel(e,t,i,a,r){if(a&&i&&!this.keysThatRemovedBaseModel.includes(t)&&this.keysThatRemovedBaseModel.push(t),!a){const i=this.keysThatRemovedBaseModel.includes(t);return this.loadedContainerForKey.get(t)?.removeAllFromScene(),e&&i&&await re(e.metadata.baseModel,e),Promise.resolve()}const n=await F(a,e,r);if(this.loadedContainerForKey.has(t)&&this.loadedContainerForKey.get(t)?.removeAllFromScene(),i){(await F(e.metadata.baseModel,e)).removeAllFromScene()}n.addAllToScene(),this.loadedContainerForKey.set(t,n);const s=[];this.materialVariantMap.forEach((async(t,i)=>{s.push(this.applyMaterial(e,i,t))})),await Promise.all(s)}}function ie(e,t,i){return t in e?Object.defineProperty(e,t,{value:i,enumerable:!0,configurable:!0,writable:!0}):e[t]=i,e}b.GLTF2.GLTFLoader.RegisterExtension("glbPostProcessor",(function(e){return new U(e)})),s.SceneLoader.OnPluginActivatedObservable.add((e=>{if("gltf"===e.name){const t=e;t.useRangeRequests=!0,t.transparencyAsCoverage=!0}}));class ae{constructor(e,t){ie(this,"engine",void 0),ie(this,"scene",void 0),ie(this,"configuration",void 0),ie(this,"loadProgress",new Map([["initialScene",0]])),ie(this,"materialReadyToLoadCallbacks",new Map),ie(this,"modelReadyToLoadCallbacks",new Map),ie(this,"queuedModelAnimation",void 0),ie(this,"queuedCameraAnimation",void 0),ie(this,"focusLostNotified",!1),ie(this,"loadObservable",new(0,m.Observable)),ie(this,"focusLostObservable",new(0,m.Observable)),ie(this,"dynamicTextures",new Map),ie(this,"highlightLayer",void 0),ie(this,"variantManager",new te),this.configuration=new j(t);d.DracoCompression.Configuration={decoder:{wasmUrl:"https://www.gstatic.com/draco/versioned/decoders/1.5.3/draco_wasm_wrapper_gltf.js",wasmBinaryUrl:"https://www.gstatic.com/draco/versioned/decoders/1.5.3/draco_decoder_gltf.wasm",fallbackUrl:"https://www.gstatic.com/draco/versioned/decoders/1.5.3/draco_decoder_gltf.js"}},e&&(e.getContext("webgl2")||e.getContext("webgl"));const r=console.log;console.log=()=>{};const n=e?new(0,i.Engine)(e,!0,{premultipliedAlpha:!1,preserveDrawingBuffer:!!t?.backgroundImage,audioEngine:!1,stencil:this.configuration.highlights.enabled,forceSRGBBufferSupportState:!0}):new(0,a.NullEngine);console.log=r,n.hideLoadingUI(),window.addEventListener("resize",this.fireResizeEvent.bind(this)),this.engine=n,this.scene=new(0,p.Scene)(n)}registerFocusLostListener(e){this.focusLostObservable.add(e)}unregisterFocusLostListener(e){this.focusLostObservable.removeCallback(e)}registerLoadProgressListener(e){this.loadObservable.add(e),e(this.getLoadListenerEvent())}unregisterLoadProgressListener(e){this.loadObservable.removeCallback(e)}registerView(e){const t=e.height,i=e.width;this.engine.registerView(e),e.setAttribute("height",t.toString()),e.setAttribute("width",i.toString()),this.orbitEnabled()||this.setCameraState(P.Pan)}getNumViewports(){return this.engine.views?.length||0}unregisterView(e){this.engine.unRegisterView(e),this.engine.inputElement=null}shutdown(){this.engine&&this.engine.dispose(),window.removeEventListener("resize",this.fireResizeEvent)}getSceneInitializationProgress(){return this.loadProgress.get("initialScene")}async initialize(i,a){if(this.scene.clearColor=this.configuration.scene.clearColor,this.scene.metadata={baseModel:i},this.scene.environmentTexture=c.CubeTexture.CreateFromPrefilteredData(this.configuration.scene.environmentFile,this.scene),i){const e=99;await re(i,this.scene,(t=>{this.loadProgress.set("initialScene",t.loaded*e/t.total),this.notifyLoadHandlers()}))}this.configuration.highlights.enabled&&this.scene.meshes.forEach((i=>{"targetcube_t"!==i.name&&"backgroundShell"!==i.name&&(i.isPickable=!0,i.actionManager||(i.actionManager=new(0,e.ActionManager)(this.scene)),i.actionManager.registerAction(new(0,t.ExecuteCodeAction)(e.ActionManager.OnPointerOutTrigger,(()=>{this.setHighlights([])}))),i.actionManager.registerAction(new(0,t.ExecuteCodeAction)(e.ActionManager.OnPointerOverTrigger,(e=>{if(e.meshUnderPointer){const t=e.meshUnderPointer.material;t&&this.setHighlights([t])}}))))}));const n=Y.create(this.scene,this.configuration),s=a?.getAll()||new Map;V(this.scene,s,this.dynamicTextures);if(this.scene.materials.some((e=>"emissiveTexture"in e&&null!==e.emissiveTexture))){new(0,r.GlowLayer)("GlowLayer",this.scene).intensity=.5}H(this.scene),this.engine.runRenderLoop((()=>{if(!this.engine.views)return;const e=this.engine.views[0],t=this.engine.inputElement;(!t||e&&t&&t.id!==e.target.id)&&this.reattachControls(e.target),s.forEach(((e,t)=>{const i=this.dynamicTextures.get(t);i&&e.getStaticContextDirty()&&i.isReady()&&(i.update(!1),e.setStaticContextDirty(!1))})),this.configuration.scene.transparentBackground&&this.engine.views.forEach((e=>{const t=this.engine.getRenderingCanvas();e.target.getContext("2d").clearRect(0,0,t.width,t.height)})),this.scene.render(),100!==this.getSceneInitializationProgress()&&(this.loadProgress.set("initialScene",100),this.materialReadyToLoadCallbacks.forEach(((e,t)=>{e.forEach(((e,i)=>{this.applyMaterialVariant(t,i,e)}))})),this.materialReadyToLoadCallbacks.clear(),this.modelReadyToLoadCallbacks.forEach((e=>{e(this.scene)})),this.modelReadyToLoadCallbacks.clear(),this.queuedModelAnimation&&(this.executeModelAnimation(this.queuedModelAnimation),this.queuedModelAnimation=void 0),this.queuedCameraAnimation&&(this.executeCameraAnimation(this.queuedCameraAnimation),this.queuedCameraAnimation=void 0),this.notifyLoadHandlers()),n.target.equalsWithEpsilon(n.lastFocus,.1)||this.focusLostNotified||(this.focusLostObservable.notifyObservers(void 0),this.focusLostNotified=!0)}))}executeModelAnimation(e){100===this.getSceneInitializationProgress()?G(this.scene,e.loop,e.to,e.from):this.queuedModelAnimation=e}executeCameraAnimation(e){100===this.getSceneInitializationProgress()&&this.scene.activeCamera?W(this.scene,this.scene.activeCamera,e):this.queuedCameraAnimation=e}getCameraPose(){if(this.scene){const e=this.scene.activeCamera;if(e)return{lonDeg:Math.round(180*e.alpha/Math.PI),latDeg:Math.round(180*e.beta/Math.PI),radius:Math.round(1e4*(e.radius+Number.EPSILON))/1e4,target:{x:e.target.x,y:e.target.y,z:e.target.z}}}}setCameraPose(e){if(this.scene){const t=this.scene.activeCamera;t&&(t.target=new(0,u.Vector3)(e.target.x,e.target.y,e.target.z),t.radius=e.radius,t.alpha=e.latDeg,t.beta=e.lonDeg)}}async applyMaterialVariant(e,t,i,a){if(100===this.getSceneInitializationProgress())await this.variantManager.applyMaterial(this.scene,e,i,((e,t,i)=>{this.loadProgress.set(`key_${i}`,e/t*100),this.notifyLoadHandlers()}),a);else if(this.materialReadyToLoadCallbacks.has(e)){this.materialReadyToLoadCallbacks.get(e)?.set(t,i)}else{this.materialReadyToLoadCallbacks.set(e,new Map);this.materialReadyToLoadCallbacks.get(e)?.set(t,i)}}async applyModelVariant(e,t,i){100===this.getSceneInitializationProgress()?(await this.variantManager.applyModel(this.scene,e,i||!1,t.model,(t=>{this.loadProgress.set(e,100*t.loaded/t.total),this.notifyLoadHandlers()})),this.scene.materials.forEach((e=>{if(e&&0===e.getBindedMeshes().length){const t=e instanceof o.PBRMaterial&&!(e.albedoTexture instanceof l.DynamicTexture);e.dispose(!1,t)}})),V(this.scene,t.contextService.getAll(),this.dynamicTextures),H(this.scene)):this.modelReadyToLoadCallbacks.set(e,(()=>{this.applyModelVariant(e,t,i)}))}setCameraState(e){if(!this.engine?.views||!this.engine?.views[0])throw new Error("No views attached, camera state requires a view to attach controls onto.");e===P.Orbit?this.reattachControls(this.engine.views[0].target,2):this.reattachControls(this.engine.views[0].target,0)}animateToLastCameraFocus(){return new Promise((e=>{const t=this.scene.activeCamera,i=this.configuration;t.rerunFramingBehavior((()=>{this.focusLostNotified=!1,i.camera.limits.min.radius&&(t.lowerRadiusLimit=i.camera.limits.min.radius),i.camera.limits.max.radius&&(t.upperRadiusLimit=i.camera.limits.max.radius),e()}))}))}setAutoRotation(e){const t=this.scene.activeCamera;this.configuration.camera.autoRotation.enabled&&t&&(e?t.enableAutoRotationBehavior(this.configuration.camera.autoRotation.idleTimeMs):t.disableAutoRotationBehavior())}getCurrentConfiguration(){return this.configuration.options}async renderSceneScreenshot(e,t){const i=this.scene.activeCamera;if(!i)throw new Error("Missing product camera, cannot render screenshot!");var a;(a=i).getScene().stopAnimation(a),a.animations=[];const r=i.alpha,n=i.beta,s=i.radius,o=t.latDeg*Math.PI/180,c=t.lonDeg*Math.PI/180;i.alpha=c,i.beta=o,i.radius=t.radius||i.radius;const l=await g.Tools.CreateScreenshotUsingRenderTargetAsync(this.engine,i,e,"image/png",2,!0);return i.alpha=r,i.beta=n,i.radius=s,l}orbitEnabled(){const e=this.configuration;if(!e)return!0;const t=e.camera.limits.min.alpha,i=e.camera.limits.max.alpha,a=e.camera.limits.min.beta,r=e.camera.limits.max.beta;if(void 0===t||void 0===i||void 0===a||void 0===r)return!0;const n=[a,r],s=[t,i].every((e=>e===t)),o=n.every((e=>e===a));return!s&&!o}fireResizeEvent(){this.getNumViewports()>0&&this.engine.resize()}onMaterialSelected(i){this.scene.meshes.forEach((a=>{"targetcube_t"!==a.name&&"backgroundShell"!==a.name&&(a.actionManager||(a.actionManager=new(0,e.ActionManager)(this.scene)),a.actionManager.registerAction(new(0,t.ExecuteCodeAction)(e.ActionManager.OnPickDownTrigger,(e=>{if(e.meshUnderPointer){const t=e.meshUnderPointer.material;t&&i({id:t.id,name:t.name})}}))))}))}listMaterials(){const e=this.scene?.materials;return e?e.map((e=>({id:e.id,name:e.name}))):[]}setHighlights(e,t){0===e.length&&(this.highlightLayer?.dispose(),this.highlightLayer=void 0),this.highlightLayer||(this.highlightLayer=new(0,n.HighlightLayer)("highlights",this.scene,{isStroke:!0,blurVerticalSize:.85,blurHorizontalSize:.85}),this.highlightLayer.innerGlow=!0,this.highlightLayer.outerGlow=!1),this.highlightLayer.removeAllMeshes();const i=t?new(0,h.Color3)(t[0],t[1],t[2]).toLinearSpace():void 0;e.forEach((e=>{const t=this.scene.materials.find((t=>t.name===e.name&&t.id===e.id));t&&t.getBindedMeshes().forEach((e=>this.highlightLayer?.addMesh(e,i||h.Color3.FromHexString("#fcba03"))))}))}reattachControls(e,t=2){this.scene.detachControl(),this.engine.inputElement=e;const i=this.scene.activeCamera;if(i){i.attachControl(!0,!1,t);i.inputs.attached.pointers.multiTouchPanning=!1}this.scene.attachControl(!0,!0,!0)}getLoadListenerEvent(){const e=Array.from(this.loadProgress.values()).filter((e=>e<100)),t=e.reduce(((e,t)=>e+t),0)/e.length||0;return{loadValue:0===e.length?100:t,sceneInitialized:100===this.getSceneInitializationProgress()}}notifyLoadHandlers(){this.loadObservable.notifyObservers(this.getLoadListenerEvent())}}async function re(e,t,i){(await F(e,t,i)).addAllToScene()}A(module.exports,R),A(module.exports,I),A(module.exports,N);
|
|
2
2
|
//# sourceMappingURL=main.js.map
|
package/dist/types.d.ts
CHANGED
|
@@ -339,6 +339,10 @@ export type PreviewOptions = {
|
|
|
339
339
|
* The color expected to be seen in the background of the product. Expects a hexadecimal value.
|
|
340
340
|
*/
|
|
341
341
|
readonly backgroundColor?: string;
|
|
342
|
+
/**
|
|
343
|
+
* When true the background will be made transparent. When false, the clear color will be shown.
|
|
344
|
+
*/
|
|
345
|
+
readonly transparency?: boolean;
|
|
342
346
|
/**
|
|
343
347
|
* Image to be used as a background when running withh transparency, the image
|
|
344
348
|
* will be scaled and centered to fill the preview based on aspect ratio.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@spiffcommerce/preview",
|
|
3
|
-
"version": "2.1.
|
|
3
|
+
"version": "2.1.8",
|
|
4
4
|
"description": "An internal implementation of the ThreeDPreviewService interface used by Spiff Commerce",
|
|
5
5
|
"source": "src/index.ts",
|
|
6
6
|
"main": "dist/main.js",
|
|
@@ -14,7 +14,9 @@
|
|
|
14
14
|
"prepare": "yarn build"
|
|
15
15
|
},
|
|
16
16
|
"targets": {
|
|
17
|
-
"main":
|
|
17
|
+
"main": {
|
|
18
|
+
"optimize": true
|
|
19
|
+
},
|
|
18
20
|
"module": {
|
|
19
21
|
"optimize": true
|
|
20
22
|
},
|