简体   繁体   English

Java3D中的光和纹理

[英]Light and textures in Java3D

My problem is the difference between this: 我的问题是这个区别:

稀土

and this: 还有这个:

球

I'm trying to create a nice looking solar system with Java3D but when I apply a texture the lighting effect disappears and the 3D effect (when not looking at the planet top-down) goes with it. 我正在尝试使用Java3D创建一个漂亮的太阳系,但是当我应用纹理时,光照效果消失,3D效果(当不是从上到下看行星时)也随之而来。 How can I have this kind of shading on textured surfaces? 如何在纹理表面上进行这种着色?

The code I used for the earth example is available below. 我用于地球示例的代码如下所示。 The texture is downloadable 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;
    }

}

With this: 有了这个:

   texAttr.setTextureMode(TextureAttributes.REPLACE);

you are saying to replace computed lighting value with value from texture. 你是说用纹理中的值替换计算的光照值。 You want to use MODULATE or BLEND , depending on how exactly you want it to look. 您希望使用MODULATEBLEND ,具体取决于您希望它的外观。 Check this to see what the different methods do. 检查这个以查看不同方法的作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM