Java3Dの文法

 

教科書 Java3Dグラフィクス入門,田中成典編集,森北出版株式会社


●座標系

Java3D では右手座標系が用いられている. 右手座標系とは,画面に向かって,右方向が X軸の+方向,上方向がY軸の+方向,画面の手前方向がZ軸の+方向,となる座標系である.

また,Java3Dで用いる座標系には「ワールド座標系」と「ローカル座標系」の2種類がある.ワールド座標系とは,表示エリア全体を表す座標系であり,これまでに用いてきた座標系はこれにあたる.ローカル座標系とは,各物体ごとが持つ座標系で,一般には物体の中心が原点となる.


●単位

Java3Dでは1.0が1メートルを表す。


●直方体の表示

new Box(
  直方体の横幅の半分の長さ【float型】,
  直方体の高さの半分の長さ【float型】,
  直方体の奥行きの半分の長さ【float型】,
  法線ベクトルの定義【int型】,
  直方体の外観【Appearance型】
 )

【例】横1.0,高さ0.8,奥行き0.6の直方体のインスタンスを生成する
  Box box = new Box( 0.5f, 0.4f, 0.3f, Box.GENERATE_NORMALS, appearance );


●円錐の表示

new Cone(
  円錐の底面の半径【float型】,
  円錐の高さ【float型】,
  法線ベクトルの定義【int型】,
  円錐の外観【Appearance型】
 )

【例】底面の半径0.3,高さ0.6の円錐のインスタンスを生成する
  Cone cone = new Cone( 0.3f, 0.6f, Cone.GENERATE_NORMALS, appearance );


●円柱の表示

new Cylinder(
  円柱の底面の半径【float型】,
  円柱の高さ【float型】,
  法線ベクトルの定義【int型】,
  円柱の外観【Appearance型】
 )

【例】底面の半径0.3,高さ0.6の円柱のインスタンスを生成する
  Cylinder cylinder = new Cylinder( 0.3f, 0.6f, Cylinder.GENERATE_NORMALS, appearance );


●球体の表示

new Sphere(
  球体の半径【float型】,
  法線ベクトルの定義【int型】,
  球体の外観【Appearance型】
 )

【例】半径0.3の球体のインスタンスを生成する
  Sphere sphere = new Sphere( 0.3f, Sphere.GENERATE_NORMALS, appearance );


●Vector3d

座標や倍率を設定する

new = Vector3d(

  x軸に関する情報【double型】,
  y軸に関する情報【double型】,
  z軸に関する情報【double型】

)


●ローカル座標の設定(Vector3dとTransform3Dを組み合わせる)

【例】ローカル座標を(0.1,0.2,0.3)に設定する
  Transform3D transform3D = new Transform3D( );
  Vector3d vector3d = new Vector3d( 0.1, 0.2, 0.3 );
  transform3D.setTranslation( vector3d );


●回転

Transform3Dクラスのインスタンス名.rotX(ラジアンで表した回転角度【double型】)

それぞれの軸周りの回転は次のようになる
 X軸回転  .rotX
 Y軸回転  .rotY
 Z軸回転  .rotZ

【例】X軸を中心に45度回転させる
  transform3D.rotX( Math.PI/4.0 );


●度をラジアンに変換する

 n (ラジアン) = π×θ (度) /180

ラジアン
45度
π/ 4
90度
π/ 2
135度
3π/ 4
180度
π
225度
5π/ 4
270度
3π/ 2
315度
7π/ 4
360度


●大きさの設定

Transform3Dクラスのインスタンス名.setScale(拡大・縮小率【Vector3d型】)

【例】高さを2倍にする
  Transform3D transform3D = new Transform3D( );
  Vector3d vector3d = new Vector3d( 1.0, 2.0, 1.0 );
  transform3D.setScale( vector3d );


●座標変換の合成

連続して2つの座標変換を行うと,後の変換だけしか有効になりません.変換操作を重ねて行いたければ,Transform3D クラスの mul メソッドを使用します.

Transform3Dクラスのインスタンス名.mul(合成する座標変換【Transform3D型】)

 

【注】考え方
 t1.mul( t2 )  →  t1 の変換結果に t2 の変換を施した結果を t1 とする

 

【例】X軸を中心に45度回転させた後,Y軸を中心に再度45度回転させる.

  Transform3D transform3D1 = new Transform3D( );
  transform3D1.rotX( Math.PI/4.0 );            // X軸回転
  Transform3D transform3D2 = new Transform3D( );
  transform3D2.rotY( 0.0*Math.PI/4.0 );          // Y軸回転
  transform3D1.mul( transform3D2 );           // 2軸の回転を合成


● 色の設定

new Color3f(
  赤色の濃度【float型】,
  緑色の濃度【float型】,
  青色の濃度【float型】
 )

それぞれの色の濃度は0.0から1.0です.

【例】赤色に設定する

  Color3f color3f = new Color3f( 1.0f , 1.0f , 1.0f );


●方向を設定する(原点と座標値を結ぶ方向を表す). Vector3f

new = Vector3f(
  x軸に関する情報【float型】,
  y軸に関する情報【float型】,
  z軸に関する情報【float型】
)

【例】原点と座標(1.0 , 1.0 , 1.0)を結ぶ方向を設定する.

  Vector3f vector3f = new Vector3f( 1.0f , 1.0f , 1.0f );


●位置の設定を行う Point3f

new = Point3f(
  x軸に関する情報【float型】,
  y軸に関する情報【float型】,
  z軸に関する情報【float型】
)

【例】座標(3.0 , 2.0 , 1.0)を指定する.

  Point3f point3f = new Point3f( 3.0f , 2.0f , 1.0f );


●描画範囲の設定 BoundingSphere

new BoundingSphere(
  球の中心【Point3f型】,
  球の半径【float型】
)

※ 引数を省略した場合は,球の中心が(0.0 , 0.0 , 0.0),半径が100.0に設定される.

【例】座標(40.0 , 25.0 , 30.0)を中心とした半径35.0の球体内部を描画範囲として設定する.

  Point3f poin3f = new Point3f( 40.0f , 25.0f , 30.0f );
  BoundingSphere boundingSphere = new BoundingSphere( point3f, 35.0f );


●光源の設定 Light

Lightクラスのインスタンス名.setInfluencingBounds(
  描画範囲【BoundingShere型】
)

 光源の種類
クラス名
機能

DirectionalLight
AmbientLight
PointLight
SpotLight

並行光源を生成(p124)
環境光源を生成(p130)
点光源を生成(p135)
スポット光源を生成(p141)

※並行光源とは,空間全体を並行に進む光
※環境光源とは,空間全体を一定の明るさで照らす光
※点光源とは,1点から広がる光
※スポット光源とは,1点から円錐状に広がって空間を照らす光

【例】並行光源を設定する
(この場合は引数を省略しているので,(0.0 , 0.0 , 0.0)を中心に,半径100.0の領域が描画範囲となる)

  BoundingSphere boundingSphere = new BoundingSphere( );
     |
  光源の設定
     |
  directionalLight.setInfluencingBounds( boundingSphere );


●並行光源の設定 DirectionalLight

new DirectionalLight(
  光の色【Color3f型】,
  光の向き【Vector3f型】
)

【例】(3.5 , -2.0 , -1.5)の方向に進む青色の光を生成する.

  Color3f color3f = new Color3f( 0.0f , 0.0f , 1.0f );
  Vector3f vector3f = new Vector3f( 3.5f, -2.0f, -1.5f );
  DirectionalLight directionalLight = new DirectionalLight( color3f, vector3f );


●環境光源の設定 AmbientLight

環境光源とは,空間全体を一定の明るさで照らす光のことをいう.

new AmbientLight(
  光の色【Color3f型】
)

【例】空間を一様に紫色で照らす.

  Color3f color3f = new Color3f( 1.0f , 0.0f , 1.0f );
  AmbientLight ambientLight = new AmbientLight( color3f );


●点光源の設定 PointLight

点光源とは,電球のように1点から広がる光のことをいう.

new PointLight(
  光の色【Color3f型】,
  光源の位置【Point3f型】,
  光源の減衰度【Point3f型】
)

※減衰度とは,距離に応じて光が弱まる程度のことをいう.減衰度が大きい程光の強弱が大きくなる.

【例】( -1.0, 1.0, 2.0 )を光源として,緑色の点光源を設定する.

  Color3f color3f = new Color3f( 0.0f , 1.0f , 0.0f );
  Point3f point3f1 = new Point3f( -1.0f, 1.0f, 2.0f );
  Point3f point3f2 = new Point3f( 0.5f, 0.5f, 0.0f );
  PointLight pointLight = new PointLight( color3f , point3f1 , point3f2 );


●スポット光源の設定 SpotLight

スポット光源とは,車のヘッドライトのように1点から円錐状に広がって空間を照らす光のことをいう.

new SpotLight(
  光の色【Color3f型】,
  光源の位置【Point3f型】,
  光源の減衰度【Point3f型】,
  光の方向【Vector3f型】,
  放射角【double型】,
  輝度【float型】
)

※放射角 :ライトの照らす円錐状の範囲の角度をラジアンで指定する.0.0〜PI/2.0の間.
※輝度 :物体のハイライト部分に影響を与える設定を行う.0.0f〜128.0fの範囲で指定し,
       数値が大きいほどハイライト部分と陰の部分の違いが曖昧になる.

【例】( -1.0, 1.0, 2.0 )を光源として,幅が60度の赤色のスポットライトを設定する.

  Color3f color3f = new Color3f( 1.0f , 0.0f , 0.0f );
  Point3f point3f1 = new Point3f( 0.0f, 3.0f, 0.0f );
  Point3f point3f2 = new Point3f( 0.5f, 0.5f, 0.0f );
  Vector3f vector3f = new Vector3f( 0.0f, -1.0f, 0.0f );
  SpotLight spotLight = new SpotLight(
    color3f , point3f1 , point3f2 , Vector3f , (float)( Math.PI/12.0 , 20.0f ));


●物体の色の設定方法

Appearanceクラスのインスタンスを作成した後,色の定義を行い,Appearanceの属性として色を設定する

【例】表面の色を赤色に設定する

Appearance appearance = new Appearance( );
Material material = new Material( );
Color3f color3f = new Color3f( 1.0f, 0.0f, 0.0f );
material.setDiffuseColor( color3f );
appearance.setMaterial( material );

 

●Appearance

外観の設定を物体に反映させるクラス.

物体の外観を設定するにはMaterialクラスを,物体の透明度を設定するにはTransparencyAttributesクラスを用いる.

●Appearanceの書式

new Appearance( )

【使用例】

Appearance appearance = new Appearance( )

●Materialクラス

Materialクラスは物体表面の色や輝度を設定するためのクラス.

色を設定するにはsetDiffuseColor,輝度を設定するにはsetShininessを使う.

●Materialクラスの書式

new Material( )

【使用例】

Material material = new Material( )

●setMaterialの書式

Appearance クラスのインスタンス名.setMaterial(

  物体の色に関する情報【Material型】

)

●物体の色の設定 setDiffuseColor

Materialクラスのインスタンス名.setDiffuseColor(

  物体の色【Color3f型】

)


●物体の輝度の設定方法

Appearanceクラスのインスタンスを作成した後,輝度の定義を行い,Appearanceの属性として輝度を設定する

【例】表面の輝度を10.0に設定する

Appearance appearance = new Appearance( );
Material material = new Material( );
material.setShininess( 10.0f );
appearance.setMaterial( material );

●物体の輝度の設定 setShininess

Materialクラスのインスタンス名. setShininess (

  物体の輝度【float型】

)

物体の輝度はfloat型で指定する.範囲は1.0から128.0で,値を小さくするほど輝度は小さくなる.


●物体の透明度の設定方法

Appearanceクラスのインスタンスを作成した後,輝度の透明度を定義し,Appearanceの属性として透明度を設定する

【例】物体の透明度を0.5に設定する

Appearance appearance = new Appearance( );

Material material = new Material( );

appearance.setMaterial( material );

TransparencyAttributes transparencyAttributes =

new TransparencyAttributes( TransparencyAttributes.FASTEST, 0.5f );

appearance.setTransparencyAttributes( transparencyAttributes );

 

 

●物体の透明度の設定 TransparencyAttributes

new TransparencyAttributes(

  TransparencyAttributes.物体の透明の描画モード【int型】,

  物体の透明度【float型】

)

・透明の描画モードとしては次の5種類が使える

透明の描画モード
機能
FASTEST
NICEST
BLENDED
SCREEN_DOOR
NONE
できる限り高速に描画
できる限り高品質に描画
アルファブレンドで描画
格子状に透過を描画
不透明に描画

・物体の透明度

 透明度をfloat型で指定する.範囲は0.0から1.0である.数字が大きいほど透明度が高くなる.0.0の場合は完全な不透明.1.0の場合は完全な透明になる.