/*
All material copyright Esri, All Rights Reserved, unless otherwise specified.
See https://js.arcgis.com/5.0/esri/copyright.txt for details.
*/
import{toConst as r}from"../../../../../core/compilerUtils.js";import{BufferObject as e}from"../../../../webgl/BufferObject.js";import"../../../../webgl/FramebufferObject.js";import"../../../../webgl/Program.js";import"../../../../../core/has.js";import"../../../../webgl/Renderbuffer.js";import"../../../../webgl/Texture.js";import{VertexArrayObject as t}from"../../../../webgl/VertexArrayObject.js";import{dataSizeInBytes as s}from"./definitions.js";import{VertexBuffer as i}from"../../../../webgl/VertexBuffer.js";class o{constructor(r,t){this._boundPart=null,this.vertexBuffers=new Map,this.indexBuffers=new Map,this.groups=[];for(const e in t.vertex){const{data:s,layout:o}=t.vertex[e],n=new i(r,o,s);this.vertexBuffers.set(e,n)}for(const s in t.index){const{data:i}=t.index[s],o=e.createIndex(r,35044,i);this.indexBuffers.set(s,o)}for(const e of t.groups)this.groups.push({...e,vertexArrays:new Map});this.parts=t.parts}bind(e,s,i){this._boundPart=i;const{group:o}=this.parts[this._boundPart],n=this.groups[o];if(!n)throw new Error(`Missing group ${o}.`);let f=n.vertexArrays.get(s.stringHash);if(!f){const i=r(new Set(s.locations.keys())),o=n.index?this.indexBuffers.get(n.index):null,a=new Map;for(const[r,e]of this.vertexBuffers){e.layout.filter(r=>i.has(r.name)).length>0&&a.set(r,e)}f=new t(e,a,o),n.vertexArrays.set(s.stringHash,f)}e.bindVAO(f)}draw(r){if(null==this._boundPart)throw new Error("Mesh.bind() has not been called.");const{start:e,count:t}=this.parts[this._boundPart],{group:i}=this.parts[this._boundPart],{primitive:o,index:n}=this.groups[i];if(n){const i=this.indexBuffers.get(n);if(!i)throw new Error(`Missing index buffer "${n}".`);const{indexType:f}=i;if(!f)throw new Error("Buffer type error.");r.drawElements(o,t,f,e*s[f])}else r.drawArrays(o,e,t)}unbind(r){this._boundPart=null,r.bindVAO(null)}destroy(){this.groups.forEach(({vertexArrays:r})=>r.forEach(r=>r.disposeVAOOnly())),this.vertexBuffers.forEach(r=>r.dispose()),this.indexBuffers.forEach(r=>r.dispose())}}export{o as Mesh};
