import { scene, camera, controls, renderer } from './common/scene'; 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; i10){ 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; // Get new camera position var temp = new THREE.Mesh(); temp.position.copy(convertToXYZ(latlng, 980)); 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); } //document.title = "asas" function onGlobeMousemove(event,originEvent,_this) { //为什么mousedown的时候会立即触发mousemove? if(window.lastMouse.x){ 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; d3.select("#introduce").select("h2").html(currentChName); d3.select("#introduce").select(".below").html(currentChosed); if(d3.select("#introduce").node().style.display === "none"){ d3.select("#introduce").node().style.display = ""; rePos({text:true,earth:true}); }else{ rePos({text:true}); } //map = isMobile ? mapTexture(geo.find(country.code), '#FF0011'):textureCache(country.code, '#FF0011'); //if(isMobile) document.title = "isMobile" map = textureCache(country.code, '#FF0000'); material = new THREE.MeshPhongMaterial({map: map,opacity:.5, transparent: true}); 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; } } if (!isMobile && country !== null && country.code !== currentCountry) { currentCountry = country.code; map = textureCache(country.code, '#FFFF00'); //map = isMobile ? mapTexture(geo.find(country.code), '#FFF900') : textureCache(country.code, '#FFF900'); material = new THREE.MeshPhongMaterial({map: map,opacity:.5, transparent: true}); 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 && _this){ d3.select("#introduce").node().style.display = "none"; } } setEvents(camera,canvas, [outerLayer], 'click'); setEvents(camera,canvas, [outerLayer], 'mousemove', 10); function rePos(o) {//introduce重新定位 //var onlyTitle = (d3.select('#introMenu').node().style.display==="none"); var intro = d3.select("#introduce").node(); var infoH = intro.clientHeight; if(infoH){ if(o.earth){ var e = Math.min(window.innerWidth/2,window.innerHeight*0.95); var offset = Math.max(0, window.innerHeight - e)/2; canvas.style.top = offset + "px"; renderer.setSize(e, e); } if(o.text){ var infoH = intro.clientHeight; var earthH = canvas.clientHeight || window.innerWidth*0.3;//(onlyTitle?0.1:0.3) if(infoH){ var f1 = parseFloat(intro.style["font-size"]); if(f1!=f1) f1 = 1; //NAN var ratio = earthH/infoH/1.2; var size = Math.min(Math.max(1, f1 * ratio ), 1.5); intro.style["font-size"] = size + "em"; (size<1.1)?(addClass(intro,"weight")):(removeClass(intro,"weight")); } } } } var canvasToImg = function(canvas){ var type = 'png'; var imgData = canvas.toDataURL(type); return imgData; } d3.json('data/worldInfo.json', function (err, introData) {//和info跳转有关 //跳转绑定: var intro = d3.select("#introduce").node(); var menu = d3.select("#introMenu").node(); var artifact = d3.select("#artifact").node(); for(var i=0; i<3; i++){//三个菜单的点击 bind(menu.children[i],"click",function(i){ return function(){ countryObj = introData.countries[currentChosed]; if(!countryObj)return;//没有改国家的展览 //if(state[state.length-1] == "earth")hangUpEarth(); var type = getIndex(this); switch(type){ /* case "countryIntro": showCountryIntro(); break; case "culture": showCultureIntro(); break; */ case "artifact": showArtifactIntro(); break; /* case "artifactIntro": artifactIntro(); break; */ /* case "interview": artifactInterview(); break; case "music": artifactMusic(); break; */ default: console.log("其他"); } } }(i)) } var hangUpEarth = function(){ renderer.render(scene, camera); var img = document.createElement('img'); img.id = "ToEarth"; img.src = canvasToImg(canvas); img.style.top = canvas.style.top; img.style.cursor = "pointer"; img.style.height = img.style.width = canvas.style.height; img.className = "canvasArea"; img.style['box-shadow'] = "none"; canvas.parentElement.appendChild(img); canvas.style.display = "none"; setTimeout(function(){ img.style.width = img.style.height = "50px"; img.style.top = "0"; bind(img, "click", backToEarth); },30) } var dropDownEarth = function(){ var img = d3.select('#ToEarth').node(); unbind(img, "click", backToEarth); img.style.width = ""; var e = Math.min(window.innerWidth/2,window.innerHeight*0.95); var offset = Math.max(0, window.innerHeight - e)/2; img.style.height = img.style.width = e + "px"; img.style.top = offset + "px"; setTimeout(function(){ img.parentElement.removeChild(img); canvas.style.display = ""; rePos({text:true,earth:true}); },400) } var backBtn = d3.select("#back").node(); bind(backBtn,"click",function(){ var from = state[state.length-1]; var to = state[state.length-2]; if(!to) return; state.pop(); switch(from){ case "countryIntro": exitCountryIntro(); break; case "culture": exitCultureIntro(); break; case "artifact": exitArtifactIntro(); break; case "itemDisplay": exitItemDisplay(); break; /* case "artifactIntro": exitArtifactIntro(); break; */ case "interview": exitArtifactInterview(); break; case "music": exitArtifactMusic(); break; default: console.log("其他"); } switch(to){ case "earth": backToEarth(); break; case "countryIntro": showCountryIntro(); break; case "culture": showCultureIntro(); break; case "artifact": showArtifactIntro(true); break; /* case "artifactIntro": artifactIntro(); break; */ case "interview": artifactInterview(); break; case "music": artifactMusic(); break; default: console.log("其他"); } }) function backToEarth(){//确保清除所有,因为包括退了一级的和直接回到地球的 state = ["earth"]; artifact.style.display = "none"; //intro.children[0].innerHTML = currentChName; menu.removeChild(menu.children[0]) menu.children[0].innerHTML = 'NO.1国家背景National Background'; menu.children[1].innerHTML = 'NO.2器物介绍Instrument Introduction'; menu.children[2].innerHTML = 'NO.3文化传承Cultural Heritage'; menu.children[2].style.display = ""; menu.style.display = ""; setIndex(menu.children[0],"countryIntro"); setIndex(menu.children[1],"artifact"); setIndex(menu.children[2],"culture"); removeClass(intro,"largeBelow"); intro.children[1].innerHTML = currentChosed; var modelArea = d3.select("#modelArea").node();//将模型和图片展示清除 var imageArea = d3.select("#imageArea").node(); if(modelArea){ modelArea.parentElement.removeChild(modelArea); } if(imageArea){ imageArea.parentElement.removeChild(imageArea); } dropDownEarth(); } function exitItemDisplay(){//从单个展示到列表 var modelArea = d3.select("#modelArea").node(); var imageArea = d3.select("#imageArea").node(); if(modelArea){ modelArea.style.display = "none"; } if(imageArea){ imageArea.style.display = "none"; } } function exitArtifactIntro(){//从文字介绍到 单个展示三个菜单 } function showArtifactIntro(backHere){//back表示是从下一级后退到这里的,此时不用重新生成列表. menu.style.display = "none"; //menu.innerHTML = '乐器介绍
专家访谈音频欣赏'; if(!backHere){ //menu.children[0].innerHTML = "乐器介绍"; hangUpEarth(); var add = document.createElement('div'); //新增一个不可点击的选项:乐器介绍 add.innerHTML = '·乐器介绍Instrument Introduction' menu.insertBefore(add,menu.children[0]); menu.children[1].innerHTML = '·专家访谈Experts Interview'; menu.children[2].innerHTML = '·音频欣赏Music Appreciation'; menu.children[3].style.display = "none"; //setIndex(menu.children[0],"artifactIntro"); setIndex(menu.children[1],"interview"); setIndex(menu.children[2],"music"); setIndex(menu.children[3],""); state.push("artifact"); intro.children[1].innerHTML = "器物介绍"; addClass(intro,"largeBelow"); var len1 = countryObj.artifact.length; var len2 = artifact.children.length; for(var i=0; ilen2-1){ var item = document.createElement('div'); var inner = document.createElement('div'); addClass(item,"item"); artifact.appendChild(item); item.appendChild(inner); setIndex(item,i); bind(item,"click",clickItem)//绑定点击事件 } else{ item = artifact.children[i]; } item.style.display = "";//解除隐藏 var itemObj = countryObj.artifact[i]; if(itemObj.type === "image"){ item.children[0].style['background-image'] = "url(data/display/600/"+itemObj.name+".jpg)"; } } if(len2>len1){//多余的隐藏 for(var i=len1; i