const gulp = require("gulp"), fs = require("fs"), sizeOf = require("image-size"), ftp = require("vinyl-ftp"), imageResize = require("gulp-image-resize"), conn = ftp.create({ host: "ssh.jim-fx.com:2221", user: process.env.FTP_USERNAME, password: process.env.FTP_PASSWORD, parallel: 10, log: console.log }); String.prototype.replaceAll = function (search, replacement) { var target = this; return target.replace(new RegExp(search, "g"), replacement); }; function getSceneConfigs(callback) { fs.readdir("./assets", function (err, dir) { dir = dir.filter(file => file.indexOf(".json") !== -1); if (!err) { let length = dir.length; let configs = []; dir.forEach(item => { fs.readFile("./assets/" + item, function (err, file) { if (!err) { try { configs.push(JSON.parse(file)); } catch (err) { console.log(err) } length -= 1; if (length === 0) { callback(configs); } } else { console.log(err); } }); }); } else { console.log(err); } }); } gulp.task("upload", function () { gulp.src("./dist/**", { base: "./", buffer: false }) .pipe(conn.dest("./modern/")); }) function buildModels() { getSceneConfigs(function (configs) { console.log("Compiling Models") configs.forEach(config => { let objectsLeft = config.objects.length; let objects = {}; config.objects.forEach(model => { fs.readFile("./assets/" + config.name + "/models/" + model.name + ".json", function (err, jsonFile) { objects[model.name] = JSON.parse(jsonFile); objectsLeft -= 1; if (objectsLeft === 0) { fs.writeFile("./dist/data/" + config.name + "/" + config.name + "_objects.json", JSON.stringify(objects).replace(/("[^"]*")|\s/g, "$1"), function (err) { if (err) { console.log(err); } else { console.log("Finished Scene File " + config.name + ".json \r") } }); } }); }); }); }); } gulp.task("buildModels", buildModels); function buildTextures() { getSceneConfigs(function (configs) { console.log("Resizing Textures \n") configs.forEach(config => { Object.keys(config.materials).forEach(mat => { Object.keys(config.materials[mat]).forEach(material => { let _mat = config.materials[mat][material]; if (typeof (_mat) === "string") { if (_mat.indexOf("jpg") !== -1 || _mat.indexOf("png") !== -1) { let size = sizeOf("./assets/" + config.name + "/textures/" + _mat); if (size.width !== size.height) { console.error("Texture " + _mat + " is not square"); } let imgConf = { format: "jpg", width: 512, height: 512, upscale: true }; switch (size.width) { case 512: imgConf.width = 512; imgConf.height = 512; break; case 1024: imgConf.width = 512; imgConf.height = 512; break; case 2048: imgConf.width = 1024; imgConf.height = 1024; break; case 4096: imgConf.width = 1024; imgConf.height = 1024; break; default: break; } if (material = "lightMap") { imgConf.width *= 2; imgConf.height *= 2; } let placeholderConf = { format: "jpg", width: imgConf.width / 8, height: imgConf.width / 8, upscale: true, quality: 0.2 }; gulp.src("./assets/" + config.name + "/textures/" + _mat) .pipe(imageResize(imgConf)) .pipe(gulp.dest("./dist/data/" + config.name + "/textures/")) .pipe(imageResize(placeholderConf)) .pipe(gulp.dest("./dist/data/" + config.name + "/textures/small/")) .on("end", () => { process.stdout.write("Finished Texture: " + _mat + "\r") }); } } }); }); }); }); } gulp.task("buildTextures", buildTextures); function buildHdris() { getSceneConfigs(function (configs) { console.log("Resizing Hdris"); configs.forEach(config => { Object.keys(config.hdris).forEach(hdri => { let url = "./assets/" + config.name + "/hdris/" + config.hdris[hdri].path; gulp.src(url) .pipe(imageResize({ format: "jpg", width: 1024, height: 512, upscale: true })) .pipe(gulp.dest("./dist/data/" + config.name + "/hdris/")) .on("end", () => { process.stdout.write("Finished HDRI: " + hdri + "\r") }); }); }); }); } gulp.task("buildHdris", buildHdris); function buildSceneFiles() { getSceneConfigs(function (configs) { console.log("Building Scene Files") configs.forEach(config => { fs.writeFile("./dist/data/" + config.name + "/" + config.name + ".json", JSON.stringify(config).replace(/("[^"]*")|\s/g, "$1").replaceAll(".png", ".jpg"), function (err) { if (err) { console.log(err); } else { console.log("Finished Scene File: " + config.name + "\n") } }); }); }); } gulp.task("buildSceneFiles", function () { buildSceneFiles(); }); gulp.task("build", function () { buildSceneFiles(); buildModels(); buildTextures(); buildHdris(); }) gulp.task("watch", function () { gulp.watch("./assets/*/textures/**", ["buildTextures"]); gulp.watch("./assets/*/models/**", ["buildModels"]); gulp.watch("./assets/*/hdris/**", ["buildHdris"]); gulp.watch("./assets/*.json", function () { console.log("Build Scene files"); setTimeout(buildSceneFiles, 200); }); });