In case Flash no longer exists; a copy of this site is included in the Flashpoint archive's "ultimate" collection.

Dead Code Preservation :: Archived AS3 works from wonderfl.net

[PV3D]grass 草

Get Adobe Flash player
by nackpan 28 Aug 2010
    Embed
/**
 * Copyright nackpan ( http://wonderfl.net/user/nackpan )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/vPhc
 */

package {
    import flash.display.Sprite;
    import flash.display.Bitmap;
    import flash.display.BitmapData;
    import flash.display.Loader;
    import flash.events.Event;
    import flash.geom.ColorTransform;
    import flash.geom.Matrix;
    import flash.geom.Point;
    import flash.geom.Rectangle;
    import flash.text.TextField;
    import org.papervision3d.core.geom.renderables.Vertex3D;
    import org.papervision3d.objects.primitives.Plane;
    import org.papervision3d.objects.primitives.Sphere;
    import org.papervision3d.view.BasicView;
    import org.papervision3d.materials.*;
    import flash.display.BitmapDataChannel;
    import flash.system.LoaderContext;
    import flash.display.StageScaleMode;
    
    import flash.net.URLRequest;
    
    import flash.system.System;
    
    public class FlashTest extends Sprite {
        private var loader:Loader;
        private var world:BasicView;
        
        
        private var grasses:Array;
        //private var txt:TextField;
        private var wind:Wind;
        public function FlashTest() {
            // write as3 code here..
            if (stage) loadImg();
            else addEventListener(Event.ADDED_TO_STAGE, loadImg);
            
        }
        
        
        private function loadImg(e:Event = null):void
        {
            stage.scaleMode = StageScaleMode.NO_SCALE;
            
            var url:String = "http://assets.wonderfl.net/images/related_images/1/1b/1b1c/1b1c459ddfbeb6583c6f4d4f26c6e597272f399b";
            var urlReq:URLRequest = new URLRequest( url );
                
            loader = new Loader();
            loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadComplete);
            loader.load(urlReq , new LoaderContext(true));
               
            
        }
        
        
        private function loadComplete(event:Event):void
        {
                init();
        }
        
        
        private function init(e:Event = null):void 
        {
            //地面
            var gbmd:BitmapData = new BitmapData(stage.stageWidth, stage.stageHeight, false, 0xffffff);
            gbmd.perlinNoise(16, 8, 6, 0, true, true, 0, true);
            var cl:ColorTransform = new ColorTransform(0.5,0.5,0.5,1,8,68,2);
            gbmd.colorTransform(gbmd.rect, cl);
            var gbmp:Bitmap = new Bitmap(gbmd);
            gbmp.y = (stage.stageHeight / 2) - 20;
            addChild(gbmp);
            
            world = new BasicView(stage.stageWidth, stage.stageHeight);
            addChild(world);
            
            //草
            var bmd:BitmapData = new BitmapData(100, 100, true, 0);
            var rect:Rectangle = new Rectangle(0, 0, 100, 100);
            var m:Matrix = new Matrix();
            m.tx = -100; 
            bmd.draw(loader, null, null, null, rect);
            var mask:BitmapData = new BitmapData(100, 100, false, 0xff0000);
            mask.draw(loader, m, null, null, rect);
            
            
            bmd.copyChannel(mask, mask.rect, new Point(0,0), BitmapDataChannel.RED, BitmapDataChannel.ALPHA);
            
            var bitmapMaterial:BitmapMaterial = new BitmapMaterial(bmd);
            var i:int;
            grasses = new Array();
            for (i = 0; i < 100; i++) {
                var plane:Plane = new Plane(bitmapMaterial, 400, 400,0,3);
                plane.x = -800 + Math.floor(Math.random() * 1600);
                plane.z = -800 + Math.floor(Math.random() * 1600);
                plane.rotationY = -30 + 60 * Math.random();
                world.scene.addChild(plane);
                var grass:Grass = new Grass(plane);
                grasses[i] = grass;
                
            }
            
            world.camera.y = 360;
            world.camera.z = -860;
            
            //風
            wind = new Wind(-500, -500, 4, 16, 600); 
            
            /*
            txt = new TextField();
            addChild(txt);
            txt.width = 200;
            txt.height = 20;
            txt.border = true;
            */
            
            
            world.startRendering();
            
            addEventListener(Event.ENTER_FRAME, enter);
            
            
        }
        
        private function enter(event:Event):void
        {
            
            wind.x += wind.vx;
            wind.z += wind.vz;
            
            if (wind.x > 1000) wind.x = -600;
            if (wind.z > 1000) wind.z = -1000;
            
            for (var i:int = 0; i < 100; i++) {
                grasses[i].checkCollision(wind.x, wind.z , wind.r, wind.vx, wind.vz);
            }
            
            
            //var memory:uint = System.totalMemory;
            //memory = Math.round(memory / 1000) * 1000;
            //txt.text = "Memory: " + memory.toString();

        }
    }
}

import org.papervision3d.objects.primitives.Plane;
class Grass {
    public var plane:Plane;
    private var vx:Number;
    private var vz:Number;
    private var spring:Number = .04;
    private var friction:Number = .96;
    private var gx:Number;
    private var gz:Number;
    private var ratio:Array;
        
    public function Grass(plane:Plane):void
    {
        vx = 0;
        vz = 0;
        this.plane = plane;
        gx = plane.x;
        gz = plane.z;
        
        ratio = [0,0.2, 0.5, 1];
        
        
    }
    
    public function checkCollision(cx:Number, cz:Number,  r:Number, cvx:Number, cvz:Number):void
    {
        
        var target_x:Number = plane.x;
        var target_z:Number = plane.z;
        var dx:Number;
        var dz:Number;
        
            
        var len:Number = Math.sqrt((cx - target_x) * (cx - target_x) 
                                + (cz - target_z) * (cz - target_z) );
                                
        var forceFlag:Boolean = false;
        if (len < r) {
            forceFlag = true;
        }else {
            forceFlag = false;
        }
            
        if (forceFlag) {
            vx += cvx * .008;// .004;
            vz += cvz * .008;// .004;
            
        }else{
            dx = target_x - gx;
            dz = target_z - gz;
            var ax:Number = dx * spring;
            var az:Number = dz * spring;
            vx += ax;
            vz += az;
            vx *= friction;
            vz *= friction;
            
        }
            
        gx += vx;
        gz += vz;
        
        
        for (var i:int = 1; i < 4; i++) {
            var vvx:Number = vx * ratio[i];
            var vvz:Number = vz * ratio[i];
            
            plane.geometry.vertices[i].x += vvx;
            plane.geometry.vertices[i].z += vvz;
            plane.geometry.vertices[i+4].x += vvx;
            plane.geometry.vertices[i + 4].z += vvz;
            
        }
        
        
    }
}

class Wind
{
    public var x:Number;
    public var z:Number;
    public var vx:Number;
    public var vz:Number;
    public var r:Number;
    public function Wind(x:Number, z:Number, vx:Number, vz:Number, r:Number):void
    {
        this.x = x;
        this.z = z;
        this.vx = vx;
        this.vz = vz;
        this.r = r;
        
    }
}