InitPage.js 7.9 KB

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