"use strict";var _interopRequireDefault=require("@babel/runtime/helpers/interopRequireDefault");exports.__esModule=!0,exports.createDefaultRendererOptoins=createDefaultRendererOptoins,exports.default=createRenderer;var _three=require("three"),_Three=require("three/src/Three.WebGPU"),_EffectComposer=require("three/examples/jsm/postprocessing/EffectComposer.js"),_util=require("../common/util"),_constant=require("../common/constant"),_executer=require("../core/executer"),_debounce=_interopRequireDefault(require("lodash/debounce"));function createDefaultRendererOptoins(){return{antialias:!0,alpha:!0,precision:"lowp"}}function createRenderer(a,b={},c,d){let e,f,g=a.type||"auto";"gpu"in navigator&&"webgl"!=g?(void 0,e=new _Three.WebGPURenderer({forceWebGL:!1,antialias:!0}),e.setPixelRatio(window.devicePixelRatio),g="webgpu"):("webgpu"==g&&void 0,void 0,e=new _three.WebGLRenderer({antialias:!0,alpha:!0,...b}),e.debug={checkShaderErrors:!1,onShaderError:null},e.shadowMap.type=_three.VSMShadowMap,e.toneMapping=_three.ACESFilmicToneMapping,e.toneMappingExposure=1,e.outputColorSpace=_three.SRGBColorSpace,e.setClearColor(4342338,1),f=new _EffectComposer.EffectComposer(e),g="webgl");let h=e.domElement;h.style.left="0px",h.style.top="0px";const i=new Set;let j,k=!1===a.autoRender;const l=(a,b)=>{let c=a||(j?j.offsetWidth:1),d=b||(j?j.offsetHeight:1);e.setSize(c,d),e.setPixelRatio(window.devicePixelRatio),f&&(f.setSize(c,d),f.setPixelRatio(devicePixelRatio),f.passes.forEach(a=>{a.setSize(c,d)})),i.forEach(a=>a(c,d))},m=()=>{k||("webgl"==g?!0===a.effect?e.render(c,d):f&&f.render():e.render(c,d))};let n;const o=a=>{const b=(0,_debounce.default)(()=>l(),200);if(ResizeObserver){const c=new ResizeObserver(b);return c.observe(a),()=>{c.unobserve(a)}}return window.addEventListener(_constant.RenderEvent.Resize,b),()=>{window.removeEventListener(_constant.RenderEvent.Resize,b)}},p=a=>{let b=(0,_util.getElement)(a);if(!b)throw Error("el \u4E0D\u5B58\u5728");j=b,"none"===j.style.display&&void 0,j.appendChild(e.domElement),l(),"webgl"==g?(0,_executer.onRender)(m):e.init().then(()=>{(0,_executer.onRender)(m)}).catch(function(){}),n=o(j)},q=()=>{e.domElement.remove(),f&&f.dispose(),i.clear(),"dispose"in e&&e.dispose(),(0,_executer.removeRender)(m),n&&n()};let r={type:g,composer:f,get isPaused(){return k},amount:p,start:()=>{k=!1},stop:()=>{k=!0},dispose:q,resize:l,render:()=>{"webgl"==g?!1===a.effect?e.render(c,d):f&&f.render():e.render(c,d)},onRender:_executer.onRender,onResize:a=>(i.add(a),()=>{i.delete(a)})};return(0,_util.extend)(r,e)}