Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
particles [2020/11/06 13:51] – [Example] admin | particles [Unknown date] (current) – removed - external edit (Unknown date) 127.0.0.1 | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== Using the ParticleSystem ====== | ||
- | {{:: | ||
- | |||
- | Gorilla3D provides an integrated particle system for rendering complex and individual particle effects. | ||
- | |||
- | A particle effect is produced by a so-called emitter represented by the basic **TGorillaParticleEmitter** class. | ||
- | Extend this class to build your own effect. | ||
- | Gorilla3D already supplies some of the most popular effects, like: fire, smoke, rain, snow, waterfall, eruption, explosion. | ||
- | |||
- | {{:: | ||
- | |||
- | It is possible to manipulate each particle by the so-called influencers. | ||
- | The framework already provides a lot of the basic influencer types, like: linear movement, spiral movement, gravity, wind, particle color, trailed movement, traktor movement and physics. | ||
- | |||
- | |||
- | ===== Features ===== | ||
- | * Randomizable particle properties by presets | ||
- | * Cachable particles | ||
- | * Count limitation | ||
- | * Auto emittance | ||
- | * Interactive emmittance | ||
- | * Endless or limited emmittance | ||
- | * Physically based particle collision detection with other scene objects (not with particles!) | ||
- | * Autosorting of particles depending on assigned camera | ||
- | |||
- | ===== Shader / Material ===== | ||
- | |||
- | The particle emitter expects a pointsprite compatible material source to be applied. | ||
- | Gorilla3D therefor provides the **TGorillaParticleMaterialSource** as default material. | ||
- | It supports rendering a single texture, texture-atlas (for animated particles) and colored particles on pointsprite basis. | ||
- | |||
- | Have a look at [[materials|Materials]] | ||
- | |||
- | Of course you can write your own shader if those features do not suit your needs. | ||
- | |||
- | ===== Shader Attributes ===== | ||
- | |||
- | To allow extended particle functionality Gorilla3D changes the default values of some vertex attributes, because at the moment it is not possible to declare our own vertex attributes. | ||
- | |||
- | In the following table we show the attribute settings for each particle, when using a TGorillaParticleEmitter: | ||
- | |||
- | ^ Vertex-Attribute ^ Value ^ | ||
- | | a_Position | position | | ||
- | | a_Normal | velocity | | ||
- | | a_Color0 | current color| | ||
- | | a_TexCoord0 | current texture or texture atlas offset | | ||
- | | a_Binormal | X = size, Y = angle, Z = weight | | ||
- | | a_Tangent | X = age, Y = life time, Z = delta time | | ||
- | ===== Physics Particle Support ===== | ||
- | |||
- | ===== Example ===== | ||
- | |||
- | ==== Fire effect ==== | ||
- | |||
- | <file pascal Form1.pas> | ||
- | procedure TForm1.FormCreate(Sender: | ||
- | var LParticles := TGorillaFireParticleEmitter; | ||
- | LParticleMat : TParticleMaterialSource; | ||
- | begin | ||
- | LParticles := TGorillaFireParticleEmitter.Create(FGorilla); | ||
- | LParticles.Parent := FGorilla; | ||
- | LParticles.Camera := FCamera; | ||
- | |||
- | // a fire effect contains multiple parts: a front and back flame and smoke | ||
- | LParticleMat := LParticles.Back.MaterialSource as TParticleMaterialSource; | ||
- | LParticleMat.Texture.LoadFromFile(' | ||
- | | ||
- | LParticles.LoadTexture(' | ||
- | LParticles.Smoke.LoadTexture(' | ||
- | LParticles.Scale.Point := Point3D(5, 5, 5); | ||
- | end; | ||
- | |||
- | procedure TForm1.Timer1Timer(Sender: | ||
- | begin | ||
- | // on some constellations you will need an interval to update the viewport | ||
- | FGorilla.Invalidate(); | ||
- | end; | ||
- | </ | ||
- | |||
- | ==== Smoke effect with wind influencer ==== | ||
- | |||
- | <file pascal Form1.pas> | ||
- | procedure TForm1.FormCreate(Sender: | ||
- | var LParticles := TGorillaSmokeParticleEmitter; | ||
- | LParticleMat : TParticleMaterialSource; | ||
- | LWind : TGorillaWindParticleInfluencer; | ||
- | begin | ||
- | LParticles := TGorillaSmokeParticleEmitter.Create(FGorilla); | ||
- | LParticles.Parent := FGorilla; | ||
- | LParticles.Camera := FCamera; | ||
- | |||
- | // load smoke texture atlas | ||
- | LParticles.LoadTexture(' | ||
- | |||
- | // randomize particle size and growth | ||
- | LParticles.ParticleSize | ||
- | LParticles.ParticleGrowth := TParticlePreset.Create(4, | ||
- | |||
- | LWind := TGorillaWindParticleInfluencer.Create(LParticles); | ||
- | LWind.Origin := TPoint3D.Create(5, | ||
- | LWind.Size | ||
- | end; | ||
- | |||
- | procedure TForm1.Timer1Timer(Sender: | ||
- | begin | ||
- | // on some constellations you will need an interval to update the viewport | ||
- | FGorilla.Invalidate(); | ||
- | end; | ||
- | </ | ||
- | |||
- | ==== Eruption effect with color influencer ==== | ||
- | |||
- | <file pascal Form1.pas> | ||
- | procedure TForm1.FormCreate(Sender: | ||
- | var LParticles : TGorillaEruptionParticleEmitter; | ||
- | LColorInfl : TGorillaColoredParticleInfluencer; | ||
- | begin | ||
- | LParticles := TGorillaEruptionParticleEmitter.Create(FGorilla); | ||
- | LParticles.Parent := FGorilla; | ||
- | LParticles.Camera := FCamera; | ||
- | |||
- | LColorInfl := TGorillaColoredParticleInfluencer.Create(LParticles); | ||
- | LColorInfl.StartColor := TAlphaColorF.Create(0, | ||
- | LColorInfl.EndColor := TAlphaColorF.Create(0, | ||
- | end; | ||
- | |||
- | procedure TForm1.Timer1Timer(Sender: | ||
- | begin | ||
- | // on some constellations you will need an interval to update the viewport | ||
- | FGorilla.Invalidate(); | ||
- | end; | ||
- | </ | ||
- | |||
- | |||
- | ==== Rain effect ==== | ||
- | |||
- | <file pascal Form1.pas> | ||
- | procedure TForm1.FormCreate(Sender: | ||
- | var LParticles : TGorillaRainParticleEmitter; | ||
- | begin | ||
- | LParticles := TGorillaRainParticleEmitter.Create(FGorilla); | ||
- | LParticles.Parent := FGorilla; | ||
- | LParticles.Camera := FCamera; | ||
- | end; | ||
- | |||
- | procedure TForm1.Timer1Timer(Sender: | ||
- | begin | ||
- | // on some constellations you will need an interval to update the viewport | ||
- | FGorilla.Invalidate(); | ||
- | end; | ||
- | </ | ||
- | |||
- | ==== Snow effect ==== | ||
- | |||
- | <file pascal Form1.pas> | ||
- | procedure TForm1.FormCreate(Sender: | ||
- | var LParticles : TGorillaSnowParticleEmitter; | ||
- | begin | ||
- | LParticles := TGorillaSnowParticleEmitter.Create(FGorilla); | ||
- | LParticles.Parent := FGorilla; | ||
- | LParticles.Camera := FCamera; | ||
- | end; | ||
- | |||
- | procedure TForm1.Timer1Timer(Sender: | ||
- | begin | ||
- | // on some constellations you will need an interval to update the viewport | ||
- | FGorilla.Invalidate(); | ||
- | end; | ||
- | </ | ||
- | |||
- | |||
- | ==== Mouse interaction ==== | ||
- | |||
- | <file pascal Form1.pas> | ||
- | var | ||
- | FParticles : TGorillaParticleEmitter; | ||
- | |||
- | procedure TForm1.FormCreate(Sender: | ||
- | var LParticleMat : TParticleMaterialSource; | ||
- | LVPreset | ||
- | LColorInfl : TGorillaColoredParticleInfluencer; | ||
- | begin | ||
- | // create the particle material shader | ||
- | LParticleMat := TParticleMaterialSource.Create(FGorilla); | ||
- | LParticleMat.Parent := FGorilla; | ||
- | LParticleMat.UseTexture := false; | ||
- | LParticleMat.IsTextureAtlas := false; | ||
- | |||
- | // create particle emitter for spraying effect | ||
- | FParticles := TGorillaParticleEmitter.Create(FGorilla); | ||
- | FParticles.Parent := FGorilla; | ||
- | | ||
- | // connect particle shader and emitter | ||
- | LParticleMat.Emitter := FParticles; | ||
- | | ||
- | // limit number of particles | ||
- | FParticles.EmitParticles := 100; | ||
- | FParticles.MaxParticles | ||
- | | ||
- | // disable auto emittance, becaus we want only to emit on mouse click | ||
- | FParticles.AutoEmit := false; | ||
- | | ||
- | // enable particle caching | ||
- | FParticles.Reuse := true; | ||
- | |||
- | // randomize particle properties | ||
- | // 1) random position of particles | ||
- | LVPreset := TParticleVectorPreset.Create(0, | ||
- | FParticles.ParticlePosition := LVPreset ; | ||
- | |||
- | // 2) random velocity of particles | ||
- | LVPreset := TParticleVectorPreset.Create(5, | ||
- | LVPreset.RangeY.Negative := false; | ||
- | FParticles.ParticleVelocity := LVPreset; | ||
- | |||
- | // 3) the rest of properties | ||
- | FParticles.ParticleSize | ||
- | FParticles.ParticleAngle | ||
- | FParticles.ParticleWeight | ||
- | FParticles.ParticleGrowth | ||
- | FParticles.ParticleLifeTime := TParticlePreset.Create(500, | ||
- | |||
- | // create a color influencer for a starting and end color | ||
- | LColorInfl: | ||
- | LColorInfl.StartColor := TAlphaColorF.Create(0, | ||
- | LColorInfl.EndColor := TAlphaColorF.Create(0, | ||
- | end; | ||
- | |||
- | var | ||
- | FLatest : TPointF; | ||
- | FMouseMove : Boolean = false; | ||
- | FClickPt : TPoint3D; | ||
- | |||
- | procedure TForm1.DoOnViewportMouseUp(ASender : TObject; AButton : TMouseButton; | ||
- | AShift : TShiftState; | ||
- | begin | ||
- | FMouseMove := false; | ||
- | end; | ||
- | |||
- | procedure TForm1.DoOnViewportMouseDown(ASender : TObject; AButton : TMouseButton; | ||
- | AShift : TShiftState; | ||
- | var I : Integer; | ||
- | begin | ||
- | if (ssLeft in AShift) then | ||
- | begin | ||
- | FMouseMove := true; | ||
- | FLatest := PointF(X, Y); | ||
- | | ||
- | // emit particles at mouse position | ||
- | FClickPt := FGorilla.ScreenToWorld(Point(round(X), | ||
- | for I := 0 to (FParticles.EmitParticles - 1) do | ||
- | FParticles.EmitParticle(FClickPt); | ||
- | end; | ||
- | end; | ||
- | |||
- | procedure TForm1.DoOnViewportMouseMove(ASender : TObject; AShiftState : TShiftState; | ||
- | X, Y : Single); | ||
- | var lDiff : TPointF; | ||
- | I : Integer; | ||
- | lOrigin : TPoint3D; | ||
- | begin | ||
- | if (ssLeft in AShiftState) then | ||
- | begin | ||
- | // emit particles at mouse position | ||
- | FClickPt := FGorilla.ScreenToWorld(Point(round(X), | ||
- | for I := 0 to (FParticles.EmitParticles - 1) do | ||
- | FParticles.EmitParticle(FClickPt); | ||
- | end; | ||
- | |||
- | if FMouseMove then | ||
- | begin | ||
- | FLatest := PointF(X, Y); | ||
- | end; | ||
- | end; | ||
- | |||
- | procedure TForm1.Timer1Timer(Sender: | ||
- | begin | ||
- | // on some constellations you will need an interval to update the viewport | ||
- | FGorilla.Invalidate(); | ||
- | end; | ||
- | </ | ||
- | |||
- | |||
- | Next step: [[physics|Physics]] |