overlay.js 5.8 KB

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