-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathaframe-terrain-model-component.min.js
1 lines (1 loc) · 4.42 KB
/
aframe-terrain-model-component.min.js
1
!function(e){var t={};function i(r){if(t[r])return t[r].exports;var a=t[r]={i:r,l:!1,exports:{}};return e[r].call(a.exports,a,a.exports,i),a.l=!0,a.exports}i.m=e,i.c=t,i.d=function(e,t,r){i.o(e,t)||Object.defineProperty(e,t,{enumerable:!0,get:r})},i.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},i.t=function(e,t){if(1&t&&(e=i(e)),8&t)return e;if(4&t&&"object"==typeof e&&e&&e.__esModule)return e;var r=Object.create(null);if(i.r(r),Object.defineProperty(r,"default",{enumerable:!0,value:e}),2&t&&"string"!=typeof e)for(var a in e)i.d(r,a,function(t){return e[t]}.bind(null,a));return r},i.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return i.d(t,"a",t),t},i.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},i.p="",i(i.s=0)}([function(e,t,i){if("undefined"==typeof AFRAME)throw new Error("Component attempted to register before AFRAME was available.");i(1),AFRAME.registerComponent("terrain-model",{schema:{planeHeight:{type:"number",default:346},planeWidth:{type:"number",default:346},segmentsHeight:{type:"number",default:199},segmentsWidth:{type:"number",default:199},zPosition:{type:"number",default:1.5},dem:{type:"asset"},map:{type:"asset"},alphaMap:{type:"asset"},wireframe:{type:"boolean",default:!1}},init:function(){const e=this.el,t=this.data;this.heightData=null,this.terrainLoader=new THREE.TerrainLoader,this.textureLoader=new THREE.TextureLoader,this._replaceTexture=this._replaceTexture.bind(this),this._updatePositionBuffer=this._updatePositionBuffer.bind(this),this._toggleWireframe=this._toggleWireframe.bind(this);const{planeWidth:i,planeHeight:r,segmentsHeight:a,segmentsWidth:n}=t;this.geometry=new THREE.PlaneBufferGeometry(i,r,n,a),this.material=new THREE.MeshLambertMaterial,this.mesh=new THREE.Mesh(this.geometry,this.material),this.mesh.rotation.x=Math.PI/180*-90,e.setObject3D("terrain",this.mesh)},update:function(e){const t=this.data,i=t.dem,r=t.map,a=t.alphaMap,n=t.zPosition,s=t.wireframe;const o=e=>t=>{!("map"===e&&this.data.map!==r||"alphaMap"===e&&this.data.alphaMap!==a)?(t.anisotropy=16,this._replaceTexture(e,t),this.el.emit("textureLoaded",{type:e})):t.dispose()};i!==e.dem&&this.terrainLoader.load(i,function(e){this.heightData=e,this._updatePositionBuffer(),this.el.emit("demLoaded",{dem:i})}.bind(this)),r!==e.map&&(r?this.textureLoader.load(r,o("map")):this._replaceTexture("map",null)),a!==e.alphaMap&&(a?(this.material.transparent=!0,this.textureLoader.load(a,o("alphaMap"))):this._replaceTexture("alphaMap",null)),n!==e.zPosition&&this.mesh.scale.set(1,1,n),s!==e.wireframe&&this._toggleWireframe()},_replaceTexture:function(e,t){const i=this.material[e];this.material[e]=t,this.material.needsUpdate=!0,i&&i.dispose()},_updatePositionBuffer:function(){if(!this.heightData)return;let e=this.geometry.getAttribute("position");for(let t=0;t<e.count;t++){let i=this.heightData[t]/65535;e.setZ(t,i)}e.needsUpdate=!0;let t=this.mesh.getObjectByName("terrain-wireframe");if(t){t.geometry.dispose();const e=new THREE.WireframeGeometry(this.geometry);t.geometry=e}this.el.emit("positionBufferUpdated")},_toggleWireframe:function(){let e=this.mesh.getObjectByName("terrain-wireframe");if(!e){if(!this.data.wireframe)return;const e=new THREE.WireframeGeometry(this.geometry),t=new THREE.LineBasicMaterial({color:8421504,linewidth:1});let i=new THREE.LineSegments(e,t);return i.name="terrain-wireframe",i.material.opacity=.3,i.material.transparent=!0,void this.mesh.add(i)}e.geometry.dispose(),e.material.dispose(),this.mesh.remove(e)},remove:function(){this.geometry.dispose(),this.material.map.dispose(),this.material.alphaMap.dispose(),this.material.dispose(),this.data.wireframe&&this._toggleWireframe(),this.el.removeObject3D("terrain")}})},function(e,t){THREE.TerrainLoader=function(e){this.manager=void 0!==e?e:THREE.DefaultLoadingManager},THREE.TerrainLoader.prototype={constructor:THREE.TerrainLoader,load:function(e,t,i,r){var a=this,n=new XMLHttpRequest;void 0!==t&&n.addEventListener("load",(function(i){t(new Uint16Array(i.target.response)),a.manager.itemEnd(e)}),!1),void 0!==i&&n.addEventListener("progress",(function(e){i(e)}),!1),void 0!==r&&n.addEventListener("error",(function(e){r(e)}),!1),void 0!==this.crossOrigin&&(n.crossOrigin=this.crossOrigin),n.open("GET",e,!0),n.responseType="arraybuffer",n.send(null),a.manager.itemStart(e)},setCrossOrigin:function(e){this.crossOrigin=e}}}]);