我想知道是否有办法改变"颗粒的外观"到我创造的图像。我在Lab: Simulating Flocking Behavior with Perlin Noise找到了实验室。
我一直想弄清楚如何上传我在草图中创建的鸟类。
有人愿意帮助我弄清楚它是否可能吗?
/**
*
* FlowField v1.00
* 14/12/2008 17:53
*
* © JUSTIN WINDLE | soulwire ltd
* http://blog.soulwire.co.uk
*
**/
package
{
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.Sprite;
import flash.display.StageAlign;
import flash.display.StageQuality;
import flash.display.StageScaleMode;
import flash.events.Event;
import flash.geom.ColorTransform;
import flash.geom.Point;
import flash.utils.getTimer;
public class FlowField extends Sprite
{
//___________________________________________________________ _____
//————————————————————————————————————————————— CLASS MEMBERS VALUE
private static const MARGIN: int = 10;
private static const UI_Y_OPEN: int = 200;
private static const UI_Y_CLOSED: int = 314;
private static const FADING: ColorTransform = new ColorTransform(1, 1, 1, 0.99);
//———————————————————————————————————————————————————————————
private var _offsets: Array = [];
private var _offsetSpeed1: Number = 2.0;
private var _offsetSpeed2: Number = -2.0;
private var _speedMultiplier: Number = 2.0;
private var _numParticles: int = 1000;
private var _noiseSeed: int = 1000;
private var _baseX: int = 180;
private var _baseY: int = 180;
private var _settingsOpen: Boolean = false;
private var _useBlitting: Boolean = false;
private var _renderBMD: BitmapData;
private var _noiseBMD: BitmapData;
private var _renderBMP: Bitmap;
private var _noiseBMP: Bitmap;
private var _particles: Sprite;
//___________________________________________________________
//————————————————————————————————————————— GETTERS + SETTERS
public function set numParticles( value:int ):void
{
_numParticles = value;
ui.particlesTF.text = value.toString();
if ( _numParticles > _particles.numChildren )
{
var particle:Particle;
while ( _particles.numChildren < _numParticles )
{
particle = new Particle();
particle.x = Math.random() * stage.stageWidth;
particle.y = Math.random() * stage.stageHeight;
_particles.addChild( particle );
}
}
else if ( _numParticles < _particles.numChildren )
{
while ( _particles.numChildren > _numParticles )
{
_particles.removeChildAt( _numParticles );
}
}
}
public function set noiseSeed( value:int ):void
{
_noiseSeed = value;
ui.seedTF.text = value.toString();
}
public function set baseX( value:int ):void
{
_baseX = value;
ui.baseXTF.text = value.toString();
}
public function set baseY( value:int ):void
{
_baseY = value;
ui.baseYTF.text = value.toString();
}
public function set offsetSpeed1( value:Number ):void
{
_offsetSpeed1 = value;
ui.offset1TF.text = value.toString();
}
public function set offsetSpeed2( value:Number ):void
{
_offsetSpeed2 = value;
ui.offset2TF.text = value.toString();
}
public function set speedMultiplier( value:Number ):void
{
_speedMultiplier = value;
ui.pSpeedTF.text = value.toString();
}
public function set useBlitting( value:Boolean ):void
{
_useBlitting = value;
if ( _useBlitting )
{
Particle.COLOUR = 0xFFFFFF;
removeChild( _particles );
addChild( _renderBMP );
}
else
{
Particle.COLOUR = 0x111111;
removeChild( _renderBMP );
addChild( _particles );
}
for (var i:int = 0; i < _numParticles; i++)
{
Particle( _particles.getChildAt(i) ).draw();
}
addChild( _noiseBMP );
addChild( ui );
}
//___________________________________________________________
//——————————————————————————————————————————————— CONSTRUCTOR
public function FlowField()
{
stage.scaleMode = StageScaleMode.NO_SCALE;
stage.quality = StageQuality.MEDIUM;
stage.align = StageAlign.TOP_LEFT;
_offsets.push( new Point() );
_offsets.push( new Point() );
_noiseSeed = Math.random() * 2000 << 0;
_renderBMD = new BitmapData( stage.stageWidth, stage.stageHeight, false, 0 );
_renderBMP = new Bitmap( _renderBMD );
addChild( _renderBMP );
_noiseBMD = new BitmapData( stage.stageWidth, stage.stageHeight );
_noiseBMD = new BitmapData( stage.stageWidth + (MARGIN * 4), stage.stageHeight + (MARGIN * 4), false );
_noiseBMP = new Bitmap( _noiseBMD );
_noiseBMP.visible = false;
addChild( _noiseBMP );
createParticles();
configureListeners();
useBlitting = false;
}
//___________________________________________________________
//——————————————————————————————————————————————————— METHODS
//———————————————————————————————————————————————————————————
private function createParticles():void
{
_particles = new Sprite();
addChild( _particles );
addChild( ui );
for (var i:int = 0; i < _numParticles; i++)
{
var particle:Particle = new Particle();
particle.x = Math.random() * stage.stageWidth;
particle.y = Math.random() * stage.stageHeight;
_particles.addChild( particle );
}
}
private function configureListeners():void
{
ui.particlesSlider.addEventListener( Event.CHANGE, onSliderChange );
ui.seedSlider.addEventListener( Event.CHANGE, onSliderChange );
ui.baseXSlider.addEventListener( Event.CHANGE, onSliderChange );
ui.baseYSlider.addEventListener( Event.CHANGE, onSliderChange );
ui.offset1Slider.addEventListener( Event.CHANGE, onSliderChange );
ui.offset2Slider.addEventListener( Event.CHANGE, onSliderChange );
ui.pSpeedSlider.addEventListener( Event.CHANGE, onSliderChange );
ui.showNoiseCB.addEventListener( Event.CHANGE, onCBSelect );
ui.blitCB.addEventListener( Event.CHANGE, onCBSelect );
ui.particlesSlider.value = _numParticles;
ui.particlesTF.text = _numParticles.toString();
ui.seedSlider.value = _noiseSeed;
ui.seedTF.text = _noiseSeed.toString();
ui.baseXSlider.value = _baseX;
ui.baseXTF.text = _baseX.toString();
ui.baseYSlider.value = _baseY;
ui.baseYTF.text = _baseX;
ui.offset1Slider.value = _offsetSpeed1;
ui.offset1TF.text = _offsetSpeed1.toString();
ui.offset2Slider.value = _offsetSpeed2;
ui.offset2TF.text = _offsetSpeed2.toString();
ui.pSpeedSlider.value = _speedMultiplier;
ui.pSpeedTF.text = _speedMultiplier.toString();
addEventListener( Event.ENTER_FRAME, update );
}
//___________________________________________________________
//———————————————————————————————————————————— EVENT HANDLERS
private function update( event:Event ):void
{
_offsets[0].x += _offsetSpeed1;
_offsets[0].y += _offsetSpeed1;
_offsets[1].x += _offsetSpeed2;
_offsets[1].y += _offsetSpeed2;
_noiseBMD.perlinNoise( _baseX, _baseY, 2, _noiseSeed, false, true, 10, true, _offsets );
var particle:Particle;
var brightness:Number;
var radians:Number;
var angle:Number;
var speed:Number;
var pixel:int;
for (var i:int = 0; i < _particles.numChildren; i++)
{
particle = _particles.getChildAt(i) as Particle;
pixel = _noiseBMD.getPixel( particle.x + (MARGIN *2), particle.y + (MARGIN *2) );
brightness = pixel / 0xFFFFFF;
speed = 0.1 + brightness * particle.speed * _speedMultiplier;
angle = 360 * brightness * particle.wander;
radians = angle * Math.PI / 180;
particle.x += Math.cos( radians ) * speed;
particle.y += Math.sin( radians ) * speed;
particle.scaleX = particle.scaleY = 0.1 + brightness * (_useBlitting ? 4 : 6);
particle.rotation = angle;
if ( particle.x < -MARGIN ) particle.x = stage.stageWidth + MARGIN;
else if ( particle.x > stage.stageWidth + MARGIN ) particle.x = -MARGIN;
if ( particle.y < -MARGIN ) particle.y = stage.stageHeight + MARGIN;
else if ( particle.y > stage.stageHeight + MARGIN ) particle.y = -MARGIN;
}
if ( _useBlitting )
{
_renderBMD.colorTransform( _renderBMD.rect, FADING );
_renderBMD.draw( _particles );
}
if ( !_settingsOpen && mouseY > UI_Y_CLOSED )
{
_settingsOpen = true;
}
else if ( _settingsOpen && mouseY < UI_Y_OPEN )
{
_settingsOpen = false;
}
if ( _settingsOpen && ui.y > UI_Y_OPEN )
{
ui.y += (UI_Y_OPEN - ui.y) / 3;
}
else if ( !_settingsOpen && ui.y < UI_Y_CLOSED )
{
ui.y += (UI_Y_CLOSED -ui.y) / 3;
}
}
private function onSliderChange( event:Event ):void
{
switch( event.currentTarget )
{
case ui.particlesSlider : numParticles = event.currentTarget.value; break;
case ui.seedSlider : noiseSeed = event.currentTarget.value; break;
case ui.baseXSlider : baseX = event.currentTarget.value; break;
case ui.baseYSlider : baseY = event.currentTarget.value; break;
case ui.offset1Slider : offsetSpeed1 = event.currentTarget.value; break;
case ui.offset2Slider : offsetSpeed2 = event.currentTarget.value; break;
case ui.pSpeedSlider : speedMultiplier = event.currentTarget.value; break;
}
}
private function onCBSelect( event:Event ):void
{
switch( event.currentTarget )
{
case ui.showNoiseCB : _noiseBMP.visible = ui.showNoiseCB.selected; break;
case ui.blitCB : useBlitting = ui.blitCB.selected; break;
}
}
}
}
import flash.display.Shape;
internal class Particle extends Shape
{
public static var COLOUR:uint = 0x111111;
public static const MIN_SPEED:int = 1;
public static const MAX_SPEED:int = 4;
public static const MIN_WANDER:Number = 0.8;
public static const MAX_WANDER:Number = 1.2;
public var speed:Number;
public var wander:Number;
public function Particle()
{
speed = Math.random() * (MAX_SPEED - MIN_SPEED) + MIN_SPEED;
wander = Math.random() * (MAX_WANDER - MIN_WANDER) + MIN_WANDER;
draw();
}
public function draw()
{
graphics.clear();
graphics.beginFill( COLOUR );
graphics.moveTo( -1, -0.5);
graphics.lineTo( 1, 0);
graphics.lineTo( -1, 0.5);
graphics.lineTo( -1, -0.5);
graphics.endFill();
}
}