overlay.js 13 KB

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