overlay1.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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: !info.media.includes('video'), //视频的声音按钮要求plane不能透明
  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. var soundBtn = addSoundBtn(video);
  53. this.add(soundBtn)
  54. this.soundBtn = soundBtn;
  55. var offsetX = 0.11, offsetY = 0.11; //到右下角的距离,单位m
  56. this.soundBtn.position.set(settings.overlay.width*(0.5-1/info.width*offsetX), -settings.overlay.height*(0.5-1/info.height*offsetY),0.01)
  57. }else if(info.media.includes('photo')){
  58. /* var img = new Image();
  59. img.src = manage.dealURL(info.file) //"https://4dkk.4dage.com/images/images"+Config.projectNum+"/overlay"+this.sid+".jpg?m="+new Date().getTime()
  60. info.media = img
  61. */
  62. info.type = "photo"
  63. }
  64. plane.material.opacity = 1;
  65. plane.material.color = new THREE.Color(1,1,1)
  66. }
  67. if(info.width == void 0) info.width = settings.overlay.width;
  68. if(info.height == void 0) info.height = settings.overlay.height;
  69. this.setFromInfo(info)
  70. }
  71. Overlay.prototype.setFromInfo = function(info){//1 恢复到编辑之前 2 初始加载
  72. var plane = this.plane;
  73. info.width && (this.scale.setX(info.width/settings.overlay.width),this.width = info.width)
  74. info.height && (this.scale.setY(info.height/settings.overlay.height),this.height = info.height)
  75. info.depth && this.scale.setZ(info.depth/settings.overlay.depth)
  76. info.pos && this.position.copy(info.pos);
  77. info.qua && this.quaternion.copy(info.qua);
  78. if(info.type){
  79. if(!plane.material.map){
  80. if(info.type == "video"){
  81. plane.material.map = new THREE.VideoTexture( info.media );
  82. this.soundBtn.scale.set(1/this.scale.x,1/this.scale.y,1/this.scale.z)
  83. }else{
  84. plane.material.map = Texture.load(info.file,()=>{
  85. if(this._loadDone)this._loadDone()
  86. })
  87. }
  88. plane.material.map.wrapS = plane.material.map.wrapT = THREE.ClampToEdgeWrapping;
  89. plane.material.map.minFilter = THREE.LinearFilter;
  90. plane.material.map.magFilter = THREE.LinearFilter;
  91. plane.material.map.generateMipmaps = true;
  92. }else plane.material.map.image = info.media;
  93. this.file = info.file;
  94. }
  95. this.overlayType = info.type;
  96. if(!!this.hasBox != !!info.hasBox){
  97. this.addBox(!this.hasBox);
  98. }
  99. }
  100. Overlay.prototype.addBox = function(state){
  101. if(state == !!this.hasBox){
  102. return;
  103. }
  104. if(state){
  105. var box = new THREE.Mesh(_boxGeometry , _boxMat)
  106. box.position.set(0,0,settings.overlay.depth/2);
  107. box.renderOrder = 3
  108. this.plane.position.set(0,0,settings.overlay.depth );
  109. this.add(box);
  110. this.box = box;
  111. }else{
  112. this.plane.position.set(0,0,0);
  113. this.remove(this.box);
  114. this.box = null;
  115. }
  116. this.hasBox = state
  117. }
  118. Overlay.prototype.preDeal = function(info){
  119. info.pos = new THREE.Vector3().fromArray(info.pos)
  120. info.qua = new THREE.Quaternion().fromArray(info.qua)
  121. info.width = parseFloat(info.width)
  122. info.height = parseFloat(info.height)
  123. info.depth = parseFloat(info.depth)
  124. info.hasBox = parseInt(info.hasBox)
  125. info.pos.x = parseFloat(info.pos.x)
  126. info.pos.y = parseFloat(info.pos.y)
  127. info.pos.z = parseFloat(info.pos.z)
  128. info.qua.x = parseFloat(info.qua.x)
  129. info.qua.y = parseFloat(info.qua.y)
  130. info.qua.z = parseFloat(info.qua.z)
  131. info.qua.w = parseFloat(info.qua.w)
  132. }
  133. window.Overlay = Overlay;
  134. }