import { scene, camera, light3, controls, renderer } from './common/scene2'; import { setEvents } from './common/setEvents'; import { convertToXYZ, getEventCenter, geodecoder } from './common/geoHelpers'; import { mapTexture } from './common/mapTexture'; import { getTween, memoize } from './common/utils'; import topojson from 'topojson'; import THREE from 'THREE'; import d3 from 'd3'; import fdage from 'fdage'; var root,rotate; var getIndex = function(elem,index){ index = index || 'index'; return elem.getAttributeNode(index)&&elem.getAttributeNode(index).nodeValue; } var setIndex = function(elem,name,index){ index = index || 'index'; elem.setAttribute(index,name); } var getByIndex = function(elem,name,index){ var result; for(var i=0; i'); } music.style.display = ""; } if(itemObj.type === "true3d" || itemObj.type === "false3d" ){ //(itemObj.type === "false3d")&& addClass(introduce,"false3d"); var modelArea = d3.select("#modelArea").node(); if(!modelArea){ modelArea = document.createElement("div"); addClass(modelArea,"canvasArea"); modelArea.id = "modelArea"; body.appendChild(modelArea); }else{ if(getIndex(modelArea)!==itemObj.name){//虽已有,但展示的是别的模型,要删除重新创建 if(modelArea.children&&modelArea.children[0].src) modelArea.children[0].src = "about:blank"; modelArea.parentElement.removeChild(modelArea); modelArea = document.createElement("div"); addClass(modelArea,"canvasArea"); modelArea.id = "modelArea"; body.appendChild(modelArea); } modelArea.style.display = ""; } if(getIndex(modelArea)!==itemObj.name){ //fdage.embed( "data/ture3d/model/"+itemObj.name+".4dage", { root:modelArea, backgroundColor:[238/255,238/255,238/255],ifPlain:true, width: 800, height: 600, autoStart: true, fullFrame: true, pagePreset: false } ); var page = document.createElement("iframe"); page.src = "data/"+itemObj.type+"/"+itemObj.name+".html"+(itemObj.htmlTail?itemObj.htmlTail:""); modelArea.appendChild(page); setIndex(modelArea,itemObj.name); } }else{ var imageArea = d3.select("#imageArea").node(); if(!imageArea){ imageArea = document.createElement("div"); addClass(imageArea,"canvasArea"); imageArea.id = "imageArea"; body.appendChild(imageArea); var inner = document.createElement("div"); imageArea.appendChild(inner); inner.style.width = "100%"; inner.style.height = "94%"; inner.style['margin-top'] = "2%"; }else{ var inner = imageArea.children[0]; imageArea.style.display = ""; } inner.style["background-image"] = "url(data/display/2048/"+itemObj.name+".jpg"+")"; } } function showIntroWithBannerAnima(o){ if(o.animateOrnot && introduce.style.display == "none"){//如果之前introduce消失,则要有动画,如果不小时,就无动画 introduce.style.display = ""; removeClass(introBanner,"show"); setTimeout(function(){ addClass(introBanner,"show"); },100) }else if(o.show){ introduce.style.display = ""; }else if(o.hide){ introduce.style.display = "none"; removeClass(introBanner,"show"); } if(o.animate){ removeClass(introBanner,"show"); setTimeout(function(){ addClass(introBanner,"show"); },100) } } function onGlobeMousemove(event,originEvent,_this) { //为什么mousedown的时候会立即触发mousemove? if(window.lastMouse.x){ hint.style.display = "none" return; } var map, material; // Get pointc, convert to latitude/longitude var latlng = getEventCenter.call(this||_this, event, 400); // Look for country at that latitude/longitude var country = geo.search(latlng[0], latlng[1]); if(country && _this && country.code !== currentChosed){//点击的区域 _this表明是由click的触发的 currentChosed = country.code; currentChName = country.chName; var imgName; switch(currentChosed){ case "Germany": imgName = "Austria"; break; //共用一张 case "Liang Yi": case "Yunnan Yi": imgName = "Yi"; break; case "England": case "United Kingdom": case "Belarus": imgName = "flower"; break; case "Indonesia": imgName = "Bali"; break; default: imgName = currentChosed; } introBanner.style["background-image"] = 'url("data/bannerImg/'+imgName+'.png")'; //出现该地区的介绍和器物列表 regionObj = introData.region[currentChosed]; layer = 1; introTitle.innerHTML = currentChName; introText.innerHTML = ""; artifact.innerHTML = ""; if(regionObj){ introText.innerHTML = regionObj.regionIntro; showArtifactList(); } map = textureCache(country.code, 'rgba(180,36,1,1)'); material = new THREE.MeshPhongMaterial({map: map,opacity:.5, transparent: true}); material.shininess = 1 ; if (!chosedOverlay) { chosedOverlay = new THREE.Mesh(new THREE.SphereGeometry(405, 30, 30), material); chosedOverlay.rotation.y = Math.PI; root.add(chosedOverlay); }else{ chosedOverlay.material = material; } }/* else if(country == null && _this){ introduce.style.display = "none"; BannerAnima(false); }else if(country&& _this){ introduce.style.display = ""; BannerAnima(true) } */ if(!isMobile && country !== null){ hint.style.display = "block"; if(originEvent){ hint.innerHTML = country.chName; hint.style.left = Math.min(originEvent.clientX + 10, window.innerWidth - hint.clientWidth)+ "px"; hint.style.top = Math.min(originEvent.clientY + 10, window.innerHeight - hint.clientHeight)+ "px"; } } if (!isMobile && country !== null && country.code !== currentCountry) { //滑动到的区域 currentCountry = country.code; map = textureCache(country.code, 'rgba(255,255,255,0.4)', '2', '#FFFFFF'); //map = isMobile ? mapTexture(geo.find(country.code), '#FFF900') : textureCache(country.code, '#FFF900'); material = new THREE.MeshPhongMaterial({map: map,transparent: true}); material.shininess = 1 ; if (!overlay) { overlay = new THREE.Mesh(new THREE.SphereGeometry(405, 30, 30), material); overlay.rotation.y = Math.PI; root.add(overlay); } else { overlay.material = material; } }else if(country == null){ hint.style.display = "none"; } if(country == null && _this){ showIntroWithBannerAnima({hide:true}) }else if(country&& _this){ showIntroWithBannerAnima({show:true,animateOrnot:true}) } } function onGlobeClick(event,originEvent) {//mouseup 在canvas上mouseup不一定会执行这个 if(lastMouse.x){ var x = originEvent.offsetX; var y = originEvent.offsetY; if((Math.abs(window.lastMouse.x - x)+Math.abs(window.lastMouse.y - y))>10){ window.lastMouse.x = null; window.lastMouse.y = null; return; } } window.lastMouse.x = null; window.lastMouse.y = null; // Get pointc, convert to latitude/longitude var latlng = getEventCenter.call(this, event,400);//radius = 150 latlng[1]+=(root.rotation.y*180/Math.PI -15); //因为一开始地球在旋转,所以要加上这个offset。另外地球左移后观察的中心点右移,所以也要改。 // Get new camera position var temp = new THREE.Mesh(); temp.position.copy(convertToXYZ(latlng, 1000)); temp.lookAt(root.position); temp.rotateY(Math.PI); for (let key in temp.rotation) { if (temp.rotation[key] - camera.rotation[key] > Math.PI) { temp.rotation[key] -= Math.PI * 2; } else if (camera.rotation[key] - temp.rotation[key] > Math.PI) { temp.rotation[key] += Math.PI * 2; } } var tweenPos = getTween.call(camera, 'position', temp.position); d3.timer(tweenPos); var tweenRot = getTween.call(camera, 'rotation', temp.rotation); d3.timer(tweenRot); onGlobeMousemove(event,null,this); } }); }); function animate() { controls.update(); light3.position.set(camera.position.x,camera.position.y,camera.position.z); light3.position.applyAxisAngle(new THREE.Vector3(0,1,0),0.9) if(root && rotate)root.rotation.y+=0.002; requestAnimationFrame(animate); renderer.render(scene, camera); } animate(); window.cc = function(lon,lat){ var scale = [0.03600360036003601,0.017366249624962495]; var translate = [-180,-90]; return [parseInt((lon-translate[0])/scale[0]), parseInt((lat-translate[1])/scale[1])]; } window.vv = function(x,y){ var scale = [0.03600360036003601,0.017366249624962495]; var translate = [-180,-90]; return [x*scale[0]+translate[0], y*scale[1]+translate[1]]; }