题记
在2020年,笔者最好的朋友浩哥找工作的时候,北京的一家公司让他做一个正方体内接一个球 然后再球里面套入一个内接四面体,然后鼠标移动,这个整体就会移动。动态图丢失了,非常抱歉没有场景给了24h,让他完成,如果做出来了,就录用他。当时,我和这位朋友研究了一下, 然后开始找资料–找到一本书好像叫什么Java3d游戏编程,然后网上有一篇Blog在eclipse搭建环境来做3d效果。最终我们没把里面的四面体做出来,结果我这个兄弟就错过这次工作机会,目前去了北京考研教育机构担任team leader。
不忍心当初写的一些demo丢失,还是水一篇Blog记录一下,那时候我们一下写的垃圾代码。
一个没做到位的实例
import com.sun.j3d.utils.applet.MainFrame;
import com.sun.j3d.utils.behaviors.mouse.MouseRotate;
import com.sun.j3d.utils.behaviors.mouse.MouseTranslate;
import com.sun.j3d.utils.behaviors.mouse.MouseWheelZoom;
import com.sun.j3d.utils.geometry.Box;
import com.sun.j3d.utils.geometry.ColorCube;
import com.sun.j3d.utils.geometry.Cone;
import com.sun.j3d.utils.geometry.Cylinder;
import com.sun.j3d.utils.geometry.GeometryInfo;
import com.sun.j3d.utils.geometry.NormalGenerator;
import com.sun.j3d.utils.geometry.Sphere;
import com.sun.j3d.utils.universe.SimpleUniverse;
import java.applet.Applet;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GraphicsConfiguration;
import javax.media.j3d.Alpha;
import javax.media.j3d.AmbientLight;
import javax.media.j3d.Appearance;
import javax.media.j3d.Background;
import javax.media.j3d.BoundingSphere;
import javax.media.j3d.BranchGroup;
import javax.media.j3d.Canvas3D;
import javax.media.j3d.DirectionalLight;
import javax.media.j3d.GeometryArray;
import javax.media.j3d.LineArray;
import javax.media.j3d.LineAttributes;
import javax.media.j3d.Material;
import javax.media.j3d.PolygonAttributes;
import javax.media.j3d.PositionInterpolator;
import javax.media.j3d.RotationInterpolator;
import javax.media.j3d.Shape3D;
import javax.media.j3d.Texture;
import javax.media.j3d.Texture2D;
import javax.media.j3d.TextureAttributes;
import javax.media.j3d.Transform3D;
import javax.media.j3d.TransformGroup;
import javax.media.j3d.TransparencyAttributes;
import javax.media.j3d.TriangleArray;
import javax.vecmath.Color3f;
import javax.vecmath.Color4f;
import javax.vecmath.Point3d;
import javax.vecmath.Point3f;
import javax.vecmath.Vector3d;
import javax.vecmath.Vector3f;
/**
*
* @author Administrator
*/
public class MyJava3D extends Applet {
public MyJava3D() {
this.setLayout(new BorderLayout());
GraphicsConfiguration configuration = SimpleUniverse.getPreferredConfiguration();
Canvas3D canvas3D = new Canvas3D(configuration);
this.add(canvas3D);
SimpleUniverse universe = new SimpleUniverse(canvas3D);
universe.addBranchGraph(getBranchGroup());
Vector3f viewTranslation = new Vector3f();
viewTranslation.z = 3;
viewTranslation.x = 0f;
viewTranslation.y = .3f;
Transform3D viewTransform = new Transform3D();
viewTransform.setTranslation(viewTranslation);
Transform3D rotation = new Transform3D();
rotation.rotX(-Math.PI / 12.0d);
rotation.mul(viewTransform);
universe.getViewingPlatform().getViewPlatformTransform().setTransform(rotation);
universe.getViewingPlatform().getViewPlatformTransform().getTransform(viewTransform);
}
private GeometryArray getGeo(float radius) {
/**
* 需要改变这个点的坐标 传入参数是外接圆的半径
*/
Point3f e = new Point3f((float) (radius / Math.cos(60.0)), -radius, 1.0f); // east
Point3f w = new Point3f((float) (-radius / Math.cos(60.0)), 0.0f, 1.0f); // west
// Point3f s = new Point3f(0.0f, 0.0f, 1.0f); // south
Point3f n = new Point3f(0.0f, 0.0f, -1.0f); // north
Point3f t = new Point3f(0.0f, 0.0f, 0.0f); // top
TriangleArray pyramidGeometry = new TriangleArray(12, TriangleArray.COORDINATES);
// 18个创建金字塔索引点
pyramidGeometry.setCoordinate(0, e);
pyramidGeometry.setCoordinate(1, t);
pyramidGeometry.setCoordinate(2, n);
pyramidGeometry.setCoordinate(3, n);
pyramidGeometry.setCoordinate(4, t);
pyramidGeometry.setCoordinate(5, w);
pyramidGeometry.setCoordinate(6, w);
pyramidGeometry.setCoordinate(7, t);
pyramidGeometry.setCoordinate(8, e);
pyramidGeometry.setCoordinate(9, n);
pyramidGeometry.setCoordinate(10, w);
pyramidGeometry.setCoordinate(11, e);
GeometryInfo geometryInfo = new GeometryInfo(pyramidGeometry);
NormalGenerator ng = new NormalGenerator();
ng.generateNormals(geometryInfo);
GeometryArray result = geometryInfo.getGeometryArray();
return result;
}
BranchGroup getBranchGroup() {
BranchGroup branchGroup = new BranchGroup();
BoundingSphere bounds = new BoundingSphere(new Point3d(0, 2.0, 7.0), 1000.0);
// set coordinates 设置坐标
Transform3D transform3D = new Transform3D();
transform3D.setTranslation(new Vector3d(0, 0, -7));
TransformGroup transformGroup = new TransformGroup();
transformGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
transformGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
transformGroup.setTransform(transform3D);
// set back color 设置背景色
Color3f backgroudColor3f = new Color3f(Color.GRAY);
Background background = new Background(backgroudColor3f);
background.setApplicationBounds(bounds);
branchGroup.addChild(background);
// set mouse's behavior 设置鼠标行为
MouseRotate mouseRotate = new MouseRotate();
mouseRotate.setTransformGroup(transformGroup);
mouseRotate.setSchedulingBounds(bounds);
transformGroup.addChild(mouseRotate);
MouseWheelZoom mouseZoom = new MouseWheelZoom();
mouseZoom.setTransformGroup(transformGroup);
mouseZoom.setSchedulingBounds(bounds);
transformGroup.addChild(mouseZoom);
MouseTranslate mouseTranslate = new MouseTranslate();
mouseTranslate.setTransformGroup(transformGroup);
mouseTranslate.setSchedulingBounds(bounds);
transformGroup.addChild(mouseTranslate);
// around in the dark 在和黑暗中
Color3f lightColor = new Color3f(Color.green);
AmbientLight ambientLight = new AmbientLight(lightColor);
ambientLight.setInfluencingBounds(bounds);
branchGroup.addChild(ambientLight);
DirectionalLight directionalLight = new DirectionalLight();
directionalLight.setColor(lightColor);
directionalLight.setInfluencingBounds(bounds);
branchGroup.addChild(directionalLight);
// add box 立方体
Color3f black = new Color3f(0.0f, 0.0f, 0.0f);
Color3f white = new Color3f(1.0f, 1.0f, 1.0f);
Color3f red = new Color3f(0.7f, .15f, .15f);
Appearance ap = new Appearance();
TransparencyAttributes t_attr = new TransparencyAttributes(TransparencyAttributes.BLENDED, 0.7f,
TransparencyAttributes.BLEND_SRC_ALPHA, TransparencyAttributes.BLEND_ONE);
ap.setTransparencyAttributes(t_attr);
ap.setMaterial(new Material(red, black, red, black, 1.0f));
TransformGroup boxGroup = new TransformGroup();
boxGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
Transform3D boxTransform3D = new Transform3D();
boxTransform3D.setTranslation(new Vector3d(0, 0, 0));
boxGroup.setTransform(boxTransform3D);
Box box = new Box(0.5f, 0.5f, 0.5f, ap); // 长宽高
boxGroup.addChild(box);
transformGroup.addChild(boxGroup);
// ------------------------------------------------------
// add sphere 添加球体
TransformGroup sphereGroup = new TransformGroup();
Transform3D sphereTransform3D = new Transform3D();
sphereTransform3D.setTranslation(new Vector3d(0, 0, 0));
sphereGroup.setTransform(sphereTransform3D);
Sphere sphere = new Sphere(0.5f, -1, 80, ap);
sphereGroup.addChild(sphere);
transformGroup.addChild(sphereGroup);
// add cone 添加锥体
TransformGroup coneGroup = new TransformGroup();
Transform3D coneTransform3D = new Transform3D();
coneTransform3D.setTranslation(new Vector3d(0, 0.25, 0));
coneGroup.setTransform(coneTransform3D);
Cone cone = new Cone(0.5f, 0.5f, ap);// 半径和高都为0.5f的圆锥
coneGroup.addChild(cone);
transformGroup.addChild(coneGroup);
// 添加正四面体
/*
* //add coordinates 添加坐标 float[] linevertX = { -5.0f, 0f, 0f, 5.0f, 0f, 0f,};
* float[] linevertY = { 0 , -5.0f, 0f, 0, 5.0f, 0f,}; float[] linevertZ = { 0 ,
* 0, -5.0f, 0, 0,5.0f,}; float[] linecolorsX = { 5.0f, 0f, 0f, 5.0f, 0.0f,
* 0f,}; float[] linecolorsY = { 0f, 5.0f, 0f, 0f, 5.0f, 0f,}; float[]
* linecolorsZ = { 0f, 0f, 5.0f, 0f, 0.0f, 5.0f,}; LineArray lineX = new
* LineArray(2, LineArray.COORDINATES | LineArray.COLOR_3);
* lineX.setCoordinates(0, linevertX); lineX.setColors(0, linecolorsX);
* LineArray lineY = new LineArray(2, LineArray.COORDINATES |
* LineArray.COLOR_3); lineY.setCoordinates(0, linevertY); lineY.setColors(0,
* linecolorsY); LineArray lineZ = new LineArray(2, LineArray.COORDINATES |
* LineArray.COLOR_3); lineZ.setCoordinates(0, linevertZ); lineZ.setColors(0,
* linecolorsZ); LineAttributes lineAttributes = new LineAttributes();
* lineAttributes.setLineWidth(3.0f);
*
* Appearance lineappearance = new Appearance();
* lineappearance.setLineAttributes(lineAttributes);
*/
Shape3D shape = getShape();
transformGroup.addChild(shape);
branchGroup.addChild(transformGroup);
// lights
// BoundingSphere bounds = new BoundingSphere(new Point3d(0.0, 0.0, 0.0), 1000.0);
Color3f light1Color = new Color3f(.7f, .7f, .7f);
Vector3f light1Direction = new Vector3f(4.0f, -7.0f, -12.0f);
DirectionalLight light1 = new DirectionalLight(light1Color, light1Direction);
light1.setInfluencingBounds(bounds);
branchGroup.addChild(light1);
Color3f ambientColor = new Color3f(.4f, .4f, .4f);
AmbientLight ambientLightNode = new AmbientLight(ambientColor);
ambientLightNode.setInfluencingBounds(bounds);
branchGroup.addChild(ambientLightNode);
return branchGroup;
}
private Shape3D getShape() {
// yellow appearance
Appearance appearance = new Appearance();
Color3f color = new Color3f(Color.red);
Color3f black = new Color3f(0.0f, 0.0f, 0.0f);
Color3f white = new Color3f(1.0f, 1.0f, 1.0f);
Texture texture = new Texture2D();
TextureAttributes texAttr = new TextureAttributes();
texAttr.setTextureMode(TextureAttributes.MODULATE);
texture.setBoundaryModeS(Texture.WRAP);
texture.setBoundaryModeT(Texture.WRAP);
texture.setBoundaryColor(new Color4f(0.0f, 1.0f, 0.0f, 0.0f));
Material mat = new Material(color, black, color, white, 70f);
appearance.setTextureAttributes(texAttr);
appearance.setMaterial(mat);
appearance.setTexture(texture);
GeometryArray result = getGeo();
Shape3D shape = new Shape3D(result, appearance);
return shape;
}
private GeometryArray getGeo() {
Point3f e = new Point3f(1.0f, 0.0f, 1.0f); // east
Point3f w = new Point3f(-1.0f, 0.0f, 1.0f); // west
// Point3f s = new Point3f(0.0f, 0.0f, 1.0f); // south
Point3f n = new Point3f(0.0f, 0.0f, -1.0f); // north
Point3f t = new Point3f(0.0f, 0.721f, 0.0f); // top
TriangleArray pyramidGeometry = new TriangleArray(12, TriangleArray.COORDINATES);
// 18个创建金字塔索引点
pyramidGeometry.setCoordinate(0, e);
pyramidGeometry.setCoordinate(1, t);
pyramidGeometry.setCoordinate(2, n);
pyramidGeometry.setCoordinate(3, n);
pyramidGeometry.setCoordinate(4, t);
pyramidGeometry.setCoordinate(5, w);
pyramidGeometry.setCoordinate(6, w);
pyramidGeometry.setCoordinate(7, t);
pyramidGeometry.setCoordinate(8, e);
pyramidGeometry.setCoordinate(9, n);
pyramidGeometry.setCoordinate(10, w);
pyramidGeometry.setCoordinate(11, e);
GeometryInfo geometryInfo = new GeometryInfo(pyramidGeometry);
NormalGenerator ng = new NormalGenerator();
ng.generateNormals(geometryInfo);
GeometryArray result = geometryInfo.getGeometryArray();
return result;
}
public static void main(String[] argsSes) {
new MainFrame(new MyJava3D(), 256, 256);
}
}
这个好像是一个网站的具体事例,应该是oracle的一个java3d网站,大家一搜就可以找到。
import javax.swing.*;
import java.awt.*;
import com.sun.j3d.utils.universe.*;
import com.sun.j3d.utils.geometry.ColorCube;
import com.sun.j3d.utils.geometry.Sphere;
import javax.media.j3d.*;
import javax.vecmath.*;
import java.awt.GraphicsConfiguration;
/**
* Simple Java 3D example program to display a spinning cube.
*/
public class HelloUniverse extends JFrame {
private SimpleUniverse univ = null;
private BranchGroup scene = null;
public BranchGroup createSceneGraph() {
// Create the root of the branch graph
BranchGroup objRoot = new BranchGroup();
// Create the TransformGroup node and initialize it to the
// identity. Enable the TRANSFORM_WRITE capability so that
// our behavior code can modify it at run time. Add it to
// the root of the subgraph.
TransformGroup objTrans = new TransformGroup();
objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
objRoot.addChild(objTrans);
// Create a simple Shape3D node; add it to the scene graph.
objTrans.addChild(new ColorCube(0.4));
//objTrans.addChild(new Sphere(.5f));
// Create a new Behavior object that will perform the
// desired operation on the specified transform and add
// it into the scene graph.
Transform3D yAxis = new Transform3D();
Alpha rotationAlpha = new Alpha(-1, 4000);
RotationInterpolator rotator = new RotationInterpolator(rotationAlpha, objTrans, yAxis, 0.0f,
(float) Math.PI * 2.0f);
BoundingSphere bounds = new BoundingSphere(new Point3d(0.0, 0.0, 0.0), 100.0);
rotator.setSchedulingBounds(bounds);
objRoot.addChild(rotator);
// Have Java 3D perform optimizations on this scene graph.
objRoot.compile();
return objRoot;
}
private Canvas3D createUniverse() {
// Get the preferred graphics configuration for the default screen
GraphicsConfiguration config = SimpleUniverse.getPreferredConfiguration();
// Create a Canvas3D using the preferred configuration
Canvas3D c = new Canvas3D(config);
// Create simple universe with view branch
univ = new SimpleUniverse(c);
// This will move the ViewPlatform back a bit so the
// objects in the scene can be viewed.
univ.getViewingPlatform().setNominalViewingTransform();
// Ensure at least 5 msec per frame (i.e., < 200Hz)
univ.getViewer().getView().setMinimumFrameCycleTime(5);
return c;
}
/**
* Creates new form HelloUniverse
*/
public HelloUniverse() {
// Initialize the GUI components
initComponents();
// Create Canvas3D and SimpleUniverse; add canvas to drawing panel
Canvas3D c = createUniverse();
drawingPanel.add(c, BorderLayout.CENTER);
// Create the content branch and add it to the universe
scene = createSceneGraph();
univ.addBranchGraph(scene);
}
// ----------------------------------------------------------------
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
// <editor-fold defaultstate="collapsed" desc=" Generated Code
// ">//GEN-BEGIN:initComponents
private void initComponents() {
drawingPanel = new JPanel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("HelloUniverse");
drawingPanel.setLayout(new java.awt.BorderLayout());
drawingPanel.setPreferredSize(new java.awt.Dimension(250, 250));
getContentPane().add(drawingPanel, java.awt.BorderLayout.CENTER);
pack();
}// </editor-fold>//GEN-END:initComponents
/**
* @param args
* the command line arguments
*/
public static void main(String args[]) {
EventQueue.invokeLater(new Runnable() {
public void run() {
new HelloUniverse().setVisible(true);
}
});
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JPanel drawingPanel;
// End of variables declaration//GEN-END:variables
}
另外参考文献可以看这个https://blog.csdn.net/gaoyunpeng/article/details/1771423
https://www.oracle.com/java/technologies/javase/java-3d.html
最后,您所需要的一些jar包和一些demo可以参考:https://download.csdn.net/download/Alearn_/14945316