mirror of
https://github.com/JannisX11/blockbench.git
synced 2025-01-18 15:26:19 +08:00
Reference textures in bbmodels via relative path
This commit is contained in:
parent
a4466bc9c5
commit
73b4aec0c7
@ -73,6 +73,22 @@ var codec = new Codec('project', {
|
|||||||
}
|
}
|
||||||
this.parse(model, file.path)
|
this.parse(model, file.path)
|
||||||
},
|
},
|
||||||
|
export() {
|
||||||
|
Blockbench.export({
|
||||||
|
resource_id: 'model',
|
||||||
|
type: this.name,
|
||||||
|
extensions: [this.extension],
|
||||||
|
name: this.fileName(),
|
||||||
|
startpath: this.startPath(),
|
||||||
|
content: isApp ? null : this.compile(),
|
||||||
|
custom_writer: isApp ? (content, path) => {
|
||||||
|
// Path needs to be changed before compiling for relative resource paths
|
||||||
|
ModelMeta.save_path = path;
|
||||||
|
content = this.compile();
|
||||||
|
this.write(content, path);
|
||||||
|
} : null,
|
||||||
|
}, path => this.afterDownload(path))
|
||||||
|
},
|
||||||
compile(options) {
|
compile(options) {
|
||||||
if (!options) options = 0;
|
if (!options) options = 0;
|
||||||
var model = {
|
var model = {
|
||||||
@ -110,6 +126,10 @@ var codec = new Codec('project', {
|
|||||||
textures.forEach(tex => {
|
textures.forEach(tex => {
|
||||||
var t = tex.getUndoCopy();
|
var t = tex.getUndoCopy();
|
||||||
delete t.selected;
|
delete t.selected;
|
||||||
|
if (isApp && ModelMeta.save_path && tex.path) {
|
||||||
|
let relative = PathModule.relative(ModelMeta.save_path, tex.path);
|
||||||
|
t.relative_path = relative.replace(/\\/g, '/');
|
||||||
|
}
|
||||||
if (options.bitmaps != false) {
|
if (options.bitmaps != false) {
|
||||||
t.source = 'data:image/png;base64,'+tex.getBase64()
|
t.source = 'data:image/png;base64,'+tex.getBase64()
|
||||||
t.mode = 'bitmap'
|
t.mode = 'bitmap'
|
||||||
@ -206,7 +226,16 @@ var codec = new Codec('project', {
|
|||||||
var tex_copy = new Texture(tex, tex.uuid).add(false);
|
var tex_copy = new Texture(tex, tex.uuid).add(false);
|
||||||
if (isApp && tex.path && fs.existsSync(tex.path) && !model.meta.backup) {
|
if (isApp && tex.path && fs.existsSync(tex.path) && !model.meta.backup) {
|
||||||
tex_copy.fromPath(tex.path)
|
tex_copy.fromPath(tex.path)
|
||||||
} else if (tex.source && tex.source.substr(0, 5) == 'data:') {
|
return;
|
||||||
|
}
|
||||||
|
if (isApp && tex.relative_path && ModelMeta.save_path) {
|
||||||
|
let resolved_path = PathModule.resolve(ModelMeta.save_path, tex.relative_path);
|
||||||
|
if (fs.existsSync(resolved_path)) {
|
||||||
|
tex_copy.fromPath(resolved_path)
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (tex.source && tex.source.substr(0, 5) == 'data:') {
|
||||||
tex_copy.fromDataURL(tex.source)
|
tex_copy.fromDataURL(tex.source)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
@ -292,20 +321,28 @@ var codec = new Codec('project', {
|
|||||||
|
|
||||||
function loadTexture(tex) {
|
function loadTexture(tex) {
|
||||||
if (isApp && Texture.all.find(tex2 => tex.path == tex2.path)) {
|
if (isApp && Texture.all.find(tex2 => tex.path == tex2.path)) {
|
||||||
new_textures.push(Texture.all.find(tex2 => tex.path == tex2.path));
|
return Texture.all.find(tex2 => tex.path == tex2.path)
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
var tex_copy = new Texture(tex, tex.uuid).add(false);
|
var tex_copy = new Texture(tex, tex.uuid).add(false);
|
||||||
if (isApp && tex.path && fs.existsSync(tex.path) && !model.meta.backup) {
|
if (isApp && tex.path && fs.existsSync(tex.path) && !model.meta.backup) {
|
||||||
tex_copy.fromPath(tex.path)
|
tex_copy.fromPath(tex.path)
|
||||||
} else if (tex.source && tex.source.substr(0, 5) == 'data:') {
|
return tex_copy;
|
||||||
tex_copy.fromDataURL(tex.source)
|
}
|
||||||
|
if (isApp && tex.relative_path && ModelMeta.save_path) {
|
||||||
|
let resolved_path = PathModule.resolve(ModelMeta.save_path, tex.relative_path);
|
||||||
|
if (fs.existsSync(resolved_path)) {
|
||||||
|
tex_copy.fromPath(resolved_path)
|
||||||
|
return tex_copy;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (tex.source && tex.source.substr(0, 5) == 'data:') {
|
||||||
|
tex_copy.fromDataURL(tex.source)
|
||||||
|
return tex_copy;
|
||||||
}
|
}
|
||||||
new_textures.push(tex_copy);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (model.textures && (!Format.single_texture || Texture.all.length == 0)) {
|
if (model.textures && (!Format.single_texture || Texture.all.length == 0)) {
|
||||||
model.textures.forEach(loadTexture)
|
new_textures.replace(model.textures.map(loadTexture))
|
||||||
}
|
}
|
||||||
|
|
||||||
if (model.elements) {
|
if (model.elements) {
|
||||||
|
Loading…
Reference in New Issue
Block a user