📄 world.java
字号:
// background Color3f bgColor = wd.backgroundColor; Background bgNode = new Background(bgColor); bgNode.setApplicationBounds(bounds); sceneTrans.addChild(bgNode); // ambient light TransformGroup tga = new TransformGroup(); AmbientLight ambientLight = new AmbientLight(wd.ambientLightColor); ambientLight.setInfluencingBounds(bounds); tga.addChild(ambientLight); sceneBranch.addChild(tga); // directional lights light1 = addLight(wd.light1Position ,wd.light1Color); light2 = addLight(wd.light2Position,wd.light2Color); light1.setEnable(wd.light1IsOn); light2.setEnable(wd.light2IsOn); createFloor(wd); if (wd.hasAxis) createAxis(); pickableSceneBranch = new BranchGroup(); sceneTrans.addChild(pickableSceneBranch); pickableSceneBranch.setCapability(BranchGroup.ALLOW_CHILDREN_EXTEND); pickableSceneBranch.setCapability(BranchGroup.ALLOW_DETACH); pickableSceneBranch.setCapability(BranchGroup.ALLOW_CHILDREN_WRITE); } /** Creates the floor of the 3d world. * Used only in the creation phase. * @param ed the environment description. */ private void createFloor(EnvironmentDescription wd){ float minx = -worldSize/2, maxx = worldSize/2; float minz = -worldSize/2,maxz = worldSize/2; Point3f[] floorCoords = { new Point3f( minx, 0.0f,minz), new Point3f( minx, 0.0f,maxz ), new Point3f( maxx, 0.0f,maxz), new Point3f( maxx, 0.0f,minz ) }; Vector3f[] floorNormals = { new Vector3f( -0.6f, 0.6f, -0.6f ), new Vector3f( -0.6f, 0.6f, 0.6f ), new Vector3f( 0.6f, 0.6f, 0.6f ), new Vector3f( 0.6f, 0.6f, -0.6f ) }; Vector3f[] floorNormalsSimple = { new Vector3f( 0, 1, 0 ), new Vector3f( 0, 1, 0 ), new Vector3f( 0, 1, 0 ), new Vector3f( 0, 1, 0 ) }; QuadArray floorQuads=null; switch(wd.normalsStyle){ case EnvironmentDescription.NORMALS_REALISTIC: floorQuads= new QuadArray( floorCoords.length, GeometryArray.COORDINATES | GeometryArray.NORMALS ); floorQuads.setNormals( 0, floorNormals ); break; case EnvironmentDescription.NORMALS_SIMPLE: floorQuads= new QuadArray( floorCoords.length, GeometryArray.COORDINATES|GeometryArray.NORMALS ); floorQuads.setNormals( 0, floorNormalsSimple ); break; } floorQuads.setCoordinates( 0, floorCoords ); Appearance floorAppear = new Appearance(); Material mat = new Material(); mat.setDiffuseColor(wd.floorColor); Color3f specular = new Color3f(wd.floorColor); specular.scale(1.1f); specular.clampMax(0.8f); mat.setSpecularColor(specular); floorAppear.setMaterial(mat); Shape3D floor = new Shape3D( floorQuads, floorAppear ); floor.setPickable(false); floor.setCollidable(false); sceneTrans.addChild(floor); } /** Creates a representation of the 3 axis of the 3d world. * Used only in the creation phase. */ private void createAxis(){ Point3f[] axisCoords = { // X axis arrow new Point3f( 0.0f, 0.001f,0.0f), new Point3f( 1, 0.001f,0.0f ), new Point3f( 1, 0.001f,0.0f ), new Point3f( 0.95f, 0.001f,0.05f ), new Point3f( 1, 0.001f,0.0f ), new Point3f( 0.95f, 0.001f,-0.05f ), // a small X new Point3f( 1.0f, 0.001f,0.1f ), new Point3f( 0.9f, 0.001f,0.2f ), new Point3f( 1.0f, 0.001f,0.2f ), new Point3f( 0.9f, 0.001f,0.1f ), // Z axis arrow new Point3f( 0.0f, 0.001f,0.0f), new Point3f( 0, 0.001f,1.0f ), new Point3f( 0, 0.001f,1.0f ), new Point3f( 0.05f, 0.001f,0.95f ), new Point3f( 0, 0.001f,1.0f ), new Point3f( -0.05f, 0.001f,0.95f ), // a small Z new Point3f( 0.1f, 0.001f,1.0f ), new Point3f( 0.2f, 0.001f,1.0f ), new Point3f( 0.1f, 0.001f,0.9f ), new Point3f( 0.2f, 0.001f,0.9f ), new Point3f( 0.1f, 0.001f,1.0f ), new Point3f( 0.2f, 0.001f,0.9f ), // Y axis arrow new Point3f( 0.0f, 0.001f,0.0f), new Point3f( 0, 1.0f,0.0f ), new Point3f( 0, 1.0f,0.0f ), new Point3f( 0.05f, 0.95f,0f ), new Point3f( 0, 1f,0f ), new Point3f( 0.00f, 0.95f,0.05f ), // a small Y new Point3f( 0.2f, 1f,0.0f ), new Point3f( 0.1f, 0.9f,0f ), new Point3f( 0.1f, 1.0f,0.0f ), new Point3f( 0.15f, 0.95f,0.0f ) }; // scale axis drawing to 5% of word size for (int i = 0;i < axisCoords.length;i++){ axisCoords[i].scale(worldSize*0.05f); } LineArray axisLines = new LineArray( axisCoords.length, GeometryArray.COORDINATES ); axisLines.setCoordinates( 0, axisCoords ); Appearance axisAppear = new Appearance(); ColoringAttributes ca = new ColoringAttributes(); ca.setColor(white); axisAppear.setColoringAttributes(ca); Material mat = new Material(); mat.setDiffuseColor(white); axisAppear.setMaterial(mat); Shape3D axis = new Shape3D( axisLines, axisAppear ); axis.setCollidable(false); axis.setPickable(false); sceneTrans.addChild(axis); } /** * Change the user view Point . * Note that we modify the ViewBranch transform not the scene transform. * @param type can be VIEW_FROM_TOP,VIEW_FROM_EAST,VIEW_BEHIND_AGENT * @param agent : specify the agent if VIEW_BEHIND_AGENT * * The VIEW_BEHIND_AGENT case has to be called regularly because of the agent * displacement. */ public void changeViewPoint(int type, SimpleAgent agent) { Point3d p1 = new Point3d(); Point3d p2 = new Point3d(); Transform3D t1 = new Transform3D(); Transform3D t2 = new Transform3D(); t1.setIdentity(); t2.setIdentity(); mouseOrbiter.resetView(); switch (type) { case VIEW_FROM_TOP: t1.lookAt(new Point3d(0, worldSize * 1.2, 0), new Point3d(0, 0, 0), new Vector3d(0, 0, -1)); t1.invert(); viewTransformGroup.setTransform(t1); break; case VIEW_FROM_EAST: t1.lookAt(new Point3d(worldSize, worldSize, 0), new Point3d(0, 0, 0), new Vector3d(-1, 0, 0)); t1.invert(); viewTransformGroup.setTransform(t1); break; case VIEW_BEHIND_AGENT: t1.setTranslation(new Vector3d(-agent.getRadius() * 2, 0, 0)); agent.getGroup().getLocalToVworld(t2); t1.mul(t2); viewTransformGroup.setTransform(t1); break; case VIEW_ABOVE_AGENT: agent.getRotationTransformGroup().getLocalToVworld(t1); t1.transform(p1); t1.transform(p2); p2.y = worldSize * .8; t1.lookAt(p2, p1, new Vector3d(0, 0, -1)); t1.invert(); viewTransformGroup.setTransform(t1); break; case VIEW_ABOVE_AGENT_NEAR: agent.getRotationTransformGroup().getLocalToVworld(t1); t1.transform(p1); t1.transform(p2); p2.y = agent.getHeight() * worldSize *0.5; // avoid front clipping if (p2.y <0.2) p2.y =0.2; t1.lookAt(p2, p1, new Vector3d(0, 0, -1)); t1.invert(); viewTransformGroup.setTransform(t1); break; case VIEW_AGENT_SIDE: agent.getRotationTransformGroup().getLocalToVworld(t1); t1.transform(p1); t1.transform(p2); agent.rotation.transform(p2); t2.setTranslation(new Vector3d(0,agent.getHeight()*2,agent.getRadius()*10)); t2.transform(p2); t1.lookAt(p2, p1, new Vector3d(0, 1, 0)); t1.invert(); viewTransformGroup.setTransform(t1); break; } } boolean checkCollisionAgainstBlockWorldObjects(BoundingSphere bs){ return false; } /** Stop rendering on main canvas 3D . Used for background mode. */ public void stopRendering(){ canvas3d.stopRenderer(); } /** Restart rendering on main canvas 3D . Used for background mode. */ public void startRendering(){ canvas3d.startRenderer(); } /** Do one rendering on main canvas 3D . Used for background mode. */ public void renderOnce(){ canvas3d.startRenderer(); try { Thread.sleep((int)(100)); } catch (InterruptedException e) { e.printStackTrace(); } canvas3d.stopRenderer(); } /** Destroy the java3d graph */ public void dispose(){ universe.removeAllLocales(); view.removeAllCanvas3Ds(); } /** * @return the canvas3D associated to the world view platform */ public Canvas3D getCanvas3D(){ return canvas3d;} /** @return the scene branchgroup containing the world's object which can be picked*/ BranchGroup getPickableSceneBranch(){ return pickableSceneBranch;} }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -