import{ACESFilmicToneMapping,SRGBColorSpace,VSMShadowMap,WebGLRenderer}from"three";import{WebGPURenderer}from"three/src/Three.WebGPU";import{EffectComposer}from"three/examples/jsm/postprocessing/EffectComposer.js";import{extend,getElement}from"../common/util";import{RenderEvent}from"../common/constant";import{onRender,removeRender}from"../core/executer";import debounce from"lodash/debounce";export function createDefaultRendererOptoins(){return{antialias:!0,alpha:!0,precision:"lowp"}}export default function createRenderer(a,b={},c,d){let e,f,g=a.type||"auto";"gpu"in navigator&&"webgl"!=g?(void 0,e=new WebGPURenderer({forceWebGL:!1,antialias:!0}),e.setPixelRatio(window.devicePixelRatio),g="webgpu"):("webgpu"==g&&void 0,void 0,e=new WebGLRenderer({antialias:!0,alpha:!0,...b}),e.debug={checkShaderErrors:!1,onShaderError:null},e.shadowMap.type=VSMShadowMap,e.toneMapping=ACESFilmicToneMapping,e.toneMappingExposure=1,e.outputColorSpace=SRGBColorSpace,e.setClearColor(4342338,1),f=new 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=debounce(()=>l(),200);if(ResizeObserver){const c=new ResizeObserver(b);return c.observe(a),()=>{c.unobserve(a)}}return window.addEventListener(RenderEvent.Resize,b),()=>{window.removeEventListener(RenderEvent.Resize,b)}},p=a=>{let b=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?onRender(m):e.init().then(()=>{onRender(m)}).catch(function(){}),n=o(j)},q=()=>{e.domElement.remove(),f&&f.dispose(),i.clear(),"dispose"in e&&e.dispose(),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,onResize:a=>(i.add(a),()=>{i.delete(a)})};return extend(r,e)}