overlay.js 20 KB

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