overlay.js 14 KB

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