放射光色の設定

広告

最後は放射光色です。今までは外部からの光に対する色の設定でしたが、放射光色とは物体が自ら発光する場合の色です。この光は物体の色となりますが、他の物体を照らすような光ではありませんので、光源ではありません。

放射光色の設定をするには"Material"クラスで用意されている"setEmissiveColor"メソッドを使います。

setEmissiveColor
public void setEmissiveColor(float r, float g, float b)
Sets this material's emissive color. This is the color of light, if 
any, that the material emits. The emissive color in this Material 
object may be overridden by per-vertex colors in some cases. If 
vertex colors are present in the geometry, and lighting is enabled,
and the colorTarget is EMISSIVE, and vertex colors are not being 
ignored, then the vertex colors are used in place of this Material's
emissive color in the lighting equation. 

Parameters:
  r - the new emissive color's red component
  g - the new emissive color's green component
  b - the new emissive color's blue component 
Throws: 
  CapabilityNotSetException - if appropriate capability is not set and
    this object is part of live or compiled scene graph

色を赤緑青の3つの組み合わせで指定します。それぞれの値は0.0fから1.0fの間の値で指定して下さい。

実際に記述する場合は下記のように記述します。

Material ma = new Material();
ma.setEmissiveColor(0.0f, 0.0f, 1.0f);

Appearance ap = new Appearance();
ap.setMaterial(ma);

サンプルプログラム

では試してみます。

Sample3D21.java

import javax.swing.*;
import java.awt.event.*;
import java.awt.BorderLayout;
import java.awt.GraphicsConfiguration;
import com.sun.j3d.utils.universe.*;
import javax.media.j3d.*;
import com.sun.j3d.utils.geometry.Sphere;
import javax.vecmath.*;

public class Sample3D21 extends JFrame {
  public BranchGroup createSceneGraph() {
    BranchGroup objRoot = new BranchGroup();

    Appearance ap = new Appearance();
    Material ma = new Material();

    ma.setEmissiveColor(1.0f, 0.0f, 0.0f);

    ap.setMaterial(ma);

    Sphere sphere = new Sphere(0.7f, Sphere.GENERATE_NORMALS, 50, ap);

    objRoot.addChild(sphere);
    objRoot.compile();

    return objRoot;
  }

  public Sample3D21() {
    getContentPane().setLayout(new BorderLayout());

    GraphicsConfiguration config = SimpleUniverse.getPreferredConfiguration();

    Canvas3D canvas = new Canvas3D(config);
    getContentPane().add(canvas, BorderLayout.CENTER);

    BranchGroup scene = createSceneGraph();
    SimpleUniverse universe = new SimpleUniverse(canvas);

    universe.getViewingPlatform().setNominalViewingTransform();
    universe.addBranchGraph(scene);
  }

  public static void main(String[] args) {
    Sample3D21 sample = new Sample3D21();

    sample.setBounds( 10, 10, 240, 240);
    sample.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    sample.setVisible(true);
  }
}

実行結果は下記のようになります。

Java3Dで放射光を設定した場合

( Written by Tatsuo Ikura+ )

Facebook Page