UNPKG

@animech-public/playcanvas

Version:
2 lines (1 loc) 5.22 kB
import{CULLFACE_BACK as t,BLENDMODE_ONE as e,BLENDEQUATION_REVERSE_SUBTRACT as s,BLENDMODE_ZERO as r,BLENDEQUATION_ADD as a,BLENDMODE_SRC_ALPHA as h,BLENDMODE_ONE_MINUS_SRC_ALPHA as n,BLENDMODE_DST_COLOR as i,BLENDMODE_SRC_COLOR as l,BLENDMODE_ONE_MINUS_DST_COLOR as d,BLENDEQUATION_MIN as p,BLENDEQUATION_MAX as o}from"../../platform/graphics/constants.js";import{BlendState as c}from"../../platform/graphics/blend-state.js";import{DepthState as u}from"../../platform/graphics/depth-state.js";import{ShaderProcessorOptions as m}from"../../platform/graphics/shader-processor-options.js";import{BLEND_NONE as _,BLEND_NORMAL as S,BLEND_SUBTRACTIVE as g,BLEND_PREMULTIPLIED as b,BLEND_ADDITIVE as f,BLEND_ADDITIVEALPHA as y,BLEND_MULTIPLICATIVE2X as I,BLEND_SCREEN as W,BLEND_MULTIPLICATIVE as B,BLEND_MIN as v,BLEND_MAX as T}from"../constants.js";import{processShader as F}from"../shader-lib/utils.js";import{getDefaultMaterial as j}from"./default-material.js";const w=[];w[g]={src:e,dst:e,op:s},w[_]={src:e,dst:r,op:a},w[S]={src:h,dst:n,op:a},w[b]={src:e,dst:n,op:a},w[f]={src:e,dst:e,op:a},w[y]={src:h,dst:e,op:a},w[I]={src:i,dst:l,op:a},w[W]={src:d,dst:e,op:a},w[B]={src:i,dst:r,op:a},w[v]={src:e,dst:e,op:p},w[T]={src:e,dst:e,op:o};let P=0;class k{constructor(){this._shader=null,this.meshInstances=[],this.name="Untitled",this.userId="",this.id=P++,this.variants=new Map,this.parameters={},this.alphaTest=0,this.alphaToCoverage=!1,this._blendState=new c,this._depthState=new u,this.cull=t,this.stencilFront=null,this.stencilBack=null,this._shaderVersion=0,this._scene=null,this.dirty=!0}set depthBias(t){this._depthState.depthBias=t}get depthBias(){return this._depthState.depthBias}set slopeDepthBias(t){this._depthState.depthBiasSlope=t}get slopeDepthBias(){return this._depthState.depthBiasSlope}set redWrite(t){this._blendState.redWrite=t}get redWrite(){return this._blendState.redWrite}set greenWrite(t){this._blendState.greenWrite=t}get greenWrite(){return this._blendState.greenWrite}set blueWrite(t){this._blendState.blueWrite=t}get blueWrite(){return this._blendState.blueWrite}set alphaWrite(t){this._blendState.alphaWrite=t}get alphaWrite(){return this._blendState.alphaWrite}set shader(t){this._shader=t}get shader(){return this._shader}get transparent(){return this._blendState.blend}_updateTransparency(){const t=this.transparent,e=this.meshInstances;for(let s=0;s<e.length;s++)e[s].transparent=t}set blendState(t){this._blendState.copy(t),this._updateTransparency()}get blendState(){return this._blendState}set blendType(t){const e=w[t];this._blendState.setColorBlend(e.op,e.src,e.dst),this._blendState.setAlphaBlend(e.op,e.src,e.dst);const s=t!==_;this._blendState.blend!==s&&(this._blendState.blend=s,this._updateTransparency()),this._updateMeshInstanceKeys()}get blendType(){if(!this.transparent)return _;const{colorOp:t,colorSrcFactor:e,colorDstFactor:s,alphaOp:r,alphaSrcFactor:a,alphaDstFactor:h}=this._blendState;for(let n=0;n<w.length;n++){const i=w[n];if(i.src===e&&i.dst===s&&i.op===t&&i.src===a&&i.dst===h&&i.op===r)return n}return S}set depthState(t){this._depthState.copy(t)}get depthState(){return this._depthState}set depthTest(t){this._depthState.test=t}get depthTest(){return this._depthState.test}set depthFunc(t){this._depthState.func=t}get depthFunc(){return this._depthState.func}set depthWrite(t){this._depthState.write=t}get depthWrite(){return this._depthState.write}copy(t){var e;return this.name=t.name,this._shader=t._shader,this.alphaTest=t.alphaTest,this.alphaToCoverage=t.alphaToCoverage,this._blendState.copy(t._blendState),this._depthState.copy(t._depthState),this.cull=t.cull,this.stencilFront=null==(e=t.stencilFront)?void 0:e.clone(),t.stencilBack&&(this.stencilBack=t.stencilFront===t.stencilBack?this.stencilFront:t.stencilBack.clone()),this}clone(){return(new this.constructor).copy(this)}_updateMeshInstanceKeys(){const t=this.meshInstances;for(let e=0;e<t.length;e++)t[e].updateKey()}updateUniforms(t,e){}getShaderVariant(t,e,s,r,a,h,n,i,l){const d=new m(n,i,l);return F(this._shader,d)}update(){this.dirty=!0,this._shader&&(this._shader.failed=!1)}clearParameters(){this.parameters={}}getParameters(){return this.parameters}clearVariants(){this.variants.clear();const t=this.meshInstances,e=t.length;for(let s=0;s<e;s++)t[s].clearShaders()}getParameter(t){return this.parameters[t]}setParameter(t,e){if(void 0===e&&"object"==typeof t){const s=t;if(s.length){for(let t=0;t<s.length;t++)this.setParameter(s[t]);return}t=s.name,e=s.value}const s=this.parameters[t];s?s.data=e:this.parameters[t]={scopeId:null,data:e}}deleteParameter(t){this.parameters[t]&&delete this.parameters[t]}setParameters(t,e){const s=this.parameters;void 0===e&&(e=s);for(const r in e){const e=s[r];e&&(e.scopeId||(e.scopeId=t.scope.resolve(r)),e.scopeId.setValue(e.data))}}destroy(){this.variants.clear(),this._shader=null;for(let t=0;t<this.meshInstances.length;t++){const e=this.meshInstances[t];if(e.clearShaders(),e._material=null,e.mesh){const t=j(e.mesh.device);this!==t&&(e.material=t)}}this.meshInstances.length=0}addMeshInstanceRef(t){this.meshInstances.push(t)}removeMeshInstanceRef(t){const e=this.meshInstances,s=e.indexOf(t);-1!==s&&e.splice(s,1)}}export{k as Material};