import {unzipSync} from '../libs/fflate.module.js'; function loadProject(ProjectID, load_is){ console.log('[' + /\d\d\:\d\d\:\d\d/.exec(new Date())[0] + ']', "加载 ProjectID:", ProjectID) // 是否加载与缓存过 if (loadProject_Map[ProjectID] == undefined) { var httpRequest = new XMLHttpRequest();//第一步:建立所需的对象 httpRequest.open('GET', IOT3D_Url + '/Project/Get?ProjectID=' + ProjectID, true);//第二步:打开连接 将请求参数写在url中 ps:"./Ptest.php?name=test&nameone=testone" httpRequest.send();//第三步:发送请求 将请求参数写在URL中 httpRequest.onreadystatechange = function () { if (httpRequest.readyState == 4 && httpRequest.status == 200) { var json = JSON.parse(httpRequest.responseText);//获取到json字符串,还需解析 console.log(json); if (json.Code != 200) { console.log("ProjectID 错误!", ProjectID) return "ProjectID 错误!" } // 如果需要兼容低版本的浏览器,需要判断一下FileReader对象是否存在。 if (window.FileReader) { blobLoad(ProjectID, json.Data, load_is) } else { console.log('你的浏览器不支持读取文件'); loadProject_Map[ProjectID] = src } } }; } else { if (load_is) { console.log('[' + /\d\d\:\d\d\:\d\d/.exec(new Date())[0] + ']', "缓存加载 ProjectID:", ProjectID) f_load(ProjectID) } } } // 解压并更新舞台 function f_load(ProjectID) { console.log('[' + /\d\d\:\d\d\:\d\d/.exec(new Date())[0] + ']', "开始加载 file:", loadProject_Map[ProjectID]) // 解压模型 // var promise = fetch('../static/16635717199e4e03e8-8850-4152-9c08-9c203c882f7a.zip') var promise = fetch(loadProject_Map[ProjectID]) .then((d) => d.arrayBuffer()) promise = promise.then(function (data) { //响应的内容 console.log("data:", data); const decompressed = unzipSync(new Uint8Array(data), { // You may optionally supply a filter for files. By default, all files in a // ZIP archive are extracted, but a filter can save resources by telling // the library not to decompress certain files filter(file) { // Don't decompress the massive image or any files larger than 10 MiB return file; } }); console.log('[' + /\d\d\:\d\d\:\d\d/.exec(new Date())[0] + ']', "file:", decompressed['app.json']) var obj = JSON.parse(new TextDecoder().decode(decompressed['app.json'])); loadJson(obj); self.inte(); self.setSize(window.innerWidth, window.innerHeight); console.log('[' + /\d\d\:\d\d\:\d\d/.exec(new Date())[0] + ']', "JSON obj:", obj) }).catch(function (err) { console.log(err); }) } // 文件缓存本地 function blobLoad(ProjectID, src, load_is) { console.log('[' + /\d\d\:\d\d\:\d\d/.exec(new Date())[0] + ']', "文件缓存本地 :", ProjectID) // let self = this; const req = new XMLHttpRequest(); req.open("GET", src, true); req.responseType = "blob"; req.onload = function () { // Onload is triggered even on 404 // so we need to check the status code if (this.status === 200) { const videoBlob = this.response; console.log("videoBlob:", videoBlob) const blobSrc = URL.createObjectURL(videoBlob); // IE10+ console.log("blobSrc:", blobSrc) loadProject_Map[ProjectID] = blobSrc console.log('[' + /\d\d\:\d\d\:\d\d/.exec(new Date())[0] + ']', "文件缓存本地完成 ProjectID:", ProjectID) if (load_is) { f_load(ProjectID) } } }; //监听进度事件 req.addEventListener("progress", function (evt) { if (evt.lengthComputable) { var percentComplete = evt.loaded / evt.total; //进度 console.log('[' + /\d\d\:\d\d\:\d\d/.exec(new Date())[0] + ']', "ProjectID:", ProjectID, " 进度:", (percentComplete * 100) + "%") } }, false); req.onerror = function () { // Error console.log("blobLoad Error!", src) loadProject_Map[ProjectID] = src }; req.send(); } // 导入 json function loadJson(json) { let loader = new THREE.ObjectLoader(); // 加载 console.log("json:", json) var project = json.project; if (project.vr !== undefined) renderer.xr.enabled = project.vr; if (project.shadows !== undefined) renderer.shadowMap.enabled = project.shadows; if (project.shadowType !== undefined) renderer.shadowMap.type = project.shadowType; if (project.toneMapping !== undefined) renderer.toneMapping = project.toneMapping; if (project.toneMappingExposure !== undefined) renderer.toneMappingExposure = project.toneMappingExposure; if (project.physicallyCorrectLights !== undefined) renderer.physicallyCorrectLights = project.physicallyCorrectLights; self.setScene(loader.parse(json.scene)); self.setCamera(loader.parse(json.camera)); events = { init: [], start: [], stop: [], keydown: [], keyup: [], pointerdown: [], pointerup: [], pointermove: [], update: [] }; var scriptWrapParams = 'IOT3D,renderer,scene,camera'; var scriptWrapResultObj = {}; for (var eventKey in events) { scriptWrapParams += ',' + eventKey; scriptWrapResultObj[eventKey] = eventKey; } var scriptWrapResult = JSON.stringify(scriptWrapResultObj).replace(/\"/g, ''); //执行代码 console.log("json.scripts:", json.scripts) for (var uuid in json.scripts) { var object = scene.getObjectByProperty('uuid', uuid, true); if (object === undefined) { console.warn('APP.IOT3D: Script without object.', uuid); continue; } var scripts = json.scripts[uuid]; for (var i = 0; i < scripts.length; i++) { var script = scripts[i]; var functions = (new Function(scriptWrapParams, script.source + '\nreturn ' + scriptWrapResult + ';').bind(object))(this, renderer, scene, camera); console.log("functions:", functions) for (var name in functions) { if (functions[name] === undefined) continue; if (events[name] === undefined) { console.warn('APP.IOT3D: Event type not supported (', name, ')'); continue; } events[name].push(functions[name].bind(object)); } } } console.log("events:", events) dispatch(events.init, arguments); }; export { loadProject,f_load,blobLoad,loadJson };