j3dutils.java
来自「The ElectricTM VLSI Design System is an 」· Java 代码 · 共 1,380 行 · 第 1/4 页
JAVA
1,380 行
} /** Create the background node based on given background color * @param scene */ public static void createBackground(BranchGroup scene) { setBackgroundColor(scene); BackgroundObserver bg = new BackgroundObserver(backgroundColor.getValue()); backgroundColor.addObserver(bg); bg.setApplicationBounds(infiniteBounds); scene.addChild(bg); } /** * Create alpha according to given interval time in miliseconds * @param speed */ public static void setAlpha(int speed) { if (jAlpha == null) jAlpha = new J3DAlpha(speed, true, 0.5f); else jAlpha.setAlphaSpeed(speed); } /** * Create the lights (directional and ambiental) for the given scene graph * based on User's data * @param scene */ public static void createLights(BranchGroup scene) { // Checking if light colors are available setDirectionalColor(scene); setAmbientalColor(scene); setDirections(scene); AmbientLightObserver ambientalLight = new AmbientLightObserver(ambientalColor.getValue()); ambientalLight.setInfluencingBounds(infiniteBounds); ambientalLight.setCapability(AmbientLight.ALLOW_COLOR_READ); ambientalLight.setCapability(AmbientLight.ALLOW_COLOR_WRITE); // adding observer ambientalColor.addObserver(ambientalLight); // Add ambiental light to the env. scene.addChild(ambientalLight); for (int i = 0; i < lights.length; i++) { if (lights[i] == null || lights[i].getValue() == null || (lights[i].getValue().x == 0 && lights[i].getValue().y == 0 && lights[i].getValue().z == 0)) continue; // invalid light DirectionalLightObserver directionalLight = new DirectionalLightObserver(directionalColor.getValue(), lights[i].getValue()); directionalLight.setInfluencingBounds(infiniteBounds); // Allow to turn off light while the scene graph is live directionalLight.setCapability(Light.ALLOW_STATE_WRITE); directionalLight.setCapability(DirectionalLight.ALLOW_DIRECTION_READ); directionalLight.setCapability(DirectionalLight.ALLOW_DIRECTION_WRITE); directionalLight.setCapability(DirectionalLight.ALLOW_COLOR_READ); directionalLight.setCapability(DirectionalLight.ALLOW_COLOR_WRITE); // adding observers lights[i].addObserver(directionalLight); directionalColor.addObserver(directionalLight); // Add lights to the env. scene.addChild(directionalLight); } } public static void setViewPoint(SimpleUniverse u, Canvas3D canvas, BranchGroup scene, Rectangle2D cellBnd) { BoundingSphere sceneBnd = (BoundingSphere)scene.getBounds(); double radius = sceneBnd.getRadius(); View view = u.getViewer().getView(); // Too expensive at this point if (canvas.getSceneAntialiasingAvailable() && is3DAntialiasing()) view.setSceneAntialiasingEnable(true); // Setting the projection policy view.setProjectionPolicy(is3DPerspective()? View.PERSPECTIVE_PROJECTION : View.PARALLEL_PROJECTION); if (!is3DPerspective()) view.setCompatibilityModeEnable(true); // Setting transparency sorting //view.setTransparencySortingPolicy(View.TRANSPARENCY_SORT_GEOMETRY); //view.setDepthBufferFreezeTransparent(false); // set to true only for transparent layers Point3d c1 = new Point3d(); sceneBnd.getCenter(c1); Vector3d vCenter = new Vector3d(c1); double vDist = 1.4 * radius / Math.tan(view.getFieldOfView()/2.0); Point3d c2 = new Point3d(); sceneBnd.getCenter(c2); c2.z += vDist; //if (User.is3DPerspective()) vCenter.z += vDist; Transform3D vTrans = new Transform3D(); vTrans.set(vCenter); view.setBackClipDistance((vDist+radius)*2.0); view.setFrontClipDistance((vDist+radius)/200.0); view.setBackClipPolicy(View.VIRTUAL_EYE); view.setFrontClipPolicy(View.VIRTUAL_EYE); if (is3DPerspective()) { u.getViewingPlatform().getViewPlatformTransform().setTransform(vTrans); } else { Transform3D proj = new Transform3D(); proj.ortho(cellBnd.getMinX(), cellBnd.getMinX(), cellBnd.getMinY(), cellBnd.getMaxY(), (vDist+radius)/200.0, (vDist+radius)*2.0); view.setVpcToEc(proj); //viewingPlatform.getViewPlatformTransform().setTransform(lookAt); } }// public static double covertToDegrees(double radiant)// {// return ((180*radiant)/Math.PI);// } public static double convertToRadiant(double degrees) { return ((Math.PI*degrees)/180); } /** * Utility class to modify live/compiled scene graph */ private static class JGeometryUpdater implements GeometryUpdater { float z1, z2, origZ1, origZ2; public JGeometryUpdater(float origZ1, float origZ2, float z1, float z2) { this.z1 = z1; this.z2 = z2; this.origZ1 = origZ1; this.origZ2 = origZ2; } public void updateData(Geometry geometry) { if (!(geometry instanceof GeometryArray)) return; GeometryArray ga = (GeometryArray)geometry; float[] vals = ga.getCoordRefFloat(); for (int i = 0; i < vals.length/3; i++) { if (DBMath.areEquals(vals[i*3+2], origZ1)) vals[i*3+2] = z1; else if (DBMath.areEquals(vals[i*3+2], origZ2)) vals[i*3+2] = z2; } ga.setCoordRefFloat(vals); } } /** * Method to reset z values of shapes created with addPolyhedron * @param shape * @param origZ1 * @param origZ2 * @param z1 * @param z2 * @return true if values were valid */ public static boolean updateZValues(Shape3D shape, float origZ1, float origZ2, float z1, float z2) { if (DBMath.areEquals(z1, z2)) return false; // nothing to do. Eg. 0 as value GeometryArray ga = (GeometryArray)shape.getGeometry(); ga.updateData(new JGeometryUpdater(origZ1, origZ2, z1, z2)); return true; } /** * Method to add a cylindrical shaped element * @param points * @param distance * @param thickness * @param ap * @param objTrans * @return */ public static Node addCylinder(Point2D[] points, double distance, double thickness, Appearance ap, TransformGroup objTrans) { double cX = points[0].getX(); double cY = points[0].getY(); double radius = points[0].distance(points[1]); Cylinder cylinder = new Cylinder((float)radius, (float)thickness, ap); Vector3d bottomCenter = new Vector3d(cX,cY,distance); Transform3D t = new Transform3D(); t.rotX(Math.PI/2); t.setTranslation(bottomCenter); t.setScale(1); TransformGroup grp = new TransformGroup(t); grp.addChild(cylinder); // adding Primitive to TransformaGroup so location can be modified. grp.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE); grp.setCapability(TransformGroup.ALLOW_TRANSFORM_READ); grp.setCapability(TransformGroup.ALLOW_CHILDREN_EXTEND); grp.setCapability(TransformGroup.ALLOW_CHILDREN_READ); grp.setCapability(TransformGroup.ALLOW_CHILDREN_WRITE); grp.setCapability(TransformGroup.ENABLE_PICK_REPORTING); grp.setCapability(TransformGroup.ALLOW_LOCAL_TO_VWORLD_READ); grp.setCapability(TransformGroup.ALLOW_PICKABLE_READ); grp.setCapability(TransformGroup.ALLOW_BOUNDS_READ); Shape3D[] shapes = new Shape3D[3]; shapes[0] = cylinder.getShape(Cylinder.BODY); shapes[1] = cylinder.getShape(Cylinder.TOP); shapes[2] = cylinder.getShape(Cylinder.BOTTOM); for (int i = 0; i < 3; i++) { shapes[i].setCapability(Primitive.ENABLE_PICK_REPORTING); shapes[i].setCapability(Primitive.ENABLE_GEOMETRY_PICKING); shapes[i].setCapability(Node.ALLOW_LOCAL_TO_VWORLD_READ); shapes[i].setCapability(Primitive.ALLOW_PICKABLE_READ); shapes[i].setCapability(Shape3D.ALLOW_APPEARANCE_READ); shapes[i].setCapability(Shape3D.ALLOW_APPEARANCE_WRITE); shapes[i].setCapability(Primitive.ALLOW_BOUNDS_READ); shapes[i].setCapability(Shape3D.ALLOW_GEOMETRY_WRITE); PickTool.setCapabilities(shapes[i], PickTool.INTERSECT_COORD); } objTrans.addChild(grp); return cylinder; } /** * Method to add a polyhedron to the transformation group * @param objTrans */ public static Shape3D addPolyhedron(Rectangle2D bounds, double distance, double thickness, Appearance ap, TransformGroup objTrans) { GeometryInfo gi = new GeometryInfo(GeometryInfo.QUAD_ARRAY); double height = thickness + distance; Point3d[] pts = new Point3d[8]; pts[0] = new Point3d(bounds.getMinX(), bounds.getMinY(), distance); pts[1] = new Point3d(bounds.getMinX(), bounds.getMaxY(), distance); pts[2] = new Point3d(bounds.getMaxX(), bounds.getMaxY(), distance); pts[3] = new Point3d(bounds.getMaxX(), bounds.getMinY(), distance); pts[4] = new Point3d(bounds.getMinX(), bounds.getMinY(), height); pts[5] = new Point3d(bounds.getMinX(), bounds.getMaxY(), height); pts[6] = new Point3d(bounds.getMaxX(), bounds.getMaxY(), height); pts[7] = new Point3d(bounds.getMaxX(), bounds.getMinY(), height); int[] indices = {0, 1, 2, 3, /* bottom z */ 0, 4, 5, 1, /* back y */ 0, 3, 7, 4, /* back x */ 1, 5, 6, 2, /* front x */ 2, 6, 7, 3, /* front y */ 4, 7, 6, 5}; /* top z */ gi.setCoordinates(pts); gi.setCoordinateIndices(indices); NormalGenerator ng = new NormalGenerator(); ng.generateNormals(gi); GeometryArray c = gi.getGeometryArray(true, false, false); c.setCapability(GeometryArray.ALLOW_INTERSECT); //c.setCapability(GeometryArray.ALLOW_COORDINATE_READ); c.setCapability(GeometryArray.ALLOW_COORDINATE_WRITE); c.setCapability(GeometryArray.BY_REFERENCE); c.setCapability(GeometryArray.ALLOW_REF_DATA_READ); c.setCapability(GeometryArray.ALLOW_REF_DATA_WRITE); Shape3D box = new Shape3D(c, ap); box.setCapability(Shape3D.ENABLE_PICK_REPORTING); box.setCapability(Node.ALLOW_LOCAL_TO_VWORLD_READ); box.setCapability(Shape3D.ALLOW_PICKABLE_READ); box.setCapability(Shape3D.ALLOW_APPEARANCE_READ); box.setCapability(Shape3D.ALLOW_APPEARANCE_WRITE); box.setCapability(Shape3D.ALLOW_BOUNDS_READ); box.setCapability(Shape3D.ALLOW_GEOMETRY_WRITE); PickTool.setCapabilities(box, PickTool.INTERSECT_FULL); objTrans.addChild(box); return(box); } /** * Simple method to generate polyhedra * @param pts * @param listLen * @param ap * @return */ public static Shape3D addShape3D(Point3d[] pts, int listLen, Appearance ap, TransformGroup objTrans) { int numFaces = listLen + 2; // contour + top + bottom int[] indices = new int[listLen*6]; int[] stripCounts = new int[numFaces]; int[] contourCount = new int[numFaces]; Arrays.fill(contourCount, 1); Arrays.fill(stripCounts, 4); stripCounts[0] = listLen; // top stripCounts[numFaces-1] = listLen; // bottom int count = 0; // Top face for (int i = 0; i < listLen; i++) indices[count++] = i; // Contour for (int i = 0; i < listLen; i++) { indices[count++] = i; indices[count++] = i + listLen; indices[count++] = (i+1)%listLen + listLen; indices[count++] = (i+1)%listLen; } // Bottom face for (int i = 0; i < listLen; i++) indices[count++] = (listLen-i)%listLen + listLen; GeometryInfo gi = new GeometryInfo(GeometryInfo.POLYGON_ARRAY); gi.setCoordinates(pts); gi.setCoordinateIndices(indices); gi.setStripCounts(stripCounts); gi.setContourCounts(contourCount); NormalGenerator ng = new NormalGenerator(); ng.setCreaseAngle ((float) Math.toRadians(30)); ng.generateNormals(gi); GeometryArray c = gi.getGeometryArray(); c.setCapability(GeometryArray.ALLOW_INTERSECT); Shape3D box = new Shape3D(c, ap); box.setCapability(Shape3D.ENABLE_PICK_REPORTING); box.setCapability(Node.ALLOW_LOCAL_TO_VWORLD_READ); box.setCapability(Shape3D.ALLOW_PICKABLE_READ); box.setCapability(Shape3D.ALLOW_APPEARANCE_READ); box.setCapability(Shape3D.ALLOW_APPEARANCE_WRITE); box.setCapability(Shape3D.ALLOW_BOUNDS_READ);
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?