LoginSignup
5
5

More than 5 years have passed since last update.

Away3D 4.0 beta PlaneGeometryの頂点を移動し、布のゆらぎを表現

Last updated at Posted at 2012-02-29
ClothMotion.as
/**
 * copyright (c) 2012 www.romatica.com
 * @auther itoz
 */
package
{
    import away3d.containers.View3D;
    import away3d.core.base.SubGeometry;
    import away3d.debug.AwayStats;
    import away3d.entities.Mesh;
    import away3d.materials.ColorMaterial;
    import away3d.primitives.PlaneGeometry;

    import flash.events.Event;
    import flash.geom.Vector3D;
    import flash.utils.getTimer;

    [SWF(backgroundColor="#FFFFFF", frameRate="60", width="800", height="600")]
    /**
     * Away3D 4.0 beta PlaneGeometryの頂点を移動し、布のゆらぎを表現
     * 動作サンプル
     * @see http://dl.dropbox.com/u/958512/sample/away3d4/clothMotion/index.html
     */
    public class ClothMotion extends View3D
    {
        private static const ZERO : Vector3D = new Vector3D(0, 0, 0);
        private static const FLOW_RANGE : int = 100;
        private static const FLOW_SPEED : Number = 3;
        private static const  RAD_VAR : Number = (Math.PI / 180);
        private var _clothMat : ColorMaterial;
        private var _clothGeo : PlaneGeometry;
        private var _cloth : Mesh;
        private var _defs : Vector.<Number>;
        private var _angs : Vector.<Number>;

        public function ClothMotion()
        {
            antiAlias = 4;
            backgroundColor = 0xa1c3c0;

            // ----------------------------------
            // 布
            // ----------------------------------
            _clothMat = new ColorMaterial(0x408aa8);
            _clothMat.bothSides = true;
            _clothGeo = new PlaneGeometry(1024, 1024, 32, 32);
            _cloth = new Mesh(_clothGeo, _clothMat);
            _cloth.showBounds = true;
            scene.addChild(_cloth);

            // ----------------------------------
            // 揺らぎ用パラメータ
            // ----------------------------------
            var max : int = _cloth.geometry.subGeometries[0].vertexData.length;
            _defs = new Vector.<Number>(max);
            _angs = new Vector.<Number>(max);
            for (var i : int = 0; i < _defs.length; i++) {
                _defs[i] = _cloth.geometry.subGeometries[0].vertexData[i];
                _angs[i] = (360 / _defs.length * i );
            }

            camera.y = -1000;
            addChild(new AwayStats());
            addEventListener(Event.ENTER_FRAME, update);
        }

        /**
         * アップデート
         */
        private function update(event : Event) : void
        {
            clothMotion();
            cameraMotion();
            // 描画
            render();
        }

        /**
         * 布頂点ゆらぎ
         */
        private function clothMotion() : void
        {
            var geo : SubGeometry = _clothGeo.subGeometries[0] as SubGeometry;
            var vlen : int = geo.vertexData.length;
            for (var i : int = 0; i < vlen;i++ ) {
                _angs[i] += (_angs[i] + FLOW_SPEED >= 360) ? FLOW_SPEED - 360 : FLOW_SPEED;
                var rad : Number = _angs[i] * RAD_VAR;
                var flow : int = Math.cos(rad) * FLOW_RANGE;
                geo.vertexData[i] = _defs[i] + flow;
            }
            geo.updateVertexData(geo.vertexData);
        }

        /**
         * カメラ移動
         */
        private function cameraMotion() : void
        {
            var range : int = 1500;
            var t : Number = getTimer() / range;
            camera.x = Math.sin(t) * range;
            camera.y = Math.cos(t) * range;
            camera.z = Math.cos(t) * range;
            camera.lookAt(ZERO);
        }
    }
}
5
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
5