overlay.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561
  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. video.play()
  69. }
  70. video.volume = 0
  71. video.muted = true
  72. plane.material.transparent = false
  73. var soundBtn = addSoundBtn(video);
  74. this.add(soundBtn)
  75. this.soundBtn = soundBtn;
  76. var offsetX = 0.11, offsetY = 0.11; //到右下角的距离,单位m
  77. this.soundBtn.position.set(settings.overlay.width*(0.5-1/info.width*offsetX), -settings.overlay.height*(0.5-1/info.height*offsetY),0.01)
  78. plane.material.opacity = 1;
  79. } else if (info.media.includes('photo')) {
  80. /* var img = new Image();
  81. img.src = manage.dealURL(info.file) //"https://4dkk.4dage.com/images/images"+Config.projectNum+"/overlay"+this.sid+".jpg?m="+new Date().getTime()
  82. info.media = img
  83. */
  84. info.type = "photo"
  85. plane.material.opacity = 0.1;
  86. /* this.animateInfo = {
  87. cellXcount : 4,
  88. cellYcount : 2,
  89. loop : true,
  90. duration : 1000
  91. }
  92. */
  93. }
  94. plane.material.color = new THREE.Color(1,1,1)
  95. }
  96. if (info.width == void 0)
  97. info.width = settings.overlay.width;
  98. if (info.height == void 0)
  99. info.height = settings.overlay.height;
  100. this.setFromInfo(info)
  101. this.fileSrc = info.file
  102. }
  103. Overlay.prototype.setFromInfo = function(info) {
  104. //1 恢复到编辑之前 2 初始加载
  105. var plane = this.plane;
  106. var transformAtPanos = {}
  107. for(var i in info.transformAtPanos){
  108. transformAtPanos[i] = {
  109. width : info.transformAtPanos[i].width,
  110. height : info.transformAtPanos[i].height,
  111. pos : info.transformAtPanos[i].pos.clone(),
  112. qua : info.transformAtPanos[i].qua.clone(),
  113. }
  114. }
  115. this.transformAtPanos = transformAtPanos
  116. //在每个漫游点独立设置的position。
  117. //var curPanoTransform = player.currentPano && this.transformAtPanos[player.currentPano.id] || {}
  118. var curPanoTransform = this.transformAtPanos[ getTransformSid()] || {}
  119. info.depth && this.scale.setZ(info.depth / settings.overlay.depth)
  120. this.posCustom = info.pos ? info.pos.clone() : this.position.clone();
  121. //没有单独设置position的漫游点使用的position
  122. this.position.copy(curPanoTransform.pos || this.posCustom)
  123. this.quaCustom = info.qua ? info.qua.clone() : this.quaternion.clone()
  124. this.quaternion.copy(curPanoTransform.qua || this.quaCustom);
  125. this.widthCustom = info.width
  126. this.heightCustom = info.height
  127. this.width = curPanoTransform.width || this.widthCustom
  128. this.height = curPanoTransform.height || this.heightCustom
  129. var a = this.getScaleBySize(this.width, this.height)
  130. this.scale.setX(a.x)
  131. this.scale.setY(a.y)
  132. if (info.type) {
  133. if (!plane.material.map) {
  134. if (info.type == "video") {
  135. plane.material.map = new THREE.VideoTexture(info.media);
  136. this.hasRequestLoad = true
  137. plane.material.map.wrapS = plane.material.map.wrapT = THREE.ClampToEdgeWrapping;
  138. plane.material.map.minFilter = THREE.LinearFilter;
  139. plane.material.map.magFilter = THREE.LinearFilter;
  140. plane.material.map.generateMipmaps = true;
  141. this.soundBtn.scale.set(1/this.scale.x,1/this.scale.y,1/this.scale.z)
  142. } else {
  143. this._loadDones = []
  144. /* plane.material.map = Texture.load(info.file,()=>{
  145. if(this._loadDones){
  146. this._loadDones.forEach(e=>e())
  147. this._loadDones = null
  148. }
  149. }) */
  150. }
  151. /* plane.material.map.wrapS = plane.material.map.wrapT = THREE.ClampToEdgeWrapping;
  152. plane.material.map.minFilter = THREE.LinearFilter;
  153. plane.material.map.magFilter = THREE.LinearFilter;
  154. plane.material.map.generateMipmaps = true; */
  155. } else{
  156. plane.material.map.image = info.media;
  157. plane.material.map.needsUpdate = true
  158. }
  159. this.file = info.file;
  160. }
  161. this.overlayType = info.type;
  162. if (!!this.hasBox != !!info.hasBox) {
  163. this.addBox(!this.hasBox);
  164. }
  165. this.updateMatrixWorld()
  166. this.getVisiblePanos()
  167. {//gif
  168. if(this.animation){
  169. GifTexDeal.remove(this.animation)
  170. }
  171. this.animateInfo = CloneObject(info.animateInfo)
  172. if(this.animateInfo && plane.material.map){
  173. this.animation = GifTexDeal.addAnimation(plane.material.map, this, this.animateInfo, this.sid )
  174. this.visible && this.inSight() && GifTexDeal.start(this.animation)
  175. }
  176. }
  177. }
  178. Overlay.prototype.addBox = function(state) {
  179. if (state == !!this.hasBox) {
  180. return;
  181. }
  182. if (state) {
  183. var box = new THREE.Mesh(_boxGeometry,_boxMat)
  184. box.position.set(0, 0, settings.overlay.depth / 2);
  185. box.renderOrder = 3
  186. this.plane.position.set(0, 0, settings.overlay.depth);
  187. this.add(box);
  188. this.box = box;
  189. } else {
  190. this.plane.position.set(0, 0, 0);
  191. this.remove(this.box);
  192. this.box = null;
  193. }
  194. this.hasBox = state
  195. }
  196. Overlay.prototype.getSizeByScale = function() {
  197. return {
  198. width: settings.overlay.width * this.scale.x,
  199. height: settings.overlay.height * this.scale.y
  200. }
  201. }
  202. Overlay.prototype.getScaleBySize = function(width, height) {
  203. return {
  204. x: width / settings.overlay.width,
  205. y: height / settings.overlay.height,
  206. }
  207. }
  208. Overlay.prototype.preDeal = function(info) {
  209. info.pos = new THREE.Vector3().fromArray(info.pos)
  210. info.qua = new THREE.Quaternion().fromArray(info.qua)
  211. info.width = parseFloat(info.width)
  212. info.height = parseFloat(info.height)
  213. info.depth = parseFloat(info.depth)
  214. info.hasBox = parseInt(info.hasBox)
  215. info.pos.x = parseFloat(info.pos.x)
  216. info.pos.y = parseFloat(info.pos.y)
  217. info.pos.z = parseFloat(info.pos.z)
  218. info.qua.x = parseFloat(info.qua.x)
  219. info.qua.y = parseFloat(info.qua.y)
  220. info.qua.z = parseFloat(info.qua.z)
  221. info.qua.w = parseFloat(info.qua.w)
  222. if (!info.transformAtPanos)
  223. info.transformAtPanos = {}
  224. for (let i in info.transformAtPanos) {
  225. info.transformAtPanos[i].pos = new THREE.Vector3().fromArray(info.transformAtPanos[i].pos)
  226. info.transformAtPanos[i].qua = new THREE.Quaternion().fromArray(info.transformAtPanos[i].qua)
  227. }
  228. }
  229. Overlay.prototype.getVisiblePanos = function() {//在不同点还不一样
  230. var depth = this.scale.z * settings.overlay.depth;
  231. var getPos = function(position, quaternion, width, height){//每个overlay位置对应5个坐标,plane中心和四个角的位置
  232. var cornerPoint = [
  233. new THREE.Vector3(0, 0, depth),
  234. new THREE.Vector3(-width/2, height/2, depth),
  235. new THREE.Vector3(width/2, height/2, depth),
  236. new THREE.Vector3(width/2, -height/2, depth),
  237. new THREE.Vector3(-width/2, -height/2, depth),
  238. ];
  239. return cornerPoint.map(e=>{
  240. return e.clone().applyQuaternion(quaternion).add(position)
  241. })
  242. }
  243. var customPositions = getPos(this.posCustom, this.quaCustom, this.widthCustom, this.heightCustom)
  244. var posAtPanos = {}
  245. for(let panoId in this.transformAtPanos){
  246. if(panoId == 'outSide')continue;
  247. posAtPanos[panoId] = getPos(this.transformAtPanos[panoId].pos, this.transformAtPanos[panoId].qua, this.transformAtPanos[panoId].width, this.transformAtPanos[panoId].height)
  248. }
  249. this.visiblePanos = common.getVisiblePano(customPositions, {
  250. model: null , posAtPanos
  251. });
  252. }
  253. Overlay.prototype.updateVisible = function(panos, visibility) {
  254. if(settings.isEdit && EditOverlay.editPlane == this){
  255. return true
  256. }
  257. this.visible = visibility != void 0 ? visibility : !!panos.find(pano=>this.visiblePanos.includes(pano))
  258. if (this.overlayType == 'video'){
  259. //this.switchPlay(this.visible, this.visible ? null : 'stop' );//可见时不操作;不可见时停止
  260. this.update()
  261. }
  262. /* if(this.animateInfo){ //在player.update里更新
  263. if(this.visible){
  264. GifTexDeal.start(this.animation)
  265. }else{
  266. GifTexDeal.stop(this.animation)
  267. }
  268. } */
  269. }
  270. Overlay.updateVisibles = function(panos) {
  271. if (panos === true) {
  272. player.overlayGroup.children.forEach(e=>e.updateVisible(null,true))
  273. } else {
  274. player.overlayGroup.children.forEach(e=>e.updateVisible(panos))
  275. }
  276. }
  277. Overlay.prototype.switchPlay = function(state){//手动播放暂停
  278. this.pausedByUser = !state
  279. this.videoControl(state)
  280. }
  281. Overlay.prototype.videoControl = function(state){
  282. if(this.overlayType != "video")return
  283. var video = this.plane.material.map.image
  284. if(!state || state == 'stop'){
  285. video.paused || video.pause()
  286. if(state == 'stop'){
  287. video.currentTime = 0;
  288. }
  289. //console.log("pause")
  290. }else if(state){
  291. video.paused && video.play()
  292. //console.log("play")
  293. }
  294. }
  295. Overlay.prototype.inSight = function(){
  296. if(player.mode == 'panorama'){
  297. var position = this.plane.getWorldPosition()
  298. var pos2d = math.getPos2d(position, player.camera, $("#player")[0])
  299. if(pos2d.trueSide && pos2d.inSight){
  300. return true
  301. }else{
  302. var cornerPoint = [
  303. new THREE.Vector3(-settings.overlay.width/2, settings.overlay.height/2, 0),
  304. new THREE.Vector3(settings.overlay.width/2, settings.overlay.height/2, 0),
  305. new THREE.Vector3(settings.overlay.width/2, -settings.overlay.height/2, 0),
  306. new THREE.Vector3(-settings.overlay.width/2, -settings.overlay.height/2, 0),
  307. ];
  308. for(var i=0;i<4;i++){//只要有一点可见就算看见
  309. cornerPoint[i].applyMatrix4(this.plane.matrixWorld);
  310. var pos2d = math.getPos2d(cornerPoint[i], player.camera, $("#player")[0])
  311. if(pos2d.trueSide && pos2d.inSight){
  312. return true
  313. }
  314. }
  315. }
  316. }else{//飞出要判断模型阻挡,有点耗时就算了
  317. return true
  318. }
  319. }
  320. Overlay.prototype.update = function(){//实时监测播放
  321. if(this.overlayType == "video"){
  322. if(this.visible && !this.pausedByUser && this.inSight()){
  323. this.videoControl(true)
  324. }else{
  325. this.videoControl(false)
  326. }
  327. }else if(this.animateInfo){
  328. if(this.visible && this.inSight()){
  329. GifTexDeal.start(this.animation)
  330. }else{
  331. GifTexDeal.stop(this.animation)
  332. }
  333. }
  334. }
  335. Overlay.prototype.addToLoadQueue = function() {
  336. if (this.overlayType == 'photo') {
  337. Overlay.loadQueue.includes(this) || Overlay.loadQueue.push(this)
  338. }
  339. }
  340. Overlay.prototype.requestDownload = function() {
  341. if (this.hasRequestLoad || this.overlayType != 'photo')
  342. return
  343. console.log('overlay beginDownload : ' + this.sid)
  344. var plane = this.plane;
  345. plane.material.map = Texture.load(this.file, ()=>{
  346. if (this._loadDones) {
  347. this._loadDones.forEach(e=>e())
  348. this._loadDones = null
  349. }
  350. setTimeout(Overlay.loadNext, 50)
  351. plane.material.opacity = 1;
  352. console.log('overlay loaded: ' + this.sid)
  353. if(this.animateInfo){
  354. this.animation = GifTexDeal.addAnimation(plane.material.map, this, this.animateInfo, this.sid )
  355. this.visible && this.inSight() && GifTexDeal.start(this.animation)
  356. }
  357. plane.material.needsUpdate = true
  358. })
  359. plane.material.map.wrapS = plane.material.map.wrapT = THREE.ClampToEdgeWrapping;
  360. plane.material.map.minFilter = THREE.LinearFilter;
  361. plane.material.map.magFilter = THREE.LinearFilter;
  362. plane.material.map.generateMipmaps = true;
  363. this.hasRequestLoad = true
  364. }
  365. Overlay.loadQueue = []; //等待下载的overlay,目前只针对photo
  366. Overlay.maxLoadingCount = 3; //同时正在load图片的数量
  367. Overlay.loadNext = ()=>{//继续requestDownload loadQueue中前排的item
  368. var loadings = player.overlayGroup.children.filter(e=>e.hasRequestLoad && e._loadDones)//开始下载了但是没加载好的
  369. Overlay.loadQueue.slice(0, Overlay.maxLoadingCount - loadings.length).forEach(e=>e.requestDownload())
  370. Overlay.loadQueue.splice(0, Overlay.maxLoadingCount - loadings.length)
  371. }
  372. Overlay.getNeedLoad = function() {//计算获取loadQueue,每次都重新计算,覆盖旧的
  373. if (!player || !player.domElement || !player.mode)
  374. return;
  375. if (player.mode != 'panorama') {
  376. if (!Overlay.loadWhenOutside)
  377. return;
  378. if (Overlay.loadQueue.length == 0) {
  379. Overlay.loadQueue = player.overlayGroup.children.filter(e=>!e.hasRequestLoad).slice(0, 5);
  380. }
  381. return;
  382. }
  383. Overlay.loadWhenOutside = true
  384. var overlays = player.overlayGroup.children.filter(e=>!e.hasRequestLoad && e.visiblePanos.includes(player.currentPano))
  385. //var maxAngle = THREE.Math.degToRad( cameraLight.getHFOVFromVFOV(70, player.domElement.clientWidth, app.player.domElement.clientHeight) / 2);
  386. var cameraDir = player.getDirection()
  387. /* var maxCount = 5;
  388. if(overlays.length>maxCount){
  389. for(var i=0;i<overlays.length;i++){
  390. //角度为可见范围
  391. var v1 = cameraDir.clone().setY(0);
  392. var v2 = overlays[i].plane.getWorldPosition().sub(player.position).setY(0)
  393. if(v1.angleTo(v2) <= maxAngle){
  394. Overlay.loadQueue.push(overlays[i])
  395. if(Overlay.loadQueue.length>=10) break;
  396. }
  397. }
  398. if(Overlay.loadQueue.length<Overlay.maxLoadingCount){
  399. Overlay.loadQueue.push()
  400. }
  401. }else{ */
  402. Overlay.loadQueue = overlays
  403. //}
  404. var request = [(overlay)=>{
  405. return true
  406. }
  407. ];
  408. var rank = [(overlay)=>{
  409. var dis = overlay.plane.getWorldPosition().distanceTo(player.position);
  410. return -dis
  411. }
  412. , (overlay)=>{
  413. var tagDir = overlay.plane.getWorldPosition().sub(player.position)
  414. var angle = tagDir.angleTo(cameraDir)
  415. return -angle * 20
  416. }
  417. ]
  418. var result = common.sortByScore(Overlay.loadQueue, request, rank);
  419. Overlay.loadQueue = result ? result.slice(0, 5).map(e=>e.item) : player.overlayGroup.children.filter(e=>!e.hasRequestLoad).slice(0, 2);
  420. }
  421. Overlay.load = ()=>{//开始下载图片
  422. Overlay.getNeedLoad()
  423. Overlay.loadNext()
  424. var unloads = player.overlayGroup.children.filter(e=>!e.hasRequestLoad)
  425. if (unloads.length) {
  426. setTimeout(Overlay.load, 200)
  427. } else {
  428. Overlay.allRequestLoad = true
  429. console.log('allRequestLoad')
  430. }
  431. }
  432. window.Overlay = Overlay;
  433. }