forked from next-sergiosantamaria/THREE-geoJson
-
Notifications
You must be signed in to change notification settings - Fork 0
/
three.geojson.min.js
2 lines (2 loc) · 3.85 KB
/
three.geojson.min.js
1
2
//Globals
function log(e){log&&console.log(e)}function init(){camera=new THREE.PerspectiveCamera(50,width/height,1,1e3);camera.position.set(0,100,500);scene=new THREE.Scene;mouse=new THREE.Vector2;renderer=new THREE.WebGLRenderer({antialias:!1,preserveDrawingBuffer:!0,alpha:!0});renderer.setSize(width,height);renderer.shadowMapEnabled=!0;renderer.shadowMapType=THREE.PCFSoftShadowMap;renderer.setViewport(0,0,width,height);group=new THREE.Object3D;group.position.y=50;group.position.z=0;scene.add(group);groupGeometry=new THREE.Geometry;log("initiation done");buildShape()}function buildShape(){log("buildShape ("+shapeCount+"/"+json.features.length+")");if(shapeCount<json.features.length){var e=0;for(var t=shapeCount;t<json.features.length&&e<subset_size;t++){e++;shapeCount++;var n=!0,i=[];if(json.features[t].geometry.coordinates.length<1||json.features[t].geometry.coordinates[0]<1)n=!1;else for(var s=0;s<json.features[t].geometry.coordinates[0].length;s++)json.features[t].geometry.coordinates[0][s][0]&&json.features[t].geometry.coordinates[0][s][1]&&json.features[t].geometry.coordinates[0][s][0]>0&&json.features[t].geometry.coordinates[0][s][1]>0?i.push(new THREE.Vector2(translateLat(json.features[t].geometry.coordinates[0][s][0]),translateLng(json.features[t].geometry.coordinates[0][s][1]))):n=!1;if(n){var o=heightFn(json.features[t].properties[heightAttr]);if(isNaN(parseFloat(json.features[t].properties[heightAttr]))){fast&&(n=!1);o=0}if(!o||o<0){fast&&(n=!1);o=0}o>max&&(o=max);o==0&&fast&&(n=!1)}if(n){var u=o/max*z_max;if(!u||u<1)u=0;var a=Math.round(o/max*255),f=Math.round(255-o/max*255),l=new THREE.Color("rgb("+a+",0,"+f+")");addShape(new THREE.Shape(i),u*z_rel,l,0,50,0,r,0,0,1)}}setTimeout(function(){buildShape()},100)}else{log("Geometry Done");var c=new THREE.ShaderMaterial({attributes:{},uniforms:{},vertexShader:THREETUT.Shaders.Lit.vertex,fragmentShader:THREETUT.Shaders.Lit.fragment,side:THREE.FrontSide}),h=[new THREE.MeshLambertMaterial({vertexColors:THREE.VertexColors,color:"rgb(0.2,0.2,0.2)",ambient:"rgb(0.2,0.2,0.2)",shininess:1,lights:!0}),new THREE.MeshLambertMaterial({vertexColors:THREE.VertexColors,color:"rgb(0.5,0.5,0.5)",ambient:"rgb(0.5,0.5,0.5)",shininess:1,lights:!0})],p=new THREE.MeshFaceMaterial(h);mesh=new THREE.Mesh(groupGeometry,p);mesh.position.set(offset_x*3,offset_y*3,offset_z*3);mesh.rotation.set(r,0,0);mesh.scale.set(scale_factor*scale_x,scale_factor*scale_y,0);mesh.castShadow=!0;mesh.receiveShadow=!0;scene.add(mesh);var d=new THREE.DirectionalLight(15658734,1);d.position.set(0,400,200);d.target=mesh;d.castShadow=!0;d.shadowDarkness=.5;scene.add(d);document.body.appendChild(renderer.domElement);log("animate");animate();renderer.domElement.addEventListener("mousemove",onDocumentMouseMove,!1)}}function addShape(e,t,n,r,i,s,o,u,a,f){var l={amount:t*50,steps:1,material:0,extrudeMaterial:1,bevelEnabled:!1},c=new THREE.ExtrudeGeometry(e,l);for(var h=0;h<c.faces.length;h++)c.faces[h].color.setRGB(n.r,n.g,n.b);groupGeometry.merge(c,c.matrix)}function onDocumentMouseMove(e){e.preventDefault();mouse.x=e.clientX/window.innerWidth*Math.PI*4;mouse.y=e.clientY/window.innerHeight*Math.PI*4}function animate(){setTimeout(function(){requestAnimationFrame(animate)},1e3/30);mesh.rotation.x=mouse.y;mesh.rotation.y=mouse.x;animateHeight&&(heightScaler+=.001);mesh.scale.set(scale_factor*scale_x,scale_factor*scale_y,heightScaler);renderer.render(scene,camera)}function saveObj(){var e=JSON.stringify(mesh.toJSON()),t=100;for(var n=0;n<t;n++){var r=n;n<10&&(r="0"+n);$.ajax({type:"POST",url:"save.php",data:{content:e.substring(e.length/t*n,e.length/t*(n+1)),name:"model_"+r+".json"}})}}var json,camera,scene,renderer,mesh,group,groupGeometry,mouse,fast=!1,width=window.innerWidth,height=window.innerHeight;$(document).ready(function(){log("start loading");$.getJSON(jsonFile,function(e){log("loading complete");json=e;init()})});var shapeCount=0,shapes=[],subset_size=5e3;