Java3D中的光和纹理

时间:2014-03-19 20:59:58

标签: java java-3d

我的问题是:

之间的区别

earths

和此:

spheres

我正在尝试使用Java3D创建一个漂亮的太阳系,但是当我应用纹理时,光照效果消失,3D效果(当不是从上到下看行星时)也随之而来。如何在纹理表面上进行这种着色?

我在下面提供了用于地球示例的代码。纹理可下载here

import com.sun.j3d.utils.geometry.Primitive;
import com.sun.j3d.utils.geometry.Sphere;
import com.sun.j3d.utils.image.TextureLoader;
import com.sun.j3d.utils.universe.SimpleUniverse;

import javax.imageio.ImageIO;
import javax.media.j3d.*;
import javax.vecmath.Color3f;
import javax.vecmath.Point3d;
import javax.vecmath.Vector3d;

import java.awt.*;

import static java.lang.Math.PI;
import static java.lang.Math.cos;
import static java.lang.Math.sin;

public class Hello3d {

    public Hello3d()

    {

        SimpleUniverse universe = new SimpleUniverse();

        BranchGroup group = new BranchGroup();

        for(double i=0; i<2*PI; i+=PI/5) {
            group.addChild(basicSphere(0.8*cos(i),0.8*sin(i),0));
        }

        universe.getViewingPlatform().setNominalViewingTransform();

        BoundingSphere bounds = new BoundingSphere(new Point3d(0.0,0.0,0.0), 10000000.0);

        PointLight light = new PointLight();
        light.setColor(new Color3f(Color.WHITE));
        light.setPosition(0.0f,0.0f,0.0f);
        light.setInfluencingBounds(bounds);
        group.addChild(light);


        universe.addBranchGraph(group);

    }

    public static void main( String[] args ) {

        new Hello3d();

    }

    private TransformGroup basicSphere(double x, double y, double z) {
        try {
        int primflags = Primitive.GENERATE_NORMALS + Primitive.GENERATE_TEXTURE_COORDS;

        Texture tex = new TextureLoader(
                ImageIO.read(getClass().getResource("earthmap.jpg"))
        ).getTexture();
        tex.setBoundaryModeS(Texture.WRAP);
        tex.setBoundaryModeT(Texture.WRAP);

        TextureAttributes texAttr = new TextureAttributes();
        texAttr.setTextureMode(TextureAttributes.REPLACE);

        Appearance ap = new Appearance();
        ap.setTexture(tex);
        ap.setTextureAttributes(texAttr);

        Sphere sphere = new Sphere(0.1f, primflags, 100, ap);

        Transform3D transform = new Transform3D();
        transform.setTranslation(new Vector3d(x, y, z));

        TransformGroup transformGroup = new TransformGroup();
        transformGroup.setTransform(transform);

        transformGroup.addChild(sphere);

        return transformGroup;
        } catch(Exception e) { e.printStackTrace(); }
        return null;
    }

}

1 个答案:

答案 0 :(得分:2)

有了这个:

   texAttr.setTextureMode(TextureAttributes.REPLACE);

你要说的是用纹理中的值替换计算的光照值。您希望使用MODULATEBLEND,具体取决于您希望它的外观。检查this以查看不同方法的作用。