31 lines
13 KiB
JavaScript
31 lines
13 KiB
JavaScript
/* JSModeler 0.37 - http://www.github.com/kovacsv/JSModeler */ 'use strict';JSM.ResizeImageToPowerOfTwoSides=function(a){if(JSM.IsPowerOfTwo(a.width)&&!JSM.IsPowerOfTwo(a.height))return a;var b=JSM.NextPowerOfTwo(a.width),d=JSM.NextPowerOfTwo(a.height),c=document.createElement("canvas");c.width=b;c.height=d;c=c.getContext("2d");c.drawImage(a,0,0,b,d);return c.getImageData(0,0,b,d)};
|
|
JSM.ConvertBodyToThreeMeshes=function(a,b,d){var c={textureLoadedCallback:null,hasConvexPolygons:!1,doubleSided:!0};void 0!==d&&null!==d&&(c.textureLoadedCallback=JSM.ValueOrDefault(d.textureLoadedCallback,c.textureLoadedCallback),c.hasConvexPolygons=JSM.ValueOrDefault(d.hasConvexPolygons,c.hasConvexPolygons),c.doubleSided=JSM.ValueOrDefault(d.doubleSided,c.doubleSided));var f=[],e=null;JSM.ExplodeBodyToTriangles(a,b,{hasConvexPolygons:c.hasConvexPolygons,onGeometryStart:function(){e=new THREE.Geometry},
|
|
onGeometryEnd:function(a){var b=null!==a.texture,d=1!==a.opacity,v=a.ambient,G=a.diffuse,k=a.specular,l=a.shininess;0===l&&(k=0,l=1);v=new THREE.MeshPhongMaterial({ambient:v,color:G,specular:k,shininess:l});c.doubleSided&&(v.side=THREE.DoubleSide);d&&(v.opacity=a.opacity,v.transparent=!0);if(b){var m=THREE.ImageUtils.loadTexture(a.texture,new THREE.UVMapping,function(){m.image=JSM.ResizeImageToPowerOfTwoSides(m.image);null!==c.textureLoadedCallback&&c.textureLoadedCallback()});m.wrapS=THREE.RepeatWrapping;
|
|
m.wrapT=THREE.RepeatWrapping;v.map=m}e.computeFaceNormals();a=new THREE.Mesh(e,v);f.push(a)},onTriangle:function(a,b,c,d,f,k,l,m,z){var n=e.vertices.length;e.vertices.push(new THREE.Vector3(a.x,a.y,a.z));e.vertices.push(new THREE.Vector3(b.x,b.y,b.z));e.vertices.push(new THREE.Vector3(c.x,c.y,c.z));a=new THREE.Face3(n+0,n+1,n+2);e.faces.push(a);null!==d&&(null!==f&&null!==k)&&(a=[],a.push(new THREE.Vector3(d.x,d.y,d.z)),a.push(new THREE.Vector3(f.x,f.y,f.z)),a.push(new THREE.Vector3(k.x,k.y,k.z)),
|
|
e.faces[e.faces.length-1].vertexNormals=a);null!==l&&(null!==m&&null!==z)&&(d=[],d.push(new THREE.Vector2(l.x,-l.y)),d.push(new THREE.Vector2(m.x,-m.y)),d.push(new THREE.Vector2(z.x,-z.y)),e.faceVertexUvs[0].push(d))}});return f};JSM.ConvertModelToThreeMeshes=function(a,b,d){var c=[],f=[],e,h;for(e=0;e<a.BodyCount();e++){f=a.GetBody(e);f=JSM.ConvertBodyToThreeMeshes(f,b,d);for(h=0;h<f.length;h++)c.push(f[h])}return c};
|
|
JSM.ConvertJSONDataToThreeMeshes=function(a,b,d){function c(a,c){function d(a){function h(a,b,c,d,e){c=new THREE.Vector2(a,b);JSM.IsZero(e)||(d=Math.sin(e*JSM.DegRad),e=Math.cos(e*JSM.DegRad),c.x=e*a-d*b,c.y=d*a+e*b);c.x=n[0]+c.x*u[0];c.y=n[1]+c.y*u[1];return c}var p=a.parameters,g=e[a.material];a=g.texture;var n=g.offset,u=g.scale,A=g.rotation,w=new THREE.Color,x=new THREE.Color,y=new THREE.Color,B=g.shininess;w.setRGB(g.ambient[0],g.ambient[1],g.ambient[2]);x.setRGB(g.diffuse[0],g.diffuse[1],g.diffuse[2]);
|
|
y.setRGB(g.specular[0],g.specular[1],g.specular[2]);if(void 0!==a&&null!==a){w.setRGB(1,1,1);x.setRGB(1,1,1);y.setRGB(1,1,1);if(void 0===n||null===n)n=[0,0];if(void 0===u||null===u)u=[1,1];if(void 0===A||null===A)A=0}0===B&&(y.setRGB(0,0,0),B=1);w=new THREE.MeshPhongMaterial({ambient:x.getHex(),color:x.getHex(),specular:y.getHex(),shininess:B,side:THREE.DoubleSide});1!==g.opacity&&(w.opacity=g.opacity,w.transparent=!0);void 0!==a&&null!==a&&(g=THREE.ImageUtils.loadTexture(a,new THREE.UVMapping,function(a){a.image=
|
|
JSM.ResizeImageToPowerOfTwoSides(a.image);void 0!==b&&null!==b&&b()}),g.wrapS=THREE.RepeatWrapping,g.wrapT=THREE.RepeatWrapping,w.map=g);var g=new THREE.Geometry,s,r,C,t,D,E,F,z,q;for(q=0;q<p.length;q+=9)s=3*p[q+0],r=3*p[q+1],C=3*p[q+2],t=3*p[q+3],D=3*p[q+4],E=3*p[q+5],x=2*p[q+6],y=2*p[q+7],B=2*p[q+8],F=g.vertices.length,z=g.faces.length,g.vertices.push(new THREE.Vector3(k[s+0],k[s+1],k[s+2])),g.vertices.push(new THREE.Vector3(k[r+0],k[r+1],k[r+2])),g.vertices.push(new THREE.Vector3(k[C+0],k[C+1],
|
|
k[C+2])),g.faces.push(new THREE.Face3(F+0,F+1,F+2)),s=[],s.push(new THREE.Vector3(l[t+0],l[t+1],l[t+2])),s.push(new THREE.Vector3(l[D+0],l[D+1],l[D+2])),s.push(new THREE.Vector3(l[E+0],l[E+1],l[E+2])),g.faces[z].vertexNormals=s,void 0!==a&&null!==a&&(t=[],t.push(h(m[x+0],m[x+1],n,u,A)),t.push(h(m[y+0],m[y+1],n,u,A)),t.push(h(m[B+0],m[B+1],n,u,A)),g.faceVertexUvs[0].push(t));p=new THREE.Mesh(g,w);p.originalJsonIndex=c;f.push(p)}var k=a.vertices;if(void 0!==k){var l=a.normals;if(void 0!==l){var m=a.uvs;
|
|
if(void 0!==m){var h=a.triangles,n;for(n=0;n<h.length;n++)d(h[n])}}}}var f=[],e=a.materials;if(void 0===e)return f;var h=a.meshes;if(void 0===h)return f;var r=0;JSM.AsyncRunTask(function(){c(h[r],r);r+=1;return!0},d,h.length,0,f);return f};JSM.JSONFileConverter=function(a,b){this.onReady=a;this.onTextureLoaded=b};JSM.JSONFileConverter.prototype.Convert=function(a){(new JSM.JSONFileLoader(this.OnReady.bind(this))).Load(a)};
|
|
JSM.JSONFileConverter.prototype.OnReady=function(a){null!==this.onReady&&(a=JSM.ConvertJSONDataToThreeMeshes(a,this.onTextureLoaded),this.onReady(a))};JSM.ThreeViewer=function(){this.enableDraw=this.drawLoop=this.settings=this.navigation=this.cameraMove=this.runAfterRender=this.runBeforeRender=this.directionalLight=this.ambientLight=this.renderer=this.camera=this.scene=this.canvas=null};
|
|
JSM.ThreeViewer.prototype.Start=function(a,b){if(!JSM.IsWebGLEnabled()||!this.InitSettings(b)||!this.InitThree(a)||!this.InitCamera(b)||!this.InitLights())return!1;this.drawLoop=!1;this.enableDraw=!0;this.DrawIfNeeded();return!0};
|
|
JSM.ThreeViewer.prototype.InitSettings=function(a){this.settings={cameraEyePosition:new JSM.Coord(1,1,1),cameraCenterPosition:new JSM.Coord(0,0,0),cameraUpVector:new JSM.Coord(0,0,1),lightAmbientColor:[0.5,0.5,0.5],lightDiffuseColor:[0.5,0.5,0.5]};void 0!==a&&(void 0!==a.cameraEyePosition&&(this.settings.cameraEyePosition=JSM.CoordFromArray(a.cameraEyePosition)),void 0!==a.cameraCenterPosition&&(this.settings.cameraCenterPosition=JSM.CoordFromArray(a.cameraCenterPosition)),void 0!==a.cameraUpVector&&
|
|
(this.settings.cameraUpVector=JSM.CoordFromArray(a.cameraUpVector)),void 0!==a.lightAmbientColor&&(this.settings.lightAmbientColor=a.lightAmbientColor),void 0!==a.lightDiffuseColor&&(this.settings.lightDiffuseColor=a.lightDiffuseColor));return!0};
|
|
JSM.ThreeViewer.prototype.InitThree=function(a){this.canvas=a;if(!this.canvas||!this.canvas.getContext)return!1;this.scene=new THREE.Scene;if(!this.scene)return!1;this.renderer=new THREE.WebGLRenderer({canvas:this.canvas,antialias:!0});if(!this.renderer)return!1;this.renderer.setClearColor(new THREE.Color(16777215));this.renderer.setSize(this.canvas.width,this.canvas.height);return!0};
|
|
JSM.ThreeViewer.prototype.InitCamera=function(a){this.cameraMove=new JSM.Camera(JSM.CoordFromArray(a.cameraEyePosition),JSM.CoordFromArray(a.cameraCenterPosition),JSM.CoordFromArray(a.cameraUpVector),a.fieldOfView,a.nearClippingPlane,a.farClippingPlane);if(!this.cameraMove)return!1;this.navigation=new JSM.Navigation;if(!this.navigation.Init(this.canvas,this.cameraMove,this.DrawIfNeeded.bind(this),this.Resize.bind(this)))return!1;this.camera=new THREE.PerspectiveCamera(this.cameraMove.fieldOfView,
|
|
this.canvas.width/this.canvas.height,this.cameraMove.nearClippingPlane,this.cameraMove.farClippingPlane);if(!this.camera)return!1;this.scene.add(this.camera);return!0};
|
|
JSM.ThreeViewer.prototype.InitLights=function(){var a=new THREE.Color,b=new THREE.Color;a.setRGB(this.settings.lightAmbientColor[0],this.settings.lightAmbientColor[1],this.settings.lightAmbientColor[2]);b.setRGB(this.settings.lightDiffuseColor[0],this.settings.lightDiffuseColor[1],this.settings.lightDiffuseColor[2]);this.ambientLight=new THREE.AmbientLight(a.getHex());if(!this.ambientLight)return!1;this.scene.add(this.ambientLight);this.directionalLight=new THREE.DirectionalLight(b.getHex());if(!this.directionalLight)return!1;
|
|
a=(new THREE.Vector3).subVectors(this.cameraMove.eye,this.cameraMove.center);this.directionalLight.position.set(a.x,a.y,a.z);this.scene.add(this.directionalLight);return!0};JSM.ThreeViewer.prototype.SetRunBeforeRender=function(a){this.runBeforeRender=a};JSM.ThreeViewer.prototype.SetRunAfterRender=function(a){this.runAfterRender=a};JSM.ThreeViewer.prototype.SetClearColor=function(a){this.renderer.setClearColor(new THREE.Color(a));this.DrawIfNeeded()};
|
|
JSM.ThreeViewer.prototype.AddMesh=function(a){this.scene.add(a);this.DrawIfNeeded()};JSM.ThreeViewer.prototype.AddMeshes=function(a){var b;for(b=0;b<a.length;b++)this.scene.add(a[b]);this.DrawIfNeeded()};JSM.ThreeViewer.prototype.MeshCount=function(){var a=0;this.scene.traverse(function(b){b instanceof THREE.Mesh&&(a+=1)});return a};JSM.ThreeViewer.prototype.VertexCount=function(){var a=0;this.scene.traverse(function(b){b instanceof THREE.Mesh&&(a+=b.geometry.vertices.length)});return a};
|
|
JSM.ThreeViewer.prototype.FaceCount=function(){var a=0;this.scene.traverse(function(b){b instanceof THREE.Mesh&&(a+=b.geometry.faces.length)});return a};JSM.ThreeViewer.prototype.GetMesh=function(a){var b=null,d=0,c;for(c=0;c<this.scene.children.length;c++)if(b=this.scene.children[c],b instanceof THREE.Mesh){if(d==a)return b;d+=1}return null};JSM.ThreeViewer.prototype.RemoveMesh=function(a){a.geometry.dispose();this.scene.remove(a);this.DrawIfNeeded()};
|
|
JSM.ThreeViewer.prototype.RemoveMeshes=function(){var a,b;for(b=0;b<this.scene.children.length;b++)a=this.scene.children[b],a instanceof THREE.Mesh&&(a.geometry.dispose(),this.scene.remove(a),b--);this.DrawIfNeeded()};JSM.ThreeViewer.prototype.RemoveLastMesh=function(){var a=null;this.scene.traverse(function(b){b instanceof THREE.Mesh&&(a=b)});null!==a&&this.scene.remove(a);this.DrawIfNeeded()};
|
|
JSM.ThreeViewer.prototype.SetCamera=function(a,b,d){this.navigation.SetCamera(a,b,d);this.navigation.SetOrbitCenter(b.Clone());this.DrawIfNeeded()};JSM.ThreeViewer.prototype.Resize=function(){this.camera.aspect=this.canvas.width/this.canvas.height;this.camera.updateProjectionMatrix();this.renderer.setSize(this.canvas.width,this.canvas.height);this.DrawIfNeeded()};
|
|
JSM.ThreeViewer.prototype.FitInWindow=function(){if(0!==this.MeshCount()){var a=this.GetBoundingSphere();this.navigation.FitInWindow(a.GetCenter(),a.GetRadius());this.DrawIfNeeded()}};JSM.ThreeViewer.prototype.AdjustClippingPlanes=function(a){this.GetBoundingSphere().GetRadius()<a?(this.camera.near=0.1,this.camera.far=1E3):(this.camera.near=10,this.camera.far=1E6);this.camera.updateProjectionMatrix();this.Draw()};JSM.ThreeViewer.prototype.GetCenter=function(){return this.GetBoundingBox().GetCenter()};
|
|
JSM.ThreeViewer.prototype.GetBoundingBox=function(){var a=new JSM.Coord(JSM.Inf,JSM.Inf,JSM.Inf),b=new JSM.Coord(-JSM.Inf,-JSM.Inf,-JSM.Inf),d,c;this.scene.traverse(function(f){if(f instanceof THREE.Mesh){d=f.geometry;var e;for(e=0;e<d.vertices.length;e++)c=d.vertices[e].clone(),c.add(f.position),a.x=JSM.Minimum(a.x,c.x),a.y=JSM.Minimum(a.y,c.y),a.z=JSM.Minimum(a.z,c.z),b.x=JSM.Maximum(b.x,c.x),b.y=JSM.Maximum(b.y,c.y),b.z=JSM.Maximum(b.z,c.z)}});return new JSM.Box(a,b)};
|
|
JSM.ThreeViewer.prototype.GetBoundingSphere=function(){var a=this.GetCenter(),b=0,d,c,f;this.scene.traverse(function(e){if(e instanceof THREE.Mesh){d=e.geometry;var h;for(h=0;h<d.vertices.length;h++)c=d.vertices[h].clone(),c.add(e.position),f=a.DistanceTo(new JSM.Coord(c.x,c.y,c.z)),JSM.IsGreater(f,b)&&(b=f)}});return new JSM.Sphere(a,b)};
|
|
JSM.ThreeViewer.prototype.GetObjectsUnderPosition=function(a,b){var d=2*(a/this.canvas.width)-1,c=2*-(b/this.canvas.height)+1,f=new THREE.Projector,e=this.camera.position,d=new THREE.Vector3(d,c,0.5);f.unprojectVector(d,this.camera);d.sub(e);d.normalize();return(new THREE.Raycaster(e,d)).intersectObjects(this.scene.children)};JSM.ThreeViewer.prototype.GetObjectsUnderMouse=function(){return this.GetObjectsUnderPosition(this.navigation.mouse.currX,this.navigation.mouse.currY)};
|
|
JSM.ThreeViewer.prototype.GetObjectsUnderTouch=function(){return this.GetObjectsUnderPosition(this.navigation.touch.currX,this.navigation.touch.currY)};JSM.ThreeViewer.prototype.ProjectVector=function(a,b,d){var c=this.canvas.width/2,f=this.canvas.height/2,e=new THREE.Projector;a=new THREE.Vector3(a,b,d);e.projectVector(a,this.camera);a.x=a.x*c+c;a.y=-(a.y*f)+f;return a};JSM.ThreeViewer.prototype.EnableDraw=function(a){this.enableDraw=a};
|
|
JSM.ThreeViewer.prototype.Draw=function(){if(this.enableDraw){null!==this.runBeforeRender&&this.runBeforeRender();this.camera.position.set(this.cameraMove.eye.x,this.cameraMove.eye.y,this.cameraMove.eye.z);this.camera.up.set(this.cameraMove.up.x,this.cameraMove.up.y,this.cameraMove.up.z);this.camera.lookAt(new THREE.Vector3(this.cameraMove.center.x,this.cameraMove.center.y,this.cameraMove.center.z));var a=(new THREE.Vector3).subVectors(this.cameraMove.eye,this.cameraMove.center);this.directionalLight.position.set(a.x,
|
|
a.y,a.z);this.renderer.render(this.scene,this.camera);null!==this.runAfterRender&&this.runAfterRender();this.drawLoop&&requestAnimationFrame(this.Draw.bind(this))}};JSM.ThreeViewer.prototype.DrawIfNeeded=function(){this.drawLoop||this.Draw()};JSM.ThreeViewer.prototype.StartDrawLoop=function(){this.drawLoop=!0;this.Draw()};
|