运行结果如下: 内容来自论文无忧网 www.paper51.com 图10无灯光效果的场景 内容来自论文无忧网 www.paper51.com
内容来自www.paper51.com
图11加入AmbientLight效果的场景 copyright paper51.com 2.创建PointLight copyright paper51.com PointLight orangePoint = new PointLight (); http://www.paper51.com orangePoint.setEnable(orangePointOnOff); 内容来自www.paper51.com orangePoint.setColor (newColor3f (1.0f, 0.5f, 0.0f)); / / 同样为光的颜色,橙色 paper51.com orangePoint.setPosition (newPoint3f (0.0f, 0.5f, 0.0f)); / / 设置光的位置 内容来自www.paper51.com orangePoint.setCapability(AmbientLight.ALLOW_STATE_WRITE); http://www.paper51.com
orangePoint.setInfluencingBounds(worldBounds); copyright paper51.com Scene1.addChild (orangePoint); 内容来自论文无忧网 www.paper51.com 运行结果如下: 内容来自www.paper51.com
paper51.com 图12加入PointLight效果的场景 内容来自论文无忧网 www.paper51.com 3.创建一个DirectionalLight: paper51.com DirectionalLight redDirectional = newDirectionalLight ( ); 内容来自论文无忧网 www.paper51.com
redDirectional.setEnable(redDirectionalOnOff); http://www.paper51.com
redDirectional.setColor (newColor3f (1.0f, 0.0f, 0.0f)); 内容来自www.paper51.com redDirectional.setDirection(new Vector3f (1.0f, -0.5f, -0.5f)); paper51.com redDirectional.setCapability(AmbientLight.ALLOW_STATE_WRITE); 内容来自论文无忧网 www.paper51.com redDirectional.setInfluencingBounds (worldBounds); copyright paper51.com Scene1.addChild (redDirectional); http://www.paper51.com 执行结果如下: http://www.paper51.com
paper51.com 图13加入DirectionalLight效果的场景 paper51.com 4.4.2 创建背景图片 内容来自论文无忧网 www.paper51.com 这里的背景图片实际上就是一张640×480的jpg图片,所以就是么把2D图片读到场景中。为了同时实现交互式更换背景图片,这里也加入了菜单。 http://www.paper51.com
读入2D图片要用到ImageComponent2D和TextureLoader两个类。其中ImageComponent2D用于定义一个二维组件,TextureLoader则是读取ImageComponent2D定义的二维组件。 http://www.paper51.com // 预载背景图片 内容来自论文无忧网 www.paper51.com // 使用TextureLoader读图片 paper51.com // 将要读取的图片装入ImageComponent2D[ ]中 内容来自论文无忧网 www.paper51.com if ( debug ) System.err.println( " background images..." ); http://www.paper51.com
TextureLoader texLoader = null; http://www.paper51.com
String value = null; paper51.com imageComponents = new ImageComponent2D[images.length]; paper51.com for ( int i = 0; i < images.length; i++ ) 内容来自论文无忧网 www.paper51.com { 内容来自论文无忧网 www.paper51.com value = (String)images[i].value; 内容来自论文无忧网 www.paper51.com if ( value == null ) 内容来自www.paper51.com { paper51.com imageComponents[i] = null; copyright paper51.com continue; paper51.com } paper51.com
texLoader = new TextureLoader( value, this ); copyright paper51.com
imageComponents[i] = texLoader.getImage( ); copyright paper51.com } 内容来自www.paper51.com paper51.com 图14纹理背景--白云 copyright paper51.com 4.4.3 指数雾 paper51.com 用ExponentialFog可以产生雾的效果 http://www.paper51.com 雾的密度通过指数控制厚度: http://www.paper51.com effect = e(-density * distance) copyright paper51.com color = effect * shapeColor + (1-effect) * fogColor 内容来自www.paper51.com 下面是场景中对雾的初始化 copyright paper51.com // 设置雾的颜色、密度和范围 内容来自www.paper51.com fog = newExponentialFog(); paper51.com fog.setColor(color); paper51.com fog.setDensity(density); http://www.paper51.com fog.setCapability(Fog.ALLOW_COLOR_WRITE); copyright paper51.com fog.setCapability(ExponentialFog.ALLOW_DENSITY_WRITE); 内容来自论文无忧网 www.paper51.com
fog.setInfluencingBounds(worldBounds); http://www.paper51.com Scene1.addChild (fog); 内容来自www.paper51.com
内容来自论文无忧网 www.paper51.com
内容来自www.paper51.com
paper51.com
内容来自www.paper51.com 图15各种不同的雾效果 copyright paper51.com 4.4.4 背景音乐 http://www.paper51.com 在场景中加入背景音乐可以是气氛更加活跃。 copyright paper51.com
首先要把音乐加入到背景中, 内容来自论文无忧网 www.paper51.com if ( debug ) System.err.println( "sounds..." ); http://www.paper51.com String path = getCurrentDirectory( ); 内容来自论文无忧网 www.paper51.com MediaContainer backgroundMedia = new MediaContainer( copyright paper51.com path +"canon.wav" ); paper51.com backgroundMedia.setCacheEnable( true ); paper51.com 然后对背景音乐初始化, 内容来自www.paper51.com backgroundSound = new BackgroundSound( ); http://www.paper51.com
backgroundSound.setEnable( true ); copyright paper51.com backgroundSound.setLoop( Sound.INFINITE_LOOPS ); // 设置音乐循环播放 内容来自www.paper51.com backgroundSound.setSoundData( backgroundMedia ); http://www.paper51.com backgroundSound.setInitialGain( 1.0f ); // 设置音量 内容来自www.paper51.com backgroundSound.setSchedulingBounds( worldBounds ); 内容来自www.paper51.com backgroundSound.setCapability( Sound.ALLOW_ENABLE_WRITE); 内容来自www.paper51.com backgroundSound.setCapability(Sound.ALLOW_INITIAL_GAIN_WRITE ); 内容来自论文无忧网 www.paper51.com backgroundSound.setCapability(Sound.ALLOW_PAUSE_WRITE); paper51.com backgroundSound.setCapability(Sound.ALLOW_PAUSE_READ); 内容来自www.paper51.com Scene1.addChild( backgroundSound ); paper51.com §4.5 在网页上显示3D图形 http://www.paper51.com
Java3D一个最大的特性是可以使用Applet作为显示容器, paper51.com <HTML> http://www.paper51.com <BODY> http://www.paper51.com <APPLET code= ExExponentialFog.class width=400height=400> paper51.com </APPLET> copyright paper51.com
</BODY> 内容来自www.paper51.com </HTML> 内容来自www.paper51.com |