Skip to content

Instantly share code, notes, and snippets.

@bouze
Last active October 11, 2015 21:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bouze/3920457 to your computer and use it in GitHub Desktop.
Save bouze/3920457 to your computer and use it in GitHub Desktop.
Stage3Dが使えるかどうかと、ソフトウェアレンダリングになってるかどうかを判定するクラスと使い方
package
{
import flash.display.Sprite;
import flash.events.Event;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;
import me.bouze.util.Stage3DChecker;
[SWF(width = "200", height = "200", frameRate = "30", backgroundColor = "#000000")]
public class Main extends Sprite
{
public function Main()
{
//そもそもエンベットしているHTMLのwmodeをdirectにしておかないとStage3Dが使えないので注意!
addEventListener(Event.ADDED_TO_STAGE, addToStageHandler);
}
private function addToStageHandler(e:Event):void
{
//インスタンス化して
var checker:Stage3DChecker = new Stage3DChecker(stage);
//非同期で結果が出るので、Event.COMPLETEを待つ
checker.addEventListener(Event.COMPLETE, function(e:Event):void
{
//結果はStage3DCheckerインスタンスのavailableとisSoftwareRenderingというプロパティに入ります
var tf1:TextField = new TextField();
tf1.x = 20;
tf1.y = 20;
tf1.textColor = 0xffffff;
tf1.autoSize = TextFieldAutoSize.LEFT;
tf1.text = "available: " + checker.available;
addChild(tf1);
var tf2:TextField = new TextField();
tf2.x = 20;
tf2.y = 60;
tf2.textColor = 0xffffff;
tf2.autoSize = TextFieldAutoSize.LEFT;
tf2.text = "isSoftwareRendering: " + checker.isSoftwareRendering;
addChild(tf2);
});
//startを呼ぶと調査が始まります
//必ずイベントハンドラーを貼った後に実行してください
checker.start();
}
}
}
package me.bouze.util
{
import flash.display.Stage;
import flash.display.Stage3D;
import flash.display3D.Context3D;
import flash.display3D.Context3DRenderMode;
import flash.events.ErrorEvent;
import flash.events.Event;
import flash.events.EventDispatcher;
import flash.system.ApplicationDomain;
/**
* ...
* @author bouze
* @via http://www.mcfunkypants.com/2011/flash11-stage3d-tutorial-handling-init-errors/
*/
public class Stage3DChecker extends EventDispatcher
{
private var stage:Stage;
private var _available:Boolean;
private var _isSoftwareRendering:Boolean;
public function Stage3DChecker(stage:Stage)
{
this.stage = stage;
}
public function start():void
{
if (ApplicationDomain.currentDomain.hasDefinition("flash.display.Stage3D"))
{
if (stage.stage3Ds && stage.stage3Ds.length > 0)
{
stage.stage3Ds[0].addEventListener(Event.CONTEXT3D_CREATE, createHandler);
stage.stage3Ds[0].addEventListener(ErrorEvent.ERROR, errorHandler);
stage.stage3Ds[0].requestContext3D();
}
else
{
throw new Error('wmodeもしくはrenderModeの値に"direct"か、"gpu"以外が指定されている可能性があります。');
}
}
else
{
_available = false;
_isSoftwareRendering = false;
dispatchEvent(new Event(Event.COMPLETE));
}
}
private function createHandler(e:Event):void
{
var context3D:Context3D = (e.target as Stage3D).context3D;
if (context3D != null)
{
_available = true;
_isSoftwareRendering = (context3D.driverInfo == Context3DRenderMode.SOFTWARE || context3D.driverInfo.indexOf('oftware') > -1);
}
else
{
_available = false;
_isSoftwareRendering = false;
}
dispatchEvent(new Event(Event.COMPLETE));
}
private function errorHandler(e:ErrorEvent):void
{
_available = false;
_isSoftwareRendering = false;
dispatchEvent(new Event(Event.COMPLETE));
}
public function dispose():void
{
stage.stage3Ds[0].removeEventListener(Event.CONTEXT3D_CREATE, createHandler);
stage.stage3Ds[0].removeEventListener(ErrorEvent.ERROR, errorHandler);
stage = null;
}
public function get available():Boolean { return _available; }
public function get isSoftwareRendering():Boolean { return _isSoftwareRendering; }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment