Hot.js 75 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945
  1. //合并热点和展览
  2. g_currentHot = null
  3. const playVideoWhenFlyOut = false
  4. //同时可播放的最大个数:
  5. const playVideoMax = window.isEdit ? 3 : browser.isMobile() ? 1 : 2;
  6. const playAniMax = window.isEdit ? 6 : browser.isMobile() ? 3 : 5;
  7. window.initHot = function(model){
  8. var objLoader = new THREE.OBJLoader()
  9. var _planeGeometry = new THREE.PlaneGeometry(1,1)
  10. var _boxGeometry = new THREE.BoxBufferGeometry(1,1,1)
  11. {//ie的mesh 加了polygonOffset也是会重叠。所以去掉前面的face: (但是突然ie又播放不了videoTexture)
  12. var newIndex = [..._boxGeometry.index.array]
  13. newIndex.splice(4 * 6, 12)
  14. _boxGeometry.setIndex(new THREE.BufferAttribute(new Uint16Array(newIndex),1))
  15. }
  16. var originPhotoCount = photoLoaded = originModelCount = modelLoaded = 0;
  17. var defaultTex1 = Texture.load(g_HotImage.point);
  18. var defaultTex2 = Texture.load(g_HotImage.point2)
  19. dealMap(defaultTex1,{ignoreResize:true} );
  20. dealMap(defaultTex2,{ignoreResize:true} ); //这张图改为linear有黑边。 但即使不改,chrome调试手机版也有黑边和锯齿
  21. /* var _boxMat = new THREE.MeshBasicMaterial({
  22. color: "#eeeeee",
  23. transparent: !0,
  24. opacity: 0.8
  25. }) */
  26. var _boxMat = new THREE.MeshPhongMaterial({
  27. color: "#eeeeee",
  28. transparent: !0,
  29. opacity: 0.8,
  30. side:THREE.DoubleSide
  31. })
  32. var autoSizeInfo = /* {width2d:50}// */{minSize : 120, maxSize : 600, nearBound : 1 , farBound : 15}
  33. var hotGroup = new THREE.Object3D; hotGroup.name = "hotGroup"
  34. model.add(hotGroup); model.hotGroup = hotGroup
  35. var animateTexSrcs = {}
  36. var getLink = function(link){
  37. var src = '';
  38. var r = link.substring(link.indexOf("html") + 4)
  39. , o = "en" == manage.number("lang") ? "&lang=" + manage.number("lang") : "";
  40. -1 == r.indexOf("?") ? src = link + "?time=" + randomTime().getTime() + "&id=" + window.number + o : src = link + "&time=" + randomTime().getTime() + "&id=" + window.number + o
  41. return src
  42. }
  43. var removeSrcPostMark = function(url){//去除texture.load时自动加上的'?'
  44. var index = url.indexOf('?')
  45. if(index>-1){
  46. return url.slice(0, index)
  47. }else return url
  48. }
  49. {//get plane Bound
  50. var planeBound = new THREE.Box3()
  51. var cornerPoint = [
  52. new THREE.Vector3(-0.5, 0.5, 0),
  53. new THREE.Vector3(0.5, 0.5, 0),
  54. new THREE.Vector3(0.5, -0.5, 0),
  55. new THREE.Vector3(-0.5, -0.5, 0),
  56. ]
  57. cornerPoint.forEach(e=>{
  58. planeBound.expandByPoint(e)
  59. })
  60. }
  61. var shineMats = [];
  62. var getShineMat = function(texture1, texture2){
  63. var mat = shineMats.find(e=>e.uniforms.texture1.value == texture1 && e.uniforms.texture2.value == texture2)
  64. if(mat) return mat
  65. else{
  66. var mat = new THREE.ShaderMaterial({
  67. uniforms: {
  68. opac: {
  69. type: "f",
  70. value: 0
  71. },
  72. texture1: {
  73. type: "t",
  74. value: texture1
  75. },
  76. texture2: {
  77. type: "t",
  78. value: texture2
  79. }
  80. },
  81. vertexShader: "varying vec2 vUv;\n\nvoid main() {\n\n vUv = uv ;\n gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );\n\n}\n",
  82. fragmentShader: "varying vec3 vNormal;\n\nvarying vec2 vUv;\n\nuniform float opac;\n\n uniform sampler2D texture1;\n\nuniform sampler2D texture2;\n\nvoid main() {\n\nvec4 tcolor1 = texture2D( texture1, vUv );\n\nvec4 tcolor2 = texture2D( texture2, vUv );\n\ngl_FragColor = mix(tcolor1,tcolor2 ,opac) + tcolor2*0.2; }\n"
  83. ,transparent: !0
  84. })
  85. shineMats.push(mat)
  86. return mat
  87. }
  88. }
  89. var modelGeos = [];
  90. var getModelGeo = function(url){
  91. var geo = modelGeos.find(e=>e.url = url )
  92. if(geo) return geo
  93. else{
  94. var geo = null;
  95. geo.url = url
  96. }
  97. }
  98. class Hot extends THREE.Object3D{
  99. constructor(info, source){
  100. super()
  101. this.sid = info.sid;
  102. this.preDeal(info, source)// source:旧版来源
  103. this.info = info
  104. this.cornerPoints = []
  105. this.build(info);
  106. this.name = "hot_" + this.sid ;
  107. model.hots[info.sid] = this
  108. }
  109. build(info) {
  110. hotGroup.add(this)
  111. this.setTitleElem()
  112. this.setFromInfo(info)
  113. }
  114. setFromInfo(info, media, objObject) {
  115. //1 恢复到编辑之前 2 初始加载
  116. var plane = this.plane;
  117. /* var transformAtPanos = {}
  118. for(var i in info.transformAtPanos){
  119. transformAtPanos[i] = {//只保留一个位移,主要原因是大小变化和热点大小设置冲突了, isSprite和qutaernion衝突
  120. pos : info.transformAtPanos[i].pos && info.transformAtPanos[i].pos.clone(),
  121. //qua : info.transformAtPanos[i].qua && info.transformAtPanos[i].qua.clone(),
  122. }
  123. }
  124. this.transformAtPanos = transformAtPanos */
  125. //在每个漫游点独立设置的position。
  126. var curPanoTransform = this.info.transformAtPanos[ getTransformSid()] || {}
  127. //没有单独设置position的漫游点使用的position
  128. this.position.copy(curPanoTransform.pos || info.position)
  129. this.rotation.copy(info.rotation)
  130. this.scale.copy(info.scale)
  131. this.changeTexType(info.texType, media)
  132. if(!info.objSrc){
  133. this.addPlane()
  134. if (!!this.hasBox != !!info.hasBox) {
  135. this.addBox(!this.hasBox);
  136. }
  137. }else{
  138. this.addModel(objObject || new THREE.Mesh())//暂时创建个空的
  139. }
  140. this.updateMatrixWorld()
  141. /* if(!this.info.visiblePanos){//移到model.build时,在collider建好之后
  142. this.getVisiblePanos()
  143. } */
  144. {//gif
  145. if(this.animation){
  146. GifTexDeal.remove(this.animation)
  147. }
  148. if(this.info.animateInfo && this.material_.map){
  149. this.animation = GifTexDeal.addAnimation(this.material_.map, this, this.info.animateInfo, this.sid )
  150. this.visible && this.inSight() && GifTexDeal.start(this.animation)
  151. }
  152. }
  153. /* if(this.info.modelBound){
  154. this.mesh.updateMatrixWorld()
  155. this.mesh.boxHelper2 = new THREE.Box3Helper( new THREE.Box3().copy(this.info.modelBound.bound).applyMatrix4(this.mesh.matrixWorld), new THREE.Color("#00aaee"));
  156. model.add(this.mesh.boxHelper2)
  157. }else{
  158. var bound = new THREE.Box3()
  159. var cornerPoint = [
  160. new THREE.Vector3(-0.5, 0.5, 0),
  161. new THREE.Vector3(0.5, 0.5, 0),
  162. new THREE.Vector3(0.5, -0.5, 0),
  163. new THREE.Vector3(-0.5, -0.5, 0),
  164. ]
  165. cornerPoint.forEach(e=>{
  166. bound.expandByPoint(e)
  167. })
  168. this.mesh.updateMatrixWorld()
  169. this.mesh.boxHelper2 = new THREE.Box3Helper( bound.applyMatrix4(this.mesh.matrixWorld), new THREE.Color("#00aaee"));
  170. model.add(this.mesh.boxHelper2)
  171. } */
  172. }
  173. changeTexType(texType, media){
  174. var plane = this.plane;
  175. if( this.texType != texType || media == 'clear'){
  176. //删除旧的
  177. if(this.texType == 'shine'){
  178. /* this.material_.uniforms.texture1.value.dispose()
  179. this.material_.uniforms.texture2.value.dispose() */ //为了恢复 不删
  180. }else{
  181. //this.material_.map.dispose()
  182. if(this.texType == 'video'){
  183. this.texMedia && this.texMedia.pause()
  184. }else{
  185. }
  186. }
  187. if(this.material_ && !hotGroup.children.find(e=>e!=this && e.material_ == this.material_ )){
  188. this.material_.dispose()
  189. }
  190. //添加新的
  191. if(texType == 'shine'){
  192. }else{
  193. this.changeMaterial(new THREE.MeshBasicMaterial({
  194. color: "#00c8af",
  195. opacity: 0.4,
  196. transparent: !0,
  197. polygonOffset: true,
  198. //是否开启多边形偏移 //ie不开启时blank也不会闪烁
  199. polygonOffsetFactor: -0.9,
  200. //多边形偏移因子
  201. polygonOffsetUnits: -4.0,
  202. //多边形偏移单位
  203. }))
  204. if(texType == 'video'){
  205. }else{
  206. }
  207. }
  208. }
  209. this.texType = texType
  210. if(media == 'clear'){
  211. this.material_.opacity = 0.4;
  212. this.texMedia = null
  213. return
  214. }
  215. if(texType == 'video'){//视频不能共用一个,否则会一起播放暂停
  216. var video
  217. if(media){
  218. video = media
  219. }else{
  220. if(!this.info.texSrc)return
  221. var video = $(`<video controls="controls" loop x5-playsinline="" webkit-playsinline="true" playsinline="true" controlslist="nodownload" preload="meta" ></video>`)[0]
  222. if(window.isEdit) video.src = manage.dealURL(this.info.texSrc); //注意,src赋值就会自动加载, preload="meta" 只加载元数据,提高加载速度,否则一开始卡
  223. video.name = this.info.fileName
  224. }
  225. video.setAttribute("crossOrigin", 'Anonymous')//要在src设置好前解决跨域
  226. $(video).on('contextmenu', function() {
  227. return false;
  228. });//禁止右键点击出
  229. this.texMedia = video;
  230. video.oncanplaythrough = (e)=> {
  231. if(this.texMedia == video){
  232. /* if(video.paused) *///console.log({str:'oncanplaythrough '+this.sid, level:1})
  233. //this.material_.map.needsUpdate = !0 //当初为何加这句?
  234. //this.update(player)
  235. }
  236. }
  237. /* video.onloadstart = (e)=> {
  238. console.log({str:'onloadstart '+this.sid, level:1})
  239. }
  240. video.onloadedmetadata = (e)=> {
  241. console.log({str:'onloadedmetadata '+this.sid, level:1})
  242. }
  243. video.onloadeddata = (e)=> {
  244. console.log({str:'onloadeddata '+this.sid, level:1})
  245. }
  246. video.oncanplay = (e)=> {
  247. console.log({str:'oncanplay '+this.sid, level:1})
  248. } */
  249. video.volume = 0
  250. video.muted = true
  251. video.currentTime = 0
  252. if(!window.isEdit) this.material_.opacity = 0.3;
  253. this.material_.map = new THREE.VideoTexture(video)
  254. this.material_.map.wrapS = this.material_.map.wrapT = THREE.ClampToEdgeWrapping;
  255. this.material_.map.generateMipmaps = true;
  256. }else if(texType == 'photo'){
  257. if(media){
  258. this.texMedia = media;
  259. this.info.texSrc = media.src;
  260. this.material_.map = new THREE.Texture(); //texture也不能共用一个,因为有的会有动画,就不一样
  261. this.material_.map.image = media //image可以共用
  262. this.material_.map.needsUpdate = !0
  263. this.material_.opacity = 1
  264. }else{
  265. if(!this.info.texSrc)return
  266. this._loadDones = []
  267. this.material_.opacity = 0.1;
  268. }
  269. }else if(texType == 'shine'){
  270. if(media){
  271. this.changeShineTex(media)
  272. }else{
  273. this.styleImg = /* this.styleImg || */ [];
  274. if(this.info.styleImg){
  275. this.styleImg = this.info.styleImg.map((src)=>{
  276. return {src:/* manage.dealURL( */src } //如果要dealURL,在predeal里
  277. })
  278. }
  279. this.changeShineTex(this.styleImg)
  280. }
  281. this.info.texSrc = null
  282. }
  283. if(this.material_.map){
  284. /* this.material_.map.minFilter = THREE.LinearFilter;
  285. this.material_.map.magFilter = THREE.LinearFilter; */
  286. dealMap(this.material_.map);
  287. this.material_.color.set("#FFFFFF");
  288. this.material_.needsUpdate = true
  289. }
  290. }
  291. changeShineTex(styleImg){
  292. styleImg = styleImg || this.styleImg
  293. var tex1, tex2
  294. if(styleImg.length){
  295. tex1 = Texture.load(styleImg[0].src);
  296. tex2 = Texture.load(styleImg[1]&&styleImg[1].src || styleImg[0].src);
  297. dealMap(tex1,{ignoreResize:true} )
  298. dealMap(tex2,{ignoreResize:true} )
  299. }else{
  300. tex1 = defaultTex1;
  301. tex2 = defaultTex2;
  302. }
  303. this.changeMaterial(getShineMat(tex1, tex2))
  304. this.styleImg = styleImg
  305. this.info.styleImg = styleImg.map(img=>img.src)
  306. }
  307. changeMaterial(mat){
  308. this.material_ = mat;
  309. this.mesh && this.mesh.traverse((mesh)=>{
  310. if(mesh.material && !(mesh instanceof THREE.Box3Helper)){
  311. mesh.material = this.material_;
  312. }
  313. })
  314. }
  315. preDeal(info, source){// source:来源
  316. var convertValue = function(v ,Type){
  317. var value;
  318. if(v instanceof Array){
  319. v.forEach((v1)=>{v1 = parseFloat(v1)})
  320. value = new Type().fromArray(v);
  321. }else{
  322. if(!(v instanceof Type)){
  323. for(let i in v){v[i] = parseFloat(v[i])}
  324. value = new Type().copy(v);
  325. }else{
  326. value = v
  327. }
  328. }
  329. return value
  330. }
  331. if (!info.transformAtPanos) info.transformAtPanos = {}
  332. if(source == 'byHot'){
  333. var infoAttribute = info.infoAttribute || {}
  334. info.title = infoAttribute.title || info.title
  335. info.model = infoAttribute.model || info.model || [] //模型链接
  336. info.images = infoAttribute.images || info.images || []
  337. info.video = infoAttribute.video || info.video || []
  338. info.bgName = infoAttribute.bgName || info.bgName
  339. info.backgroundMusic = info.backgroundMusic || info.backgroundMusic
  340. info.iframe = infoAttribute.iframe || info.iframe || []
  341. info.styleImg = infoAttribute.styleImg || info.styleImg || []
  342. info.content = infoAttribute.content || info.content
  343. {
  344. let action = CloneObject(settings.hotClickEvent.shine);
  345. if(info.actionType == 'noAction' || info.noAction){
  346. action.examine = false,
  347. action.openHot = false
  348. }else if(info.actionType == 'dontExam'){
  349. action.examine = false
  350. }
  351. info.actionType = action
  352. }
  353. if(info.quaternion){
  354. info.rotation = new THREE.Euler().setFromVector3(convertValue(info.quaternion, THREE.Quaternion ))
  355. }else{
  356. info.rotation = new THREE.Euler().setFromVector3(convertValue(info.rotation, THREE.Vector3 )) //热点的旧数据很多是字符串
  357. }
  358. var s = Hot.getDefaulScale(info.hotIconScale) //旧版的大小,统一转换成新版
  359. info.scale = new THREE.Vector3(s,s,0.02)
  360. delete info.infoAttribute;
  361. /* for (let i in this.transformAtPanos) {
  362. info.transformAtPanos[i].pos = new THREE.Vector3().fromArray(info.transformAtPanos[i].pos)
  363. info.transformAtPanos[i].qua && (info.transformAtPanos[i].qua = new THREE.Quaternion().fromArray(info.transformAtPanos[i].qua))
  364. } */
  365. info.texType = "shine"
  366. }else{
  367. if(source == 'byOverlay'){
  368. info.texType = info.media[0]
  369. //info.title = info.texType == 'video'?'视频':'图片'
  370. info.texSrc = info.file
  371. info.actionType = CloneObject(settings.hotClickEvent[info.texType]);//给一个默认
  372. delete info.media
  373. info.rotation = new THREE.Euler().setFromQuaternion(convertValue(info.qua, THREE.Quaternion ))
  374. info.position = info.pos
  375. delete info.pos;
  376. delete info.qua;
  377. let a = info.texSrc.split('/');
  378. info.fileName = a.pop()
  379. info.scale = new THREE.Vector3(
  380. info.width,
  381. info.height,
  382. info.depth
  383. )
  384. delete info.width; delete info.height; delete info.depth;
  385. delete info.file
  386. }else{
  387. info.rotation = new THREE.Euler().fromArray(info.rotation)//.setFromVector3(info.rotation)
  388. }
  389. info.model = info.model || [] //模型链接
  390. info.images = info.images || []
  391. info.video = info.video || []
  392. info.iframe = info.iframe || []
  393. info.styleImg = info.styleImg || []
  394. info.imagesDesc = info.imagesDesc || []
  395. info.videosDesc = info.videosDesc || []
  396. info.titleShowType = info.titleShowType || 'hover'
  397. info.titlePos = info.titlePos || 'right'
  398. }
  399. if(info.texSrc){
  400. info.texSrc = manage.removeSrcPostMark(info.texSrc)
  401. }
  402. //whole:
  403. //为了兼容旧数据,尽量和hot的数据靠近,最后保存在hot里
  404. info.position = convertValue(info.position, THREE.Vector3)
  405. info.scale = convertValue(info.scale, THREE.Vector3)
  406. delete info.quaternion
  407. info.linkType = info.linkType || "common"
  408. for (let i in info.transformAtPanos) {
  409. info.transformAtPanos[i].pos = new THREE.Vector3().fromArray(info.transformAtPanos[i].pos)
  410. //info.transformAtPanos[i].qua = new THREE.Quaternion().fromArray(info.transformAtPanos[i].qua)
  411. }
  412. }
  413. addBox(state) {
  414. if (state == !!this.hasBox) {
  415. return;
  416. }
  417. if (state) {
  418. var box = new THREE.Mesh(_boxGeometry,_boxMat)
  419. box.position.set(0, 0, 1 / 2);
  420. box.renderOrder = 3
  421. this.plane.position.set(0, 0, 1);
  422. this.add(box);
  423. this.box = box;
  424. } else {
  425. this.plane.position.set(0, 0, 0);
  426. this.remove(this.box);
  427. this.box = null;
  428. }
  429. this.hasBox = this.info.hasBox = state
  430. }
  431. /* getSizeByScale() {
  432. return {
  433. width: settings.defaultOverlaySize[0] * this.scale.x,
  434. height: settings.defaultOverlaySize[1] * this.scale.y
  435. }
  436. }
  437. getScaleBySize(width, height) {
  438. return {
  439. x: width / settings.defaultOverlaySize[0],
  440. y: height / settings.defaultOverlaySize[1],
  441. }
  442. }*/
  443. setVisiblePanos(visibleData){
  444. if(visibleData)this.info.visiblePanos = visibleData
  445. else if(!this.info.visiblePanos) this.getVisiblePanos()
  446. }
  447. getVisiblePanos(){//在不同点还不一样
  448. var depth = this.hasBox ? this.scale.z : 0;
  449. var width = this.scale.x, height = this.scale.y
  450. var cornerPoint
  451. if(this.plane){
  452. cornerPoint = [
  453. new THREE.Vector3(0, 0, depth),
  454. new THREE.Vector3(-width/2, height/2, depth),
  455. new THREE.Vector3(width/2, height/2, depth),
  456. new THREE.Vector3(width/2, -height/2, depth),
  457. new THREE.Vector3(-width/2, -height/2, depth),
  458. ]
  459. }else{
  460. var bound = new THREE.Box3().copy(this.info.modelBound.bound)
  461. var center = bound.center()
  462. cornerPoint = [
  463. new THREE.Vector3(center.x,center.y,center.z),
  464. new THREE.Vector3(bound.min.x, bound.min.y, bound.min.z ),
  465. new THREE.Vector3(bound.min.x, bound.min.y, bound.max.z ),
  466. new THREE.Vector3(bound.min.x, bound.max.y, bound.min.z ),
  467. new THREE.Vector3(bound.max.x, bound.min.y, bound.min.z ),
  468. new THREE.Vector3(bound.max.x, bound.max.y, bound.min.z ),
  469. new THREE.Vector3(bound.max.x, bound.min.y, bound.max.z ),
  470. new THREE.Vector3(bound.min.x, bound.max.y, bound.max.z ),
  471. new THREE.Vector3(bound.max.x, bound.max.y, bound.max.z ),
  472. ];
  473. }
  474. var getPos = (position)=>{//每个overlay位置对应5个坐标,plane中心和四个角的位置
  475. if(this.plane){
  476. return cornerPoint.map(e=>{
  477. return e.clone().applyEuler(this.info.rotation).add(position)
  478. })
  479. }else{
  480. var matrixWorld = new THREE.Matrix4().compose(position, this.quaternion, this.scale)
  481. matrixWorld.multiplyMatrices(matrixWorld, this.mesh.matrix)
  482. return cornerPoint.map(e=>{
  483. return e.clone().applyMatrix4(matrixWorld);
  484. })
  485. }
  486. }
  487. this.info.visiblePanos = []
  488. var customPositions = getPos(this.info.position )
  489. var posAtPanos = {}
  490. for(let panoId in this.info.transformAtPanos){
  491. if(panoId == 'outSide')continue;
  492. posAtPanos[panoId] = getPos(this.info.transformAtPanos[panoId].pos )
  493. }
  494. let maxCount = browser.isMobile() ? 2000 : 5000
  495. let possiblePanos = model.panos.list
  496. if(this.plane){//目前都是单面,所以只要可以看到的一面的热点
  497. let dir1 = new THREE.Vector3(0,0,-1).applyQuaternion(this.quaternion).negate()
  498. possiblePanos = possiblePanos.filter(pano=>{
  499. let dir2 = new THREE.Vector3().subVectors(pano.position, this.position).normalize();
  500. return dir1.dot(dir2)>0
  501. })
  502. }
  503. let c = model.panos.list.length * model.colliders.length
  504. if(window.isEdit || c < maxCount){ //编辑页面保险起见还是全部算完后才可浏览,就能保证保存全部的visiblePano
  505. this.info.visiblePanos = common.getVisiblePano(customPositions, possiblePanos , {
  506. model: model.colliders , posAtPanos
  507. })
  508. }else{
  509. let start = 0
  510. let interval = setInterval(()=>{
  511. let end = start + Hot.visiPanosCountSlice;
  512. end = Math.min(end, possiblePanos.length)
  513. let i = start
  514. start = end
  515. let panos = possiblePanos.slice(i,end)
  516. this.info.visiblePanos = this.info.visiblePanos.concat( common.getVisiblePano(customPositions, panos, {
  517. model: model.colliders , posAtPanos
  518. }));
  519. if(end>=possiblePanos.length){
  520. //console.log(window.hotsi ?(++window.hotsi): (window.hotsi = 1))
  521. clearInterval(interval)
  522. }
  523. }, Hot.visiEveryDurSlice )// visiEveryDurSlice 等在main中定义
  524. }
  525. }
  526. updateVisible(panos, visibility) {
  527. if(window.isEdit && editTool.hotpoint.editSpot == this){
  528. return this.visible = true
  529. }
  530. this.visible = visibility != void 0 ? visibility : (!this.info.visiblePanos || !!panos.find(pano=>this.info.visiblePanos.includes(pano.id)))
  531. this.titleElem && this.titleElem.setVisible(this.visible, 'hotVisible')
  532. if (this.texType == 'video'){
  533. //this.switchPlay(this.visible, this.visible ? null : 'stop' );//可见时不操作;不可见时停止
  534. this.update(player)
  535. }
  536. }
  537. getBoundOri(){
  538. let bound
  539. if(this.objObject){
  540. bound = new THREE.Box3().copy(this.info.modelBound.bound)
  541. }else{
  542. bound = planeBound.clone()
  543. }
  544. return bound
  545. }
  546. getCornerPoint(){//获取在每个漫游点上的视觉边界点 可以打开boxHelper和addBall来观测是否准确
  547. if(this.cornerPoints[player.currentPano.id]){
  548. return this.cornerPoints[player.currentPano.id]
  549. }else{
  550. var boundPoint, cornerPoint
  551. var center//中心点
  552. if(this.plane){
  553. center = this.plane.getWorldPosition()
  554. boundPoint = [
  555. new THREE.Vector3(-0.5, 0.5, 0),
  556. new THREE.Vector3(0.5, 0.5, 0),
  557. new THREE.Vector3(0.5, -0.5, 0),
  558. new THREE.Vector3(-0.5, -0.5, 0),
  559. ]
  560. }else{
  561. var bound = new THREE.Box3().copy(this.info.modelBound.bound)
  562. boundPoint = [
  563. new THREE.Vector3(bound.min.x, bound.min.y, bound.min.z ),
  564. new THREE.Vector3(bound.min.x, bound.min.y, bound.max.z ),
  565. new THREE.Vector3(bound.min.x, bound.max.y, bound.min.z ),
  566. new THREE.Vector3(bound.max.x, bound.min.y, bound.min.z ),
  567. new THREE.Vector3(bound.max.x, bound.max.y, bound.min.z ),
  568. new THREE.Vector3(bound.max.x, bound.min.y, bound.max.z ),
  569. new THREE.Vector3(bound.min.x, bound.max.y, bound.max.z ),
  570. new THREE.Vector3(bound.max.x, bound.max.y, bound.max.z ),
  571. ];
  572. }
  573. var maxLon = -Infinity
  574. var minLon = +Infinity
  575. var maxLat = -Infinity
  576. var minLat = +Infinity
  577. var pos1 = player.currentPano.position.clone();
  578. center = this.position.clone() //模型bound的中心点已经位移到了hot中心点。 注意不能用getWorldPosition,得到的会是偏移的
  579. var dir = center.clone().sub(pos1).normalize()
  580. var centerDirInfo = { }
  581. player.cameraControls.controls.panorama.lookAt.call( centerDirInfo , null, dir )
  582. boundPoint.forEach(e=>{//lon左右
  583. var point = e.applyMatrix4(this.mesh.matrixWorld);
  584. var dir = point.clone().sub(pos1).normalize()
  585. var dirInfo = { }
  586. player.cameraControls.controls.panorama.lookAt.call( dirInfo , null, dir )
  587. var diffLon = (dirInfo.lon-centerDirInfo.lon) % 360
  588. if(Math.abs(diffLon)>180){//因为有时需要根据符号判断是在中心的左边还是右边,所以限制在180内
  589. diffLon += (diffLon>0 ? -360 : 360)
  590. }
  591. var diffLat = dirInfo.lat-centerDirInfo.lat
  592. maxLon = Math.max(diffLon, maxLon)
  593. minLon = Math.min(diffLon, minLon)
  594. maxLat = Math.max(diffLat, maxLat)
  595. minLat = Math.min(diffLat, minLat)
  596. })
  597. var diffLon = maxLon - minLon
  598. var diffLat = maxLat - minLat
  599. if(diffLat>180) {//可能是到了反面。不好算,直接返回所有boundPoint
  600. cornerPoint = boundPoint
  601. }else{
  602. //读取lon lat的最大最小值,勾勒出一个没有倾斜的矩形 。它比boundPoint看起来范围更大些
  603. maxLon = maxLon + centerDirInfo.lon
  604. maxLat = maxLat + centerDirInfo.lat
  605. minLon = minLon + centerDirInfo.lon
  606. minLat = minLat + centerDirInfo.lat
  607. var dirs = [
  608. math.getDirByLonLat(maxLon, maxLat),
  609. math.getDirByLonLat(minLon, minLat),
  610. math.getDirByLonLat(maxLon, minLat),
  611. math.getDirByLonLat(minLon, maxLat),
  612. ]
  613. cornerPoint = dirs.map(e=>{
  614. return e.clone().add(pos1)
  615. })
  616. cornerPoint = [center, ...cornerPoint] //最后增加一个中心点
  617. }
  618. /* if(this.objObject){
  619. cornerPoint = [pos2, ...cornerPoint]
  620. } */
  621. //addPoints(cornerPoint)
  622. this.cornerPoints[player.currentPano.id] = {cornerPoint, diffLon, diffLat}
  623. return this.cornerPoints[player.currentPano.id]
  624. }
  625. }
  626. getMediaSize(){
  627. let size = new THREE.Vector2;
  628. if(this.texMedia){
  629. if(this.texType == 'photo'){
  630. size.x = this.texMedia.width;
  631. size.y = this.texMedia.height;
  632. //动画的话再变
  633. if(this.info.animateInfo){
  634. size.x /= this.info.animateInfo.cellXcount
  635. size.y /= this.info.animateInfo.cellYcount
  636. }
  637. }else{
  638. size.x = this.texMedia.videoWidth || this.videoWidth
  639. size.y = this.texMedia.videoHeight || this.videoHeight
  640. }
  641. }
  642. return size
  643. }
  644. inSight(){
  645. //return true
  646. if(window.isEdit)return true // 太容易move了
  647. if(player.mode == 'panorama' && player.currentPano){
  648. if(!player.camera) return
  649. var cornerPointInfo = this.getCornerPoint();
  650. var cornerPoint
  651. let min = new THREE.Vector2(5,5)
  652. let scaleRatio = 1/player.zoomLevel;
  653. //根据media原始大小来调整阈值: media的原始大小能代表期望显示的大小,如果显示大小的远小于期望大小,就不显示(此时能感受到贴图锯齿严重,清晰度被浪费)。比如如果gif是一个很小的按钮,即使diffLon很小也要显示。缺点:需要用户根据所需上传合适清晰度的图。
  654. let size = this.getMediaSize()
  655. if(size.x>0){
  656. scaleRatio *= Math.sqrt(size.x * size.y) / 1000
  657. }
  658. min.multiplyScalar(scaleRatio)
  659. if(cornerPointInfo.diffLon < min.x || cornerPointInfo.diffLat < min.y ){
  660. //console.log('two far and small')
  661. return false
  662. }
  663. if(cornerPointInfo.diffLon < 15 && cornerPointInfo.diffLat < 15){//当很小的时候,只判断中心点即可
  664. cornerPoint = [cornerPointInfo.cornerPoint[0]];
  665. }else{
  666. cornerPoint = cornerPointInfo.cornerPoint
  667. }
  668. for(let i=0,j=cornerPoint.length;i<j;i++){//只要有一点可见就算看见
  669. var pos2d = math.getPos2d(cornerPoint[i], player.camera, $("#player")[0])
  670. if(pos2d.trueSide && pos2d.inSight) return true
  671. }
  672. }else{//飞出 只判断在不在画面内
  673. //return playVideoWhenFlyOut//true
  674. let frustumMatrix = new THREE.Matrix4
  675. frustumMatrix.multiplyMatrices(player.camera.projectionMatrix, player.camera.matrixWorldInverse)
  676. let frustum = new THREE.Frustum();
  677. frustum.setFromMatrix(frustumMatrix)
  678. let bound = this.getBoundOri()
  679. bound.applyMatrix4(this.matrixWorld)
  680. return frustum.intersectsBox(bound)
  681. }
  682. }
  683. update(player){
  684. if(this.info.isSprite){
  685. this.quaternion.copy(player.camera.quaternion)
  686. }
  687. this.updateScale()
  688. this.updateTitle()
  689. }
  690. updateScale(e, t) {//自适应调节大小
  691. if(!DATA.autoAdjustHotScale || this.texType != 'shine' || !this.plane )return
  692. var scale = convertTool.getScaleForConstantSize($.extend({},autoSizeInfo,{
  693. position: this.position.clone()
  694. }))
  695. this.plane.scale.set(scale,scale,scale)
  696. }
  697. switchPlay(state){//手动播放暂停
  698. this.pausedByUser = !state
  699. this.videoControl(state)
  700. }
  701. loadVideo(video){
  702. if(video.src_)return
  703. video.src = video.src_ = manage.dealURL(this.info.texSrc);
  704. }
  705. videoControl(state){
  706. if(this.texType != "video" || !this.material_.map)return
  707. var video = this.texMedia
  708. this.shouldPlay = state
  709. if(!state || state == 'stop'){
  710. if(!video.paused){
  711. video.pause()
  712. console.log({str: "paused " + this.sid, level:1})
  713. }
  714. /* video.lastCurTime = state == 'stop' ? 0 : video.currentTime //记录
  715. video.src = video.src_ = '' */
  716. if(state == 'stop'){
  717. video.currentTime = 0;
  718. }
  719. }else if(state){
  720. if(/* !isVideoPlayed(video) */ video.paused ){
  721. console.log({str: 'videoControl play ' +", "+ this.sid, level:1})
  722. this.loadVideo(video)
  723. video.play()
  724. //video.currentTime = video.lastCurTime || 0
  725. this.changeOpaWhenPlay(video)
  726. if(isVideoPlayed(video))console.log({str:"played " + this.sid + video.duration ,level:1})
  727. }
  728. }
  729. }
  730. changeOpaWhenPlay(video){//当播放成功后,恢复为不透明
  731. if(isNaN(video.duration)){//未加载好
  732. return setTimeout(()=>{
  733. this.changeOpaWhenPlay(video)
  734. },100)
  735. }
  736. const minPlayedTime = Math.min(video.duration/10, 0.1); //到这个时间说明加载成功
  737. if(!video.hasInitedOpacity){
  738. setTimeout(e=>{
  739. //console.log('trychangeOpa ',this.sid,video.currentTime)
  740. if(video.currentTime>minPlayedTime){
  741. this.material_.opacity = 1
  742. video.hasInitedOpacity = true
  743. //console.log('changeOpaWhenPlay',this.sid)
  744. }else{
  745. if(!video.paused){//再次尝试
  746. return setTimeout(()=>{
  747. this.changeOpaWhenPlay(video)
  748. },500)
  749. }
  750. }
  751. },minPlayedTime*1000+500)
  752. }
  753. }
  754. /* setDefaultHotScale = function(){//设置成默认热点大小
  755. var w = DATA.hotIconScale) * g_HotMeshSize.g_HotMeshWidth
  756. this.scale.set( w, w, this.scale.z)
  757. } */
  758. setTitleElem(){
  759. var title = this.info.title;
  760. if(title){
  761. if(!this.titleElem){
  762. this.titleElem = new Label2D({position:this.position, innerHTML:`<div>${title}</div>`, domElement:$("#hot")[0], shelterByModel:true })
  763. }else{
  764. this.titleElem.elem.html(`<div>${title}</div>`)
  765. }
  766. this.titleElem.setVisible(this.visible, 'hotVisible')
  767. this.setHoverState(false)
  768. this.setTitleDir()
  769. }else{
  770. if(this.titleElem){
  771. this.titleElem.dispose()
  772. this.titleElem = null;
  773. }
  774. }
  775. }
  776. setTitleDir(){
  777. this.titleElem && this.titleElem.elem.attr('pos',this.info.titlePos)
  778. }
  779. updateTitle(){
  780. this.titleElem && this.titleElem.update()
  781. }
  782. setHoverState(state){
  783. if(!this.titleElem)return
  784. this.hovered = !!state
  785. var v = this.info.titleShowType != 'unvisible' && (this.info.titleShowType == 'always' || this.hovered)
  786. this.titleElem.setVisible(v, 'hoveredVisi')
  787. //this.updateTitle()
  788. }
  789. closestPanoTowardTag(e, t) {
  790. var i = []
  791. , n = []
  792. , r = this.mesh.getWorldPosition();
  793. if (e === "panorama") {
  794. /* var o = t.position.clone().sub(r).normalize();
  795. n.push(function(t, i) {//scoreFunctions.direction 最好这个漫游点在currentPano到热点之间的路径上。但是这样的话可能就看不到热点正面,所以删掉
  796. return function(e) {
  797. return e.position.clone().sub(t).normalize().dot(i) * window._settings.navigation.directionFactor
  798. }}(r, o)
  799. ) */
  800. }
  801. var a = new THREE.Vector3;
  802. i.push(function(e) {
  803. return Math.abs(e.position.x - r.x) > window._settings.tags.visibility.cameraClearance || Math.abs(e.position.z - r.z) > window._settings.tags.visibility.cameraClearance
  804. },
  805. function(e) {
  806. a.copy(r).sub(e.position);
  807. var t = -THREE.Math.radToDeg(Math.atan(a.y / Math.sqrt(a.x * a.x + a.z * a.z)))
  808. , i = window._settings.tags.navigate.tiltTolerance
  809. return window._settings.insideLookLimitDown - i < t && t < window._settings.insideLookLimitUp + i
  810. },
  811. (pano)=>{ // add
  812. return player.checkHasNeighbor(pano)
  813. },
  814. (pano)=>{ // add
  815. // 周恩光改 解决visiblePanos为undefined时的bug
  816. return !this.info.visiblePanos || this.info.visiblePanos.includes(pano.id)
  817. }
  818. )
  819. n.push(
  820. (function(hot, i) { //scoreFunctions.distanceSquared
  821. return function(pano) {
  822. //i = a.navigation.distanceFactor
  823. return hot ? hot.position.distanceToSquared(pano.position) * i : 0
  824. }
  825. })(this, -2)
  826. ,
  827. (pano)=>{//尽量正对hot
  828. let dir = new THREE.Vector3(0,0,1).applyQuaternion(this.quaternion)
  829. let dir2 = new THREE.Vector3().subVectors(pano.position, this.position).normalize();
  830. let s = dir.dot(dir2) * 60;
  831. //console.log(pano.id + ":" + s)
  832. return s
  833. }
  834. );
  835. /* var tanD = player.domElement.clientWidth / player.domElement.clientHeight * Math.tan((THREE.Math.degToRad(player.zoomFov/2))) ;
  836. var goodDistance = overlay.width / 2 / tanD;
  837. n.push(
  838. function (pano) { //寻找正对着overlay的pano
  839. var overlayDir = new THREE.Vector3(0,0,1).applyQuaternion(overlay.quaternion)
  840. var v1 = overlayDir//.setY(0); 朝上的话set0会得到0,0,0 无法求angle
  841. var v2 = pano.position.clone().sub(overlay.position)//.setY(0)
  842. var angle = v1.angleTo(v2)
  843. angles[pano.id] = angle;
  844. angle = -angle * 100
  845. return angle
  846. },
  847. function (pano) { //寻找正对着overlay的pano
  848. var dis = pano.position.clone().distanceToSquared(overlay.position)
  849. var goodDistance2 = goodDistance * Math.abs(Math.cos(angles[pano.id]))//考虑了倾斜角度后的最佳距离
  850. var result = - 300 * Math.abs(dis-goodDistance2) / goodDistance2 //dis和goodDistance2差距越大分数越低
  851. return result;
  852. }
  853. ) */
  854. var s = t.model.panos.sortByScore(i, n);
  855. console.log(s)
  856. return s && 0 < s.length && s[0].item
  857. }
  858. examine(options={}) {
  859. var openHot = this.info.link && this.info.actionType.openHot && !options.dontOpen
  860. var fastTran = !options.dontFastTran && ( this.info.actionType.fastTran || settings.hotFastTran || options.fastTran)
  861. if(fastTran){//瞬间过渡 1到固定方位 2到和普通过渡一样的位置,也就是最适合的位置
  862. let info = this.info.cameraData
  863. if(info){
  864. player.blackToPano({
  865. pano: player.model.panos.index[info.pano.uuid],
  866. quaternion: new THREE.Quaternion().fromArray(info.camera.quaternion)
  867. })
  868. }
  869. }
  870. var needExamine = !fastTran && (options.examine || (!settings.dontExamHot && this.info.actionType.examine))
  871. if(!openHot && !needExamine)return;
  872. if(openHot && this.info.linkType!="common" && this.info.iframe && this.info.iframe[0]){
  873. var src = getLink(this.info.iframe[0]);
  874. if(this.info.linkType=="jumpLink"){
  875. var newPage = window.open(src, "_blank" );
  876. newPage.focus();
  877. }else if(this.info.linkType=="iframeDiv"){
  878. var div = document.createElement("div");
  879. div.style.position = 'fixed';
  880. div.style.width = div.style.height = "100%";
  881. div.style.left = div.style.top = '0';
  882. div.style["z-index"] = "999"
  883. var exit = document.createElement("div");
  884. exit.style["background-image"] = "url(images/vrOffImg.png)";
  885. exit.style.position = 'absolute';
  886. exit.style.width = exit.style.height = "50px";
  887. exit.style.left = '17px'; exit.style.top = "20px"
  888. exit.style.cursor = "pointer";
  889. exit.style["background-repeat"] = "no-repeat";
  890. exit.style["background-size"] = "25%";
  891. exit.style["background-position"] = "center center";
  892. exit.style["background-color"] = "rgba(0, 0, 0, 0.2)";
  893. exit.style["border-radius"] = "50%";
  894. exit.style["z-index"] = "3"
  895. exit.onclick = ()=>{
  896. $(div).remove()
  897. Hot.closePopup()
  898. }
  899. var myElement = document.createElement("iframe");
  900. myElement.style.position = 'absolute';
  901. myElement.style.width = myElement.style.height = "100%";
  902. myElement.style.left = myElement.style.top = '0';
  903. myElement.src = src
  904. $("body").append(div);
  905. div.appendChild(exit);
  906. div.appendChild(myElement)
  907. SoundManager.play('hot')
  908. }
  909. return;
  910. }
  911. if(!player.currentPano)return;
  912. var popup = document.getElementById("popup");
  913. if (openHot) {
  914. g_currentHot = this,
  915. popup.style.display = "block",
  916. popup.classList.add("wait");
  917. var n = document.createElement("iframe");
  918. SoundManager.play('hot')
  919. var src = getLink(this.info.link )
  920. n.src = src;
  921. n.id = "id1",
  922. n.allowTransparency = "true";
  923. var a = document.getElementById("id1");
  924. if (void 0 === a || null == a) {
  925. document.querySelector(".popup-content").appendChild(n);
  926. var s = !1;
  927. window.loaddingSuccess = function() {
  928. s = !0
  929. }
  930. ,
  931. setTimeout(function e() {
  932. if (s) {
  933. var t = document.querySelector("#id1").contentWindow.document;
  934. t.querySelector("video") && (t.querySelector("video").play(),
  935. !t.querySelector("video").paused && t.querySelector(".playPause") && t.querySelector(".playPause").classList.add("fa-pause")),
  936. t.querySelector("audio") && t.querySelector("audio").play()
  937. } else
  938. setTimeout(e, 300)
  939. }, 800)
  940. }
  941. }
  942. var done = function() {
  943. player.flyingToTag = !1;
  944. openHot && popup.classList.remove("wait")
  945. }.bind(this);
  946. if(!needExamine){
  947. done()
  948. return;
  949. }
  950. var c = this.closestPanoTowardTag(player.mode, player.currentPano) || player.currentPano
  951. , h = this.mesh.getWorldPosition();
  952. player.flyingToTag = !0;
  953. if (player.mode === 'panorama') {
  954. var d = {
  955. pano: c,
  956. lookAtPoint: h,
  957. duration: options.duration,
  958. maxDistanceOverride: null,
  959. skipWarpingCheck: !1,
  960. aimDuration: options.aimDuration,
  961. };
  962. if(fastTran){
  963. let f = (new THREE.Matrix4).lookAt(c.position, h, new THREE.Vector3(0,1,0));
  964. let quaternion = (new THREE.Quaternion).setFromRotationMatrix(f)
  965. player.blackToPano({
  966. pano:c,
  967. quaternion,
  968. })
  969. done()
  970. }else{
  971. player.flyToPano(d, done)
  972. }
  973. } else {
  974. var p = {
  975. pano: c
  976. };
  977. if (h) {
  978. var f = (new THREE.Matrix4).lookAt(c.position, h, new THREE.Vector3(0,1,0));
  979. p.quaternion = (new THREE.Quaternion).setFromRotationMatrix(f)
  980. }
  981. p.callback = done,
  982. p.duration = options.duration || 1500,
  983. p.mode = 'panorama',
  984. p.aimDuration = options.aimDuration
  985. player.flyToNewMode(p)
  986. }
  987. }
  988. addModel(object){
  989. if(this.objObject){
  990. this.remove(this.objObject)
  991. }
  992. this.objObject = object;
  993. /* object.traverse((mesh)=>{
  994. if(mesh.material && mesh.type == "hotSprite"){
  995. mesh.material = this.material_;
  996. }
  997. }) */
  998. object.name = this.info.objName;
  999. object.src = this.info.objSrc
  1000. this.info.hasBox = false
  1001. this.addBox(false)
  1002. this.remove(this.plane);
  1003. this.plane = null;
  1004. this.setMesh(this.objObject)
  1005. //this.adjustModelAuto()
  1006. if(this.info.modelBound){//应该不会改变
  1007. var s = this.info.modelBound.scaleRatio
  1008. this.mesh.scale.set(s,s,s)
  1009. this.mesh.position.fromArray(this.info.modelBound.position)
  1010. this.mesh.modelBound = this.info.modelBound
  1011. }
  1012. this.material_.side = THREE.FrontSide
  1013. this.changeBoxHelperDisplay(false)
  1014. //this.mesh.boxHelper.visible = true
  1015. }
  1016. addPlane(){//换成plane
  1017. if(this.plane)return
  1018. this.plane = new THREE.Mesh(_planeGeometry, this.material_)
  1019. this.remove(this.objObject)
  1020. this.objObject = null
  1021. delete this.info.objSrc
  1022. delete this.info.objName
  1023. delete this.info.modelBound
  1024. this.setMesh(this.plane)
  1025. //this.material_.side = THREE.DoubleSide //双面的话飞出来会看到悬空的
  1026. }
  1027. setMesh(mesh){
  1028. this.mesh = mesh
  1029. this.add(this.mesh);
  1030. this.mesh.renderOrder = 3
  1031. this.changeMaterial(this.material_) //re applyTo every mesh
  1032. this.mesh.traverse((mesh)=>{
  1033. mesh.type = "hotSprite" //raycaster use
  1034. })
  1035. if(!this.mesh.boxHelper){
  1036. var boxHelper = this.mesh.children.find(e=>e instanceof THREE.Box3Helper)
  1037. if(boxHelper){
  1038. this.mesh.boxHelper = boxHelper
  1039. }else{
  1040. var bound = this.getBoundOri()
  1041. bound.expandByVector(new THREE.Vector3(0.0001,0.0001,0.0001))
  1042. this.mesh.boxHelper = new THREE.Box3Helper( bound, new THREE.Color( "#00ffff"));
  1043. this.mesh.add(this.mesh.boxHelper)
  1044. this.mesh.boxHelper.material.depthTest = false;
  1045. this.mesh.boxHelper.material.transparent = true
  1046. this.mesh.boxHelper.visible = false
  1047. }
  1048. }
  1049. }
  1050. changeBoxHelperDisplay(show){
  1051. if(show){
  1052. this.visible_ = this.visible
  1053. this.visible = true
  1054. this.mesh.boxHelper.visible = true
  1055. }else{
  1056. if(this.visible_ != void 0){
  1057. this.visible = this.visible_
  1058. }
  1059. this.mesh.boxHelper.visible = false
  1060. }
  1061. }
  1062. /* addToLoadQueue() {
  1063. if (this.texType == 'photo') {
  1064. Hot.loadQueue.includes(this) || Hot.loadQueue.push(this)
  1065. }
  1066. } */
  1067. requestDownload(type, callback) {
  1068. var plane = this.plane;
  1069. if(type == 'photo'){
  1070. if(this.photoHasRequestLoad || this.texType != 'photo'){
  1071. return; //为什么之前1191需要在这加callback() 才能呢。现在又没事了
  1072. }
  1073. //console.log(' beginDownload : ' + this.sid)
  1074. /* this.material_.map = */Texture.load(this.info.texSrc, (tex)=>{
  1075. callback && callback()
  1076. if(!tex.image ){
  1077. return //只是单纯用了相同src的tex,但image仍未加载完
  1078. }
  1079. if(!this._loadDones){
  1080. return
  1081. }
  1082. dealMap(tex)
  1083. setTimeout(Hot.loadNext, 50)
  1084. hotGroup.children.forEach(e=>{
  1085. if(e.info.texSrc == this.info.texSrc && e.info.texType == type ){
  1086. e.material_.color.set("#FFFFFF")
  1087. e.material_.opacity = 1;
  1088. //console.log('overlay loaded: ' + e.sid + " - " + this.info.texSrc.split('/').pop());
  1089. e.texMedia = tex.image
  1090. {//animation不同致使的不能使用同一个texture
  1091. if(window.isEdit){
  1092. if(animateTexSrcs[e.info.texSrc]){
  1093. e.material_.map = tex.clone(); //编辑动画直接不用一个texture, 故而animation也不同
  1094. e.material_.map.needsUpdate = true
  1095. }else{
  1096. e.material_.map = tex
  1097. animateTexSrcs[e.info.texSrc] = 1
  1098. }
  1099. }else{
  1100. if(animateTexSrcs[e.info.texSrc]){//已有该texSrc
  1101. let finded = false
  1102. for(let i of animateTexSrcs[e.info.texSrc]){
  1103. if(ifSame(i[0], e.info.animateInfo)){
  1104. e.material_.map = i[1]; finded = true; break;
  1105. }
  1106. }
  1107. if(!finded){
  1108. let tex_ = tex.clone();
  1109. tex_.needsUpdate = true //clone后不写这句会黑块
  1110. animateTexSrcs[e.info.texSrc].set(e.info.animateInfo, tex_)
  1111. e.material_.map = tex_
  1112. }
  1113. }else{
  1114. let object = new Map();
  1115. object.set(e.info.animateInfo, tex)
  1116. animateTexSrcs[e.info.texSrc] = object//注册第一个texSrc
  1117. e.material_.map = tex
  1118. }
  1119. }
  1120. }
  1121. if(e.info.animateInfo && !e.animation){
  1122. e.animation = GifTexDeal.addAnimation(e.material_.map, e, e.info.animateInfo, e.sid )
  1123. e.visible && e.inSight() && GifTexDeal.start(e.animation)
  1124. }
  1125. if(++photoLoaded == originPhotoCount){//data2.js中的所有photo加载完毕
  1126. Hot.allPhotoLoaded = true;
  1127. Hot.whenAllFileLoaded && Hot.allModelLoaded && Hot.whenAllFileLoaded()
  1128. }
  1129. {
  1130. e._loadDones.forEach(a=>a())
  1131. e._loadDones = null
  1132. //e.photoHasRequestLoad = true //这句不能加,否则会无法执行callback
  1133. }
  1134. e.material_.needsUpdate = true
  1135. }
  1136. })
  1137. })
  1138. this.photoHasRequestLoad = true
  1139. }else if(type == 'model'){
  1140. if(this.modelHasRequestLoad || !this.info.objSrc)return;
  1141. //需要处理重复?
  1142. objLoader.load(this.info.objSrc, (object)=>{
  1143. this.remove(this.mesh)
  1144. this.addModel(object)
  1145. callback && callback()
  1146. if(++modelLoaded == originModelCount){//data2.js中的所有photo加载完毕
  1147. Hot.allModelLoaded = true;
  1148. Hot.whenAllFileLoaded && Hot.allPhotoLoaded && Hot.whenAllFileLoaded()
  1149. }
  1150. })
  1151. this.modelHasRequestLoad = true
  1152. }
  1153. }
  1154. dispose(){
  1155. this.parent.remove(this)
  1156. this.titleElem && this.titleElem.dispose()
  1157. delete player.model.hots[this.sid]
  1158. }
  1159. }
  1160. Hot.updateVisibles = function(panos) { //只显示没被遮挡的,否则会卡
  1161. if (panos === true) {
  1162. model.hotGroup.children.forEach(e=>e.updateVisible(null,true))
  1163. } else {
  1164. model.hotGroup.children.forEach(e=>e.updateVisible(panos))
  1165. }
  1166. }
  1167. Hot.beginShineHot = function(){
  1168. if(!window.isEdit && shineMats.length == 0)return
  1169. transitions.trigger({
  1170. func: function(e) {
  1171. var opa = e <= .5 ? 2 * e : -2 * e + 2
  1172. shineMats.forEach(mat=>{
  1173. mat.uniforms.opac.value = opa;
  1174. })
  1175. },
  1176. cycling: !0,
  1177. duration: 3e3,
  1178. name: "hotShine"
  1179. })
  1180. }
  1181. Hot.getDefaulScale = function(hotIconScale){
  1182. return (hotIconScale || DATA.hotIconScale) * g_HotMeshSize.g_HotMeshWidth
  1183. }
  1184. var loadings = [];
  1185. Hot.loadQueue = []; //等待下载的overlay,目前只针对photo
  1186. Hot.maxLoadingCount = 3; //同时正在load图片的数量
  1187. Hot.loadNext = ()=>{//继续requestDownload loadQueue中前排的item
  1188. let count = Hot.maxLoadingCount - loadings.length
  1189. Hot.loadQueue.slice(0, count).forEach(e=>{
  1190. loadings.push(e)
  1191. //console.log('requestDownload', e.hot.info.texSrc)
  1192. e.hot.requestDownload(e.type, ()=>{
  1193. var i = loadings.indexOf(e)
  1194. //console.log('requestDownloaded index', i)
  1195. i > -1 && loadings.splice(i,1)
  1196. })
  1197. })
  1198. Hot.loadQueue.splice(0, count)
  1199. }
  1200. Hot.getNeedLoad = function() {//计算获取loadQueue,每次都重新计算,覆盖旧的
  1201. if (!player || !player.domElement || !player.mode)
  1202. return;
  1203. var hots1, hots2
  1204. if (player.mode != 'panorama') {
  1205. if (Hot.loadQueue.length == 0) {
  1206. hots1 = model.hotGroup.children.filter(e=>e.texType == 'photo' && !e.photoHasRequestLoad )
  1207. hots2 = model.hotGroup.children.filter(e=>e.info.objSrc && !e.modelHasRequestLoad )
  1208. Hot.loadQueue = hots1.map(e=>{return {hot:e, type:"photo"}}).concat(
  1209. hots2.map(e=>{return {hot:e, type:"model"}})
  1210. )
  1211. }
  1212. return
  1213. }
  1214. //Hot.loadWhenOutside = true
  1215. hots1 = model.hotGroup.children.filter(e=>e.texType == 'photo' && !e.photoHasRequestLoad && (!e.info.visiblePanos || e.info.visiblePanos.includes(player.currentPano.id)))
  1216. hots2 = model.hotGroup.children.filter(e=>e.info.objSrc && !e.modelHasRequestLoad && (!e.info.visiblePanos || e.info.visiblePanos.includes(player.currentPano.id)))
  1217. if(hots1.length+hots2.length == 0){
  1218. hots1 = model.hotGroup.children.filter(e=>e.texType == 'photo' && !e.photoHasRequestLoad )
  1219. hots2 = model.hotGroup.children.filter(e=>e.info.objSrc && !e.modelHasRequestLoad )
  1220. }
  1221. var cameraDir = player.getDirection()
  1222. Hot.loadQueue = hots1.map(e=>{return {hot:e, type:"photo"}}).concat(
  1223. hots2.map(e=>{return {hot:e, type:"model"}})
  1224. )
  1225. var request = [(item)=>{
  1226. return true
  1227. }];
  1228. var rank = [(item)=>{
  1229. var dis = item.hot.mesh.getWorldPosition().distanceTo(player.position);
  1230. return -dis
  1231. }
  1232. , (item)=>{
  1233. var tagDir = item.hot.mesh.getWorldPosition().sub(player.position)
  1234. var angle = tagDir.angleTo(cameraDir)
  1235. return -angle * 20
  1236. }]
  1237. var result = common.sortByScore(Hot.loadQueue, request, rank);
  1238. //Hot.loadQueue = result ? result.slice(0, 5).map(e=>e.item) : model.hotGroup.children.filter(e=>e.texType == 'photo' && !e.hasRequestLoad).slice(0, 2);
  1239. Hot.loadQueue = result ? result.slice(0, 5).map(e=>e.item) : []
  1240. }
  1241. Hot.load = ()=>{//开始下载图片
  1242. Hot.getNeedLoad()
  1243. Hot.loadNext()
  1244. var hots1 = model.hotGroup.children.filter(e=>e.texType == 'photo' && !e.photoHasRequestLoad )
  1245. var hots2 = model.hotGroup.children.filter(e=>e.info.objSrc && !e.modelHasRequestLoad )
  1246. if (hots1.length+hots2.length > 0) {
  1247. setTimeout(Hot.load, 200)
  1248. } else {
  1249. Hot.allRequestLoad = true
  1250. console.log('allRequestLoad')
  1251. }
  1252. }
  1253. Hot.startLoad = ()=>{
  1254. originPhotoCount = hotGroup.children.filter(e=>e.texType == 'photo').length
  1255. originModelCount = hotGroup.children.filter(e=>!!e.info.objSrc).length
  1256. if(originPhotoCount == 0 )Hot.allPhotoLoaded = true
  1257. if(originModelCount == 0) Hot.allModelLoaded = true;
  1258. if(Hot.allModelLoaded && Hot.allPhotoLoaded) Hot.whenAllFileLoaded && Hot.whenAllFileLoaded()//所有加载完毕
  1259. else{
  1260. Hot.load()
  1261. }
  1262. setTimeout(Hot.beginShineHot, 1000)
  1263. player.emit('gotHotAndStartload')
  1264. }
  1265. window.Hot = Hot
  1266. /* var ball = new THREE.Mesh(new THREE.SphereBufferGeometry(0.01),new THREE.MeshBasicMaterial({color:"#f00",depthTest:false,transparent:true}))
  1267. var balls = []
  1268. var addPoint = function(point){
  1269. console.log(point)
  1270. var ball1 = ball.clone()
  1271. model.add(ball1);
  1272. ball1.position.copy(point)
  1273. balls.push(ball1)
  1274. }
  1275. var addPoints = function(points){
  1276. balls.forEach(e=>model.remove(e))
  1277. balls = []
  1278. points.forEach(e=>addPoint(e))
  1279. }
  1280. */
  1281. //判断是否是移动端,如果是给关闭按钮添加touchstart事件
  1282. Hot.closePopup = ()=>{// 关闭热点页面
  1283. if(!g_currentHot) return;
  1284. g_currentHot = null;
  1285. var hotPop = document.getElementById('popup');
  1286. hotPop.style.display = "none";
  1287. document.querySelector(".popup-content").removeChild(document.getElementById("id1"));
  1288. $("#popup iframe:last").remove();
  1289. SoundManager.pause('hot', true)//自动播放被中断的音频 (bgm
  1290. return false
  1291. }
  1292. if(browser.isMobile()){
  1293. $('#closepop').on("touchstart",Hot.closePopup);
  1294. }else{
  1295. $('#closepop').on("click",Hot.closePopup);
  1296. }
  1297. Hot.createHotList = function() {
  1298. if(!window.DATA.showHotListSta)return
  1299. var docFragment = document.createDocumentFragment();
  1300. var hots = hotGroup.children.filter(hot=>hot.info.actionType.openHot);
  1301. hots = hots.sort((a,b)=>{return a.order - b.order});
  1302. hots.forEach((hot)=>{
  1303. var li = document.createElement('li');
  1304. var span = document.createElement('span');
  1305. span.innerHTML = hot.info.title || '热点';
  1306. // console.log(span.innerHTML);
  1307. li.hot = hot; // 列表每一项对应一个热点
  1308. li.appendChild(span);
  1309. docFragment.appendChild(li);
  1310. })
  1311. setTimeout(()=>{
  1312. var ul = document.querySelector('#hotListContent ul');
  1313. ul && ul.appendChild(docFragment);
  1314. },3000) //部分安卓手机的钉钉和支付宝浏览器如果立即添加到列表会卡住
  1315. }
  1316. Hot.mobileAutoPlay = function(player){//移动端。不这么写video不会播放 . (2022.11.29: 可为何加了Hot.updateHots之后又会自动播了?https有关?
  1317. if(browser.isMobile()){
  1318. hotGroup.children.forEach((hot)=>{
  1319. /* if(hot.texType == 'video' && hot.shouldPlay){
  1320. console.log(1)
  1321. } */
  1322. if(!hot.clickToPlayInited && hot.texType == 'video' && !isVideoPlayed(hot.texMedia) && hot.shouldPlay){
  1323. //hot.update(player)
  1324. console.log({str:'try mobileAutoPlay '+ hot.sid, level:1})
  1325. hot.videoControl(true)
  1326. if(isVideoPlayed(hot.texMedia)){
  1327. console.log({str:'clickToPlayInited '+ hot.sid, level:1})
  1328. hot.clickToPlayInited = true
  1329. hot.changeOpaWhenPlay(video)
  1330. }
  1331. }
  1332. })
  1333. }
  1334. }
  1335. Hot.updateHots = function(){
  1336. for(var i in player.model.hots){
  1337. player.model.hots[i].update(player)
  1338. }
  1339. if( !player.ready)return
  1340. this.needUpdate = true
  1341. common.intervalTool.isWaiting('updateHots', ()=>{ //延时update,防止卡顿
  1342. if(!this.needUpdate)return
  1343. this.needUpdate = false
  1344. let videoCanPlay = [], aniCanPlay = [];
  1345. for(var i in player.model.hots){
  1346. let hot = player.model.hots[i]
  1347. {//实时监测播放
  1348. if(hot.texType == "video" && !player.flying){ //飞行时不判断
  1349. if(hot.visible && !hot.pausedByUser && hot.inSight()){ //注意edit时insight一直为true
  1350. //this.videoControl(true)
  1351. videoCanPlay.push(hot)
  1352. }else{
  1353. hot.videoControl(false)
  1354. }
  1355. }else if(hot.info.animateInfo){
  1356. if(hot.visible && hot.inSight()){
  1357. aniCanPlay.push(hot)
  1358. }else{
  1359. GifTexDeal.stop(hot.animation)
  1360. }
  1361. }
  1362. }
  1363. }
  1364. let filter = (type,max,list,playFun)=>{
  1365. if(list.length < max){
  1366. list.forEach(hot=>type == 'video' ? hot.videoControl(true) : GifTexDeal.start(hot.animation))
  1367. }else{
  1368. let playerDir = player.getDirection()
  1369. let request = []
  1370. let planeDir = new Map()
  1371. if(player.mode != 'panorama'){
  1372. request.push((hot)=>{
  1373. if(hot.plane){//考虑plane的方向,它甚至可能背对镜头
  1374. let dir = new THREE.Vector3(0,0,-1).applyQuaternion(hot.quaternion)
  1375. let angle = dir.dot(playerDir)
  1376. planeDir.set(hot, angle)
  1377. return angle > 0
  1378. }else return true
  1379. })
  1380. }
  1381. let r = common.sortByScore(list,request,[(hot)=>{ //方向因素
  1382. var cornerPointInfo = hot.getCornerPoint(); //仅支持漫游模式
  1383. let dir = new THREE.Vector3().subVectors(hot.position, player.position).normalize()
  1384. score = dir.dot(playerDir)
  1385. player.mode == 'panorama' && (score *= Math.pow(cornerPointInfo.diffLat,0.5))// pow降低高度的权重,因为宽度更重要些。之所以两个函数都乘以diffLat,也是为了防止在两个item的分数相同diffLon不同时,diffLat增长相同倍数却能造成分数差异的情况
  1386. return score
  1387. },
  1388. (hot)=>{//面积因素
  1389. if(player.mode == 'panorama'){
  1390. var cornerPointInfo = hot.getCornerPoint(); //仅支持漫游模式
  1391. let area = cornerPointInfo.diffLon * Math.pow(cornerPointInfo.diffLat,0.5) / 1000 //占据面积
  1392. return area
  1393. }else{
  1394. let dis = player.position.distanceTo(hot.position);
  1395. let size = hot.getBoundOri().applyMatrix4(hot.matrixWorld).size(new THREE.Vector3).length() / 2
  1396. let score = Math.atan(size / Math.pow(dis,1.2)) * 50 // 在镜头中所占fov angle 的一半 。 pow是因为实际感受还是尽量显示近处的
  1397. if(hot.plane){//考虑plane的方向,越倾斜分越低
  1398. score *= planeDir.get(hot)
  1399. }
  1400. return score
  1401. }
  1402. } ])
  1403. //getCornerPoint仅支持漫游模式 Lat高度(纬度).
  1404. //console.log(r)
  1405. r && r.forEach((e,j)=>{
  1406. let hot = e.item
  1407. if(j<max){
  1408. type == 'video' ? hot.videoControl(true) : GifTexDeal.start(hot.animation)
  1409. }else{
  1410. type == 'video' ? hot.videoControl(false) : GifTexDeal.stop(hot.animation)
  1411. }
  1412. })
  1413. }
  1414. }
  1415. filter('video',playVideoMax, videoCanPlay);
  1416. filter('animateInfo',playAniMax, aniCanPlay);
  1417. //console.log('updateHots')
  1418. return true
  1419. }, 800)
  1420. }
  1421. }
  1422. function isVideoPlayed(video){
  1423. return !video.paused && !isNaN(video.duration) //注意,有的手机首次play时会立即paused为false,但其实没加载好, duration为NAN
  1424. }
  1425. /*
  1426. 保存 JSON.stringify(editTool.hotpoint.getSavingInfo())
  1427. 可能需要再写一份保存到overlay 给旧场景项目使用
  1428. 最好后台有针对手机版的做一个压缩。压缩成几个档位。
  1429. 安卓手机firefox出现过视频mesh不可见或者闪烁的情况。
  1430. 视频最容易导致崩溃, 模型还好
  1431. 数据速率为4064kbps,1920*1080px 时测试部门电脑崩溃
  1432. 数据速率为1824kbps,720 *576px 时正常
  1433. 所以尽量降到2000以下 同时播放个数最好不超过2个 可能需要将src归零 并延迟加载、不自动播放
  1434. 有遇到某张图在ios里显示不出,但在ps重新输出后就可以。这张图的dpi高达500,不知道是否与此有关。
  1435. 遇到过ios14.8.1、14.7.1 无法播放webm(来自1050)。 onloadedmetadata失败
  1436. */