//★ Java3D で Sphere(球)を描画する import java.awt.*; import javax.swing.*; import javax.media.j3d.*; import com.sun.j3d.utils.universe.*; import com.sun.j3d.utils.geometry.*; import javax.vecmath.*; public class View_Sphere extends JFrame { // main Method public static void main(String[] args) { new View_Sphere(); } // Constructor public View_Sphere() { // JFrame の初期化 super("View_Sphere test"); setSize(250,250); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Java3D 関係の設定 GraphicsConfiguration config = SimpleUniverse.getPreferredConfiguration(); Canvas3D canvas = new Canvas3D(config); add(canvas); // SimpleUniverseを生成 SimpleUniverse universe = new SimpleUniverse(canvas); universe.getViewingPlatform().setNominalViewingTransform(); // Scene を生成 universe.addBranchGraph(CreateScene()); setVisible(true); } // Scene の生成 public BranchGroup CreateScene() { BranchGroup objRoot = new BranchGroup(); BoundingSphere bounds = new BoundingSphere(new Point3d(),100.0); // Light の設定 DirectionalLight dlight = new DirectionalLight(true, new Color3f(1.0f,1.0f,1.0f), new Vector3f(0.3f,-0.3f,-0.3f)); dlight.setInfluencingBounds(bounds); objRoot.addChild(dlight); // 環境光 AmbientLight alight = new AmbientLight(); alight.setInfluencingBounds(bounds); objRoot.addChild(alight); // 球を生成 //objRoot.addChild(new Sphere(0.7f,createAppearance())); objRoot.addChild(new Sphere(0.7f,Sphere.GENERATE_NORMALS,50,createAppearance())); return objRoot; } // Material の設定 public Appearance createAppearance() { Appearance app = new Appearance(); Material mat = new Material(); // デフォルト値 mat.setLightingEnable(true); // 照明が有効 mat.setAmbientColor(0.2f, 0.2f, 0.2f); // 環境光による色は 濃いグレー (AmbientLight を設定する) mat.setEmissiveColor(0.0f, 0.0f, 0.0f); // 発光による色は 黒(発光しない) mat.setDiffuseColor(1.0f, 1.0f, 1.0f); // 拡散反射による色は 白 mat.setSpecularColor(1.0f, 1.0f, 1.0f); // 鏡面反射による色は 白 mat.setShininess(64.0f); // 輝度は 64(中間値) app.setMaterial(mat); return app; } } |
// Scene の生成 public BranchGroup CreateScene() { BranchGroup objRoot = new BranchGroup(); BoundingSphere bounds = new BoundingSphere(new Point3d(),100.0); // Light の設定 DirectionalLight dlight = new DirectionalLight(true, new Color3f(1.0f,1.0f,1.0f), new Vector3f(0.3f,-0.3f,-0.3f)); dlight.setInfluencingBounds(bounds); objRoot.addChild(dlight); // 環境光 AmbientLight alight = new AmbientLight(); alight.setInfluencingBounds(bounds); objRoot.addChild(alight); // 球を生成 //objRoot.addChild(new Sphere(0.7f,createAppearance())); objRoot.addChild(new Sphere(0.7f,Sphere.GENERATE_NORMALS,50,createAppearance())); return objRoot; } |
// Material の設定 public Appearance createAppearance() { Appearance app = new Appearance(); Material mat = new Material(); // デフォルト値 mat.setLightingEnable(true); // 照明が有効 mat.setAmbientColor(0.2f, 0.2f, 0.2f); // 環境光による色は 濃いグレー (AmbientLight を設定すること) mat.setEmissiveColor(0.0f, 0.0f, 0.0f); // 発光による色は 黒(発光しない) mat.setDiffuseColor(1.0f, 1.0f, 1.0f); // 拡散反射による色は 白 mat.setSpecularColor(1.0f, 1.0f, 1.0f); // 鏡面反射による色は 白 mat.setShininess(64.0f); // 輝度は 64(中間値) app.setMaterial(mat); return app; } |
mat.setDiffuseColor(0.0f, 0.0f, 0.8f); |
mat.setAmbientColor(0.2f, 0.0f, 0.0f); |
mat.setSpecularColor(0.0f, 1.0f, 0.0f); mat.setShininess(12.0f); |
new DirectionalLight(true, new Color3f(1.0f,1.0f,1.0f), new Vector3f(0.3f,-0.3f,-0.3f)); |
new DirectionalLight(true, new Color3f(1.0f,0.0f,0.0f), new Vector3f(0.0f, 0.0f, -1.0f)); |
public BranchGroup CreateScene() { BranchGroup objRoot = new BranchGroup(); // Light の設定 BoundingSphere bounds = new BoundingSphere(new Point3d(),100.0); DirectionalLight dlight = new DirectionalLight(true, new Color3f(0.0f,0.0f,1.0f), new Vector3f(0.3f,-0.3f,-0.3f)); dlight.setInfluencingBounds(bounds); objRoot.addChild(dlight); // 環境光 AmbientLight alight = new AmbientLight(); alight.setInfluencingBounds(bounds); objRoot.addChild(alight); objRoot.addChild(new Sphere(0.7f,Sphere.GENERATE_NORMALS,50,createAppearance())); return objRoot; } // Material の設定 public Appearance createAppearance() { Appearance app = new Appearance(); Material mat = new Material(); mat.setAmbientColor(0.2f, 0.2f, 0.2f); mat.setShininess(100.0f); app.setMaterial(mat); return app; } |
// スポットライト SpotLight light = new SpotLight( new Color3f(1.0f, 1.0f, 0.0f), // 光源の色 new Point3f(0.5f, 0.0f, 0.8f), // 光源の位置 new Point3f(1.0f, 0.0f, 0.0f), // 光の減衰係数 new Vector3f(-0.5f, -0.5f, -0.7f), // 光の方向ベクトル (float)(Math.PI / 2.0), // スポットライトの角度 0.0f ); // スポットライトの収束度 light.setInfluencingBounds(bounds); objRoot.addChild(light); |
// Scene の生成 public BranchGroup CreateScene() { BranchGroup objRoot = new BranchGroup(); // Light の設定 BoundingSphere bounds = new BoundingSphere(new Point3d(),100.0); DirectionalLight dlight = new DirectionalLight(true, new Color3f(0.0f,0.0f,1.0f), new Vector3f(0.3f,-0.3f,-0.3f)); dlight.setInfluencingBounds(bounds); objRoot.addChild(dlight); // 環境光 AmbientLight alight = new AmbientLight(); alight.setInfluencingBounds(bounds); objRoot.addChild(alight); // 点光源 PointLight light = new PointLight( new Color3f(1.0f, 1.0f, 0.0f), // 光源の色 new Point3f(0.5f, 0.5f, 0.8f), // 光源の位置 new Point3f(0.7f, 0.0f, 0.0f) ); // 光の減衰定数、係数 light.setInfluencingBounds(bounds); objRoot.addChild(light); objRoot.addChild(new Sphere(0.7f,Sphere.GENERATE_NORMALS,50,createAppearance())); return objRoot; } |
// Scene の生成 public BranchGroup CreateScene() { BranchGroup objRoot = new BranchGroup(); // Light の設定 BoundingSphere bounds = new BoundingSphere(new Point3d(),100.0); DirectionalLight dlight = new DirectionalLight(true, new Color3f(1.0f,1.0f,1.0f), new Vector3f(0.3f,-0.3f,-0.3f)); dlight.setInfluencingBounds(bounds); objRoot.addChild(dlight); // 環境光 AmbientLight alight = new AmbientLight(); alight.setInfluencingBounds(bounds); objRoot.addChild(alight); // 背景色の設定 Color3f bgColor = new Color3f(0.05f, 0.05f, 0.5f); Background bgNode = new Background(bgColor); bgNode.setApplicationBounds(bounds); objRoot.addChild(bgNode); objRoot.addChild(new Sphere(0.7f,Sphere.GENERATE_NORMALS,50,createAppearance())); return objRoot; } |
// Appearance の設定 public Appearance createAppearance() { Appearance app = new Appearance(); // Material の設定 Material mat = new Material(); app.setMaterial(mat); // 透明度の設定 TransparencyAttributes tattr = new TransparencyAttributes(TransparencyAttributes.BLENDED, 0.7f); app.setTransparencyAttributes(tattr); return app; } |