我的问题是这之间的区别:
还有这个:
我正在尝试使用 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;
}
}
最佳答案
有了这个:
texAttr.setTextureMode(TextureAttributes.REPLACE);
你是说用纹理值替换计算的光照值。您想要使用 MODULATE
或 BLEND
,具体取决于您希望它看起来如何。检查this看看不同方法的作用。
关于java - Java3D 中的光和纹理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22517973/