Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
Help us understand the problem. What is going on with this article?

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

More than 5 years have passed since last update.
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);
        }
    }
}
itoz
http://itoz.jp/ 最近はnuxt。
http://itoz.jp/
kakusininc
Webサイト構築 から実空間でセンサー系デジタルエンターテイメントまで幅広く制作するデジタル・クリエイティブスタジオ
http://kakus.in/
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away