(function (global, factory) { if (typeof define === "function" && define.amd) { define(["exports", "three", "../loaders/MD2Loader.js"], factory); } else if (typeof exports !== "undefined") { factory(exports, require("three"), require("../loaders/MD2Loader.js")); } else { var mod = { exports: {} }; factory(mod.exports, global.three, global.MD2Loader); global.MD2Character = mod.exports; } })(typeof globalThis !== "undefined" ? globalThis : typeof self !== "undefined" ? self : this, function (_exports, _three, _MD2Loader) { "use strict"; Object.defineProperty(_exports, "__esModule", { value: true }); _exports.MD2Character = void 0; function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; } var MD2Character = /*#__PURE__*/function () { function MD2Character() { _classCallCheck(this, MD2Character); this.scale = 1; this.animationFPS = 6; this.root = new _three.Object3D(); this.meshBody = null; this.meshWeapon = null; this.skinsBody = []; this.skinsWeapon = []; this.weapons = []; this.activeAnimation = null; this.mixer = null; this.onLoadComplete = function () {}; this.loadCounter = 0; } _createClass(MD2Character, [{ key: "loadParts", value: function loadParts(config) { var scope = this; function createPart(geometry, skinMap) { var materialWireframe = new _three.MeshLambertMaterial({ color: 0xffaa00, wireframe: true }); var materialTexture = new _three.MeshLambertMaterial({ color: 0xffffff, wireframe: false, map: skinMap }); // var mesh = new _three.Mesh(geometry, materialTexture); mesh.rotation.y = -Math.PI / 2; mesh.castShadow = true; mesh.receiveShadow = true; // mesh.materialTexture = materialTexture; mesh.materialWireframe = materialWireframe; return mesh; } function loadTextures(baseUrl, textureUrls) { var textureLoader = new _three.TextureLoader(); var textures = []; for (var i = 0; i < textureUrls.length; i++) { textures[i] = textureLoader.load(baseUrl + textureUrls[i], checkLoadingComplete); textures[i].mapping = _three.UVMapping; textures[i].name = textureUrls[i]; textures[i].encoding = _three.sRGBEncoding; } return textures; } function checkLoadingComplete() { scope.loadCounter -= 1; if (scope.loadCounter === 0) scope.onLoadComplete(); } this.loadCounter = config.weapons.length * 2 + config.skins.length + 1; var weaponsTextures = []; for (var i = 0; i < config.weapons.length; i++) { weaponsTextures[i] = config.weapons[i][1]; } // SKINS this.skinsBody = loadTextures(config.baseUrl + 'skins/', config.skins); this.skinsWeapon = loadTextures(config.baseUrl + 'skins/', weaponsTextures); // BODY var loader = new _MD2Loader.MD2Loader(); loader.load(config.baseUrl + config.body, function (geo) { var boundingBox = new _three.Box3(); boundingBox.setFromBufferAttribute(geo.attributes.position); scope.root.position.y = -scope.scale * boundingBox.min.y; var mesh = createPart(geo, scope.skinsBody[0]); mesh.scale.set(scope.scale, scope.scale, scope.scale); scope.root.add(mesh); scope.meshBody = mesh; scope.meshBody.clipOffset = 0; scope.activeAnimationClipName = mesh.geometry.animations[0].name; scope.mixer = new _three.AnimationMixer(mesh); checkLoadingComplete(); }); // WEAPONS var generateCallback = function generateCallback(index, name) { return function (geo) { var mesh = createPart(geo, scope.skinsWeapon[index]); mesh.scale.set(scope.scale, scope.scale, scope.scale); mesh.visible = false; mesh.name = name; scope.root.add(mesh); scope.weapons[index] = mesh; scope.meshWeapon = mesh; checkLoadingComplete(); }; }; for (var _i = 0; _i < config.weapons.length; _i++) { loader.load(config.baseUrl + config.weapons[_i][0], generateCallback(_i, config.weapons[_i][0])); } } }, { key: "setPlaybackRate", value: function setPlaybackRate(rate) { if (rate !== 0) { this.mixer.timeScale = 1 / rate; } else { this.mixer.timeScale = 0; } } }, { key: "setWireframe", value: function setWireframe(wireframeEnabled) { if (wireframeEnabled) { if (this.meshBody) this.meshBody.material = this.meshBody.materialWireframe; if (this.meshWeapon) this.meshWeapon.material = this.meshWeapon.materialWireframe; } else { if (this.meshBody) this.meshBody.material = this.meshBody.materialTexture; if (this.meshWeapon) this.meshWeapon.material = this.meshWeapon.materialTexture; } } }, { key: "setSkin", value: function setSkin(index) { if (this.meshBody && this.meshBody.material.wireframe === false) { this.meshBody.material.map = this.skinsBody[index]; } } }, { key: "setWeapon", value: function setWeapon(index) { for (var i = 0; i < this.weapons.length; i++) { this.weapons[i].visible = false; } var activeWeapon = this.weapons[index]; if (activeWeapon) { activeWeapon.visible = true; this.meshWeapon = activeWeapon; this.syncWeaponAnimation(); } } }, { key: "setAnimation", value: function setAnimation(clipName) { if (this.meshBody) { if (this.meshBody.activeAction) { this.meshBody.activeAction.stop(); this.meshBody.activeAction = null; } var action = this.mixer.clipAction(clipName, this.meshBody); if (action) { this.meshBody.activeAction = action.play(); } } this.activeClipName = clipName; this.syncWeaponAnimation(); } }, { key: "syncWeaponAnimation", value: function syncWeaponAnimation() { var clipName = this.activeClipName; if (this.meshWeapon) { if (this.meshWeapon.activeAction) { this.meshWeapon.activeAction.stop(); this.meshWeapon.activeAction = null; } var action = this.mixer.clipAction(clipName, this.meshWeapon); if (action) { this.meshWeapon.activeAction = action.syncWith(this.meshBody.activeAction).play(); } } } }, { key: "update", value: function update(delta) { if (this.mixer) this.mixer.update(delta); } }]); return MD2Character; }(); _exports.MD2Character = MD2Character; });