blockbench/js/io/formats/optifine_jem.js

291 lines
7.4 KiB
JavaScript
Raw Normal View History

2019-07-18 00:02:07 +08:00
(function() {
var codec = new Codec('optifine_entity', {
name: 'OptiFine JEM',
extension: 'jem',
remember: true,
load_filter: {
type: 'json',
extensions: ['jem'],
condition(file) {
return file && file.models != undefined;
}
},
2019-07-18 00:02:07 +08:00
compile(options) {
if (options === undefined) options = {}
var entitymodel = {}
var geo_code = 'geometry.'+Project.geometry_name
2020-07-16 15:32:59 +08:00
if (Texture.getDefault()) {
entitymodel.texture = Texture.getDefault().name
2019-07-18 00:02:07 +08:00
}
entitymodel.textureSize = [Project.texture_width, Project.texture_height];
entitymodel.models = []
Outliner.root.forEach(function(g) {
2020-03-05 03:56:17 +08:00
if (g instanceof Group == false) return;
2019-07-18 00:02:07 +08:00
//Bone
var bone = {
part: g.name,
id: g.name,
invertAxis: 'xy',
translate: g.origin.slice()
}
bone.translate[1] *= -1
bone.translate[2] *= -1
if (!g.rotation.allEqual(0)) {
bone.rotate = g.rotation.slice()
}
if (entityMode.hardcodes[geo_code]) {
var codes = entityMode.hardcodes[geo_code]
var bone_codes = codes[bone.part] || codes[bone.part+'1']
if (bone_codes) {
if (!bone.rotate) bone.rotate = [0, 0, 0];
entityMode.hardcodes[geo_code][bone.part].rotation.forEach((dft, i) => {
bone.rotate[i] += dft;
})
}
}
if (g.mirror_uv) {
bone.mirrorTexture = 'u'
}
2019-12-23 00:33:55 +08:00
function populate(p_model, group, depth) {
2019-07-18 00:02:07 +08:00
if (group.children.length === 0) return;
var mirror_sub;
group.children.forEach(obj => {
if (!obj.export) return;
if (obj.type === 'cube') {
if (Project.box_uv) {
var box = new oneLiner()
} else {
var box = {};
}
var c_size = obj.size()
box.coordinates = [
obj.from[0],
obj.from[1],
obj.from[2],
c_size[0],
c_size[1],
c_size[2]
]
if (p_model && p_model.part === undefined) {
box.coordinates[0] -= p_model.translate[0];
box.coordinates[1] -= p_model.translate[1];
box.coordinates[2] -= p_model.translate[2];
}
if (Project.box_uv) {
box.textureOffset = obj.uv_offset
} else {
for (var face in obj.faces) {
2019-12-23 00:33:55 +08:00
if (obj.faces[face].texture !== null) {
var uv = obj.faces[face].uv;
box[`uv${capitalizeFirstLetter(face)}`] = uv;
}
2019-07-18 00:02:07 +08:00
}
}
if (obj.inflate && typeof obj.inflate === 'number') {
box.sizeAdd = obj.inflate
}
if (obj.mirror_uv !== group.mirror_uv) {
if (!mirror_sub) {
mirror_sub = {
invertAxis: 'xy',
mirrorTexture: 'u',//xxx
boxes: []
}
if (!p_model.submodels) p_model.submodels = [];
p_model.submodels.splice(0, 0, mirror_sub)
}
mirror_sub.boxes.push(box)
} else {
if (!p_model.boxes) p_model.boxes = []
p_model.boxes.push(box)
}
} else if (obj.type === 'group') {
var bone = {
id: obj.name,
invertAxis: 'xy',
translate: obj.origin.slice()
}
if (obj.mirror_uv) {
bone.mirrorTexture = 'u'
}
if (!obj.rotation.allEqual(0)) {
bone.rotate = obj.rotation.slice()
}
2019-12-23 00:33:55 +08:00
populate(bone, obj, depth+1)
if (depth >= 1) {
bone.translate[0] -= group.origin[0];
bone.translate[1] -= group.origin[1];
bone.translate[2] -= group.origin[2];
}
2019-07-18 00:02:07 +08:00
if (!p_model.submodels) p_model.submodels = [];
p_model.submodels.push(bone)
}
})
}
2019-12-23 00:33:55 +08:00
populate(bone, g, 0)
2020-07-16 15:32:59 +08:00
if (g.cem_animations.length) {
bone.animations = g.cem_animations;
}
2019-07-18 00:02:07 +08:00
entitymodel.models.push(bone)
})
2020-03-05 03:56:17 +08:00
this.dispatchEvent('compile', {entitymodel, options});
2019-07-18 00:02:07 +08:00
if (options.raw) {
return entitymodel
} else {
return autoStringify(entitymodel)
}
},
parse(model, path) {
2020-03-05 03:56:17 +08:00
this.dispatchEvent('parse', {model});
2019-07-18 00:02:07 +08:00
if (model.textureSize) {
2019-12-16 03:04:31 +08:00
Project.texture_width = parseInt(model.textureSize[0])||16;
Project.texture_height = parseInt(model.textureSize[1])||16;
2019-07-18 00:02:07 +08:00
}
2019-12-23 00:33:55 +08:00
let empty_face = {uv: [0, 0, 0, 0], texture: null}
2019-07-18 00:02:07 +08:00
if (model.models) {
model.models.forEach(function(b) {
if (typeof b !== 'object') return;
var subcount = 0;
//Bone
var group = new Group({
name: b.part,
origin: b.translate,
rotation: b.rotate,
2020-07-16 15:32:59 +08:00
mirror_uv: (b.mirrorTexture && b.mirrorTexture.includes('u')),
cem_animations: b.animations
2019-07-18 00:02:07 +08:00
})
2019-12-16 03:04:31 +08:00
group.origin[1] *= -1;
group.origin[2] *= -1;
2019-07-18 00:02:07 +08:00
2019-12-23 00:33:55 +08:00
function readContent(submodel, p_group, depth) {
2019-07-18 00:02:07 +08:00
if (submodel.boxes && submodel.boxes.length) {
submodel.boxes.forEach(box => {
var base_cube = new Cube({
name: box.name || p_group.name,
autouv: 0,
uv_offset: box.textureOffset,
inflate: box.sizeAdd,
mirror_uv: p_group.mirror_uv
})
if (box.coordinates) {
base_cube.extend({
from: [
box.coordinates[0],
box.coordinates[1],
box.coordinates[2]
],
to: [
box.coordinates[0]+box.coordinates[3],
box.coordinates[1]+box.coordinates[4],
box.coordinates[2]+box.coordinates[5]
]
})
}
2019-12-23 00:33:55 +08:00
if (!box.textureOffset && (
box.uvNorth
|| box.uvEast
|| box.uvSouth
|| box.uvWest
|| box.uvUp
|| box.uvDown
)) {
2019-07-18 00:02:07 +08:00
Project.box_uv = false;
base_cube.extend({faces: {
2019-12-23 00:33:55 +08:00
north: box.uvNorth ? {uv: box.uvNorth} : empty_face,
east: box.uvEast ? {uv: box.uvEast} : empty_face,
south: box.uvSouth ? {uv: box.uvSouth} : empty_face,
west: box.uvWest ? {uv: box.uvWest} : empty_face,
up: box.uvUp ? {uv: box.uvUp} : empty_face,
down: box.uvDown ? {uv: box.uvDown} : empty_face,
2019-07-18 00:02:07 +08:00
}})
}
if (p_group.parent !== 'root') {
for (var i = 0; i < 3; i++) {
base_cube.from[i] += p_group.origin[i];
base_cube.to[i] += p_group.origin[i];
}
}
base_cube.addTo(p_group).init()
})
}
if (submodel.submodels && submodel.submodels.length) {
submodel.submodels.forEach(subsub => {
2019-12-23 00:33:55 +08:00
if (depth >= 1 && subsub.translate) {
subsub.translate[0] += p_group.origin[0];
subsub.translate[1] += p_group.origin[1];
subsub.translate[2] += p_group.origin[2];
}
2019-07-18 00:02:07 +08:00
var group = new Group({
2019-12-23 00:33:55 +08:00
name: subsub.id || `${b.part}_sub_${subcount}`,
2020-08-15 22:38:39 +08:00
origin: subsub.translate || (depth >= 1 ? submodel.translate : undefined),
2019-07-18 00:02:07 +08:00
rotation: subsub.rotate,
mirror_uv: (subsub.mirrorTexture && subsub.mirrorTexture.includes('u'))
})
subcount++;
group.addTo(p_group).init()
2019-12-23 00:33:55 +08:00
readContent(subsub, group, depth+1)
2019-07-18 00:02:07 +08:00
})
}
}
group.init().addTo()
2019-12-23 00:33:55 +08:00
readContent(b, group, 0)
2019-07-18 00:02:07 +08:00
})
}
if (model.texture) {
var path = path.replace(/\\[\w .-]+$/, '\\'+model.texture)
new Texture().fromPath(path).add(false)
}
2020-07-16 15:32:59 +08:00
this.dispatchEvent('parsed', {model});
2020-12-12 22:14:48 +08:00
Canvas.updateAllBones()
2019-07-18 00:02:07 +08:00
}
})
var format = new ModelFormat({
id: 'optifine_entity',
extension: 'jem',
icon: 'icon-format_optifine',
box_uv: true,
optional_box_uv: true,
single_texture: true,
integer_size: true,
bone_rig: true,
2019-12-16 03:04:31 +08:00
centered_grid: true,
2019-07-18 00:02:07 +08:00
codec
})
2020-05-31 21:54:04 +08:00
Object.defineProperty(format, 'integer_size', {get: _ => Project.box_uv})
2019-07-18 00:02:07 +08:00
codec.format = format;
2019-08-18 00:26:14 +08:00
2019-07-18 00:02:07 +08:00
BARS.defineActions(function() {
2019-08-18 00:26:14 +08:00
codec.export_action = new Action('export_optifine_full', {
2019-07-18 00:02:07 +08:00
icon: 'icon-optifine_file',
category: 'file',
condition: () => Format == format,
click: function () {
codec.export()
}
})
})
})()