InitPage.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. var camera, cameraOrtho, scene, sceneOrtho, renderer, orbitCtl, cube, distance;
  2. var width, height, depth;
  3. var languageContent;
  4. initPage();
  5. function initPage() {
  6. // 解析的url后缀 e.g.:
  7. // ?={"Load":"加载中...","RotateR":"旋转-","RotateL":"旋转+","Move":"移动","Clip":"切割","QuickClip":"AI切割","Delete":"删除","Reset":"重置"}
  8. // let url = location.href;
  9. // let language = decodeURI(
  10. // url.substr(url.indexOf("=") + 1, url.length - url.indexOf("="))
  11. // );
  12. // console.log(language);
  13. // languageContent = JSON.parse(language);
  14. languageContent = {
  15. Load: "加载中...",
  16. RotateR: "旋转-",
  17. RotateL: "旋转+",
  18. Move: "移动",
  19. Clip: "切割",
  20. QuickClip: "AI切割",
  21. Delete: "删除",
  22. Reset: "重置",
  23. };
  24. var text = document.getElementById("loadText");
  25. text.innerHTML = languageContent.Load;
  26. var infoText = document.getElementById("infoText");
  27. infoText.innerHTML = languageContent.QuickClipFail;
  28. OSHelper.init();
  29. scene = new THREE.Scene();
  30. sceneOrtho = new THREE.Scene();
  31. scene.add(new THREE.AmbientLight(0xffffff));
  32. // Renderer
  33. renderer = new THREE.WebGLRenderer({ antialias: true });
  34. renderer.setClearColor(0x373737, 1.0);
  35. renderer.setPixelRatio(window.devicePixelRatio);
  36. renderer.setSize(window.innerWidth, window.innerHeight);
  37. renderer.autoClear = false;
  38. window.addEventListener("resize", onWindowResize, false);
  39. document.body.appendChild(renderer.domElement);
  40. renderer.localClippingEnabled = true;
  41. SpriteManager.init();
  42. LinesDrawingManager.initLine();
  43. // Controls
  44. camera = new THREE.PerspectiveCamera(
  45. 45,
  46. window.innerWidth / window.innerHeight,
  47. 20,
  48. 20000
  49. );
  50. //camera = new THREE.PerspectiveCamera(30, window.innerWidth / window.innerHeight, 20, 20000);
  51. camera.up.set(0, 0, 1);
  52. cameraOrtho = new THREE.OrthographicCamera(
  53. -window.innerWidth / 2,
  54. window.innerWidth / 2,
  55. window.innerHeight / 2,
  56. -window.innerHeight / 2,
  57. 1,
  58. 10
  59. );
  60. cameraOrtho.position.z = 10;
  61. orbitCtl = new THREE.OrbitControls(camera, renderer.domElement);
  62. orbitCtl.enablePan = false;
  63. animate();
  64. initClipArgs();
  65. // 触发方式 setSurface(100,100,100,"../3DWeb/FaceImage")
  66. EventCenter.addEventHandler("SetSurface", createCube);
  67. EventCenter.addEventHandler("ChangeSurface", changeCube);
  68. EventCenter.addEventHandler("ResetCubeImage", resetCubeImage);
  69. EventCenter.addEventHandler("RemoveCube", removeCube);
  70. }
  71. function createCube(surfaceInfo) {
  72. SpriteManager.allowClip = false;
  73. //remove old
  74. removeCube();
  75. //create new
  76. width = surfaceInfo.cubeWidth;
  77. height = surfaceInfo.cubeHeight;
  78. depth = surfaceInfo.cubeDepth;
  79. let surfacePaths = [];
  80. for (let i = 1; i <= 6; i++) {
  81. surfacePaths.push(surfaceInfo.surfaceFolder + "/" + i + ".jpg");
  82. }
  83. let materials = [];
  84. let textures = loadSurface(surfacePaths);
  85. textures.forEach((texture) => {
  86. materials.push(
  87. new THREE.MeshPhongMaterial({ map: texture, clippingPlanes: [] })
  88. );
  89. });
  90. let geometry = new THREE.BoxGeometry(width, height, depth);
  91. cube = new THREE.Mesh(geometry, materials);
  92. cube.position.set(width / 2, height / 2, -depth / 2);
  93. scene.add(cube);
  94. resetCubePosition();
  95. EdgesManager.edgesNeedUpdate = true;
  96. }
  97. function changeCube(surfaceInfo) {
  98. SpriteManager.allowClip = false;
  99. //remove old
  100. removeCube();
  101. //create new
  102. width = surfaceInfo.cubeWidth;
  103. height = surfaceInfo.cubeHeight;
  104. depth = surfaceInfo.cubeDepth;
  105. let surfacePaths = [];
  106. for (const surface of surfaceInfo.surfacesList) {
  107. surfacePaths.push(surface);
  108. }
  109. let materials = [];
  110. let textures = loadBase64Surface(surfacePaths);
  111. textures.forEach((texture) => {
  112. materials.push(
  113. new THREE.MeshPhongMaterial({ map: texture, clippingPlanes: [] })
  114. );
  115. });
  116. let geometry = new THREE.BoxGeometry(width, height, depth);
  117. cube = new THREE.Mesh(geometry, materials);
  118. cube.position.set(width / 2, height / 2, -depth / 2);
  119. scene.add(cube);
  120. resetCubePosition();
  121. EdgesManager.edgesNeedUpdate = true;
  122. }
  123. function resetCubeImage(surfaceFolder) {
  124. let surfacePaths = [];
  125. for (let i = 1; i <= 6; i++) {
  126. surfacePaths.push(surfaceFolder + "/" + i + ".jpg");
  127. }
  128. if (surfacePaths.length > 0) {
  129. loadSurface(surfacePaths, (texture, number) => {
  130. if (cube.material[number] != undefined) {
  131. texture.minFilter = texture.magFilter = THREE.LinearFilter;
  132. texture.mapping = THREE.UVMapping;
  133. cube.material[number].map.dispose();
  134. cube.material[number].map = texture;
  135. }
  136. });
  137. cube.material.needsUpdate = true;
  138. }
  139. }
  140. function removeCube() {
  141. if (cube != undefined) {
  142. SpriteManager.waitMdlFileLoading();
  143. SpriteManager.resetChecked();
  144. cube.material.forEach((material) => {
  145. if (material.map != undefined) {
  146. material.map.dispose();
  147. }
  148. material.dispose();
  149. });
  150. cube.geometry.dispose();
  151. scene.remove(cube);
  152. }
  153. }
  154. function resetCubePosition() {
  155. camera.position.set(width * 1.2, -width * 1.8, depth / 2);
  156. orbitCtl.target = cube.position;
  157. orbitCtl.update();
  158. orbitCtl.maxDistance = width * OSHelper.orbitCtlDistanceMultiple;
  159. orbitCtl.minDistance = width * (OSHelper.orbitCtlDistanceMultiple - 1);
  160. }
  161. function loadSurface(images, callback) {
  162. var textures = [];
  163. for (let i = 0; i < images.length; i++) {
  164. textures[i] = LoaderUtility.loadUniqueTexture(images[i], (texture) => {
  165. texture.minFilter = texture.magFilter = THREE.LinearFilter;
  166. texture.mapping = THREE.UVMapping;
  167. texture.center.set(0.5, 0.5);
  168. if (i === 0) {
  169. texture.rotation = 1.57;
  170. }
  171. if (i === 1) {
  172. texture.rotation = -1.57;
  173. }
  174. if (i === 5) {
  175. texture.rotation = 3.14;
  176. }
  177. if (callback != undefined) {
  178. callback(texture, i);
  179. }
  180. });
  181. }
  182. return textures;
  183. }
  184. function loadBase64Surface(images, callback) {
  185. var textures = [];
  186. for (let i = 0; i < images.length; i++) {
  187. textures[i] = LoaderUtility.loadBase64Texture(images[i], (texture) => {
  188. texture.minFilter = texture.magFilter = THREE.LinearFilter;
  189. texture.mapping = THREE.UVMapping;
  190. texture.center.set(0.5, 0.5);
  191. if (i === 0) {
  192. texture.rotation = 1.57;
  193. }
  194. if (i === 1) {
  195. texture.rotation = -1.57;
  196. }
  197. if (i === 5) {
  198. texture.rotation = 3.14;
  199. }
  200. if (callback != undefined) {
  201. callback(texture, i);
  202. }
  203. });
  204. }
  205. return textures;
  206. }
  207. function onWindowResize() {
  208. camera.aspect = window.innerWidth / window.innerHeight;
  209. camera.updateProjectionMatrix();
  210. cameraOrtho.left = -window.innerWidth / 2;
  211. cameraOrtho.right = window.innerWidth / 2;
  212. cameraOrtho.top = window.innerHeight / 2;
  213. cameraOrtho.bottom = -window.innerHeight / 2;
  214. cameraOrtho.updateProjectionMatrix();
  215. SpriteManager.resetSpritePosition();
  216. renderer.setSize(window.innerWidth, window.innerHeight);
  217. }
  218. function animate() {
  219. renderer.clear();
  220. renderer.render(scene, camera);
  221. renderer.clearDepth();
  222. renderer.render(sceneOrtho, cameraOrtho);
  223. requestAnimationFrame(animate);
  224. }