将 Flare3D 集成到 Flex 应用程序中
我正在使用 Flex 框架和 3D 图形库 Flex 构建一个应用程序。 flare3d.com/" rel="nofollow">Flare3D。
我希望能够将 3D 场景嵌入到 MXML 应用程序中,并且仍然将鼠标事件分派到 3D 场景。
这是我的代码:
- MXML 应用程序:
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
xmlns:views="views.*"
minWidth="955" minHeight="600"
backgroundAlpha="0">
<s:HGroup width="100%" height="100%">
<s:Panel width="250" height="100%">
</s:Panel>
<views:MyView
id="myView"
width="100%" height="100%" />
</s:HGroup>
</s:Application>
负责 3D 渲染的 UIComponent(只是一个立方体)。
package views
{
import flare.basic.Scene3D;
import flare.basic.Viewer3D;
import flare.core.Camera3D;
import flare.primitives.Cube;
import flash.display.Stage3D;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.events.Event;
import mx.controls.Alert;
import mx.core.UIComponent;
public class MyView extends UIComponent
{
private var scene:Scene3D;
private var stage3D:Stage3D;
private var cube:Cube;
public function MyView()
{
super();
this.addEventListener(Event.ADDED_TO_STAGE, init);
}
public function init(event:Event):void
{
stage.scaleMode = StageScaleMode.NO_SCALE;
stage.align = StageAlign.TOP_LEFT;
//stage.focus = null;
//stage.getChildAt(0).visible = false;
scene = new Viewer3D(stage);
scene.setViewport(250, 0);
scene.addEventListener(Scene3D.PROGRESS_EVENT, progressEvent);
scene.addEventListener(Scene3D.COMPLETE_EVENT, completeEvent);
scene.camera = new Camera3D();
scene.camera.setPosition(150, 150, -300);
scene.camera.lookAt(0, 0, 0);
cube = new Cube("cube");
scene.addChild(cube);
}
protected function progressEvent(e:Event):void {}
protected function completeEvent(e:Event):void {}
}
}
问题在于 3D 渲染默认在 2D 层后面的“层”中完成(我们可以在其中放置 MXML 组件)。
默认情况下,如果我们混合 3D 和 2D 元素,我们将看不到 3D 元素。
所以我添加了backgroundAlpha="0"
。这解决了显示 3D 视图的问题。 但现在我有另一个问题,当我单击场景并移动鼠标时,我无法移动立方体(这是默认行为)。 我理解这是因为 3D 视图位于 2D 视图后面。
是否可以为 3D 视图提供鼠标事件的焦点(需要时)?
或者有不同的方式来做我想做的事吗? 我想到的唯一办法是取消注释此 //stage.getChildAt(0).visible = false;
这使得 2D 视图不可见。但我想混合 2D 元素和 3D 视图,所以这不是一个解决方案。
感谢您的任何帮助。
I'm building an application with the Flex framework and the 3D graphics library Flare3D.
I want to be able to embed my 3D scene into an MXML application and still have the mouse events dispatched to the 3D scene.
Here is my code:
- The MXML app:
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
xmlns:views="views.*"
minWidth="955" minHeight="600"
backgroundAlpha="0">
<s:HGroup width="100%" height="100%">
<s:Panel width="250" height="100%">
</s:Panel>
<views:MyView
id="myView"
width="100%" height="100%" />
</s:HGroup>
</s:Application>
The UIComponent responsible of the 3D rendering (just a cube).
package views
{
import flare.basic.Scene3D;
import flare.basic.Viewer3D;
import flare.core.Camera3D;
import flare.primitives.Cube;
import flash.display.Stage3D;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.events.Event;
import mx.controls.Alert;
import mx.core.UIComponent;
public class MyView extends UIComponent
{
private var scene:Scene3D;
private var stage3D:Stage3D;
private var cube:Cube;
public function MyView()
{
super();
this.addEventListener(Event.ADDED_TO_STAGE, init);
}
public function init(event:Event):void
{
stage.scaleMode = StageScaleMode.NO_SCALE;
stage.align = StageAlign.TOP_LEFT;
//stage.focus = null;
//stage.getChildAt(0).visible = false;
scene = new Viewer3D(stage);
scene.setViewport(250, 0);
scene.addEventListener(Scene3D.PROGRESS_EVENT, progressEvent);
scene.addEventListener(Scene3D.COMPLETE_EVENT, completeEvent);
scene.camera = new Camera3D();
scene.camera.setPosition(150, 150, -300);
scene.camera.lookAt(0, 0, 0);
cube = new Cube("cube");
scene.addChild(cube);
}
protected function progressEvent(e:Event):void {}
protected function completeEvent(e:Event):void {}
}
}
The problem is that the 3D rendering is done by default in a "layer" that is behind the 2D layer (where we can put MXML components).
By default, if we mix 3D and 2D elements, we can't see 3D elements.
So I added backgroundAlpha="0"
. This solved the problem of showing the 3D view.
But now I have another problem, I can't move the cube (which is the default behaviour) when I click on the scene and move the mouse.
I understand that this is because the 3D view is behind the 2D view.
Is it possible to give the 3D view the focus for mouse events (when it's needed) ?
Or is there a different way of doing what I want ?
The only hack I figured out is to uncomment this //stage.getChildAt(0).visible = false;
which make the 2D view invisible. But I want to mix 2D elements and 3D view, so it's not a solution.
Thanks for any help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我在使用 Alternativa3D 和 Flex 4.6 时遇到了完全相同的问题。我尝试的是在应用程序标签中添加 UIComponent 而不是 Panel 标签,并且我的 Flex 和 Stage3D 鼠标事件都有效。
所以代码看起来像这样:
I had the exact same issue working with Alternativa3D and Flex 4.6. What I tried was to add the UIComponent in the application tag instead of the Panel tag and both my Flex and Stage3D mouse events worked.
So the code looks like this:
设置
backgroundAlpha="0"
对我有用。我试图获取 http://www.beyond-reality-face.com/sdk 使用 Flex / Flash Builder。
现在我明白了这个问题。 Flex 应用程序是 2D 并且不透明,因此
backgroundAlpha="0"
解决了这个问题。此外,stage3D 不是透明的 - 但 Face SDK 中的示例已经通过将网络摄像头绘制为 Flare3D 平面上的动画纹理来说明这一点。
Setting
backgroundAlpha="0"
worked for me.I was trying to get http://www.beyond-reality-face.com/sdk working with Flex / Flash Builder.
Now I understand the issue. Flex app is 2D and is not transparent, so
backgroundAlpha="0"
fixes that.Also stage3D is not transparent - but the examples in the face SDK already account for that by painting the webcam as an animated texture onto a Flare3D plane.