---
title: "[转]M2D的介绍——针对Flash ‘Molehill’ API的GPU加速2D框架"
author: "AS3"
date: "2011-03-22T14:37:45Z"
tags: ["Flash", "嵌入式", "框架", "浏览器", "游戏"]
categories: ["编程语言"]
slug: "m2d-flash-molehill-api-gpu-2d"
draft: false
lastmod: "2011-03-22T14:37:45Z"
id: 970546
url: "https://as3.iteye.com/blog/970546"
---

[http://bbs.9ria.com/viewthread.php?tid=76603&extra=page%3D1%26amp%3Borderby%3Ddateline%26amp%3Bfilter%3D2592000](http://bbs.9ria.com/viewthread.php?tid=76603&extra=page%3D1%26amp%3Borderby%3Ddateline%26amp%3Bfilter%3D2592000)

 

 

可以确定，你们当中很多人都知道Molehill —— Flash Player的一套可以实现GPU加速的3dAPI。有些人误以为他只是针对3d实现GPU加速，有一个好消息就是你实际上可以在stage3d上而不是在stage（DisplayList）上加速2d图形的渲染。这个是有一点棘手，但绝对是可能的。  
例如这个demo —— [http://www.bytearray.org/wp-content/projects/molehill2d/molehill/](http://www.bytearray.org/wp-content/projects/molehill2d/molehill/) – 这个2d图形是通过Molehill在GPU上进行渲染的。但你看这个[例子代码](http://blog.kaourantin.net/?p=110)，如果你不是真正了解OpenGL或者DirectX世界的话，你可能需要花很多的时间去研究他。  
为了使你更方便的去了解，[Ely Greenfield](http://www.quietlyscheming.com/)写了一个名叫M2D的框架。  
**特点如下：**

- 嵌入式渲染。
- spritesheets动画。
- 位置，旋转对象。
- 粒子。

Ely太伟大了！我的意思是，他对2d flash游戏真是件大事情。现在你可以轻而易举的渲染60帧成千上万个sprite了，我期待着在游戏中看到使用他。  
我有分开Ely的原始M2D框架，并使用Box2D C++ Alchemy 端口添加了一个RenderScene和Physics类。RenderScene是为了使用M2D而被简化了的API，他将满足你在大多数情况下的使用，也方便其他人很轻易的使用他。

**他的工作原理是什么？**  
在3d中有一些东西可以供你使用——几何geometry （在我们项目中的三角形）、纹理textures 和着色shaders。  
要真正显示一张图片 —— 你需要通过两个三角形组成一个矩形同时通过正确的UVs加载一个bitmap数据。如图：

![](http://zaneblog.info/usr/uploads/2011/03/2991376064.png)

**我可以加快 Flash DisplayList中的DisplayObjects吗？**  
是的，你当然可以。唯一一点是现在只支持静态的DisplayObject对象，但是计划在将来实现MovieClip。目前通过SpriteSheets支持sprite动画——这里有个工具可以轻松的让你把SWF转换成PNG ——[Zoë by Grant Skinner](http://easeljs.com/zoe.html)。

**未来计划**

- 文档。
- 鼠标事件 —— 目前的框架还不支持鼠标事件。
- 影片剪辑动画 —— 动画可以通过SpriteSheets做到。
- 优化 —— 我们比较肯定的是他在实际运用中会有多次优化过程。
- ATF的支持（Adobe Texture Format）

**开源项目**

该框架可以无限制的应用于公共领域，你可以在免费的应用在你的商业项目中，同时也可以自己修改、升级这个版本。我们期待在社区里看到这个框架的更新以及应用这个框架的项目和对未来版本的一些建议。

**The API**

01. // create RenderScene - the sprite which renders Actors to Stage3D (GPU accelerated layer)
02. scene = new RenderScene(800,480);
03. addChild(scene);
04. // Add rectangle 100x100 px
05. var rect:Actor = scene.addRect(100,100);
06. rect.x = 100;
07. rect.y = 100;
08. rect.rotation = 10;
09. // Add embedded image
10. var image:Actor = scene.addImageEmbed(imageEmbed);
11. image.x = 300;
12. image.y = 100;
13. image.rotation = 45;

*复制代码*

**实例**

- [M2D HelloWorld](http://flashrealtime.com/demos/m2d/ExampleHelloWorld.html)
- [Simple Physics Game with Box2D](http://flashrealtime.com/demos/m2d/ExamplePhysicsGame.html)
- [Simple Mouse Click Events](http://flashrealtime.com/demos/m2d/ExampleClicker.html)
- [Animated Sprite sheets](http://flashrealtime.com/demos/m2d/MHSpriteTest.html)
- [Particles](http://flashrealtime.com/demos/m2d/MHPTest.html)
- [Driver Test](http://flashrealtime.com/demos/m2d/ExampleDriverTest.html)
- [15×10 sprites](http://flashrealtime.com/demos/m2d/ExampleThousands.html)
- [40×40 sprites](http://flashrealtime.com/demos/m2d/ExampleThousandsBig.html)
- [Animation with TweenMax](http://flashrealtime.com/demos/m2d/ExampleBasicAnimation.html)

提醒：你必须通过支持Molehill API的Flash Player Incubator版本运行，可以从[这里获取](http://labs.adobe.com/technologies/flashplatformruntimes/incubator/)。也有可能遇到浏览器不支持GPU加速的情况，你可以换一个浏览器试试看。

**下载M2D类库**

[M2DSamples.zip](http://flashrealtime.com/demos/m2d/M2DSamples.zip)(包含M2D.swc 、 Box2DAlchemy.swc 、RenderScene.as 和Physics.as)。

**M2D源代码地址**  
[https://github.com/egreenfield/M2D](https://github.com/egreenfield/M2D)