如何使用照明表明我的形状是3D?

发布于 2025-01-25 14:25:38 字数 3996 浏览 2 评论 0原文

我正在Java3D进行课程项目,并且遇到了一个问题。我需要给我的形状一种材料,以便光线可以显示阴影,从而使形状实际上看起来3D。现在,我拥有显示形状所需的一切,但是我不喜欢它具有的固体白色,我想制作它,以便光对象实际上显示出对形状的反射。我该怎么做?我将在底部添加当前对象的图片。谢谢!

public static void main(String[] args) {
    System.setProperty("sun.awt.noerasebackground", "true");
    new MainFrame(new Assignment7(), 640, 480);
}

public void init() {
    // create canvas
    GraphicsConfiguration gc = SimpleUniverse.getPreferredConfiguration();
    Canvas3D cv = new Canvas3D(gc);
    setLayout(new BorderLayout());
    add(cv, BorderLayout.CENTER);
    BranchGroup bg = createBranchGraph();
    bg.compile();
    SimpleUniverse su = new SimpleUniverse(cv);
    su.getViewingPlatform().setNominalViewingTransform();
    TransformGroup tg = su.getViewingPlatform().getMultiTransformGroup().getTransformGroup(0);
    Transform3D tx = new Transform3D();
    tx.lookAt(new Point3d(0,.5,2), new Point3d(0,0,0), new Vector3d(0,1,0));
    tx.invert();
    tg.setTransform(tx);
    
    View view = su.getViewer().getView();
    //view.setProjectionPolicy(View.PARALLEL_PROJECTION);
    view.setFieldOfView(0.4*Math.PI);
    
    su.addBranchGraph(bg);
}

private BranchGroup createBranchGraph() {
    BranchGroup root = new BranchGroup();
    TransformGroup spin = new TransformGroup();
    spin.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
    root.addChild(spin);
    //object
    Appearance ap = new Appearance();
    PolygonAttributes pa = new PolygonAttributes();
    //pa.setPolygonMode(PolygonAttributes.POLYGON_LINE);
    pa.setCullFace(PolygonAttributes.CULL_NONE);
    Shape3D shape = new Shape3D(new Octahedron(), ap);
    ap.setMaterial(new Material());
    ap.setPolygonAttributes(new PolygonAttributes(PolygonAttributes.POLYGON_FILL, PolygonAttributes.CULL_NONE, 0));
    Material mat = new Material();
    mat.setLightingEnable(true);
    mat.setShininess(30);
    ap.setMaterial(mat);
    shape.setAppearance(ap);
    Shape3D bracket1 = new Shape3D(new bracket(), ap);
    Shape3D partition2 = new Shape3D(new partition2(), ap);
    Shape3D bracket2 = new Shape3D(new bracket2(), ap);
    Shape3D partition3 = new Shape3D(new partition3(), ap);
    Shape3D head = new Shape3D(new head(), ap);
    Shape3D claw = new Shape3D(new claw1(), ap);
    Shape3D claw2 = new Shape3D(new claw2(), ap);
    
    bracket1.setAppearance(ap);
    partition2.setAppearance(ap);
    bracket2.setAppearance(ap);
    partition3.setAppearance(ap);
    head.setAppearance(ap);
    claw.setAppearance(ap);
    claw2.setAppearance(ap);
    
    //transform
    Transform3D tr = new Transform3D();
    tr.setScale(0.25);
    tr.setTranslation(new Vector3d(0, 0, 0));
    TransformGroup tg = new TransformGroup(tr);
    spin.addChild(tg);
    tg.addChild(shape);
    tg.addChild(bracket1);
    tg.addChild(partition2);
    tg.addChild(bracket2);
    tg.addChild(partition3);
    tg.addChild(head);
    tg.addChild(claw);
    tg.addChild(claw2);

    BoundingSphere bounds = new BoundingSphere();
    
    MouseRotate rt = new MouseRotate(spin);
    rt.setSchedulingBounds(bounds);
    spin.addChild(rt);
    
    Background bg = new Background(0f, .5f, 1.0f);
    bg.setApplicationBounds(bounds);
    root.addChild(bg);
    
    //lighting
    AmbientLight light = new AmbientLight(true, new Color3f(Color.white));
    light.setColor(new Color3f(Color.black));
    light.setInfluencingBounds(bounds);
    root.addChild(light);
    
    PointLight pl = new PointLight(new Color3f(Color.white), new Point3f(3f,3f,3f), new Point3f(1f,0f,0f));
    pl.setInfluencingBounds(bounds);
    root.addChild(pl);
    
    PointLight pl2 = new PointLight(new Color3f(Color.black), new Point3f(3f,3f,3f), new Point3f(1f,0f,0f));
    pl2.setInfluencingBounds(bounds);
    root.addChild(pl2);
    
    return root;
}

这是当前形状的外观(没有照明):

”在此处输入图像描述”

I am working on my class project in java3D and I have run into an issue. I need to give my shape a material so that the light can show shadows, thus making the shape actually look 3D. Right now I have everything I need to display the shape, but I don't like the solid white color that it has, and I would like to make it so that the light object actually shows reflection on the shape. How can I go about doing this? I will add a picture of the current object at the bottom. Thanks!

public static void main(String[] args) {
    System.setProperty("sun.awt.noerasebackground", "true");
    new MainFrame(new Assignment7(), 640, 480);
}

public void init() {
    // create canvas
    GraphicsConfiguration gc = SimpleUniverse.getPreferredConfiguration();
    Canvas3D cv = new Canvas3D(gc);
    setLayout(new BorderLayout());
    add(cv, BorderLayout.CENTER);
    BranchGroup bg = createBranchGraph();
    bg.compile();
    SimpleUniverse su = new SimpleUniverse(cv);
    su.getViewingPlatform().setNominalViewingTransform();
    TransformGroup tg = su.getViewingPlatform().getMultiTransformGroup().getTransformGroup(0);
    Transform3D tx = new Transform3D();
    tx.lookAt(new Point3d(0,.5,2), new Point3d(0,0,0), new Vector3d(0,1,0));
    tx.invert();
    tg.setTransform(tx);
    
    View view = su.getViewer().getView();
    //view.setProjectionPolicy(View.PARALLEL_PROJECTION);
    view.setFieldOfView(0.4*Math.PI);
    
    su.addBranchGraph(bg);
}

private BranchGroup createBranchGraph() {
    BranchGroup root = new BranchGroup();
    TransformGroup spin = new TransformGroup();
    spin.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
    root.addChild(spin);
    //object
    Appearance ap = new Appearance();
    PolygonAttributes pa = new PolygonAttributes();
    //pa.setPolygonMode(PolygonAttributes.POLYGON_LINE);
    pa.setCullFace(PolygonAttributes.CULL_NONE);
    Shape3D shape = new Shape3D(new Octahedron(), ap);
    ap.setMaterial(new Material());
    ap.setPolygonAttributes(new PolygonAttributes(PolygonAttributes.POLYGON_FILL, PolygonAttributes.CULL_NONE, 0));
    Material mat = new Material();
    mat.setLightingEnable(true);
    mat.setShininess(30);
    ap.setMaterial(mat);
    shape.setAppearance(ap);
    Shape3D bracket1 = new Shape3D(new bracket(), ap);
    Shape3D partition2 = new Shape3D(new partition2(), ap);
    Shape3D bracket2 = new Shape3D(new bracket2(), ap);
    Shape3D partition3 = new Shape3D(new partition3(), ap);
    Shape3D head = new Shape3D(new head(), ap);
    Shape3D claw = new Shape3D(new claw1(), ap);
    Shape3D claw2 = new Shape3D(new claw2(), ap);
    
    bracket1.setAppearance(ap);
    partition2.setAppearance(ap);
    bracket2.setAppearance(ap);
    partition3.setAppearance(ap);
    head.setAppearance(ap);
    claw.setAppearance(ap);
    claw2.setAppearance(ap);
    
    //transform
    Transform3D tr = new Transform3D();
    tr.setScale(0.25);
    tr.setTranslation(new Vector3d(0, 0, 0));
    TransformGroup tg = new TransformGroup(tr);
    spin.addChild(tg);
    tg.addChild(shape);
    tg.addChild(bracket1);
    tg.addChild(partition2);
    tg.addChild(bracket2);
    tg.addChild(partition3);
    tg.addChild(head);
    tg.addChild(claw);
    tg.addChild(claw2);

    BoundingSphere bounds = new BoundingSphere();
    
    MouseRotate rt = new MouseRotate(spin);
    rt.setSchedulingBounds(bounds);
    spin.addChild(rt);
    
    Background bg = new Background(0f, .5f, 1.0f);
    bg.setApplicationBounds(bounds);
    root.addChild(bg);
    
    //lighting
    AmbientLight light = new AmbientLight(true, new Color3f(Color.white));
    light.setColor(new Color3f(Color.black));
    light.setInfluencingBounds(bounds);
    root.addChild(light);
    
    PointLight pl = new PointLight(new Color3f(Color.white), new Point3f(3f,3f,3f), new Point3f(1f,0f,0f));
    pl.setInfluencingBounds(bounds);
    root.addChild(pl);
    
    PointLight pl2 = new PointLight(new Color3f(Color.black), new Point3f(3f,3f,3f), new Point3f(1f,0f,0f));
    pl2.setInfluencingBounds(bounds);
    root.addChild(pl2);
    
    return root;
}

Here is what the current shape looks like (without lighting):

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文