overlay.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. var MathLight = {};
  2. MathLight.RADIANS_PER_DEGREE = Math.PI / 180;
  3. MathLight.DEGREES_PER_RADIAN = 180 / Math.PI;
  4. window.initOverlay = function(THREE) {
  5. var _planeGeometry = new THREE.PlaneGeometry(
  6. settings.overlay.width,
  7. settings.overlay.height,
  8. 1,
  9. 1
  10. );
  11. var _boxGeometry = new THREE.BoxBufferGeometry(
  12. settings.overlay.width,
  13. settings.overlay.height,
  14. settings.overlay.depth
  15. );
  16. //ie的mesh 加了polygonOffset也是会重叠。所以去掉前面的face: (但是突然ie又播放不了videoTexture)
  17. var newIndex = [..._boxGeometry.index.array];
  18. newIndex.splice(4 * 6, 6);
  19. _boxGeometry.setIndex(
  20. new THREE.BufferAttribute(new Uint16Array(newIndex), 1)
  21. );
  22. var _boxMat = new THREE.MeshBasicMaterial({
  23. //MeshStandardMaterial
  24. color: "#eeeeee",
  25. transparent: !0,
  26. opacity: 0.8,
  27. });
  28. var overlayGroup = new THREE.Object3D();
  29. player.model.add(overlayGroup);
  30. overlayGroup.name = "overlayGroup";
  31. player.overlayGroup = overlayGroup;
  32. var Overlay = function(info) {
  33. THREE.Object3D.call(this);
  34. this.sid = info.sid;
  35. if (info.media) this.preDeal(info);
  36. this.build(info);
  37. this.name = "overlay_" + this.sid;
  38. };
  39. Overlay.prototype = Object.create(THREE.Object3D.prototype);
  40. Overlay.prototype.build = function(info) {
  41. var plane = new THREE.Mesh(
  42. _planeGeometry,
  43. new THREE.MeshBasicMaterial({
  44. //MeshStandardMaterial
  45. color: "#00c8af",
  46. opacity: 0.4,
  47. transparent: !0,
  48. polygonOffset: true,
  49. //是否开启多边形偏移 //ie不开启时blank也不会闪烁
  50. polygonOffsetFactor: -0.9,
  51. //多边形偏移因子
  52. polygonOffsetUnits: -4.0,
  53. //多边形偏移单位
  54. })
  55. );
  56. plane.renderOrder = 3;
  57. this.add(plane);
  58. this.plane = plane;
  59. if (info.hasBox) {
  60. this.addBox(true);
  61. }
  62. overlayGroup.add(this);
  63. if (info.media) {
  64. if (info.media.includes("video")) {
  65. //var id = "video"+ this.sid id="${ id }"
  66. var video = $(
  67. `<video controls="controls" loop autoplay x5-playsinline="" webkit-playsinline="true" playsinline="true" controlslist="nodownload"></video>`
  68. )[0];
  69. video.setAttribute("crossOrigin", "Anonymous");
  70. //要在src设置好前解决跨域
  71. $(video).on("contextmenu", function() {
  72. return false;
  73. });
  74. //禁止右键点击出
  75. video.src = manage.dealURL(info.file);
  76. info.media = video;
  77. info.type = "video";
  78. /* video.addEventListener('loadeddata', ()=>{
  79. console.log(this.sid + " loaded!!!")
  80. }) */
  81. video.volume = 0;
  82. video.muted = true;
  83. plane.material.opacity = 1;
  84. } else if (info.media.includes("photo")) {
  85. /* var img = new Image();
  86. img.src = manage.dealURL(info.file) //"https://4dkk.4dage.com/images/images"+Config.projectNum+"/overlay"+this.sid+".jpg?m="+new Date().getTime()
  87. info.media = img
  88. */
  89. info.type = "photo";
  90. plane.material.opacity = 0.1;
  91. }
  92. plane.material.color = new THREE.Color(1, 1, 1);
  93. }
  94. if (info.width == void 0) info.width = settings.overlay.width;
  95. if (info.height == void 0) info.height = settings.overlay.height;
  96. this.setFromInfo(info);
  97. this.fileSrc = info.file;
  98. };
  99. Overlay.prototype.setFromInfo = function(info) {
  100. //1 恢复到编辑之前 2 初始加载
  101. var plane = this.plane;
  102. this.transformAtPanos = info.transformAtPanos || {};
  103. //在每个漫游点独立设置的position。
  104. //var curPanoTransform = player.currentPano && this.transformAtPanos[player.currentPano.id] || {}
  105. var curPanoTransform = this.transformAtPanos[this.getTransformSid()] || {};
  106. info.depth && this.scale.setZ(info.depth / settings.overlay.depth);
  107. this.posCustom = info.pos ? info.pos.clone() : this.position.clone();
  108. //没有单独设置position的漫游点使用的position
  109. this.position.copy(curPanoTransform.pos || this.posCustom);
  110. this.quaCustom = info.qua ? info.qua.clone() : this.quaternion.clone();
  111. this.quaternion.copy(curPanoTransform.qua || this.quaCustom);
  112. this.widthCustom = info.width;
  113. this.heightCustom = info.height;
  114. this.width = curPanoTransform.width || this.widthCustom;
  115. this.height = curPanoTransform.height || this.heightCustom;
  116. var a = this.getScaleBySize(this.width, this.height);
  117. this.scale.setX(a.x);
  118. this.scale.setY(a.y);
  119. if (info.type) {
  120. if (!plane.material.map) {
  121. if (info.type == "video") {
  122. plane.material.map = new THREE.VideoTexture(info.media);
  123. this.hasRequestLoad = true;
  124. plane.material.map.wrapS = plane.material.map.wrapT =
  125. THREE.ClampToEdgeWrapping;
  126. plane.material.map.minFilter = THREE.LinearFilter;
  127. plane.material.map.magFilter = THREE.LinearFilter;
  128. plane.material.map.generateMipmaps = true;
  129. } else {
  130. this._loadDones = [];
  131. /* plane.material.map = Texture.load(info.file,()=>{
  132. if(this._loadDones){
  133. this._loadDones.forEach(e=>e())
  134. this._loadDones = null
  135. }
  136. }) */
  137. }
  138. /* plane.material.map.wrapS = plane.material.map.wrapT = THREE.ClampToEdgeWrapping;
  139. plane.material.map.minFilter = THREE.LinearFilter;
  140. plane.material.map.magFilter = THREE.LinearFilter;
  141. plane.material.map.generateMipmaps = true; */
  142. } else plane.material.map.image = info.media;
  143. this.file = info.file;
  144. }
  145. this.overlayType = info.type;
  146. if (!!this.hasBox != !!info.hasBox) {
  147. this.addBox(!this.hasBox);
  148. }
  149. this.updateMatrixWorld();
  150. this.getVisiblePanos();
  151. };
  152. Overlay.prototype.getTransformSid = function() {
  153. var name;
  154. if (player.mode == "panorama") {
  155. name = player.currentPano ? player.currentPano.id : "outside";
  156. } else {
  157. name = "outside";
  158. }
  159. return name;
  160. };
  161. Overlay.prototype.addBox = function(state) {
  162. if (state == !!this.hasBox) {
  163. return;
  164. }
  165. if (state) {
  166. var box = new THREE.Mesh(_boxGeometry, _boxMat);
  167. box.position.set(0, 0, settings.overlay.depth / 2);
  168. box.renderOrder = 3;
  169. this.plane.position.set(0, 0, settings.overlay.depth);
  170. this.add(box);
  171. this.box = box;
  172. } else {
  173. this.plane.position.set(0, 0, 0);
  174. this.remove(this.box);
  175. this.box = null;
  176. }
  177. this.hasBox = state;
  178. };
  179. Overlay.prototype.getSizeByScale = function() {
  180. return {
  181. width: settings.overlay.width * this.scale.x,
  182. height: settings.overlay.height * this.scale.y,
  183. };
  184. };
  185. Overlay.prototype.getScaleBySize = function(width, height) {
  186. return {
  187. x: width / settings.overlay.width,
  188. y: height / settings.overlay.height,
  189. };
  190. };
  191. Overlay.prototype.preDeal = function(info) {
  192. info.pos = new THREE.Vector3().fromArray(info.pos);
  193. info.qua = new THREE.Quaternion().fromArray(info.qua);
  194. info.width = parseFloat(info.width);
  195. info.height = parseFloat(info.height);
  196. info.depth = parseFloat(info.depth);
  197. info.hasBox = parseInt(info.hasBox);
  198. info.pos.x = parseFloat(info.pos.x);
  199. info.pos.y = parseFloat(info.pos.y);
  200. info.pos.z = parseFloat(info.pos.z);
  201. info.qua.x = parseFloat(info.qua.x);
  202. info.qua.y = parseFloat(info.qua.y);
  203. info.qua.z = parseFloat(info.qua.z);
  204. info.qua.w = parseFloat(info.qua.w);
  205. if (!info.transformAtPanos) info.transformAtPanos = {};
  206. for (let i in info.transformAtPanos) {
  207. info.transformAtPanos[i].pos = new THREE.Vector3().fromArray(
  208. info.transformAtPanos[i].pos
  209. );
  210. info.transformAtPanos[i].qua = new THREE.Quaternion().fromArray(
  211. info.transformAtPanos[i].qua
  212. );
  213. }
  214. };
  215. Overlay.prototype.getVisiblePanos = function() {
  216. this.visiblePanos = common.getVisiblePano(this.plane.getWorldPosition(), {
  217. model: null,
  218. });
  219. };
  220. Overlay.prototype.updateVisibles = function(panos) {
  221. if (settings.isEdit && EditOverlay.editPlane == this) {
  222. return true;
  223. }
  224. this.visible = !!panos.find((pano) => this.visiblePanos.includes(pano));
  225. if (!this.visible && this.overlayType == "video") this.videoControl("stop");
  226. };
  227. Overlay.updateVisibles = function(panos) {
  228. if (panos === true) {
  229. player.overlayGroup.children.forEach((e) => (e.visible = true));
  230. } else {
  231. player.overlayGroup.children.forEach((e) => e.updateVisibles(panos));
  232. }
  233. };
  234. Overlay.prototype.videoControl = function(state) {
  235. if (this.overlayType != "video") return;
  236. if (!state || state == "stop") {
  237. this.plane.material.map.image.paused ||
  238. this.plane.material.map.image.pause();
  239. if (state == "stop") {
  240. this.plane.material.map.image.currentTime = 0;
  241. }
  242. //console.log("pause")
  243. } else if (state) {
  244. this.plane.material.map.image.paused &&
  245. this.plane.material.map.image.play();
  246. //console.log("play")
  247. }
  248. };
  249. Overlay.prototype.inSight = function() {
  250. if (player.mode == "panorama") {
  251. var position = this.plane.getWorldPosition();
  252. var pos2d = math.getPos2d(position, player.camera, $("#player")[0]);
  253. if (pos2d.trueSide && pos2d.inSight) {
  254. return true;
  255. } else {
  256. var cornerPoint = [
  257. new THREE.Vector3(
  258. -settings.overlay.width / 2,
  259. settings.overlay.height / 2,
  260. 0
  261. ),
  262. new THREE.Vector3(
  263. settings.overlay.width / 2,
  264. settings.overlay.height / 2,
  265. 0
  266. ),
  267. new THREE.Vector3(
  268. settings.overlay.width / 2,
  269. -settings.overlay.height / 2,
  270. 0
  271. ),
  272. new THREE.Vector3(
  273. -settings.overlay.width / 2,
  274. -settings.overlay.height / 2,
  275. 0
  276. ),
  277. ];
  278. for (var i = 0; i < 4; i++) {
  279. cornerPoint[i].applyMatrix4(this.plane.matrixWorld);
  280. var pos2d = math.getPos2d(
  281. cornerPoint[i],
  282. player.camera,
  283. $("#player")[0]
  284. );
  285. if (pos2d.trueSide && pos2d.inSight) {
  286. return true;
  287. }
  288. }
  289. }
  290. } else {
  291. return true;
  292. }
  293. };
  294. Overlay.prototype.addToLoadQueue = function() {
  295. if (this.overlayType == "photo") {
  296. Overlay.loadQueue.includes(this) || Overlay.loadQueue.push(this);
  297. }
  298. };
  299. Overlay.prototype.requestDownload = function() {
  300. if (this.hasRequestLoad || this.overlayType != "photo") return;
  301. console.log("overlay beginDownload : " + this.sid);
  302. var plane = this.plane;
  303. plane.material.map = Texture.load(this.file, () => {
  304. plane.material.needsUpdate = true;
  305. if (this._loadDones) {
  306. this._loadDones.forEach((e) => e());
  307. this._loadDones = null;
  308. }
  309. setTimeout(Overlay.loadNext, 50);
  310. plane.material.opacity = 1;
  311. console.log("overlay loaded: " + this.sid);
  312. });
  313. plane.material.map.wrapS = plane.material.map.wrapT =
  314. THREE.ClampToEdgeWrapping;
  315. plane.material.map.minFilter = THREE.LinearFilter;
  316. plane.material.map.magFilter = THREE.LinearFilter;
  317. plane.material.map.generateMipmaps = true;
  318. this.hasRequestLoad = true;
  319. };
  320. Overlay.loadQueue = []; //等待下载的overlay,目前只针对photo
  321. Overlay.maxLoadingCount = 3; //同时正在load图片的数量
  322. Overlay.loadNext = () => {
  323. //继续requestDownload loadQueue中前排的item
  324. var loadings = player.overlayGroup.children.filter(
  325. (e) => e.hasRequestLoad && e._loadDones
  326. ); //开始下载了但是没加载好的
  327. Overlay.loadQueue
  328. .slice(0, Overlay.maxLoadingCount - loadings.length)
  329. .forEach((e) => e.requestDownload());
  330. Overlay.loadQueue.splice(0, Overlay.maxLoadingCount - loadings.length);
  331. };
  332. Overlay.getNeedLoad = function() {
  333. //计算获取loadQueue,每次都重新计算,覆盖旧的
  334. if (!player || !player.domElement || !player.mode) return;
  335. if (player.mode != "panorama") {
  336. if (!Overlay.loadWhenOutside) return;
  337. if (Overlay.loadQueue.length == 0) {
  338. Overlay.loadQueue = player.overlayGroup.children
  339. .filter((e) => !e.hasRequestLoad)
  340. .slice(0, 5);
  341. }
  342. return;
  343. }
  344. Overlay.loadWhenOutside = true;
  345. var overlays = player.overlayGroup.children.filter(
  346. (e) => !e.hasRequestLoad && e.visiblePanos.includes(player.currentPano)
  347. );
  348. //var maxAngle = THREE.Math.degToRad( cameraLight.getHFOVFromVFOV(70, player.domElement.clientWidth, app.player.domElement.clientHeight) / 2);
  349. var cameraDir = player.getDirection();
  350. /* var maxCount = 5;
  351. if(overlays.length>maxCount){
  352. for(var i=0;i<overlays.length;i++){
  353. //角度为可见范围
  354. var v1 = cameraDir.clone().setY(0);
  355. var v2 = overlays[i].plane.getWorldPosition().sub(player.position).setY(0)
  356. if(v1.angleTo(v2) <= maxAngle){
  357. Overlay.loadQueue.push(overlays[i])
  358. if(Overlay.loadQueue.length>=10) break;
  359. }
  360. }
  361. if(Overlay.loadQueue.length<Overlay.maxLoadingCount){
  362. Overlay.loadQueue.push()
  363. }
  364. }else{ */
  365. Overlay.loadQueue = overlays;
  366. //}
  367. var request = [
  368. (overlay) => {
  369. return true;
  370. },
  371. ];
  372. var rank = [
  373. (overlay) => {
  374. var dis = overlay.plane.getWorldPosition().distanceTo(player.position);
  375. return -dis;
  376. },
  377. (overlay) => {
  378. var tagDir = overlay.plane.getWorldPosition().sub(player.position);
  379. var angle = tagDir.angleTo(cameraDir);
  380. return -angle * 20;
  381. },
  382. ];
  383. var result = common.sortByScore(Overlay.loadQueue, request, rank);
  384. Overlay.loadQueue = result
  385. ? result.slice(0, 5).map((e) => e.item)
  386. : player.overlayGroup.children
  387. .filter((e) => !e.hasRequestLoad)
  388. .slice(0, 2);
  389. };
  390. Overlay.load = () => {
  391. //开始下载图片
  392. Overlay.getNeedLoad();
  393. Overlay.loadNext();
  394. var unloads = player.overlayGroup.children.filter((e) => !e.hasRequestLoad);
  395. if (unloads.length) {
  396. setTimeout(Overlay.load, 200);
  397. } else {
  398. Overlay.allRequestLoad = true;
  399. console.log("allRequestLoad");
  400. }
  401. };
  402. window.Overlay = Overlay;
  403. };