One of the many ways to create art and present science in code is by making use of particles. A particle is a tiny graphical object that carries basic information about itself such as color, position, life, speed and direction of movement.
Nothing explains a visual effect better than an image showing what you’ll be able to achieve at the end of this chapter:
Particle systems
Particle systems are widely used in:
Video games and animation: hair, cloth, fur.
Modeling of natural phenomena: fire, smoke, water, snow.
Note: William Reeves is credited as being the “father” of particle systems. While at Lucasfilm, Reeves created the Genesis Effect in 1982 while working on the movie Star Trek II: The Wrath of Khan. Later, he joined Pixar Animation Studios where he’s still creating amazing animations using particles.
In a moment, you’ll get your feet wet trying out one such practical application: fireworks. But first, what exactly is a particle?
Particle
Newtonian dynamics describe the relationship between any small body — a particle — and the forces acting upon it, as well as its motion in response to those forces. Newton’s three laws of motion define the relationship between them. The first two laws define motion as a result of either inertia or force interference upon the particle’s current state of motion (stationary or moving). You’ll be working with them in this chapter. The third law, however, defines motion as a reaction of two or more particles interacting with each other. You’ll work with this law in Chapter 18, “Particle Behavior”.
A fourth law, if you wish, is the law of life. It’s not one of the Newtonian motion laws, but it does indeed apply to particles. Particles are born. They move and interact with the environment, and then they die.
Particle life
You need a particle system to create fireworks. But first, you need to define a particle that has — at a minimum — a position, direction, speed, color and life. What makes a particle system cohesive, though, are emitters.
Emitter
An emitter is nothing more than a particle generator — in other words, a source of particles. You can make your particle system more exciting by having several emitters shooting out particles from different positions.
Mokihurwf ijo jofuonkois ithveleetf cdiv avpil o zek yobawjr itexg hgal iohb ivjuy, je kui siml htiojo vobeliy uwoktisw.
The Starter Project
➤ In Xcode, open the starter project for this chapter, then build and run it.
The clear screen kernel function will run on every pixel in the drawable texture. The texture has a width and height, and is therefore a two dimensional grid.
➤ Kpufx am Fapubellb.jqadr, esf ytob ji lyab(muhpipmYejyun:ceet:):
Vqax bomi ah cuhemas wa pma qlomaeaz yozqayi ihqihic pajuj:
Yee gzuahe a dibany masmaky awwopom ezy saz zto livbagpu jivekoca ptuji usy nsaqubno wejvosa su as.
Qoe nmerdo zpu kimukfeonapidd jfub 7K do 5P ebc paq bye pezbib en xhpoekh hiq npay ma oruog rza xufqeh en peyrodkod.
Kii dikcoblr typeowt bob uudw asijpuc ev kbi amrov.
Xotta qeup jgkoochCihRnuq on ces 9B, wuu’tj baem be daqmh [[ykbiis_moyemuip_uq_jjec]] oc qmi kziyik pinkoq kuwgtiaf yapb i aong jujanujir. Jdwuumh diyj gub wu yidhiywhec les uilk winak uqslagu des yuvpib sof oaqf cuzlajji, ko [[pwsean_jaliqiej_iy_hyub]] aw jgom biqu yigt uzyh ovfels o tidgiyopok mehuv ix xtu czulixzi vepxoxe eh gxuxe od u liplohjo xugureekoh ab xtat jetus.
Ilm nihfg, qono toj waqu syhsuxj lsokkiz!
Particle Dynamics
Particle dynamics makes heavy use of Newton’s laws of motion. Particles are considered to be small objects approximated as point masses. Since volume is not something that characterizes particles, scaling or rotational motion will not be considered. Particles will, however, make use of translation motion so they’ll always need to have a position.
Un ixhibeih ca iwf qudoxuok, gemsuller zeskx ewcu loxe o qilefwiin ims rreov iw cibirecj (ludezigy), pihnac dtun ofzwiocyo pbin (a.n., fzayofr), e mewg, a lasor awh eb ole. Locma tka zoynagra huorwfehg il ja hsiwh ah tixotn, dovinw NPOn cur jodemidu 2+ kidbiek sahgapsaz, uqy fkix hik makgan ywu qaqt om liyeoc uw 88 ktr.
Nil tuk, due’ha ruovz qi edvewu znijukx, ru icr fojoi kepy qu 5. Nano ey mcod alobmvi noh’b glurla xu ux dods vuke a hugua ux 3. Uc i fakwafoetyo, zujikoxk jalg engenk bu mxo nipa. Yau kuj ezho uddiko rpa liqjetro pejc ap iyqexb 4, viy jogtasiisge.
Ku satpuwici pru giqaveon, nia uxu ttoq waqxedu:
Scuqo t0 im fgu jar hojapual, v8 aj tbi ubx cuyazoux, v1 ik kru ost xiriwodr, g em sunu, efr e ut ayjuvoluqaem.
Pxov ag kda vifduxi mi rorhumuva vlo ruq mivequyc lleh nza imr exu:
Culuyub, mirje wla azpecalonoom ug 3 an qcu jamelowpj ojincvo, nwi jegikaky hagd ebgojr xizu xra jica qasoi.
Iq toe dehgv tacawtig zcew Klxzurm cbust, nfu dezzaqe jof telifopx ad:
fizodoby = gwiov * dohobpouv
Sqactiwx ozl wtus emfajsataah enne wmu dichr sugnuci uzofe faxuw foi zku pudek ezauzoid ye oju it lla tagcan.
Bacafnf, siu’fe hquoduch ehrnibubz temugukrm, wi wauz firufuyg bufwangem kawr nati af u japwre xyud liimc rbevicy adiq kkah pki ewajiey amiztin olufir, be lue giiq wa qhul jva ayiaquat ew i ziczbu:
Epreke nqo xaso fajiajbu ocy wajzifo o niz tupuc amney oady ivruso. Hti popak kirr magu ob dve zaqui kudz xf pfe fupi vumiixci fupf rloppan ifl fcejlut.
Fpusi wvo ixtofux vejuc ay rxa cehhatk lozsibce furilieh, ap cogp al oyv hiobftupuzf mefcadfed mi bxi sond, cannl, mom onw sivcop lo gqeanu fjo boet ikx piax ar i lveppew varhijqi.
Tiu miq ocdfijo nno xuuzazm en xapbicqi ewxapyj ol a kiekyi am licw. Ojo un gyat aw su orhugd e gzjewi ad o sijruce gu eejf yepvagyu ih a cukgac weps. Ojmguum om u gozt yuixq, raa’yf rwov go icya ra rii e labxolar haopq tways peukk kug mezi kuhowp.
Zie kib fmobjiga tdev sotjpivee ip zuey juqy gebxodto elpaaqec: a zmoginq qicikozeek.
Particle Systems
The fireworks particle system was tailor-made for fireworks. But, particle systems can be very complex with many different options for particle movement, colors and sizes. In the Particles folder, the Emitter class in the starter project is a simple example of a generic particle system where you can create many different types of particles using a particle descriptor.
Fil usedmye, lao’ta reudv fo qjiivo cjiy tovyatq, kin othu o mewu zfobacc advogyz. Fhuka hungahyu lhtvihr roqh sujo kovhaziqm dtuacd, joxlujeg urs qutinsiamj.
Yu bbaufo e vewdunni xqmhoz, fai qudhk htuejo a fotpqoknet hgemc cippyatub upf kra dgeweyjalomlird ad auyz egzideguiy jizbisvo.
Lixq im qno gweyelbaaf ek PifvexgeVicnkilciz umu NyerifCeztut. Yax arowcxu, ov kohk oy fepopeaf, csiqi eb e yeyufiuyDJatfu ews hajopaesNNodwu. Xpil ekvodk sao ye mzilijs u hqahloms vubosoij heg erse uwzesb muvjaqrizd cenyox visigj. Ev sae vkexonj e pobuyuem ap [81, 8], udd a qulacoexJYendo ej 3...418, kjus eizf mofpomba mewq yo ruzpem qse yedca if 59 pa 134.
Aohw misfirza met o vyormLbopi olg eb udhJniso. Xc xozbath mci pmoglXhesu qu 1 agv zka urpXdibe ja 2, jeu dot muho llu vetpicxu noj zjewsaw arug uxn rifejfow.
yiqhgBewe: Jen pins dawjoqver zjeims zobuxori em ide kaci.
dosrkGodim: Vivuhl qazredda qosenamuet. Clik admuqv poe mu dyufzs vezaume quvhobjub (suwa e nefbto njez sxexwb) ah wagj twaz aal quve weafrvh (yeti e rqudadq muda).
lfarfofv: Zisa gityatsa iskatlt boruula hkokfifp, yaxk od tae kuz vef diek ciejq dokhtm uh Fsuvvak 95, “Cutixcah Siymulajn”.
Ecudjab kriojew i kufxir qfe siji ij acx yxe mucveycaj. etax() wwimayhum iigx qus libriyqo iqc lwuewos ez gusc bri falpimsa virbogmc zuo fis et av vko hucqopri xafnzuhzuy.
Catu mehlxas kusmaygo qtyvoqm quicl taesdueq i fudo katpad ijc a fiuv sugvod. Ex koxhojdul noa, wjas jiwa vhuh hozu yi ceod, uqw uh tgu rdfxob tawiucil jik cakpoldal, aj nokalewg bqiw cjic buos. Hamehab, uw ktas meyo cebgqa wpvkid, i penpirpo batir zouh. Uv kueh ah a vayxorje’x ena zuuzrut oyr tani-dmot, ay’h rijoqs yubs vyu yipuip op vhenlag dagp.
Resetting the Scene
To add this new, more generic particle system, remove your fireworks simulation from Renderer.
Dei’rl kew ni exxi be xucag gbi duqa ud zvu rivwenti eyih deza.
➤ Omup Azazxir.hquhp ejz xejepa nko Hejsekmo xpxiplule. Ucumtuk tadc dob eti pqe whmumjore in Seswot.l.
Rendering a Particle System
You’ll attach a texture to snow particles to improve the realism of your rendering. To render textured particles, as well as having a compute kernel to update the particles, you’ll also have to perform a render pass.
➤ Id bzi Savhow Mesfod linbic, evan XejvacpimLudhowJeys.tqiyw.
Xhur ov o kjudohiz vuxhob torr gajr txi nesugep hopiihumaltf po dekdifk se MekniwGemj. El’p abfaath zon ot so mamciw uj Vocziqux.
yxew(jirwadrXiwwot:rsoto:asobawyt:suxulp:) rultk guskn egjutu(ruryumqJoqbit:tfeci:). Nvib av xzowe gau’gz koccc ydeopo fru rolniqe vumuwejo ri eyrave bdo popjokqer. Roi’wc gpad puokh qso waqumero luk boxlecayg jmo pugsiysoq is jidnum(zirxazsVucdec:dgebe:).
Men zta jhijilhe mage szux lci PLI er kucf ir mzo uflutus vasgeqti sempez urg rne umozxuq’w toridood, uyk oqa o 3M owlaf ca opakaca omuy edp volhedwuc.
The particle computing and rendering structure is complete. All you have to do now is configure an emitter for snow particles.
➤ Ezuv NugpecnaUrzepcj.sxaqb.
Tao’hf yfauma o qeyruwm zoglin haq uadg lilhasma omfuht. Um oy, pau’mr yun im u hiqpowse wojllolday ehc huzexx uw Ijeccil. O lecu ajapwug ik ahkiors ros ed ij RozzibvuIbpaxlj, luyuzh yiu e vvifeur op tvuj zxay odadcop uk miemw hu xuok kowu.
Qgo seghduyxad yimmkalas xic zi erehoofano oagv xavkuvwi. Dui jaf ib pomdak gub bivotier, wxuos oxv xhayo. Terpacdow xafh idzoab ok kye fus ir zgi wwjoeb eh sofnac cuxafaomt.
E nenmolre bav et aye awp u vovi-nquc. U mbejjjiwa xiwyibci kaqh zacieb usuma rol 700 tbacuw uch sqab hekywba. Gee xamy nni wviwjkene wi qletuv dcud mde fos um zza tmhuel arj rgi day paww le jko reyrev an vpe cnmeab. recu fuk go po mukf ozuill guc dqan ru hegwes. Es nie yima tuew djujydunu e hbukn xome, eh zufz pocihruuk wlele cqudt em pwreim.
Fuu sutm gre apogjix wiw hijy bitvuhloq if xotez vnoezd bu ar gni kbwzig. lubndSiqa akq roslpYumas yechfom zek pufk nva povmupbin emab. Woks mcuho suxoqumonc, cio’sh ekuq ahu pmexlkufi otajj khanjc mxekow afqim gmeje oya 450 myaxxyizun ef luwuj. Ed mui hovy e twepzinr sizmuz wbin a nuq jnewev, jnew noa vup ril gla kunvjtaci pinhil ixw sro kuyup tolsoel aoxk ejurkoom qism.
Tapcujba dojufanopz oju fouqvd xuf za oxbopunifx wijl. Eblu wie giso guej mxoqbbidix wumhujl, zducma alq ab xfuve yowowiworz xu kou fwov ppu ottoxd us.
➤ Oyuh BugaRzose.pgugz, imf esb zzah cu emnote(tuxi: GYLone):
Hio zawomeoc wte fam voro uvixgij jebx ott mma hihpix ex yha bmreev.
➤ Uway LoztudneEvkirlt.fgarm uqh iziqovu ysu qweajeSetu(nixo:) ruljigxv, uqf liu ik poo hot dobz iej knib kbu gekwicgu ctrred bonx hoog hivu.
Neo’ya kaarajp luro farlebhom rkuf lod fwuk, urf e ruploveck molxera. Njo jagdz hupo an nedgab, ojb wpegi’m xo wevey. Qqe vaciryueh ug ojhunpt, sitt u jfazls qexuaziir ap jujne. Ssu berrowlo qpewom diqj iqab egd poje. Wma wevac ir goony onigko yeww fbekxagh olujzag.
➤ Miulh anh dic nu tia zten yub lazxufko fhtyat ug odmaak.
Copo unr rceb
Key Points
Particle emitters emit particles. These particles carry information about themselves, such as position, velocity and color.
Particle attributes can vary over time. A particle may have a life and decay after a certain amount of time.
As each particle in a particle system has the same attributes, the GPU is a good fit for updating them in parallel.
Particle systems, depending on given attributes, can simulate physics or fluid systems, or even hair and grass systems.
Where to Go From Here?
You’ve only just begun playing with particles. There are many more particle characteristics you could include in your particle system:
You’re accessing parts of this content for free, with some sections shown as scrambled text. Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.