overlay.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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({//MeshStandardMaterial
  9. color:"#eeeeee",
  10. transparent: !0 ,
  11. opacity:0.8
  12. })
  13. var overlayGroup = new THREE.Object3D; player.model.add(overlayGroup); overlayGroup.name = "overlayGroup"
  14. player.overlayGroup = overlayGroup;
  15. var Overlay = function(info){
  16. THREE.Object3D.call(this);
  17. this.sid = info.sid;
  18. if(info.media)this.preDeal(info)
  19. this.build(info);
  20. this.name = "overlay_"+this.sid;
  21. }
  22. Overlay.prototype = Object.create(THREE.Object3D.prototype);
  23. Overlay.prototype.build = function(info){
  24. var plane = new THREE.Mesh(_planeGeometry, new THREE.MeshBasicMaterial({//MeshStandardMaterial
  25. color:"#00c8af",
  26. opacity:0.4,
  27. transparent: !0,
  28. polygonOffset : true,//是否开启多边形偏移 //ie不开启时blank也不会闪烁
  29. polygonOffsetFactor : -0.9,//多边形偏移因子
  30. polygonOffsetUnits : -4.0,//多边形偏移单位
  31. }) )
  32. plane.renderOrder = 3
  33. this.add(plane);
  34. this.plane = plane;
  35. if(info.hasBox){
  36. this.addBox(true)
  37. }
  38. overlayGroup.add(this);
  39. if(info.media){
  40. if(info.media.includes('video')){
  41. var video = $('<video controls="controls" loop autoplay x5-playsinline="" webkit-playsinline="true" playsinline="true" controlslist="nodownload"></video>')[0]
  42. video.setAttribute("crossOrigin", 'Anonymous')//要在src设置好前解决跨域
  43. $(video).on('contextmenu', function () { return false; });//禁止右键点击出
  44. video.src = manage.dealURL(info.file) ;
  45. info.media = video;
  46. info.type = "video"
  47. video.addEventListener('loadeddata', ()=>{
  48. console.log(this.sid + " loaded!!!")
  49. })
  50. video.volume = 0
  51. video.muted = true
  52. }else if(info.media.includes('photo')){
  53. /* var img = new Image();
  54. img.src = manage.dealURL(info.file) //"https://4dkk.4dage.com/images/images"+Config.projectNum+"/overlay"+this.sid+".jpg?m="+new Date().getTime()
  55. info.media = img
  56. */
  57. info.type = "photo"
  58. }
  59. plane.material.opacity = 1;
  60. plane.material.color = new THREE.Color(1,1,1)
  61. }
  62. if(info.width == void 0) info.width = settings.overlay.width;
  63. if(info.height == void 0) info.height = settings.overlay.height;
  64. this.setFromInfo(info)
  65. }
  66. Overlay.prototype.setFromInfo = function(info){//1 恢复到编辑之前 2 初始加载
  67. var plane = this.plane;
  68. this.transformAtPanos = info.transformAtPanos || {} //在每个漫游点独立设置的position。
  69. var curPanoTransform = player.currentPano && this.transformAtPanos[player.currentPano.id] || {}
  70. info.depth && this.scale.setZ(info.depth/settings.overlay.depth)
  71. this.posCustom = info.pos ? info.pos.clone() : this.position.clone(); //没有单独设置position的漫游点使用的position
  72. this.position.copy(curPanoTransform.pos || this.posCustom )
  73. this.quaCustom = info.qua ? info.qua.clone() : this.quaternion.clone()
  74. this.quaternion.copy(curPanoTransform.qua || this.quaCustom);
  75. this.widthCustom = info.width
  76. this.heightCustom = info.height
  77. this.width = curPanoTransform.width || this.widthCustom
  78. this.height = curPanoTransform.height || this.heightCustom
  79. var a = this.getScaleBySize(this.width, this.height)
  80. this.scale.setX( a.x )
  81. this.scale.setY( a.y )
  82. if(info.type){
  83. if(!plane.material.map){
  84. if(info.type == "video"){
  85. plane.material.map = new THREE.VideoTexture( info.media );
  86. }else{
  87. plane.material.map = Texture.load(info.file,()=>{
  88. if(this._loadDone)this._loadDone()
  89. })
  90. }
  91. plane.material.map.wrapS = plane.material.map.wrapT = THREE.ClampToEdgeWrapping;
  92. plane.material.map.minFilter = THREE.LinearFilter;
  93. plane.material.map.magFilter = THREE.LinearFilter;
  94. plane.material.map.generateMipmaps = true;
  95. }else plane.material.map.image = info.media;
  96. this.file = info.file;
  97. }
  98. this.overlayType = info.type;
  99. if(!!this.hasBox != !!info.hasBox){
  100. this.addBox(!this.hasBox);
  101. }
  102. }
  103. Overlay.prototype.addBox = function(state){
  104. if(state == !!this.hasBox){
  105. return;
  106. }
  107. if(state){
  108. var box = new THREE.Mesh(_boxGeometry , _boxMat)
  109. box.position.set(0,0,settings.overlay.depth/2);
  110. box.renderOrder = 3
  111. this.plane.position.set(0,0,settings.overlay.depth );
  112. this.add(box);
  113. this.box = box;
  114. }else{
  115. this.plane.position.set(0,0,0);
  116. this.remove(this.box);
  117. this.box = null;
  118. }
  119. this.hasBox = state
  120. }
  121. Overlay.prototype.getSizeByScale = function(){
  122. return {
  123. width : settings.overlay.width * this.scale.x,
  124. height : settings.overlay.height * this.scale.y
  125. }
  126. }
  127. Overlay.prototype.getScaleBySize = function(width, height){
  128. return {
  129. x : width / settings.overlay.width,
  130. y : height / settings.overlay.height,
  131. }
  132. }
  133. Overlay.prototype.preDeal = function(info){
  134. info.pos = new THREE.Vector3().fromArray(info.pos)
  135. info.qua = new THREE.Quaternion().fromArray(info.qua)
  136. info.width = parseFloat(info.width)
  137. info.height = parseFloat(info.height)
  138. info.depth = parseFloat(info.depth)
  139. info.hasBox = parseInt(info.hasBox)
  140. info.pos.x = parseFloat(info.pos.x)
  141. info.pos.y = parseFloat(info.pos.y)
  142. info.pos.z = parseFloat(info.pos.z)
  143. info.qua.x = parseFloat(info.qua.x)
  144. info.qua.y = parseFloat(info.qua.y)
  145. info.qua.z = parseFloat(info.qua.z)
  146. info.qua.w = parseFloat(info.qua.w)
  147. if(!info.transformAtPanos)info.transformAtPanos = {}
  148. for(let i in info.transformAtPanos){
  149. info.transformAtPanos[i].pos = new THREE.Vector3().fromArray(info.transformAtPanos[i].pos)
  150. info.transformAtPanos[i].qua = new THREE.Quaternion().fromArray(info.transformAtPanos[i].qua)
  151. }
  152. }
  153. window.Overlay = Overlay;
  154. }