[Away3D] 光の効果 (1)

Away3Dを使って、光のエフェクトを作るよ。 :boy:

away3dEffect1

Mesh を使ってみるよ。 :bouzu:
ConeGeometry と TextureMaterial, BitmapTexture を使ってみたよ。 :cake:

itoz 師匠 :hakase: のスクリプトをこっそり使わせてもらったよ。

:caution: 要 Flash Player 11.8 以上

This movie requires Flash Player 11.8.0

Main.as
package {

  import flash.display.Sprite;
  import flash.system.System;
  import flash.events.Event;
  import flash.geom.Vector3D;
  import flash.geom.Matrix3D;

  import away3d.Away3D;
  import away3d.containers.View3D;
  import away3d.containers.Scene3D;
  import away3d.cameras.Camera3D;
  import away3d.containers.ObjectContainer3D;
  import away3d.entities.Mesh;
  import away3d.primitives.ConeGeometry;
  import away3d.materials.TextureMaterial;
  import away3d.textures.BitmapTexture;
  import away3d.utils.Cast;


  [SWF(backgroundColor="#000000", width="600", height="600", frameRate="60")]

  public class Main extends Sprite {
    [Embed(source="assets/light.png")]
    private var LightTexture:Class;
    // プロパティ
    private var view:View3D;
    private var scene:Scene3D;
    private var camera:Camera3D;
    private var container:ObjectContainer3D;
    private var lights:Array;
    private static var radius:uint = 500;
    private static var radian:Number = Math.PI/180;
    private static var center:Vector3D = new Vector3D();

    // コンストラクタ
    public function Main() {
      System.pauseForGCIfCollectionImminent(1);
      init();
    }

    // メソッド
    private function init():void {
      view = new View3D();
      scene = view.scene;
      camera = view.camera;

      addChild(view);
      setup();
      initialize();
      addEventListener(Event.ENTER_FRAME, render, false, 0, true);
    }
    private function setup():void {
      view.backgroundColor = 0x000000;
      view.antiAlias = 4;
      camera.x = 0;
      camera.y = 0;
      camera.z = - radius;
    }
    private function initialize():void {
      container = new ObjectContainer3D();
      scene.addChild(container);

      var texture:BitmapTexture = Cast.bitmapTexture(LightTexture);
      var material:TextureMaterial = new TextureMaterial(texture);
      material.bothSides = true;
      material.alphaBlending = true;

      lights = new Array();
      for (var n:uint = 0; n < 3; n++) {
        var offset:uint = 64*n;
        var matrix:Matrix3D = new Matrix3D();
        matrix.appendTranslation(0, - offset/2, 0);

        var geometry:ConeGeometry = new ConeGeometry(256 - offset, offset, 60, 15, false, true);
        geometry.applyTransformation(matrix);

        var light1:Mesh = new Mesh(geometry, material);
        light1.rotationY = 120*n;
        var light2:Mesh = new Mesh(geometry, material);
        light2.rotationY = 120*n;
        light2.rotationX = 180;
        var light3:Mesh = new Mesh(geometry, material);
        light3.rotationY = 120*n;
        light3.rotationZ = - 90;
        container.addChild(light1);
        container.addChild(light2);
        container.addChild(light3);

        lights.push(light1);
        lights.push(light2);
        lights.push(light3);
      }
    }
    private function render(evt:Event):void {
      container.rotationX += 0.1;
      container.rotationY += 0.2;
      container.rotationZ += 0.15;
      for (var n:uint = 0; n < lights.length; n++) {
        var light:Mesh = lights[n];
        light.rotationY += 0.2*n;
        var material:TextureMaterial = TextureMaterial(light.material);
        material.alpha = Math.random()*0.4 + 0.6;
      }
      camera.lookAt(center);
      view.render();
    }

  }

}

Flash CC + Flex 4 SDK でパブリッシュ。


[修正] (12/09/19 Wed 22:50)
lightの位置補正をMatrix3Dを用いる方法に修正。
[修正] (13/01/14 Mon 23:04)
Away3D 4.1.0 Alpha にバージョンアップ。
[修正] (13/03/08 Fri 18:08)
Castクラスを使用したスクリプトに変更。
[修正] (13/05/09 Thu 06:40)
Away3D 4.1.1 Beta にバージョンアップ。
[修正] (13/07/17 Wed 15:39)
Away3D 4.1.4 Gold にバージョンアップ。
[修正] (13/11/12 Tue 16:43)
Away3D 4.1.5 Gold にバージョンアップ。
[修正] (14/02/03 Mon 22:32)
Away3D 4.1.6 にバージョンアップ。