overlay.js 17 KB

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