java整合webgl stl_使用WebGL实现一个Viewer来显示STL文件

WebViewer - STL

varcontainer;varcamera, cameraTarget, scene, renderer;varcameraType= 1;varperspectiveAngle= 45;varcameraPosX= 200;varcameraPosY= 200;varcameraPosZ= 200;varcameraTargetX= 0;varcameraTargetY= 0;varcameraTargetZ= 0;varupVectorX= 0;varupVectorY= 1;varupVectorZ= 0;varcameralScale= 5;

init();

animate();functiongetQueryStringByName(name){varresult=location.search.match(newRegExp("[\?\&]" +name+ "=([^\&]+)","i"));if(result== null ||result.length< 1){return "";

}returnresult[1];

}functioninit() {

container=document.createElement('div');

document.body.appendChild( container );//set camera

varcameraTypeStr=getQueryStringByName('cameraType');

cameraType=cameraTypeStr== "" ?cameraType : parseInt(cameraTypeStr);varperspectiveAngleStr=getQueryStringByName('perspectiveAngle');

perspectiveAngle=perspectiveAngleStr== "" ?perspectiveAngle : parseFloat(perspectiveAngleStr);varcameraPosXStr=getQueryStringByName('cameraPosX');

cameraPosX=cameraPosXStr== "" ?cameraPosX : parseFloat(cameraPosXStr)*cameralScale;varcameraPosYStr=getQueryStringByName('cameraPosY');

cameraPosY=cameraPosYStr== "" ?cameraPosY : parseFloat(cameraPosYStr)*cameralScale;varcameraPosZStr=getQueryStringByName('cameraPosZ');

cameraPosZ=cameraPosZStr== "" ?cameraPosZ : parseFloat(cameraPosZStr)*cameralScale;varcameraTargetXStr=getQueryStringByName('cameraTargetX');

cameraTargetX=cameraTargetXStr== "" ?cameraTargetX : parseFloat(cameraTargetXStr)*cameralScale;varcameraTargetYStr=getQueryStringByName('cameraTargetY');

cameraTargetY=cameraTargetYStr== "" ?cameraTargetY : parseFloat(cameraTargetYStr)*cameralScale;varcameraTargetZStr=getQueryStringByName('cameraTargetZ');

cameraTargetZ=cameraTargetZStr== "" ?cameraTargetZ : parseFloat(cameraTargetZStr)*cameralScale;varupVectorXStr=getQueryStringByName('upVectorX');

upVectorX=upVectorXStr== "" ?upVectorX : parseFloat(upVectorXStr)*cameralScale;varupVectorYStr=getQueryStringByName('upVectorY');

upVectorY=upVectorYStr== "" ?upVectorY : parseFloat(upVectorYStr)*cameralScale;varupVectorZStr=getQueryStringByName('upVectorZ');

upVectorZ=upVectorZStr== "" ?upVectorZ : parseFloat(upVectorZStr)*cameralScale;if(cameraType== 0) {

camera= newTHREE.OrthographicCamera( window.innerWidth/ - 2, window.innerWidth/ 2, window.innerHeight/ 2, window.innerHeight/ - 2,1,10000);

}else{

camera= newTHREE.PerspectiveCamera( perspectiveAngle, window.innerWidth/window.innerHeight,1,10000);

}

camera.position.set( cameraPosX, cameraPosY, cameraPosZ);

camera.up.set(upVectorX, upVectorY, upVectorZ);

cameraTarget= newTHREE.Vector3( cameraTargetX, cameraTargetY, cameraTargetZ );

camera.lookAt( cameraTarget );

scene= newTHREE.Scene();

scene.fog= newTHREE.Fog(0xffffff,1,10000);//load file

varloader= newTHREE.STLLoader();varmodelName=getQueryStringByName('modelName');

loader.load('../Models/' +modelName,function( geometry ) {varmaterial= newTHREE.MeshPhongMaterial( { color:0x808080, specular:0x111111, shininess:200} );varmesh= newTHREE.Mesh( geometry, material );

mesh.castShadow= true;

mesh.receiveShadow= true;

scene.add( mesh );

} );//lights

scene.add(newTHREE.AmbientLight(0x333333) );

addDirectionalLight(-1,1,1,0xFFFFFF,1.35);

addDirectionalLight(1,-1,-1,0xFFFFFF,1);//renderer

renderer= newTHREE.WebGLRenderer( { antialias:true} );

renderer.setClearColor( scene.fog.color );

renderer.setSize( window.innerWidth, window.innerHeight );

renderer.gammaInput= true;

renderer.gammaOutput= true;

renderer.shadowMapEnabled= true;

renderer.shadowMapCullFace=THREE.CullFaceBack;

container.appendChild( renderer.domElement );//orbit control

control= newTHREE.OrbitControls( camera, renderer.domElement );//events

window.addEventListener('resize', onWindowResize,false);

}functionaddDirectionalLight( x, y, z, color, intensity ) {vardirectionalLight= newTHREE.DirectionalLight( color, intensity );

directionalLight.position.set( x, y, z )

scene.add( directionalLight );

}functiononWindowResize() {

camera.aspect=window.innerWidth/window.innerHeight;

camera.updateProjectionMatrix();

renderer.setSize( window.innerWidth, window.innerHeight );

}functionanimate() {

requestAnimationFrame( animate );

render();

}functionrender() {//var timer = Date.now() * 0.0005;//

//camera.position.x = Math.cos( timer ) * 3;//camera.position.z = Math.sin( timer ) * 3;

renderer.render( scene, camera );

}

你可能感兴趣的:(java整合webgl,stl)