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