Hot.js 76 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948
  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. //let renderSize = player.sceneRenderer.renderer.domElement
  693. let renderSize = player.sceneRenderer.renderer.getSize()
  694. var scale = convertTool.getScaleForConstantSize($.extend(autoSizeInfo,{
  695. camera:player.camera, resolution:{x:renderSize.width, y:renderSize.height},
  696. position: this.position.clone() ,
  697. }))
  698. this.plane.scale.set(scale,scale,scale)
  699. }
  700. switchPlay(state){//手动播放暂停
  701. this.pausedByUser = !state
  702. this.videoControl(state)
  703. }
  704. loadVideo(video){
  705. if(video.src_)return
  706. video.src = video.src_ = manage.dealURL(this.info.texSrc);
  707. }
  708. videoControl(state){
  709. if(this.texType != "video" || !this.material_.map)return
  710. var video = this.texMedia
  711. this.shouldPlay = state
  712. if(!state || state == 'stop'){
  713. if(!video.paused){
  714. video.pause()
  715. console.log({str: "paused " + this.sid, level:1})
  716. }
  717. /* video.lastCurTime = state == 'stop' ? 0 : video.currentTime //记录
  718. video.src = video.src_ = '' */
  719. if(state == 'stop'){
  720. video.currentTime = 0;
  721. }
  722. }else if(state){
  723. if(/* !isVideoPlayed(video) */ video.paused ){
  724. console.log({str: 'videoControl play ' +", "+ this.sid, level:1})
  725. this.loadVideo(video)
  726. video.play()
  727. //video.currentTime = video.lastCurTime || 0
  728. this.changeOpaWhenPlay(video)
  729. if(isVideoPlayed(video))console.log({str:"played " + this.sid + video.duration ,level:1})
  730. }
  731. }
  732. }
  733. changeOpaWhenPlay(video){//当播放成功后,恢复为不透明
  734. if(isNaN(video.duration)){//未加载好
  735. return setTimeout(()=>{
  736. this.changeOpaWhenPlay(video)
  737. },100)
  738. }
  739. const minPlayedTime = Math.min(video.duration/10, 0.1); //到这个时间说明加载成功
  740. if(!video.hasInitedOpacity){
  741. setTimeout(e=>{
  742. //console.log('trychangeOpa ',this.sid,video.currentTime)
  743. if(video.currentTime>minPlayedTime){
  744. this.material_.opacity = 1
  745. video.hasInitedOpacity = true
  746. //console.log('changeOpaWhenPlay',this.sid)
  747. }else{
  748. if(!video.paused){//再次尝试
  749. return setTimeout(()=>{
  750. this.changeOpaWhenPlay(video)
  751. },500)
  752. }
  753. }
  754. },minPlayedTime*1000+500)
  755. }
  756. }
  757. /* setDefaultHotScale = function(){//设置成默认热点大小
  758. var w = DATA.hotIconScale) * g_HotMeshSize.g_HotMeshWidth
  759. this.scale.set( w, w, this.scale.z)
  760. } */
  761. setTitleElem(){
  762. var title = this.info.title;
  763. if(title){
  764. if(!this.titleElem){
  765. this.titleElem = new Label2D({position:this.position, innerHTML:`<div>${title}</div>`, domElement:$("#hot")[0], shelterByModel:true })
  766. }else{
  767. this.titleElem.elem.html(`<div>${title}</div>`)
  768. }
  769. this.titleElem.setVisible(this.visible, 'hotVisible')
  770. this.setHoverState(false)
  771. this.setTitleDir()
  772. }else{
  773. if(this.titleElem){
  774. this.titleElem.dispose()
  775. this.titleElem = null;
  776. }
  777. }
  778. }
  779. setTitleDir(){
  780. this.titleElem && this.titleElem.elem.attr('pos',this.info.titlePos)
  781. }
  782. updateTitle(){
  783. this.titleElem && this.titleElem.update()
  784. }
  785. setHoverState(state){
  786. if(!this.titleElem)return
  787. this.hovered = !!state
  788. var v = this.info.titleShowType != 'unvisible' && (this.info.titleShowType == 'always' || this.hovered)
  789. this.titleElem.setVisible(v, 'hoveredVisi')
  790. //this.updateTitle()
  791. }
  792. closestPanoTowardTag(e, t) {
  793. var i = []
  794. , n = []
  795. , r = this.mesh.getWorldPosition();
  796. if (e === "panorama") {
  797. /* var o = t.position.clone().sub(r).normalize();
  798. n.push(function(t, i) {//scoreFunctions.direction 最好这个漫游点在currentPano到热点之间的路径上。但是这样的话可能就看不到热点正面,所以删掉
  799. return function(e) {
  800. return e.position.clone().sub(t).normalize().dot(i) * window._settings.navigation.directionFactor
  801. }}(r, o)
  802. ) */
  803. }
  804. var a = new THREE.Vector3;
  805. i.push(function(e) {
  806. 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
  807. },
  808. function(e) {
  809. a.copy(r).sub(e.position);
  810. var t = -THREE.Math.radToDeg(Math.atan(a.y / Math.sqrt(a.x * a.x + a.z * a.z)))
  811. , i = window._settings.tags.navigate.tiltTolerance
  812. return window._settings.insideLookLimitDown - i < t && t < window._settings.insideLookLimitUp + i
  813. },
  814. (pano)=>{ // add
  815. return player.checkHasNeighbor(pano)
  816. },
  817. (pano)=>{ // add
  818. // 周恩光改 解决visiblePanos为undefined时的bug
  819. return !this.info.visiblePanos || this.info.visiblePanos.includes(pano.id)
  820. }
  821. )
  822. n.push(
  823. (function(hot, i) { //scoreFunctions.distanceSquared
  824. return function(pano) {
  825. //i = a.navigation.distanceFactor
  826. return hot ? hot.position.distanceToSquared(pano.position) * i : 0
  827. }
  828. })(this, -2)
  829. ,
  830. (pano)=>{//尽量正对hot
  831. let dir = new THREE.Vector3(0,0,1).applyQuaternion(this.quaternion)
  832. let dir2 = new THREE.Vector3().subVectors(pano.position, this.position).normalize();
  833. let s = dir.dot(dir2) * 60;
  834. //console.log(pano.id + ":" + s)
  835. return s
  836. }
  837. );
  838. /* var tanD = player.domElement.clientWidth / player.domElement.clientHeight * Math.tan((THREE.Math.degToRad(player.zoomFov/2))) ;
  839. var goodDistance = overlay.width / 2 / tanD;
  840. n.push(
  841. function (pano) { //寻找正对着overlay的pano
  842. var overlayDir = new THREE.Vector3(0,0,1).applyQuaternion(overlay.quaternion)
  843. var v1 = overlayDir//.setY(0); 朝上的话set0会得到0,0,0 无法求angle
  844. var v2 = pano.position.clone().sub(overlay.position)//.setY(0)
  845. var angle = v1.angleTo(v2)
  846. angles[pano.id] = angle;
  847. angle = -angle * 100
  848. return angle
  849. },
  850. function (pano) { //寻找正对着overlay的pano
  851. var dis = pano.position.clone().distanceToSquared(overlay.position)
  852. var goodDistance2 = goodDistance * Math.abs(Math.cos(angles[pano.id]))//考虑了倾斜角度后的最佳距离
  853. var result = - 300 * Math.abs(dis-goodDistance2) / goodDistance2 //dis和goodDistance2差距越大分数越低
  854. return result;
  855. }
  856. ) */
  857. var s = t.model.panos.sortByScore(i, n);
  858. console.log(s)
  859. return s && 0 < s.length && s[0].item
  860. }
  861. examine(options={}) {
  862. var openHot = this.info.link && this.info.actionType.openHot && !options.dontOpen
  863. var fastTran = !options.dontFastTran && ( this.info.actionType.fastTran || settings.hotFastTran || options.fastTran)
  864. if(fastTran){//瞬间过渡 1到固定方位 2到和普通过渡一样的位置,也就是最适合的位置
  865. let info = this.info.cameraData
  866. if(info){
  867. player.blackToPano({
  868. pano: player.model.panos.index[info.pano.uuid],
  869. quaternion: new THREE.Quaternion().fromArray(info.camera.quaternion)
  870. })
  871. }
  872. }
  873. var needExamine = !fastTran && (options.examine || (!settings.dontExamHot && this.info.actionType.examine))
  874. if(!openHot && !needExamine)return;
  875. if(openHot && this.info.linkType!="common" && this.info.iframe && this.info.iframe[0]){
  876. var src = getLink(this.info.iframe[0]);
  877. if(this.info.linkType=="jumpLink"){
  878. var newPage = window.open(src, "_blank" );
  879. newPage.focus();
  880. }else if(this.info.linkType=="iframeDiv"){
  881. var div = document.createElement("div");
  882. div.style.position = 'fixed';
  883. div.style.width = div.style.height = "100%";
  884. div.style.left = div.style.top = '0';
  885. div.style["z-index"] = "999"
  886. var exit = document.createElement("div");
  887. exit.style["background-image"] = "url(images/vrOffImg.png)";
  888. exit.style.position = 'absolute';
  889. exit.style.width = exit.style.height = "50px";
  890. exit.style.left = '17px'; exit.style.top = "20px"
  891. exit.style.cursor = "pointer";
  892. exit.style["background-repeat"] = "no-repeat";
  893. exit.style["background-size"] = "25%";
  894. exit.style["background-position"] = "center center";
  895. exit.style["background-color"] = "rgba(0, 0, 0, 0.2)";
  896. exit.style["border-radius"] = "50%";
  897. exit.style["z-index"] = "3"
  898. exit.onclick = ()=>{
  899. $(div).remove()
  900. Hot.closePopup()
  901. }
  902. var myElement = document.createElement("iframe");
  903. myElement.style.position = 'absolute';
  904. myElement.style.width = myElement.style.height = "100%";
  905. myElement.style.left = myElement.style.top = '0';
  906. myElement.src = src
  907. $("body").append(div);
  908. div.appendChild(exit);
  909. div.appendChild(myElement)
  910. SoundManager.play('hot')
  911. }
  912. return;
  913. }
  914. if(!player.currentPano)return;
  915. var popup = document.getElementById("popup");
  916. if (openHot) {
  917. g_currentHot = this,
  918. popup.style.display = "block",
  919. popup.classList.add("wait");
  920. var n = document.createElement("iframe");
  921. SoundManager.play('hot')
  922. var src = getLink(this.info.link )
  923. n.src = src;
  924. n.id = "id1",
  925. n.allowTransparency = "true";
  926. var a = document.getElementById("id1");
  927. if (void 0 === a || null == a) {
  928. document.querySelector(".popup-content").appendChild(n);
  929. var s = !1;
  930. window.loaddingSuccess = function() {
  931. s = !0
  932. }
  933. ,
  934. setTimeout(function e() {
  935. if (s) {
  936. var t = document.querySelector("#id1").contentWindow.document;
  937. t.querySelector("video") && (t.querySelector("video").play(),
  938. !t.querySelector("video").paused && t.querySelector(".playPause") && t.querySelector(".playPause").classList.add("fa-pause")),
  939. t.querySelector("audio") && t.querySelector("audio").play()
  940. } else
  941. setTimeout(e, 300)
  942. }, 800)
  943. }
  944. }
  945. var done = function() {
  946. player.flyingToTag = !1;
  947. openHot && popup.classList.remove("wait")
  948. }.bind(this);
  949. if(!needExamine){
  950. done()
  951. return;
  952. }
  953. var c = this.closestPanoTowardTag(player.mode, player.currentPano) || player.currentPano
  954. , h = this.mesh.getWorldPosition();
  955. player.flyingToTag = !0;
  956. if (player.mode === 'panorama') {
  957. var d = {
  958. pano: c,
  959. lookAtPoint: h,
  960. duration: options.duration,
  961. maxDistanceOverride: null,
  962. skipWarpingCheck: !1,
  963. aimDuration: options.aimDuration,
  964. };
  965. if(fastTran){
  966. let f = (new THREE.Matrix4).lookAt(c.position, h, new THREE.Vector3(0,1,0));
  967. let quaternion = (new THREE.Quaternion).setFromRotationMatrix(f)
  968. player.blackToPano({
  969. pano:c,
  970. quaternion,
  971. })
  972. done()
  973. }else{
  974. player.flyToPano(d, done)
  975. }
  976. } else {
  977. var p = {
  978. pano: c
  979. };
  980. if (h) {
  981. var f = (new THREE.Matrix4).lookAt(c.position, h, new THREE.Vector3(0,1,0));
  982. p.quaternion = (new THREE.Quaternion).setFromRotationMatrix(f)
  983. }
  984. p.callback = done,
  985. p.duration = options.duration || 1500,
  986. p.mode = 'panorama',
  987. p.aimDuration = options.aimDuration
  988. player.flyToNewMode(p)
  989. }
  990. }
  991. addModel(object){
  992. if(this.objObject){
  993. this.remove(this.objObject)
  994. }
  995. this.objObject = object;
  996. /* object.traverse((mesh)=>{
  997. if(mesh.material && mesh.type == "hotSprite"){
  998. mesh.material = this.material_;
  999. }
  1000. }) */
  1001. object.name = this.info.objName;
  1002. object.src = this.info.objSrc
  1003. this.info.hasBox = false
  1004. this.addBox(false)
  1005. this.remove(this.plane);
  1006. this.plane = null;
  1007. this.setMesh(this.objObject)
  1008. //this.adjustModelAuto()
  1009. if(this.info.modelBound){//应该不会改变
  1010. var s = this.info.modelBound.scaleRatio
  1011. this.mesh.scale.set(s,s,s)
  1012. this.mesh.position.fromArray(this.info.modelBound.position)
  1013. this.mesh.modelBound = this.info.modelBound
  1014. }
  1015. this.material_.side = THREE.FrontSide
  1016. this.changeBoxHelperDisplay(false)
  1017. //this.mesh.boxHelper.visible = true
  1018. }
  1019. addPlane(){//换成plane
  1020. if(this.plane)return
  1021. this.plane = new THREE.Mesh(_planeGeometry, this.material_)
  1022. this.remove(this.objObject)
  1023. this.objObject = null
  1024. delete this.info.objSrc
  1025. delete this.info.objName
  1026. delete this.info.modelBound
  1027. this.setMesh(this.plane)
  1028. //this.material_.side = THREE.DoubleSide //双面的话飞出来会看到悬空的
  1029. }
  1030. setMesh(mesh){
  1031. this.mesh = mesh
  1032. this.add(this.mesh);
  1033. this.mesh.renderOrder = 3
  1034. this.changeMaterial(this.material_) //re applyTo every mesh
  1035. this.mesh.traverse((mesh)=>{
  1036. mesh.type = "hotSprite" //raycaster use
  1037. })
  1038. if(!this.mesh.boxHelper){
  1039. var boxHelper = this.mesh.children.find(e=>e instanceof THREE.Box3Helper)
  1040. if(boxHelper){
  1041. this.mesh.boxHelper = boxHelper
  1042. }else{
  1043. var bound = this.getBoundOri()
  1044. bound.expandByVector(new THREE.Vector3(0.0001,0.0001,0.0001))
  1045. this.mesh.boxHelper = new THREE.Box3Helper( bound, new THREE.Color( "#00ffff"));
  1046. this.mesh.add(this.mesh.boxHelper)
  1047. this.mesh.boxHelper.material.depthTest = false;
  1048. this.mesh.boxHelper.material.transparent = true
  1049. this.mesh.boxHelper.visible = false
  1050. }
  1051. }
  1052. }
  1053. changeBoxHelperDisplay(show){
  1054. if(show){
  1055. this.visible_ = this.visible
  1056. this.visible = true
  1057. this.mesh.boxHelper.visible = true
  1058. }else{
  1059. if(this.visible_ != void 0){
  1060. this.visible = this.visible_
  1061. }
  1062. this.mesh.boxHelper.visible = false
  1063. }
  1064. }
  1065. /* addToLoadQueue() {
  1066. if (this.texType == 'photo') {
  1067. Hot.loadQueue.includes(this) || Hot.loadQueue.push(this)
  1068. }
  1069. } */
  1070. requestDownload(type, callback) {
  1071. var plane = this.plane;
  1072. if(type == 'photo'){
  1073. if(this.photoHasRequestLoad || this.texType != 'photo'){
  1074. return; //为什么之前1191需要在这加callback() 才能呢。现在又没事了
  1075. }
  1076. //console.log(' beginDownload : ' + this.sid)
  1077. /* this.material_.map = */Texture.load(this.info.texSrc, (tex)=>{
  1078. callback && callback()
  1079. if(!tex.image ){
  1080. return //只是单纯用了相同src的tex,但image仍未加载完
  1081. }
  1082. if(!this._loadDones){
  1083. return
  1084. }
  1085. dealMap(tex)
  1086. setTimeout(Hot.loadNext, 50)
  1087. hotGroup.children.forEach(e=>{
  1088. if(e.info.texSrc == this.info.texSrc && e.info.texType == type ){
  1089. e.material_.color.set("#FFFFFF")
  1090. e.material_.opacity = 1;
  1091. //console.log('overlay loaded: ' + e.sid + " - " + this.info.texSrc.split('/').pop());
  1092. e.texMedia = tex.image
  1093. {//animation不同致使的不能使用同一个texture
  1094. if(window.isEdit){
  1095. if(animateTexSrcs[e.info.texSrc]){
  1096. e.material_.map = tex.clone(); //编辑动画直接不用一个texture, 故而animation也不同
  1097. e.material_.map.needsUpdate = true
  1098. }else{
  1099. e.material_.map = tex
  1100. animateTexSrcs[e.info.texSrc] = 1
  1101. }
  1102. }else{
  1103. if(animateTexSrcs[e.info.texSrc]){//已有该texSrc
  1104. let finded = false
  1105. for(let i of animateTexSrcs[e.info.texSrc]){
  1106. if(ifSame(i[0], e.info.animateInfo)){
  1107. e.material_.map = i[1]; finded = true; break;
  1108. }
  1109. }
  1110. if(!finded){
  1111. let tex_ = tex.clone();
  1112. tex_.needsUpdate = true //clone后不写这句会黑块
  1113. animateTexSrcs[e.info.texSrc].set(e.info.animateInfo, tex_)
  1114. e.material_.map = tex_
  1115. }
  1116. }else{
  1117. let object = new Map();
  1118. object.set(e.info.animateInfo, tex)
  1119. animateTexSrcs[e.info.texSrc] = object//注册第一个texSrc
  1120. e.material_.map = tex
  1121. }
  1122. }
  1123. }
  1124. if(e.info.animateInfo && !e.animation){
  1125. e.animation = GifTexDeal.addAnimation(e.material_.map, e, e.info.animateInfo, e.sid )
  1126. e.visible && e.inSight() && GifTexDeal.start(e.animation)
  1127. }
  1128. if(++photoLoaded == originPhotoCount){//data2.js中的所有photo加载完毕
  1129. Hot.allPhotoLoaded = true;
  1130. Hot.whenAllFileLoaded && Hot.allModelLoaded && Hot.whenAllFileLoaded()
  1131. }
  1132. {
  1133. e._loadDones.forEach(a=>a())
  1134. e._loadDones = null
  1135. //e.photoHasRequestLoad = true //这句不能加,否则会无法执行callback
  1136. }
  1137. e.material_.needsUpdate = true
  1138. }
  1139. })
  1140. })
  1141. this.photoHasRequestLoad = true
  1142. }else if(type == 'model'){
  1143. if(this.modelHasRequestLoad || !this.info.objSrc)return;
  1144. //需要处理重复?
  1145. objLoader.load(this.info.objSrc, (object)=>{
  1146. this.remove(this.mesh)
  1147. this.addModel(object)
  1148. callback && callback()
  1149. if(++modelLoaded == originModelCount){//data2.js中的所有photo加载完毕
  1150. Hot.allModelLoaded = true;
  1151. Hot.whenAllFileLoaded && Hot.allPhotoLoaded && Hot.whenAllFileLoaded()
  1152. }
  1153. })
  1154. this.modelHasRequestLoad = true
  1155. }
  1156. }
  1157. dispose(){
  1158. this.parent.remove(this)
  1159. this.titleElem && this.titleElem.dispose()
  1160. delete player.model.hots[this.sid]
  1161. }
  1162. }
  1163. Hot.updateVisibles = function(panos) { //只显示没被遮挡的,否则会卡
  1164. if (panos === true) {
  1165. model.hotGroup.children.forEach(e=>e.updateVisible(null,true))
  1166. } else {
  1167. model.hotGroup.children.forEach(e=>e.updateVisible(panos))
  1168. }
  1169. }
  1170. Hot.beginShineHot = function(){
  1171. if(!window.isEdit && shineMats.length == 0)return
  1172. transitions.trigger({
  1173. func: function(e) {
  1174. var opa = e <= .5 ? 2 * e : -2 * e + 2
  1175. shineMats.forEach(mat=>{
  1176. mat.uniforms.opac.value = opa;
  1177. })
  1178. },
  1179. cycling: !0,
  1180. duration: 3e3,
  1181. name: "hotShine"
  1182. })
  1183. }
  1184. Hot.getDefaulScale = function(hotIconScale){
  1185. return (hotIconScale || DATA.hotIconScale) * g_HotMeshSize.g_HotMeshWidth
  1186. }
  1187. var loadings = [];
  1188. Hot.loadQueue = []; //等待下载的overlay,目前只针对photo
  1189. Hot.maxLoadingCount = 3; //同时正在load图片的数量
  1190. Hot.loadNext = ()=>{//继续requestDownload loadQueue中前排的item
  1191. let count = Hot.maxLoadingCount - loadings.length
  1192. Hot.loadQueue.slice(0, count).forEach(e=>{
  1193. loadings.push(e)
  1194. //console.log('requestDownload', e.hot.info.texSrc)
  1195. e.hot.requestDownload(e.type, ()=>{
  1196. var i = loadings.indexOf(e)
  1197. //console.log('requestDownloaded index', i)
  1198. i > -1 && loadings.splice(i,1)
  1199. })
  1200. })
  1201. Hot.loadQueue.splice(0, count)
  1202. }
  1203. Hot.getNeedLoad = function() {//计算获取loadQueue,每次都重新计算,覆盖旧的
  1204. if (!player || !player.domElement || !player.mode)
  1205. return;
  1206. var hots1, hots2
  1207. if (player.mode != 'panorama') {
  1208. if (Hot.loadQueue.length == 0) {
  1209. hots1 = model.hotGroup.children.filter(e=>e.texType == 'photo' && !e.photoHasRequestLoad )
  1210. hots2 = model.hotGroup.children.filter(e=>e.info.objSrc && !e.modelHasRequestLoad )
  1211. Hot.loadQueue = hots1.map(e=>{return {hot:e, type:"photo"}}).concat(
  1212. hots2.map(e=>{return {hot:e, type:"model"}})
  1213. )
  1214. }
  1215. return
  1216. }
  1217. //Hot.loadWhenOutside = true
  1218. hots1 = model.hotGroup.children.filter(e=>e.texType == 'photo' && !e.photoHasRequestLoad && (!e.info.visiblePanos || e.info.visiblePanos.includes(player.currentPano.id)))
  1219. hots2 = model.hotGroup.children.filter(e=>e.info.objSrc && !e.modelHasRequestLoad && (!e.info.visiblePanos || e.info.visiblePanos.includes(player.currentPano.id)))
  1220. if(hots1.length+hots2.length == 0){
  1221. hots1 = model.hotGroup.children.filter(e=>e.texType == 'photo' && !e.photoHasRequestLoad )
  1222. hots2 = model.hotGroup.children.filter(e=>e.info.objSrc && !e.modelHasRequestLoad )
  1223. }
  1224. var cameraDir = player.getDirection()
  1225. Hot.loadQueue = hots1.map(e=>{return {hot:e, type:"photo"}}).concat(
  1226. hots2.map(e=>{return {hot:e, type:"model"}})
  1227. )
  1228. var request = [(item)=>{
  1229. return true
  1230. }];
  1231. var rank = [(item)=>{
  1232. var dis = item.hot.mesh.getWorldPosition().distanceTo(player.position);
  1233. return -dis
  1234. }
  1235. , (item)=>{
  1236. var tagDir = item.hot.mesh.getWorldPosition().sub(player.position)
  1237. var angle = tagDir.angleTo(cameraDir)
  1238. return -angle * 20
  1239. }]
  1240. var result = common.sortByScore(Hot.loadQueue, request, rank);
  1241. //Hot.loadQueue = result ? result.slice(0, 5).map(e=>e.item) : model.hotGroup.children.filter(e=>e.texType == 'photo' && !e.hasRequestLoad).slice(0, 2);
  1242. Hot.loadQueue = result ? result.slice(0, 5).map(e=>e.item) : []
  1243. }
  1244. Hot.load = ()=>{//开始下载图片
  1245. Hot.getNeedLoad()
  1246. Hot.loadNext()
  1247. var hots1 = model.hotGroup.children.filter(e=>e.texType == 'photo' && !e.photoHasRequestLoad )
  1248. var hots2 = model.hotGroup.children.filter(e=>e.info.objSrc && !e.modelHasRequestLoad )
  1249. if (hots1.length+hots2.length > 0) {
  1250. setTimeout(Hot.load, 200)
  1251. } else {
  1252. Hot.allRequestLoad = true
  1253. console.log('allRequestLoad')
  1254. }
  1255. }
  1256. Hot.startLoad = ()=>{
  1257. originPhotoCount = hotGroup.children.filter(e=>e.texType == 'photo').length
  1258. originModelCount = hotGroup.children.filter(e=>!!e.info.objSrc).length
  1259. if(originPhotoCount == 0 )Hot.allPhotoLoaded = true
  1260. if(originModelCount == 0) Hot.allModelLoaded = true;
  1261. if(Hot.allModelLoaded && Hot.allPhotoLoaded) Hot.whenAllFileLoaded && Hot.whenAllFileLoaded()//所有加载完毕
  1262. else{
  1263. Hot.load()
  1264. }
  1265. setTimeout(Hot.beginShineHot, 1000)
  1266. player.emit('gotHotAndStartload')
  1267. }
  1268. window.Hot = Hot
  1269. /* var ball = new THREE.Mesh(new THREE.SphereBufferGeometry(0.01),new THREE.MeshBasicMaterial({color:"#f00",depthTest:false,transparent:true}))
  1270. var balls = []
  1271. var addPoint = function(point){
  1272. console.log(point)
  1273. var ball1 = ball.clone()
  1274. model.add(ball1);
  1275. ball1.position.copy(point)
  1276. balls.push(ball1)
  1277. }
  1278. var addPoints = function(points){
  1279. balls.forEach(e=>model.remove(e))
  1280. balls = []
  1281. points.forEach(e=>addPoint(e))
  1282. }
  1283. */
  1284. //判断是否是移动端,如果是给关闭按钮添加touchstart事件
  1285. Hot.closePopup = ()=>{// 关闭热点页面
  1286. if(!g_currentHot) return;
  1287. g_currentHot = null;
  1288. var hotPop = document.getElementById('popup');
  1289. hotPop.style.display = "none";
  1290. document.querySelector(".popup-content").removeChild(document.getElementById("id1"));
  1291. $("#popup iframe:last").remove();
  1292. SoundManager.pause('hot', true)//自动播放被中断的音频 (bgm
  1293. return false
  1294. }
  1295. if(browser.isMobile()){
  1296. $('#closepop').on("touchstart",Hot.closePopup);
  1297. }else{
  1298. $('#closepop').on("click",Hot.closePopup);
  1299. }
  1300. Hot.createHotList = function() {
  1301. if(!window.DATA.showHotListSta)return
  1302. var docFragment = document.createDocumentFragment();
  1303. var hots = hotGroup.children.filter(hot=>hot.info.actionType.openHot);
  1304. hots = hots.sort((a,b)=>{return a.order - b.order});
  1305. hots.forEach((hot)=>{
  1306. var li = document.createElement('li');
  1307. var span = document.createElement('span');
  1308. span.innerHTML = hot.info.title || '热点';
  1309. // console.log(span.innerHTML);
  1310. li.hot = hot; // 列表每一项对应一个热点
  1311. li.appendChild(span);
  1312. docFragment.appendChild(li);
  1313. })
  1314. setTimeout(()=>{
  1315. var ul = document.querySelector('#hotListContent ul');
  1316. ul && ul.appendChild(docFragment);
  1317. },3000) //部分安卓手机的钉钉和支付宝浏览器如果立即添加到列表会卡住
  1318. }
  1319. Hot.mobileAutoPlay = function(player){//移动端。不这么写video不会播放 . (2022.11.29: 可为何加了Hot.updateHots之后又会自动播了?https有关?
  1320. if(browser.isMobile()){
  1321. hotGroup.children.forEach((hot)=>{
  1322. /* if(hot.texType == 'video' && hot.shouldPlay){
  1323. console.log(1)
  1324. } */
  1325. if(!hot.clickToPlayInited && hot.texType == 'video' && !isVideoPlayed(hot.texMedia) && hot.shouldPlay){
  1326. //hot.update(player)
  1327. console.log({str:'try mobileAutoPlay '+ hot.sid, level:1})
  1328. hot.videoControl(true)
  1329. if(isVideoPlayed(hot.texMedia)){
  1330. console.log({str:'clickToPlayInited '+ hot.sid, level:1})
  1331. hot.clickToPlayInited = true
  1332. hot.changeOpaWhenPlay(video)
  1333. }
  1334. }
  1335. })
  1336. }
  1337. }
  1338. Hot.updateHots = function(){
  1339. for(var i in player.model.hots){
  1340. player.model.hots[i].update(player)
  1341. }
  1342. if( !player.ready)return
  1343. this.needUpdate = true
  1344. common.intervalTool.isWaiting('updateHots', ()=>{ //延时update,防止卡顿
  1345. if(!this.needUpdate)return
  1346. this.needUpdate = false
  1347. let videoCanPlay = [], aniCanPlay = [];
  1348. for(var i in player.model.hots){
  1349. let hot = player.model.hots[i]
  1350. {//实时监测播放
  1351. if(hot.texType == "video" && !player.flying){ //飞行时不判断
  1352. if(hot.visible && !hot.pausedByUser && hot.inSight()){ //注意edit时insight一直为true
  1353. //this.videoControl(true)
  1354. videoCanPlay.push(hot)
  1355. }else{
  1356. hot.videoControl(false)
  1357. }
  1358. }else if(hot.info.animateInfo){
  1359. if(hot.visible && hot.inSight()){
  1360. aniCanPlay.push(hot)
  1361. }else{
  1362. GifTexDeal.stop(hot.animation)
  1363. }
  1364. }
  1365. }
  1366. }
  1367. let filter = (type,max,list,playFun)=>{
  1368. if(list.length < max){
  1369. list.forEach(hot=>type == 'video' ? hot.videoControl(true) : GifTexDeal.start(hot.animation))
  1370. }else{
  1371. let playerDir = player.getDirection()
  1372. let request = []
  1373. let planeDir = new Map()
  1374. if(player.mode != 'panorama'){
  1375. request.push((hot)=>{
  1376. if(hot.plane){//考虑plane的方向,它甚至可能背对镜头
  1377. let dir = new THREE.Vector3(0,0,-1).applyQuaternion(hot.quaternion)
  1378. let angle = dir.dot(playerDir)
  1379. planeDir.set(hot, angle)
  1380. return angle > 0
  1381. }else return true
  1382. })
  1383. }
  1384. let r = common.sortByScore(list,request,[(hot)=>{ //方向因素
  1385. var cornerPointInfo = hot.getCornerPoint(); //仅支持漫游模式
  1386. let dir = new THREE.Vector3().subVectors(hot.position, player.position).normalize()
  1387. score = dir.dot(playerDir)
  1388. player.mode == 'panorama' && (score *= Math.pow(cornerPointInfo.diffLat,0.5))// pow降低高度的权重,因为宽度更重要些。之所以两个函数都乘以diffLat,也是为了防止在两个item的分数相同diffLon不同时,diffLat增长相同倍数却能造成分数差异的情况
  1389. return score
  1390. },
  1391. (hot)=>{//面积因素
  1392. if(player.mode == 'panorama'){
  1393. var cornerPointInfo = hot.getCornerPoint(); //仅支持漫游模式
  1394. let area = cornerPointInfo.diffLon * Math.pow(cornerPointInfo.diffLat,0.5) / 1000 //占据面积
  1395. return area
  1396. }else{
  1397. let dis = player.position.distanceTo(hot.position);
  1398. let size = hot.getBoundOri().applyMatrix4(hot.matrixWorld).size(new THREE.Vector3).length() / 2
  1399. let score = Math.atan(size / Math.pow(dis,1.2)) * 50 // 在镜头中所占fov angle 的一半 。 pow是因为实际感受还是尽量显示近处的
  1400. if(hot.plane){//考虑plane的方向,越倾斜分越低
  1401. score *= planeDir.get(hot)
  1402. }
  1403. return score
  1404. }
  1405. } ])
  1406. //getCornerPoint仅支持漫游模式 Lat高度(纬度).
  1407. //console.log(r)
  1408. r && r.forEach((e,j)=>{
  1409. let hot = e.item
  1410. if(j<max){
  1411. type == 'video' ? hot.videoControl(true) : GifTexDeal.start(hot.animation)
  1412. }else{
  1413. type == 'video' ? hot.videoControl(false) : GifTexDeal.stop(hot.animation)
  1414. }
  1415. })
  1416. }
  1417. }
  1418. filter('video',playVideoMax, videoCanPlay);
  1419. filter('animateInfo',playAniMax, aniCanPlay);
  1420. //console.log('updateHots')
  1421. return true
  1422. }, 800)
  1423. }
  1424. }
  1425. function isVideoPlayed(video){
  1426. return !video.paused && !isNaN(video.duration) //注意,有的手机首次play时会立即paused为false,但其实没加载好, duration为NAN
  1427. }
  1428. /*
  1429. 保存 JSON.stringify(editTool.hotpoint.getSavingInfo())
  1430. 可能需要再写一份保存到overlay 给旧场景项目使用
  1431. 最好后台有针对手机版的做一个压缩。压缩成几个档位。
  1432. 安卓手机firefox出现过视频mesh不可见或者闪烁的情况。
  1433. 视频最容易导致崩溃, 模型还好
  1434. 数据速率为4064kbps,1920*1080px 时测试部门电脑崩溃
  1435. 数据速率为1824kbps,720 *576px 时正常
  1436. 所以尽量降到2000以下 同时播放个数最好不超过2个 可能需要将src归零 并延迟加载、不自动播放
  1437. 有遇到某张图在ios里显示不出,但在ps重新输出后就可以。这张图的dpi高达500,不知道是否与此有关。
  1438. 遇到过ios14.8.1、14.7.1 无法播放webm(来自1050)。 onloadedmetadata失败
  1439. */