Modding:Simple Particles: Difference between revisions
Mirotworez (talk | contribs) (Marked this version for translation) |
m (Updated to 1.19.3.) |
||
Line 1: | Line 1: | ||
{{GameVersion|1. | {{GameVersion|1.19.3}} | ||
<languages/><translate> | <languages/><translate> | ||
<!--T:1--> | <!--T:1--> | ||
Line 11: | Line 11: | ||
<!--T:4--> | <!--T:4--> | ||
<syntaxhighlight lang="c#"> | <syntaxhighlight lang="c#"> | ||
public static SimpleParticleProperties myParticles = new SimpleParticleProperties(1, 1, ColorUtil.ColorFromRgba(220, 220, 220, 255), new Vec3d(), new Vec3d(), new Vec3f(), new Vec3f()); | |||
</syntaxhighlight> | </syntaxhighlight> | ||
Line 17: | Line 18: | ||
Now we have the property, the only thing left to do is to set the position and spawn the particles into the world. I'm gonna use the <code>OnHeldInteractStart</code> and *Step method inside a custom made Item class (you can read more about this in the [[Collectible|Collectibles Tutorial]]): | Now we have the property, the only thing left to do is to set the position and spawn the particles into the world. I'm gonna use the <code>OnHeldInteractStart</code> and *Step method inside a custom made Item class (you can read more about this in the [[Collectible|Collectibles Tutorial]]): | ||
<syntaxhighlight lang="c#"> | <syntaxhighlight lang="c#"> | ||
public override void OnHeldInteractStart(ItemSlot slot, EntityAgent byEntity, BlockSelection blockSel, EntitySelection entitySel, ref EnumHandHandling | public override void OnHeldInteractStart(ItemSlot slot, EntityAgent byEntity, BlockSelection blockSel, EntitySelection entitySel, bool firstEvent, ref EnumHandHandling handling) | ||
{ | { | ||
handling = EnumHandHandling.Handled; | |||
} | } | ||
Line 25: | Line 26: | ||
public override bool OnHeldInteractStep(float secondsUsed, ItemSlot slot, EntityAgent byEntity, BlockSelection blockSel, EntitySelection entitySel) | public override bool OnHeldInteractStep(float secondsUsed, ItemSlot slot, EntityAgent byEntity, BlockSelection blockSel, EntitySelection entitySel) | ||
{ | { | ||
myParticles.minPos = byEntity.Pos.XYZ.Add(0, byEntity. | myParticles.minPos = byEntity.Pos.XYZ.Add(0, byEntity.LocalEyePos, 0).Ahead(1f, byEntity.Pos.Pitch, byEntity.Pos.Yaw); | ||
byEntity.World.SpawnParticles(myParticles); | byEntity.World.SpawnParticles(myParticles); | ||
Line 47: | Line 48: | ||
<!--T:13--> | <!--T:13--> | ||
Basically there are two properties. One of them is the <code> | Basically there are two properties. One of them is the <code>MinPos</code> property, which will determine the exact position where particles will spawn. <code>AddPos</code> is relative to this position, once a particle will spawn it will be randomized and added to the particle's position. If <code>AddPos</code> is not defined all particles will spawn at the exact some position. | ||
<!--T:14--> | <!--T:14--> | ||
Line 56: | Line 57: | ||
<!--T:16--> | <!--T:16--> | ||
If we now set <code> | If we now set <code>AddPos</code> ... | ||
<!--T:17--> | <!--T:17--> | ||
<syntaxhighlight lang="c#"> | <syntaxhighlight lang="c#"> | ||
myParticles. | myParticles.AddPos = new Vec3d(0.5, 0.5, 0.5); | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Line 72: | Line 73: | ||
<!--T:21--> | <!--T:21--> | ||
It follows the same way as position. You can configure a minimum velocity (<code> | It follows the same way as position. You can configure a minimum velocity (<code>MinVelocity</code>) and an additional velocity (AddVelocity). | ||
<!--T:22--> | <!--T:22--> | ||
<syntaxhighlight lang="c#"> | <syntaxhighlight lang="c#"> | ||
myParticles. | myParticles.MinVelocity = new Vec3f(0, 3, 0); | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Line 95: | Line 96: | ||
<!--T:28--> | <!--T:28--> | ||
<syntaxhighlight lang="c#"> | <syntaxhighlight lang="c#"> | ||
myParticles. | myParticles.LifeLength = 0.2F; | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Line 107: | Line 108: | ||
<!--T:31--> | <!--T:31--> | ||
<syntaxhighlight lang="c#"> | <syntaxhighlight lang="c#"> | ||
myParticles. | myParticles.LifeLength = 1F; | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Line 120: | Line 121: | ||
<!--T:35--> | <!--T:35--> | ||
<syntaxhighlight lang="c#"> | <syntaxhighlight lang="c#"> | ||
myParticles. | myParticles.MinQuantity = 3; | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Line 134: | Line 135: | ||
<!--T:39--> | <!--T:39--> | ||
<syntaxhighlight lang="c#"> | <syntaxhighlight lang="c#"> | ||
myParticles. | myParticles.MinQuantity = 0; | ||
myParticles. | myParticles.AddQuantity = 100; | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Line 151: | Line 152: | ||
<!--T:44--> | <!--T:44--> | ||
<syntaxhighlight lang="c#"> | <syntaxhighlight lang="c#"> | ||
myParticles. | myParticles.Color = ColorUtil.ColorFromRgba(255, 0, 0, 255); | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Line 163: | Line 164: | ||
<syntaxhighlight lang="c#"> | <syntaxhighlight lang="c#"> | ||
Random rand = new Random(); | Random rand = new Random(); | ||
myParticles. | myParticles.Color = ColorUtil.ColorFromRgba(rand.Next(0, 255), rand.Next(0, 255), rand.Next(0, 255), 255); | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Line 169: | Line 170: | ||
This will spawn particles with a random color ... | This will spawn particles with a random color ... | ||
[[File:Particles (random colors).gif|300px]] | [[File:Particles (random colors).gif|300px]] | ||
=== Gravity === <!--T:56--> | === Gravity === <!--T:56--> | ||
Line 200: | Line 179: | ||
<!--T:58--> | <!--T:58--> | ||
<syntaxhighlight lang="c#"> | <syntaxhighlight lang="c#"> | ||
myParticles. | myParticles.GravityEffect = 0.1F; | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Line 216: | Line 195: | ||
<!--T:63--> | <!--T:63--> | ||
<syntaxhighlight lang="c#"> | <syntaxhighlight lang="c#"> | ||
myParticles. | myParticles.MinSize = 0.1F; | ||
myParticles. | myParticles.MaxSize = 1.0F; | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Line 228: | Line 207: | ||
<!--T:66--> | <!--T:66--> | ||
<syntaxhighlight lang="c#"> | <syntaxhighlight lang="c#"> | ||
myParticles. | myParticles.MinSize = 0.1F; | ||
myParticles. | myParticles.MaxSize = 1.0F; | ||
myParticles.SizeEvolve = new EvolvingNatFloat(EnumTransformFunction.LINEAR, 2); | myParticles.SizeEvolve = new EvolvingNatFloat(EnumTransformFunction.LINEAR, 2); | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Line 246: | Line 225: | ||
<!--T:71--> | <!--T:71--> | ||
<syntaxhighlight lang="c#"> | <syntaxhighlight lang="c#"> | ||
myParticles. | myParticles.ParticleModel = EnumParticleModel.Quad; | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Line 259: | Line 238: | ||
<!--T:75--> | <!--T:75--> | ||
<syntaxhighlight lang="c#"> | <syntaxhighlight lang="c#"> | ||
myParticles. | myParticles.Color = ColorUtil.ColorFromRgba(rand.Next(0, 255), rand.Next(0, 255), rand.Next(0, 255), 130); | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Line 283: | Line 262: | ||
<!--T:82--> | <!--T:82--> | ||
<syntaxhighlight lang="c#"> | <syntaxhighlight lang="c#"> | ||
myParticles. | myParticles.MinVelocity = new Vec3f((float) (rand.NextDouble() - 0.5), 1f, (float) (rand.NextDouble() - 0.5)); | ||
myParticles.SelfPropelled = true; | myParticles.SelfPropelled = true; | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Line 293: | Line 272: | ||
<!--T:85--> | <!--T:85--> | ||
This property is useful for particles in water, which should disappear once they reach the surface (die in liquid). Of course this can also be used the other way round (die in air). | This property is useful for particles in water, which should disappear once they reach the surface (die in liquid). Of course this can also be used the other way round (die in air).<syntaxhighlight lang="c#"> | ||
myParticles.ShouldDieInAir = true; | |||
myParticles.ShouldDieInLiquid = true; | |||
myParticles.ShouldSwimOnLiquid = true; | |||
</syntaxhighlight> | |||
</translate> | </translate> | ||
{{Navbox/modding|Vintage Story}} | {{Navbox/modding|Vintage Story}} |
Revision as of 19:54, 24 February 2024
This page was last verified for Vintage Story version 1.19.3.
VintageStory offers a variety of different types of particles. This tutorial will explain you how to create simple particles and show you some of the possibilities. If you want to have an example of where to use them I either suggested you to the read about the block particles or about collectibles.
Spawn particles
So let's spawn some basic particles. I suggest to create a static field for your particle properties:
public static SimpleParticleProperties myParticles = new SimpleParticleProperties(1, 1, ColorUtil.ColorFromRgba(220, 220, 220, 255), new Vec3d(), new Vec3d(), new Vec3f(), new Vec3f());
Now we have the property, the only thing left to do is to set the position and spawn the particles into the world. I'm gonna use the OnHeldInteractStart
and *Step method inside a custom made Item class (you can read more about this in the Collectibles Tutorial):
public override void OnHeldInteractStart(ItemSlot slot, EntityAgent byEntity, BlockSelection blockSel, EntitySelection entitySel, bool firstEvent, ref EnumHandHandling handling)
{
handling = EnumHandHandling.Handled;
}
public override bool OnHeldInteractStep(float secondsUsed, ItemSlot slot, EntityAgent byEntity, BlockSelection blockSel, EntitySelection entitySel)
{
myParticles.minPos = byEntity.Pos.XYZ.Add(0, byEntity.LocalEyePos, 0).Ahead(1f, byEntity.Pos.Pitch, byEntity.Pos.Yaw);
byEntity.World.SpawnParticles(myParticles);
return true;
}
This will spawn some white cube particles right in front of the player, which simply fall to the ground.
Properties Overview
There are several things you can configure. Here is an overview which covers all of the properties:
Position
Basically there are two properties. One of them is the MinPos
property, which will determine the exact position where particles will spawn. AddPos
is relative to this position, once a particle will spawn it will be randomized and added to the particle's position. If AddPos
is not defined all particles will spawn at the exact some position.
Same position:
If we now set AddPos
...
myParticles.AddPos = new Vec3d(0.5, 0.5, 0.5);
... particles will spawn in an 0.5³ area:
Velocity
It follows the same way as position. You can configure a minimum velocity (MinVelocity
) and an additional velocity (AddVelocity).
myParticles.MinVelocity = new Vec3f(0, 3, 0);
In this case all particles have the same velocity (particles fly in the air but will eventually fall down again):
Life length
This property is pretty straight forward. A life length of one equals one second.
Particles will exist for one 200 milliseconds ...
myParticles.LifeLength = 0.2F;
Particles will exist for one second ...
myParticles.LifeLength = 1F;
Quantity
Determines how many particles will spawn.
myParticles.MinQuantity = 3;
This will spawn 3 particles each tick.
Furthermore you can specify an additional quantity chance ...
myParticles.MinQuantity = 0;
myParticles.AddQuantity = 100;
This will spawn 0 to 100 particles per tick.
Color
A color can be specified using ARGB (Alhpa, Red, Green, Blue). All values have a range of 0 to 255.
myParticles.Color = ColorUtil.ColorFromRgba(255, 0, 0, 255);
This will spawn red particles ...
Random rand = new Random();
myParticles.Color = ColorUtil.ColorFromRgba(rand.Next(0, 255), rand.Next(0, 255), rand.Next(0, 255), 255);
This will spawn particles with a random color ...
Gravity
Gravity effect can not only be increase or decreased, ...
myParticles.GravityEffect = 0.1F;
... but you can also disable it completely (using zero gravity) or invert it (particles will fly up).
Size
Again, you can specify a minimum size and an additional randomized size ...
myParticles.MinSize = 0.1F;
myParticles.MaxSize = 1.0F;
Furthermore you can specify a SizeEvolve
like so ...
myParticles.MinSize = 0.1F;
myParticles.MaxSize = 1.0F;
myParticles.SizeEvolve = new EvolvingNatFloat(EnumTransformFunction.LINEAR, 2);
There are many EnumTransformFunctions you can play around with. Linear is the most basic one.
Model
There are two types of models. Cube
(the one we used so far) and Quads
:
myParticles.ParticleModel = EnumParticleModel.Quad;
Opacity
Quads support custom opacity. This allows you to make them transparent ...
myParticles.Color = ColorUtil.ColorFromRgba(rand.Next(0, 255), rand.Next(0, 255), rand.Next(0, 255), 130);
Similar to SizeEvolve
you can add an OpacityEvolve
...
myParticles.OpacityEvolve = new EvolvingNatFloat(EnumTransformFunction.LINEAR, -255);
Self Propelled
This property will make particles don't lose their velocity, even if they would collide with something, as soon as there is enough space they will continue to fly in the direction (with the same speed) ...
myParticles.MinVelocity = new Vec3f((float) (rand.NextDouble() - 0.5), 1f, (float) (rand.NextDouble() - 0.5));
myParticles.SelfPropelled = true;
Die In Air/ Liquid
This property is useful for particles in water, which should disappear once they reach the surface (die in liquid). Of course this can also be used the other way round (die in air).
myParticles.ShouldDieInAir = true;
myParticles.ShouldDieInLiquid = true;
myParticles.ShouldSwimOnLiquid = true;
Wondering where some links have gone?
The modding navbox is going through some changes! Check out Navigation Box Updates for more info and help finding specific pages.
Modding | |
---|---|
Modding Introduction | Getting Started • Theme Pack |
Content Modding | Content Mods • Developing a Content Mod • Basic Tutorials • Intermediate Tutorials • Advanced Tutorials • Content Mod Concepts |
Code Modding | Code Mods • Setting up your Development Environment |
Property Overview | Item • Entity • Entity Behaviors • Block • Block Behaviors • Block Classes • Block Entities • Block Entity Behaviors • Collectible Behaviors • World properties |
Workflows & Infrastructure | Modding Efficiency Tips • Mod-engine compatibility • Mod Extensibility • VS Engine |
Additional Resources | Community Resources • Modding API Updates • Programming Languages • List of server commands • List of client commands • Client startup parameters • Server startup parameters Example Mods • API Docs • GitHub Repository |