java 纹理_Java3D中的光和纹理

在尝试使用Java3D创建一个太阳系模型时,遇到一个问题:当应用纹理后,光照效果消失,导致3D效果变得不明显。代码示例展示了如何创建带有纹理的地球,但未能同时保持光照和3D视觉效果。问题在于如何在保留纹理的同时实现表面的正确着色和光照效果。
摘要由CSDN通过智能技术生成

我的问题是这个区别:

还有这个:

我正在尝试使用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;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值