@megavr/ecsy-babylon
Version:
babylon.js ecsy binding and helpers
2 lines (1 loc) • 11.4 kB
JavaScript
!function(e,t){"use strict";class i{}function s(t){return e.Angle.FromDegrees(t).radians()}function r(t){return new e.Vector3(t.x,t.y,t.z)}function n(t){return new e.Vector3(s(t.x),s(t.y),s(t.z))}function o(e,t,i){return e&&t&&i?{x:e,y:t,z:i}:{x:0,y:0,z:0}}class a{constructor(){this.position=o(),this.rotation=o(),this.scale=o(1,1,1)}}class c{constructor(){this.pointerLock=!1}}var h,d,u,l;!function(e){e.Box="Box",e.Plane="Plane",e.Sphere="Sphere",e.Ground="Ground",e.Url="Url"}(h||(h={}));class p{constructor(){this.type=h.Box}}!function(e){e.Point="Point",e.Directional="Directional",e.Spot="Spot",e.Hemispheric="Hemispheric"}(d||(d={}));class m{constructor(){this.type=d.Hemispheric,this.direction=o()}}function g(e,t){e.object[t]=e[t]}function _(e,t){e.object[t]=r(e[t])}function f(e){let t=e.getComponents();for(let i in t){let s=t[i];s.object&&e.hasComponent(a)&&b(e.getMutableComponent(a),s)}}function b(e,t){let i=t.object;i.position&&(i.position=r(e.position)),i.rotation&&(i.rotation=n(e.rotation)),i.scaling&&(i.scaling=r(e.scale))}function C(e){e.object&&e.object.dispose()}function y(e,t,i){for(let s in t){let r=t[s];i.addTextureTask(s,r.url).onSuccess=t=>{let i=t.texture;for(let e in r)"url"!==e&&(i[e]=r[e]);let n=`${s}Texture`,o=e.object;o[n]&&C(o[n]),o[n]=i}}i.load(),i.reset()}function S(t){return e.Color3.FromHexString(t)}function x(t){return e.Color4.FromHexString(t)}class A{constructor(){var e;this.color=e?{diffuse:e}:{diffuse:"#ffffff"}}}!function(e){e.Point="Point",e.Box="Box",e.Sphere="Sphere",e.DirectedSphere="DirectedSphere",e.Hemisphere="Hemisphere",e.Cylinder="Cylinder",e.DirectedCylinder="DirectedCylinder",e.Cone="Cone"}(u||(u={}));class E{constructor(){this.type=u.Point,this.capacity=100,this.emitter=o(),this.direction1=o(),this.direction2=o(10,10,10),this.minEmitBox=o(),this.maxEmitBox=o()}}!function(e){e.Keyboard="Keyboard"}(l||(l={}));class M{constructor(){this.type=l.Keyboard}}function v(e){return e.world}class k extends t.System{constructor(){super(...arguments),this._assetManagers=new Map,this.onSceneSwitched=new e.Observable}get renderingCanvas(){return this._engine.getRenderingCanvas()}get scenes(){return this._engine.scenes}get activeScene(){return this._activeScene}init(){this._render=this._render.bind(this)}execute(){this.queries.scene.added.forEach(t=>{let s=t.getComponent(i);s.object=new e.Scene(this._engine,s.options),1===this._engine.scenes.length&&(this._activeScene=s.object),this._updateScene(t);let r=new e.AssetsManager(s.object);r.useDefaultLoadingScreen=!1,this._assetManagers.set(s.object.uid,r)}),this.queries.scene.changed.forEach(e=>{this._updateScene(e)}),this.queries.scene.removed.forEach(e=>{C(e.getComponent(i))})}_updateScene(e){let t=e.getComponent(i);for(let i in t)switch(i){case"texture":y(t,t.texture,this.getAssetManager(e));break;case"color":this._updateColor(t,t.color);break;default:g(t,i)}}_updateColor(e,t){for(let i in t)switch(i){case"clear":e.object[`${i}Color`]=x(t[i]);break;default:e.object[`${i}Color`]=S(t[i])}}start(t,i,s,r){return this._engine=new e.Engine(t,i,s,r),this._engine.runRenderLoop(this._render),this}switchScene(e){return this._activeScene=e.getComponent(i).object,this.onSceneSwitched.notifyObservers(this._activeScene),this}getAssetManager(e){return e?this._assetManagers.get(e.getComponent(i).object.uid):this._assetManagers.get(this._activeScene.uid)}_render(){v(this).execute(this._engine.getDeltaTime(),performance.now()),v(this).enabled&&this._activeScene.render()}}k.queries={scene:{components:[i],listen:{added:!0,removed:!0,changed:[i]}}};class j extends t.System{execute(){this.queries.transforms.changed.forEach(e=>{f(e)})}}function w(e,t){return v(e).getSystem(t)}function B(e){return w(e,k).renderingCanvas}function P(e){return w(e,k).scenes}function q(e,t){return t?t.getComponent(i).object:w(e,k).activeScene}function L(e,t){return w(e,k).getAssetManager(t)}j.queries={transforms:{components:[a],listen:{changed:[a]}}};class U extends t.System{constructor(){super(...arguments),this._cameras=new Map}init(){w(this,k).onSceneSwitched.add(e=>this._updateControl(e)),this._pointerLock=this._pointerLock.bind(this)}execute(){this.queries.camera.added.forEach(t=>{let i=t.getComponent(c),s=q(this,i.scene);i.object=new e.FreeCamera("",e.Vector3.Zero(),s),f(t),this._cameras.set(s.uid,i),this._updateControl(s)}),this.queries.camera.removed.forEach(e=>{let t=e.getComponent(c),i=q(this,t.scene);this._removeControl(i),this._cameras.has(i.uid)&&this._cameras.delete(i.uid),C(t)})}_updateControl(e){if(e.uid===q(this).uid){P(this).forEach(e=>this._removeControl(e));let t=this._cameras.get(e.uid);t.object.attachControl(B(this)),t.pointerLock?e.onPointerObservable.add(this._pointerLock):document.exitPointerLock()}}_removeControl(e){this._cameras.forEach((t,i)=>i===e.uid&&t.object.detachControl(B(this))),e.onPointerObservable.removeCallback(this._pointerLock)}_pointerLock(e){"pointerdown"===e.event.type&&(document.pointerLockElement||B(this).requestPointerLock())}}U.queries={camera:{components:[c],listen:{added:!0,removed:!0}}};class K extends t.System{execute(){this.queries.mesh.added.forEach(e=>{this._updateMesh(e,e.getComponent(p))}),this.queries.mesh.changed.forEach(e=>{C(e.getComponent(p)),this._updateMesh(e,e.getComponent(p))}),this.queries.mesh.removed.forEach(e=>{C(e.getComponent(p))})}_updateMesh(t,i){switch(i.type){case h.Url:i.url&&e.SceneLoader.IsPluginForExtensionAvailable(this._fileExt(i.url))?this._loadUrlMesh(t,i,i.url):this._unsupportedMesh(t,i);break;default:i.object=e.MeshBuilder[`Create${i.type}`].call(this,i.type,i.options?i.options:{},q(this,i.scene)),this._updateMeshValue(i),f(t)}}_fileExt(e){return e.substring(e.lastIndexOf("."),e.length)}_loadUrlMesh(e,t,i){let s=L(this,t.scene),r=i.lastIndexOf("/")+1,n=s.addMeshTask(t.type,"",i.substring(0,r),i.substring(r,i.length));n.onSuccess=i=>{t.object=i.loadedMeshes[0],this._updateMeshValue(t),f(e)},n.onError=()=>{this._unsupportedMesh(e,t)},s.load(),s.reset()}_updateMeshValue(e){for(let t in e)g(e,t)}_unsupportedMesh(t,i){let s=q(this,i.scene);i.object=e.MeshBuilder.CreatePlane("",{},s);let r=new e.StandardMaterial("",s);r.diffuseTexture=new e.Texture("data:errorData",s,!1,!1,2,null,null,"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAEAAAABAAgMAAADXB5lNAAAABGdBTUEAALGPC/xhBQAAAAFzUkdCAK7OHOkAAAAMUExURSgoKCgoKCgoKO88OVVpm6AAAAACdFJOU/LlgHlY8AAAAP9JREFUOMvN1MFtwzAMBVB3nU7RcQsSKDcI96EPvrPAZ0nZkRUZKXKMb36xPr+UxMvX8nB9LDRdy+fj/ff7gES4bKYUBPYDdLOQcEgDY1cx6AqIjUADQFYvUNohkAFBEbFP+Y1MRAPboU1JOKdUj8rgp1BLWo+jaQutQUdotDuqMS+eRxblzM8F3kHDkIAD8kNxcBbukCuidmADaILcITOheST/g2wDVIYEX+Gckj2q7NkjmyYMTRlU4K/+HJQNEWttGDtIwZZffgcH8rkJgtFBC2h44oYCHeCWoRztUBugQP0E15pCP32JyzT2AiZT0zyL2gvd4T3+YpeXwfy6+ANZO9QlB5svlAAAAABJRU5ErkJggg==",!0),i.object.material=r,f(t)}}K.queries={mesh:{components:[p],listen:{added:!0,removed:!0,changed:[p]}}};class D extends t.System{execute(){this.queries.light.added.forEach(t=>{let i=t.getComponent(m),n=r(i.direction),o=q(this,i.scene);switch(i.type){case d.Point:i.object=new e.PointLight(i.type,e.Vector3.Zero(),o);break;case d.Directional:i.object=new e.DirectionalLight(i.type,n,o);break;case d.Spot:i.object=new e.SpotLight(i.type,e.Vector3.Zero(),n,s(i.angle),i.exponent,o);break;default:i.object=new e.HemisphericLight(d.Hemispheric,n,o)}this._updateLight(i),f(t)}),this.queries.light.changed.forEach(e=>{this._updateLight(e.getComponent(m))}),this.queries.light.removed.forEach(e=>{C(e.getComponent(m))})}_updateLight(e){for(let t in e)switch(t){case"direction":_(e,t);break;case"color":this._updateColor(e,e.color);break;default:g(e,t)}}_updateColor(e,t){for(let i in t)e.object[i]=S(t[i])}}D.queries={light:{components:[m],listen:{added:!0,removed:!0,changed:[m]}}};class O extends t.System{execute(){this.queries.meshMaterial.added.forEach(t=>{if(!this._isUrlMesh(t)){let i=t.getComponent(A);i.object=new e.StandardMaterial(i.color.diffuse,q(this,i.scene)),this._updateMaterial(i),t.getComponent(p).object.material=i.object}}),this.queries.meshMaterial.changed.forEach(e=>{this._isUrlMesh(e)||this._updateMaterial(e.getComponent(A))}),this.queries.meshMaterial.removed.forEach(e=>{this._isUrlMesh(e)||(C(e.getComponent(A)),e.getComponent(p).object.material=null)})}_isUrlMesh(e){return e.getComponent(p).type===h.Url}_updateMaterial(e){for(let t in e)switch(t){case"color":this._updateColor(e,e.color);break;case"texture":y(e,e.texture,L(this,e.scene));break;default:g(e,t)}}_updateColor(e,t){for(let i in t)e.object[`${i}Color`]=S(t[i])}}O.queries={meshMaterial:{components:[p,A],listen:{added:!0,removed:!0,changed:[A]}}};class T extends t.System{execute(){this.queries.particle.added.forEach(t=>{let i=t.getComponent(E);i.object=new e.ParticleSystem(i.type,i.capacity,q(this,i.scene));let s=i.object;switch(i.type){case u.Point:s.createPointEmitter(r(i.direction1),r(i.direction2));break;case u.Box:s.createBoxEmitter(r(i.direction1),r(i.direction2),r(i.minEmitBox),r(i.maxEmitBox));break;case u.Sphere:s.createSphereEmitter(i.radius,i.radiusRange);break;case u.DirectedSphere:s.createDirectedSphereEmitter(i.radius,r(i.direction1),r(i.direction2));break;case u.Hemisphere:s.createHemisphericEmitter(i.radius,i.radiusRange);break;case u.Cylinder:s.createCylinderEmitter(i.radius,i.height,i.radiusRange,Math.random());break;case u.DirectedCylinder:s.createDirectedCylinderEmitter(i.radius,i.height,i.radiusRange,r(i.direction1),r(i.direction2));break;case u.Cone:s.createConeEmitter(i.radius,i.angle)}this._updateParticle(i),f(t),s.start()}),this.queries.particle.changed.forEach(e=>{this._updateParticle(e.getComponent(E))}),this.queries.particle.removed.forEach(e=>{let t=e.getComponent(E);t.object.stop(),C(t)})}_updateParticle(e){for(let t in e)switch(t){case"emitter":case"direction1":case"direction2":case"minEmitBox":case"maxEmitBox":_(e,t);break;case"texture":y(e,e.texture,L(this,e.scene));break;case"color":this._updateColor(e,e.color);break;default:g(e,t)}}_updateColor(e,t){for(let i in t)e.object[i]=x(t[i])}}T.queries={particle:{components:[E],listen:{added:!0,removed:!0,changed:[E]}}};class R extends t.System{constructor(){super(...arguments),this._inputs=new Map}init(){w(this,k).onSceneSwitched.add(e=>this._updateOnKey(e)),this._onKey=this._onKey.bind(this)}execute(){this.queries.input.added.forEach(e=>{let t=e.getComponent(M),i=q(this,t.scene);t.type,this._inputs.has(i.uid)||(this._inputs.set(i.uid,t),this._updateOnKey(i))}),this.queries.input.removed.forEach(e=>{let t=e.getComponent(M),i=q(this,t.scene);t.type,this._inputs.has(i.uid)&&(this._inputs.delete(i.uid),this._removeOnKey(i))})}_updateOnKey(e){e.uid===q(this).uid&&(P(this).forEach(e=>this._removeOnKey(e)),this._input=this._inputs.get(e.uid),this._input.onKey&&e.onKeyboardObservable.add(this._onKey))}_removeOnKey(e){e.onKeyboardObservable.removeCallback(this._onKey)}_onKey(e){this._input.onKey.call(this._input,e.event.key,1===e.type,2===e.type)}}R.queries={input:{components:[M],listen:{added:!0,removed:!0}}};var H=Object.freeze({__proto__:null,GameSystem:k,TransformSystem:j,CameraSystem:U,MeshSystem:K,LightSystem:D,MaterialSystem:O,ParticleSystem:T,InputSystem:R,Scene:i,Transform:a,Camera:c,get MeshTypes(){return h},Mesh:p,get LightTypes(){return d},Light:m,Material:A,get ParticleTypes(){return u},Particle:E,get InputTypes(){return l},Input:M,getScene:q,getAssetManager:L,degreeToRadians:s,xyzToVector3:r,xyzToVector3Radians:n,hexToColor3:S,hexToColor4:x});window.EB=H}(BABYLON,ECSY);