1 line
9.4 KiB
Plaintext
1 line
9.4 KiB
Plaintext
|
|
{"version":3,"file":"MD2Character.cjs","sources":["../../src/misc/MD2Character.js"],"sourcesContent":["import { AnimationMixer, Box3, Mesh, MeshLambertMaterial, Object3D, TextureLoader, UVMapping } from 'three'\nimport { MD2Loader } from '../loaders/MD2Loader'\n\nclass MD2Character {\n constructor() {\n this.scale = 1\n this.animationFPS = 6\n\n this.root = new Object3D()\n\n this.meshBody = null\n this.meshWeapon = null\n\n this.skinsBody = []\n this.skinsWeapon = []\n\n this.weapons = []\n\n this.activeAnimation = null\n\n this.mixer = null\n\n this.onLoadComplete = function () {}\n\n this.loadCounter = 0\n }\n\n loadParts(config) {\n const scope = this\n\n function createPart(geometry, skinMap) {\n const materialWireframe = new MeshLambertMaterial({\n color: 0xffaa00,\n wireframe: true,\n morphTargets: true,\n morphNormals: true,\n })\n const materialTexture = new MeshLambertMaterial({\n color: 0xffffff,\n wireframe: false,\n map: skinMap,\n morphTargets: true,\n morphNormals: true,\n })\n\n //\n\n const mesh = new Mesh(geometry, materialTexture)\n mesh.rotation.y = -Math.PI / 2\n\n mesh.castShadow = true\n mesh.receiveShadow = true\n\n //\n\n mesh.materialTexture = materialTexture\n mesh.materialWireframe = materialWireframe\n\n return mesh\n }\n\n function loadTextures(baseUrl, textureUrls) {\n const textureLoader = new TextureLoader()\n const textures = []\n\n for (let i = 0; i < textureUrls.length; i++) {\n textures[i] = textureLoader.load(baseUrl + textureUrls[i], checkLoadingComplete)\n textures[i].mapping = UVMapping\n textures[i].name = textureUrls[i]\n if ('colorSpace' in textures[i]) textures[i].colorSpace = 'srgb'\n else textures[i].encoding = 3001 // sRGBEncoding\n }\n\n return textures\n }\n\n function checkLoadingComplete() {\n scope.loadCounter -= 1\n\n if (scope.loadCounter === 0) scope.onLoadComplete()\n }\n\n this.loadCounter = config.weapons.length * 2 + config.skins.length + 1\n\n const weaponsTextures = []\n for (let i = 0; i < config.weapons.length; i++) weaponsTextures[i] = config.weapons[i][1]\n // SKINS\n\n this.skinsBody = loadTextures(config.baseUrl + 'skins/', config.skins)\n this.skinsWeapon = loadTextures(config.baseUrl + 'skins/', weaponsTextures)\n\n // BODY\n\n const loader = new MD2Loader()\n\n loader.load(config.baseUrl + config.body, function (geo) {\n const boundingBox = new Box3()\n boundingBox.setFromBufferAttribute(geo.attributes.position)\n\n scope.root.position.y = -scope.scale * boundingBox.min.y\n\n const mesh = createPart(geo, scope.skinsBody[0])\n mesh.scale.set(scope.scale, scope.scale, scope.scale)\n\n scope.root.add(mesh)\n\n scope.meshBody = mesh\n\n scope.meshBody.clipOffset = 0\n scope.activeAnimationClipName = mesh.geometry.animations[0].name\n\n scope.mixer = new AnimationMixer(mesh)\n\n checkLoadingComplete()\n })\n\n // WEAPONS\n\n const generateCallback = function (index, name) {\n return function (geo) {\n const mesh = createPart(geo, scope.skinsWeapon[index])\n mesh.scale.set(scope.scale, scope.scale, scope.scale)\n mesh.visible = false\n\n mesh.name = name\n\n scope.root.add(mesh)\n\n scope.weapons[index] = mesh\n scope.meshWeapon = mesh\n\n checkLoadingComplete()\n }\n }\n\n for (let i = 0; i < config.weapons.length; i++) {\n loader.load(config.baseUrl + config.weapons[i][0], generateCallback(i, config.weapons[i][0]))\n }\n }\n\n setPlaybackRate(rate) {\n if (rate !== 0) {\n this.mixer.timeScale = 1 / rate\n } else {\n this.mixer.timeScale = 0\n }\n }\n\n setWireframe(wireframeEnabled) {\n if (wireframeEnabled) {\n if (this.meshBody) this.meshBody.material = this.meshBody.materialWireframe\n if (this.
|