IOT3D_Lib.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. import {unzipSync} from '../libs/fflate.module.js';
  2. function loadProject(ProjectID, load_is){
  3. console.log('[' + /\d\d\:\d\d\:\d\d/.exec(new Date())[0] + ']', "加载 ProjectID:", ProjectID)
  4. // 是否加载与缓存过
  5. if (loadProject_Map[ProjectID] == undefined) {
  6. var httpRequest = new XMLHttpRequest();//第一步:建立所需的对象
  7. httpRequest.open('GET', IOT3D_Url + '/Project/Get?ProjectID=' + ProjectID, true);//第二步:打开连接 将请求参数写在url中 ps:"./Ptest.php?name=test&nameone=testone"
  8. httpRequest.send();//第三步:发送请求 将请求参数写在URL中
  9. httpRequest.onreadystatechange = function () {
  10. if (httpRequest.readyState == 4 && httpRequest.status == 200) {
  11. var json = JSON.parse(httpRequest.responseText);//获取到json字符串,还需解析
  12. console.log(json);
  13. if (json.Code != 200) {
  14. console.log("ProjectID 错误!", ProjectID)
  15. return "ProjectID 错误!"
  16. }
  17. // 如果需要兼容低版本的浏览器,需要判断一下FileReader对象是否存在。
  18. if (window.FileReader) {
  19. blobLoad(ProjectID, json.Data, load_is)
  20. } else {
  21. console.log('你的浏览器不支持读取文件');
  22. loadProject_Map[ProjectID] = src
  23. }
  24. }
  25. };
  26. } else {
  27. if (load_is) {
  28. console.log('[' + /\d\d\:\d\d\:\d\d/.exec(new Date())[0] + ']', "缓存加载 ProjectID:", ProjectID)
  29. f_load(ProjectID)
  30. }
  31. }
  32. }
  33. // 解压并更新舞台
  34. function f_load(ProjectID) {
  35. console.log('[' + /\d\d\:\d\d\:\d\d/.exec(new Date())[0] + ']', "开始加载 file:", loadProject_Map[ProjectID])
  36. // 解压模型
  37. // var promise = fetch('../static/16635717199e4e03e8-8850-4152-9c08-9c203c882f7a.zip')
  38. var promise = fetch(loadProject_Map[ProjectID])
  39. .then((d) => d.arrayBuffer())
  40. promise = promise.then(function (data) {
  41. //响应的内容
  42. console.log("data:", data);
  43. const decompressed = unzipSync(new Uint8Array(data), {
  44. // You may optionally supply a filter for files. By default, all files in a
  45. // ZIP archive are extracted, but a filter can save resources by telling
  46. // the library not to decompress certain files
  47. filter(file) {
  48. // Don't decompress the massive image or any files larger than 10 MiB
  49. return file;
  50. }
  51. });
  52. console.log('[' + /\d\d\:\d\d\:\d\d/.exec(new Date())[0] + ']', "file:", decompressed['app.json'])
  53. var obj = JSON.parse(new TextDecoder().decode(decompressed['app.json']));
  54. loadJson(obj);
  55. self.inte();
  56. self.setSize(window.innerWidth, window.innerHeight);
  57. console.log('[' + /\d\d\:\d\d\:\d\d/.exec(new Date())[0] + ']', "JSON obj:", obj)
  58. }).catch(function (err) {
  59. console.log(err);
  60. })
  61. }
  62. // 文件缓存本地
  63. function blobLoad(ProjectID, src, load_is) {
  64. console.log('[' + /\d\d\:\d\d\:\d\d/.exec(new Date())[0] + ']', "文件缓存本地 :", ProjectID)
  65. // let self = this;
  66. const req = new XMLHttpRequest();
  67. req.open("GET", src, true);
  68. req.responseType = "blob";
  69. req.onload = function () {
  70. // Onload is triggered even on 404
  71. // so we need to check the status code
  72. if (this.status === 200) {
  73. const videoBlob = this.response;
  74. console.log("videoBlob:", videoBlob)
  75. const blobSrc = URL.createObjectURL(videoBlob); // IE10+
  76. console.log("blobSrc:", blobSrc)
  77. loadProject_Map[ProjectID] = blobSrc
  78. console.log('[' + /\d\d\:\d\d\:\d\d/.exec(new Date())[0] + ']', "文件缓存本地完成 ProjectID:", ProjectID)
  79. if (load_is) {
  80. f_load(ProjectID)
  81. }
  82. }
  83. };
  84. //监听进度事件
  85. req.addEventListener("progress", function (evt) {
  86. if (evt.lengthComputable) {
  87. var percentComplete = evt.loaded / evt.total;
  88. //进度
  89. console.log('[' + /\d\d\:\d\d\:\d\d/.exec(new Date())[0] + ']', "ProjectID:", ProjectID, " 进度:", (percentComplete * 100) + "%")
  90. }
  91. }, false);
  92. req.onerror = function () {
  93. // Error
  94. console.log("blobLoad Error!", src)
  95. loadProject_Map[ProjectID] = src
  96. };
  97. req.send();
  98. }
  99. // 导入 json
  100. function loadJson(json) {
  101. let loader = new THREE.ObjectLoader(); // 加载
  102. console.log("json:", json)
  103. var project = json.project;
  104. if (project.vr !== undefined) renderer.xr.enabled = project.vr;
  105. if (project.shadows !== undefined) renderer.shadowMap.enabled = project.shadows;
  106. if (project.shadowType !== undefined) renderer.shadowMap.type = project.shadowType;
  107. if (project.toneMapping !== undefined) renderer.toneMapping = project.toneMapping;
  108. if (project.toneMappingExposure !== undefined) renderer.toneMappingExposure = project.toneMappingExposure;
  109. if (project.physicallyCorrectLights !== undefined) renderer.physicallyCorrectLights = project.physicallyCorrectLights;
  110. self.setScene(loader.parse(json.scene));
  111. self.setCamera(loader.parse(json.camera));
  112. events = {
  113. init: [],
  114. start: [],
  115. stop: [],
  116. keydown: [],
  117. keyup: [],
  118. pointerdown: [],
  119. pointerup: [],
  120. pointermove: [],
  121. update: []
  122. };
  123. var scriptWrapParams = 'IOT3D,renderer,scene,camera';
  124. var scriptWrapResultObj = {};
  125. for (var eventKey in events) {
  126. scriptWrapParams += ',' + eventKey;
  127. scriptWrapResultObj[eventKey] = eventKey;
  128. }
  129. var scriptWrapResult = JSON.stringify(scriptWrapResultObj).replace(/\"/g, '');
  130. //执行代码
  131. console.log("json.scripts:", json.scripts)
  132. for (var uuid in json.scripts) {
  133. var object = scene.getObjectByProperty('uuid', uuid, true);
  134. if (object === undefined) {
  135. console.warn('APP.IOT3D: Script without object.', uuid);
  136. continue;
  137. }
  138. var scripts = json.scripts[uuid];
  139. for (var i = 0; i < scripts.length; i++) {
  140. var script = scripts[i];
  141. var functions = (new Function(scriptWrapParams, script.source + '\nreturn ' + scriptWrapResult + ';').bind(object))(this, renderer, scene, camera);
  142. console.log("functions:", functions)
  143. for (var name in functions) {
  144. if (functions[name] === undefined) continue;
  145. if (events[name] === undefined) {
  146. console.warn('APP.IOT3D: Event type not supported (', name, ')');
  147. continue;
  148. }
  149. events[name].push(functions[name].bind(object));
  150. }
  151. }
  152. }
  153. console.log("events:", events)
  154. dispatch(events.init, arguments);
  155. };
  156. export { loadProject,f_load,blobLoad,loadJson };