java3d lookat_Java3d透明效果实现

其实只要细细留心Java3d API就可以发现透明属性的API,最近需要对一个模型做透明效果,对于实现做下描述。

原理就是得到模型的shape3d的Appearance,将Appearance的TransparencyAttributes进行设置即可,在使用Alpha可以实现透明和不透明效果之间的切换。什么都不说了上代码和截图。

运行前

0818b9ca8b590ca3270a3433284dd417.png

双击后

0818b9ca8b590ca3270a3433284dd417.png

代码

package ta;

import java.awt.BorderLayout;

import java.awt.GraphicsConfiguration;

import java.awt.event.MouseAdapter;

import java.awt.event.MouseEvent;

import java.util.Enumeration;

import javax.media.j3d.Alpha;

import javax.media.j3d.AmbientLight;

import javax.media.j3d.Appearance;

import javax.media.j3d.Background;

import javax.media.j3d.BoundingSphere;

import javax.media.j3d.BranchGroup;

import javax.media.j3d.Canvas3D;

import javax.media.j3d.DirectionalLight;

import javax.media.j3d.Group;

import javax.media.j3d.Leaf;

import javax.media.j3d.Node;

import javax.media.j3d.Shape3D;

import javax.media.j3d.Transform3D;

import javax.media.j3d.TransformGroup;

import javax.media.j3d.TransparencyAttributes;

import javax.media.j3d.TransparencyInterpolator;

import javax.swing.JApplet;

import javax.vecmath.Color3f;

import javax.vecmath.Point3d;

import javax.vecmath.Vector3d;

import javax.vecmath.Vector3f;

import com.sun.j3d.utils.applet.MainFrame;

import com.sun.j3d.utils.behaviors.vp.OrbitBehavior;

import com.sun.j3d.utils.geometry.Primitive;

import com.sun.j3d.utils.geometry.Sphere;

import com.sun.j3d.utils.universe.SimpleUniverse;

import com.sun.j3d.utils.universe.ViewingPlatform;

public class TransparencyTest extends JApplet {

/**

*

*/

private static final long serialVersionUID = 4179606627626695905L;

/**

* 场景

*/

private SimpleUniverse universe;

/**

* 画布

*/

private Canvas3D canvas3d;

/**

* 场景大小

*/

private BoundingSphere bounds;

/**

* 根节点

*/

private BranchGroup objRoot;

/**

* 透明属性

*/

private TransparencyAttributes ta;

/**

* 透明的alpha

*/

private Alpha transparencyAlpha;

/**

* 透明效果的Interpolator

*/

private TransparencyInterpolator transparencyInterpolator;

/**

* 展示状态

*/

private static boolean SHOWSTATUS = false;

/**

* 操作behavior

*/

private OrbitBehavior orbit;

/**

* 初始化

*/

public void init() {

setSize(800, 600);

setLayout(new BorderLayout());

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

GraphicsConfiguration gc = SimpleUniverse.getPreferredConfiguration();

canvas3d = new Canvas3D(gc);

add(canvas3d, BorderLayout.CENTER);

universe = new SimpleUniverse(canvas3d);

canvas3d.addMouseListener(new MouseAdapter() {

public void mouseClicked(MouseEvent e) {

if(e.getButton() == MouseEvent.BUTTON1){

if(e.getClickCount() == 2){

changeTransparency();

}

}

}

});

objRoot = new BranchGroup();

objRoot.setCapability(BranchGroup.ALLOW_DETACH);

objRoot.setCapability(BranchGroup.ALLOW_CHILDREN_EXTEND);

objRoot.setCapability(BranchGroup.ALLOW_CHILDREN_WRITE);

objRoot.setCapability(BranchGroup.ALLOW_CHILDREN_READ);

setupView();

addBackground();

lightScene();

addBox();

initializeAlpha();

universe.addBranchGraph(objRoot);

universe.getViewer().getView().setBackClipDistance(40.f);

}

/**

* 添加box

*/

private void addBox() {

Sphere sphere = new Sphere(1.0f, new Appearance());

initializeTransparency(sphere);

TransformGroup objTransG = new TransformGroup();

objTransG.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);

objTransG.addChild(sphere);

//调整视野

resizeView(sphere);

objRoot.addChild(objTransG);

}

/**

* 调整视野

*/

private void resizeView(Primitive primitive) {

BoundingSphere bounds = (BoundingSphere) primitive.getBounds();

Point3d center = new Point3d();

bounds.getCenter(center);

System.out.println(center);

ViewingPlatform viewingPlatform = universe.getViewingPlatform();

TransformGroup viewGroup = viewingPlatform.getViewPlatformTransform();

Transform3D t3d = new Transform3D();

viewGroup.getTransform(t3d);

t3d.lookAt(new Point3d(0, 0, -5), center, new Vector3d(0, 1, 0));

t3d.invert();

viewGroup.setTransform(t3d);

orbit.setRotationCenter(center);

}

/**

* 初始化视野

*/

public void setupView() {

orbit = new OrbitBehavior(canvas3d);

orbit.setSchedulingBounds(bounds);

ViewingPlatform viewingPlatform = universe.getViewingPlatform();

viewingPlatform.setNominalViewingTransform();

viewingPlatform.setViewPlatformBehavior(orbit);

}

/**

* 初始化alpha信息

*/

private void initializeAlpha() {

transparencyAlpha = new Alpha(1, 0);

transparencyAlpha.setMode(Alpha.DECREASING_ENABLE);

transparencyAlpha.setDecreasingAlphaDuration(0);

transparencyInterpolator = new TransparencyInterpolator(transparencyAlpha, getTransparencyAttributes());

BoundingSphere bounds = new BoundingSphere(new Point3d(0, 0, 0), 1000);

transparencyInterpolator.setSchedulingBounds(bounds);

objRoot.addChild(transparencyInterpolator);

}

/**

* 初始化Transparency信息

* @param bgFile

*/

@SuppressWarnings("rawtypes")

private void initializeTransparency(Node aNode){

// recursively assign transparency attributes to the tree

if(aNode instanceof Group){

Group g = (Group) aNode;

Enumeration children = g.getAllChildren();

while(children.hasMoreElements()){

Node aChild = (Node) children.nextElement();

initializeTransparency(aChild);

}

}

else if(aNode instanceof Leaf){

if(aNode instanceof Shape3D){

Shape3D shape3d = (Shape3D) aNode;

Appearance app = shape3d.getAppearance();

if(app != null){

app.setTransparencyAttributes(getTransparencyAttributes());

}

}

}

}

/**

* 改变透明状态

*/

private void changeTransparency(){

if(!SHOWSTATUS){

transparencyAlpha.setMode(Alpha.INCREASING_ENABLE);

transparencyAlpha.setStartTime(System.currentTimeMillis());

transparencyAlpha.resume();

SHOWSTATUS = true;

}else{

transparencyAlpha.setMode(Alpha.DECREASING_ENABLE);

transparencyAlpha.setStartTime(System.currentTimeMillis());

transparencyAlpha.resume();

SHOWSTATUS = false;

}

}

/**

* 获得透明属性

* @return

*/

private TransparencyAttributes getTransparencyAttributes(){

if(ta == null){

ta = new TransparencyAttributes();

ta.setTransparencyMode(TransparencyAttributes.SCREEN_DOOR);

ta.setTransparency(1.0f);

ta.setCapability(TransparencyAttributes.ALLOW_VALUE_READ);

ta.setCapability(TransparencyAttributes.ALLOW_VALUE_WRITE);

}

return ta;

}

/**

* 添加光照

*/

private void lightScene() {

Color3f white = new Color3f(1.0f, 1.0f, 1.0f);

AmbientLight ambientLight = new AmbientLight(white);

ambientLight.setCapability(AmbientLight.ALLOW_COLOR_WRITE);

ambientLight.setInfluencingBounds(bounds);

objRoot.addChild(ambientLight);

Vector3f light1Direction = new Vector3f(-1.0f, -1.0f, -1.0f);

DirectionalLight dirLight1 = new DirectionalLight(white, light1Direction);

dirLight1.setCapability(DirectionalLight.ALLOW_COLOR_WRITE);

dirLight1.setInfluencingBounds(bounds);

objRoot.addChild(dirLight1);

}

/**

* 设置场景的背景

*/

private void addBackground() {

Background back = new Background();

back.setApplicationBounds(bounds);

back.setColor(0.17f, 0.65f, 0.92f); // sky colour

objRoot.addChild(back);

}

public static void main(String[] args) {

new MainFrame(new TransparencyTest(), 256, 256);

}

} 可是很用心写的哦,希望对你有帮助!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值