wonder.js
Version:
210 lines (197 loc) • 11.3 kB
JavaScript
'use strict';
var Js_option = require("bs-platform/lib/js/js_option.js");
var Log$WonderLog = require("wonder-log/lib/js/src/Log.js");
var Contract$WonderLog = require("wonder-log/lib/js/src/Contract.js");
var GLTFUtils$Wonderjs = require("../utils/GLTFUtils.js");
var BufferUtils$Wonderjs = require("../utils/BufferUtils.js");
var StateDataMain$Wonderjs = require("../../service/state/main/data/StateDataMain.js");
var DataViewCommon$Wonderjs = require("../generate/DataViewCommon.js");
var TypeArrayService$Wonderjs = require("../../service/primitive/buffer/TypeArrayService.js");
var ConvertSceneSystem$Wonderjs = require("./ConvertSceneSystem.js");
var IsDebugMainService$Wonderjs = require("../../service/state/main/state/IsDebugMainService.js");
var ArrayService$WonderCommonlib = require("wonder-commonlib/lib/js/src/ArrayService.js");
var ConvertImagesSystem$Wonderjs = require("./ConvertImagesSystem.js");
var ConvertLightsSystem$Wonderjs = require("./ConvertLightsSystem.js");
var ConvertStreamSystem$Wonderjs = require("./ConvertStreamSystem.js");
var ConvertBuffersSystem$Wonderjs = require("./ConvertBuffersSystem.js");
var ConvertCamerasSystem$Wonderjs = require("./ConvertCamerasSystem.js");
var ConvertIndicesSystem$Wonderjs = require("./indices/ConvertIndicesSystem.js");
var ConvertScriptsSystem$Wonderjs = require("./ConvertScriptsSystem.js");
var ConvertTexturesSystem$Wonderjs = require("./ConvertTexturesSystem.js");
var ConvertGeometrysSystem$Wonderjs = require("./ConvertGeometrysSystem.js");
var ConvertMaterialsSystem$Wonderjs = require("./ConvertMaterialsSystem.js");
var BuildGLTFJsonDataSystem$Wonderjs = require("./BuildGLTFJsonDataSystem.js");
var ConvertTransformsSystem$Wonderjs = require("./ConvertTransformsSystem.js");
var ConvertGameObjectsSystem$Wonderjs = require("./ConvertGameObjectsSystem.js");
var ConvertMeshRenderersSystem$Wonderjs = require("./ConvertMeshRenderersSystem.js");
var ConvertMultiPrimitivesSystem$Wonderjs = require("./ConvertMultiPrimitivesSystem.js");
var ConvertGLTFJsonToRecordSystem$Wonderjs = require("./ConvertGLTFJsonToRecordSystem.js");
function _checkAndWarn(gltf) {
var hasTexCoord_1 = /* record */[/* contents */false];
ArrayService$WonderCommonlib.forEach((function (param) {
var match = param[/* primitives */0][0];
var match$1 = Js_option.isSome(match[/* attributes */0][/* texCoord_1 */3]);
if (match$1) {
hasTexCoord_1[0] = true;
return /* () */0;
} else {
return /* () */0;
}
}), gltf[/* meshes */11]);
var match = hasTexCoord_1[0];
if (match) {
Log$WonderLog.warn("not support texCoord_1");
}
return gltf;
}
function _buildWDBJsonUint8Array(gltf) {
var gltf$1 = _checkAndWarn(gltf);
var gltf$2 = ConvertMultiPrimitivesSystem$Wonderjs.convertMultiPrimitivesToNodes(gltf$1);
var transforms = ConvertTransformsSystem$Wonderjs.convertToTransforms(gltf$2);
var match = BuildGLTFJsonDataSystem$Wonderjs.buildJsonData(transforms, gltf$2);
var gltf$3 = match[2];
var match$1 = ConvertLightsSystem$Wonderjs.convertToLights(gltf$3);
var indices = ConvertIndicesSystem$Wonderjs.convertToIndices(gltf$3);
var encoder = new TextEncoder();
return /* tuple */[
match[0],
match[1],
encoder.encode(JSON.stringify(/* record */[
/* asset : record */[
/* version */gltf$2[/* asset */0][/* version */0],
/* generator */GLTFUtils$Wonderjs.getGenerator(/* () */0)
],
/* scene */ConvertSceneSystem$Wonderjs.convertToScene(match$1[0], gltf$3),
/* indices */indices,
/* gameObjects */ConvertGameObjectsSystem$Wonderjs.convert(gltf$3),
/* images */ConvertImagesSystem$Wonderjs.convertToImages(gltf$3),
/* basicSourceTextures */ConvertTexturesSystem$Wonderjs.convertToBasicSourceTextures(gltf$3),
/* cubemapTextures */ConvertTexturesSystem$Wonderjs.convertToCubemapTextures(gltf$3),
/* samplers */ConvertTexturesSystem$Wonderjs.convertToSamplers(gltf$3),
/* buffers */ConvertBuffersSystem$Wonderjs.convertToBuffers(gltf$3),
/* bufferViews */ConvertBuffersSystem$Wonderjs.convertToBufferViews(gltf$3),
/* accessors */ConvertBuffersSystem$Wonderjs.convertToAccessors(gltf$3),
/* directionLights */match$1[1],
/* pointLights */match$1[2],
/* basicCameraViews */ConvertCamerasSystem$Wonderjs.convertToBasicCameraViews(gltf$3),
/* perspectiveCameraProjections */ConvertCamerasSystem$Wonderjs.convertToPerspectiveCameraProjections(gltf$3),
/* flyCameraControllers */ConvertCamerasSystem$Wonderjs.convertToFlyCameraControllers(gltf$3),
/* arcballCameraControllers */ConvertCamerasSystem$Wonderjs.convertToArcballCameraControllers(gltf$3),
/* transforms */transforms,
/* geometrys */ConvertGeometrysSystem$Wonderjs.convertToGeometrys(gltf$3),
/* meshRenderers */ConvertMeshRenderersSystem$Wonderjs.convertToMeshRenderers(indices[/* gameObjectIndices */0][/* geometryGameObjectIndexData */10], gltf$3),
/* basicMaterials */ConvertMaterialsSystem$Wonderjs.convertToBasicMaterials(gltf$3),
/* lightMaterials */ConvertMaterialsSystem$Wonderjs.convertToLightMaterials(gltf$3),
/* scripts */ConvertScriptsSystem$Wonderjs.convertToScripts(gltf$3)
]))
];
}
function _writeHeader(param, dataView) {
var __x = DataViewCommon$Wonderjs.writeUint32_1(1179937896, 0, dataView);
var __x$1 = DataViewCommon$Wonderjs.writeUint32_1(1, __x, dataView);
var __x$2 = DataViewCommon$Wonderjs.writeUint32_1(param[0], __x$1, dataView);
var __x$3 = DataViewCommon$Wonderjs.writeUint32_1(param[1], __x$2, dataView);
var __x$4 = DataViewCommon$Wonderjs.writeUint32_1(param[2], __x$3, dataView);
return DataViewCommon$Wonderjs.writeUint32_1(param[3], __x$4, dataView);
}
function _writeJson(byteOffset, param, dataView) {
return BufferUtils$Wonderjs.copyUint8ArrayToArrayBuffer(byteOffset, /* tuple */[
param[0],
param[1],
param[2]
], dataView);
}
function _getEmptyEncodedUint8Data(param) {
var encoder = new TextEncoder();
var emptyUint8DataArr = encoder.encode(" ");
return TypeArrayService$Wonderjs.getUint8_1(0, emptyUint8DataArr);
}
function _convertGLBToWDB(gltf, binBuffer) {
var match = _buildWDBJsonUint8Array(gltf);
var jsonUint8Array = match[2];
var streamChunkArr = match[1];
var bufferViewDataArr = match[0];
var jsonChunkByteLength = jsonUint8Array.byteLength;
var jsonChunkAlignedByteLength = BufferUtils$Wonderjs.alignedLength(jsonChunkByteLength);
var totalByteLength = ((BufferUtils$Wonderjs.getWDBHeaderTotalByteLength(/* () */0) + jsonChunkAlignedByteLength | 0) + BufferUtils$Wonderjs.alignedLength(ConvertStreamSystem$Wonderjs.getStreamChunkTotalByteLength(streamChunkArr)) | 0) + ConvertStreamSystem$Wonderjs.getBinBufferChunkTotalAlignedByteLength(bufferViewDataArr) | 0;
var wdb = new ArrayBuffer(totalByteLength);
var dataView = DataViewCommon$Wonderjs.create(wdb);
var byteOffset = _writeHeader(/* tuple */[
totalByteLength,
jsonChunkByteLength,
ConvertStreamSystem$Wonderjs.getStreamChunkTotalByteLength(streamChunkArr),
ConvertStreamSystem$Wonderjs.getBinBufferChunkTotalAlignedByteLength(bufferViewDataArr)
], dataView);
var emptyEncodedUint8Data = _getEmptyEncodedUint8Data(/* () */0);
var match$1 = _writeJson(byteOffset, /* tuple */[
emptyEncodedUint8Data,
jsonChunkAlignedByteLength,
jsonUint8Array
], dataView);
var match$2 = ConvertStreamSystem$Wonderjs.buildStreamChunk(match$1[0], streamChunkArr, match$1[2]);
ConvertStreamSystem$Wonderjs.buildBinBufferChunk(match$2[0], bufferViewDataArr, binBuffer, match$2[1]);
return wdb;
}
function _checkGLB(dataView) {
Contract$WonderLog.requireCheck((function (param) {
Contract$WonderLog.test(Log$WonderLog.buildAssertMessage("Source file to be a GLB (glTF Binary) model", "not"), (function (param) {
var match = DataViewCommon$Wonderjs.getUint32_1(0, dataView);
return Contract$WonderLog.Operators[/* = */0](match[0], 1179937895);
}));
var match = DataViewCommon$Wonderjs.getUint32_1(4, dataView);
var readVersion = match[0];
return Contract$WonderLog.test(Log$WonderLog.buildAssertMessage("Only GLB version 2 is supported", "Detected version: " + (String(readVersion) + "")), (function (param) {
return Contract$WonderLog.Operators[/* = */0](readVersion, 2);
}));
}), IsDebugMainService$Wonderjs.getIsDebug(StateDataMain$Wonderjs.stateData));
return dataView;
}
function _fixGLTF(gltf) {
var nodeMaxIndex = gltf[/* nodes */10].length - 1 | 0;
return /* record */[
/* asset */gltf[/* asset */0],
/* scenes */gltf[/* scenes */1].map((function (scene) {
return /* record */[
/* nodes */Js_option.map((function (nodes) {
return nodes.filter((function (nodeIndex) {
return nodeIndex <= nodeMaxIndex;
}));
}), scene[/* nodes */0]),
/* extensions */scene[/* extensions */1],
/* extras */scene[/* extras */2]
];
})),
/* scene */gltf[/* scene */2],
/* images */gltf[/* images */3],
/* textures */gltf[/* textures */4],
/* samplers */gltf[/* samplers */5],
/* buffers */gltf[/* buffers */6],
/* bufferViews */gltf[/* bufferViews */7],
/* accessors */gltf[/* accessors */8],
/* cameras */gltf[/* cameras */9],
/* nodes */gltf[/* nodes */10],
/* meshes */gltf[/* meshes */11],
/* materials */gltf[/* materials */12],
/* extensionsUsed */gltf[/* extensionsUsed */13],
/* extensions */gltf[/* extensions */14],
/* extras */gltf[/* extras */15]
];
}
function convertGLBData(gltf, binBuffer) {
return _convertGLBToWDB(_fixGLTF(ConvertGLTFJsonToRecordSystem$Wonderjs.convert(gltf)), binBuffer);
}
function convertGLB(glb) {
var match = BufferUtils$Wonderjs.decodeGLB(glb, _checkGLB);
return convertGLBData(JSON.parse(match[0]), match[1]);
}
exports._checkAndWarn = _checkAndWarn;
exports._buildWDBJsonUint8Array = _buildWDBJsonUint8Array;
exports._writeHeader = _writeHeader;
exports._writeJson = _writeJson;
exports._getEmptyEncodedUint8Data = _getEmptyEncodedUint8Data;
exports._convertGLBToWDB = _convertGLBToWDB;
exports._checkGLB = _checkGLB;
exports._fixGLTF = _fixGLTF;
exports.convertGLBData = convertGLBData;
exports.convertGLB = convertGLB;
/* Log-WonderLog Not a pure module */