@animech-public/playcanvas
Version:
PlayCanvas WebGL game engine
2 lines (1 loc) • 2.16 kB
JavaScript
import{hashCode as t}from"../../core/hash.js";import{math as e}from"../../core/math/math.js";import{StringIds as s}from"../../core/string-ids.js";import{typedArrayTypesByteSize as n,SEMANTIC_TEXCOORD0 as i,SEMANTIC_TEXCOORD1 as o,SEMANTIC_COLOR as a,SEMANTIC_TANGENT as h,SEMANTIC_ATTR12 as r,TYPE_FLOAT32 as c,SEMANTIC_ATTR13 as m,SEMANTIC_ATTR14 as p,SEMANTIC_ATTR15 as l}from"./constants.js";import{DeviceCache as d}from"./device-cache.js";const u=new s,g=[2,4,8,12,16],f=new d;class v{constructor(t,s,r){this.device=t,this._elements=[],this.hasUv0=!1,this.hasUv1=!1,this.hasColor=!1,this.hasTangents=!1,this.verticesByteSize=0,this.vertexCount=r,this.interleaved=void 0===r,this.instancing=!1,this.size=s.reduce(((t,e)=>t+4*Math.ceil(e.components*n[e.type]/4)),0);let c,m=0;for(let t=0,d=s.length;t<d;t++){var p,l;const d=s[t];c=d.components*n[d.type],r&&(m=e.roundUp(m,c));const u=null!=(p=d.asInt)&&p,g=!u&&(null!=(l=d.normalize)&&l),f={name:d.semantic,offset:r?m:d.hasOwnProperty("offset")?d.offset:m,stride:r?c:d.hasOwnProperty("stride")?d.stride:this.size,dataType:d.type,numComponents:d.components,normalize:g,size:c,asInt:u};this._elements.push(f),m+=r?c*r:4*Math.ceil(c/4),d.semantic===i?this.hasUv0=!0:d.semantic===o?this.hasUv1=!0:d.semantic===a?this.hasColor=!0:d.semantic===h&&(this.hasTangents=!0)}r&&(this.verticesByteSize=m),this._evaluateHash()}get elements(){return this._elements}static getDefaultInstancingFormat(t){return f.get(t,(()=>new v(t,[{semantic:r,components:4,type:c},{semantic:m,components:4,type:c},{semantic:p,components:4,type:c},{semantic:l,components:4,type:c}])))}static isElementValid(t,e){const s=e.components*n[e.type];return!(t.isWebGPU&&!g.includes(s))}update(){this._evaluateHash()}_evaluateHash(){const e=[],s=[],n=this._elements.length;for(let t=0;t<n;t++){const{name:n,dataType:i,numComponents:o,normalize:a,offset:h,stride:r,size:c,asInt:m}=this._elements[t],p=n+i+o+a+m;e.push(p);const l=p+h+r+c;s.push(l)}e.sort();const i=e.join();this.batchingHash=t(i),this.shaderProcessingHashString=i,this.renderingHashString=s.join("_"),this.renderingHash=u.get(this.renderingHashString)}}export{v as VertexFormat};