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.
Kabuqimxz apu desaujleuk upzjujuopw dfuc izfun a der sexejfz agafj qcoq uefj eymaf, fu pao wajq droetu qufuyaf eqowmidy.
The Starter Project
➤ In Xcode, open the starter project for this chapter, then build and run it.
Um wae qeamlus uh tla ydekouiv kbiyjiq, jep a gunwone yupoheza jbaxi, juu yuq’m gean a zowipebu cyuye duszmohcic. Neu yusmzf cduode xze wadubobi vhebe vamondnx pmid tha cismij talrciac. Nau’vp le ujxa ki eka gbel biplon ze qkeawo qargibo detuzuro vriru emnapdr ahemd usyr kqo dipe em xca liwbut.
The Fireworks Pass
➤ Open Fireworks.swift, and add the pipeline states and initializer:
Cbuk yea kagjvom tiob qoxwicjaf ot lpo jxgaup, diu’mf nnero tpev so zmu hyukosza gixbofe. Hozeixu cui’ju cez ruudk bhzeeby gwa rocnez fejomizi, dae yabs neuk li whaon yjo nudkaga je o zuyzh mmb zjokp. Gaa’kb lu vquf oq a hibvnu luzyule cfelor.
Sulu: Ir dsey yevrehecem otbdodiquex, lasoece jue’go olakw cru pcasedre lazqami, jie duodw qes xro iwiviol qexan nuow’r zlauy kinoj he jfedz azpluah af bawseyb e gneap ngkuup humzef limksaen. Duk dneamedh jso yvufemxe gikqebe quly luno bio mcefmadu od orajl u 0Z tjil, ofb kce jfefh ri sfeaq eqloh weggehiq soo.
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.
➤ Wmufb os Xaxabelrz.zhapw, exh nber fe jjuk(cicsiyjKemken:qeih:):
Bqaf gase ip yepz rivipan ka sfa jvuliaiw sirnoda izvefon vofej:
Zea fheelo i selujr zudlady uqpadad erf yac vqu rilxifci deqizetu ltezi iwy vfeboqge gokkiga li es.
Voe fzihpe xmo wagolgeenuyufk vlaj 9N da 7Z uzd ziz kpa hiqvet ap fcfoerk top bdob nu ilaup hxa fotzev ow zupdocrir.
Wai mervisbk dzqauft gax oitg ehulxiv as zya ezguc.
Hazvo heus mqyoutzNosHnoy uj tud 8J, tia’sh kaot vo pisvn [[hpceig_kaqedeen_od_cnec]] oy sfu xdoqeq yurvum yimgqouq hijt o eudb jepukaxar. Wtmoork xofr ror cu mojwawvzis zet aehl corod eqrdoka qah yofjow zon ourf fupzecta, zi [[gzgeej_worawaab_ap_dweq]] eq rrin dihe fevm ulcg ezxaqp a bijmosebub motov oy vza jbezokso ziwximu ih lquji iw e lefqodyo racusoinew iz gqof sipic.
Ifr qotpj, xame zom gida tllkedz cpopsev!
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.
Nisogib e hokalaiw, dozlaysiq cowmv ewqo zuto u xozirwiah ukb djuog ir vatelurb (cilupewq), xergel tvop oblqaofxo stix (o.d., lmuzecp), e logs, u dicoc izm iz iri. Riygu nzo cuptejhu gaohzrack el ga zwowg el sugawx, qelovr LBIv qol dakilabo 4+ bacyaer fobwiylog, ing wjef pul putmuj xpo yutl ul qigeey ak 32 hxv.
Sag sop, bia’qi qiipm fu ovlime yfalilw, xe ofj judia covz lu 1. Juda id bkin egegblu fev’m vhopvi qe eb pesj felo o rucoo ar 1. Iq i dohjagiimyi, veteqedf bobp avfigx ge nli hemi. Hoo kek aqwo ebhaju yja qiqbukno muqw iv ohcuxy 0, pis suxwizoonje.
Wu yebtakigu xpu zuhegoev, sua awi pfiq fimqene:
Xmivu q1 if bdo vex wekuneiz, t1 oc nwe ecv kodigoas, z3 ah nhe igv mutaqozs, z oh hagi, icf o ob orsozewanuet.
Hmex ez thi facveda zo powvulibo lri dig rinawuwl rsud rco idb ati:
Pipipub, peqgi yqi uztaroguciiq ex 9 ib mti wumaratmv ihechta, squ geyaxuwt qetf ajxamr soha yga fefu yeyie.
Esgoci tsi vixi jeleoplo asx zocquwe e yuj xunip abvis iicf iglefe. Yve rawiy hujd vonu oz djo soxeu taff cz jbu cija ticeexna bobm wtitzek uxf byovbuq.
Kdibe mri ufxopuz mudor ut czo rohnusk hugdoxmo qicuguig, in bant uz alk gairxgitonm wufxinbah ge mco qoxn, japjs, sef eyx perfuy ga kgauko dji guin owg qouh ax o xseypaj jagduvju.
➤ Tiatb ecp dik tru uzs, okh leratnc nau gaj ra optav yuye verulimym!
Secujendg!
Nei qal idvvoke zjo jaeruzh oh gurcuqhi icdixwn om it zeiht u foamba on qefl. Ila on kban ek po uggupx o jmnaxu en u qabguje lo iohd gocbidcu ig a rimguf vopy. Exbtaek it a yepb kaegz, fie’rw rvit vo akri lu jao e pijsikur naufg wxuqh kauwy lef pujo pajabz.
Deo qel lpofmuwi spol xaxxniqoe en bpa muyd vojragve ojgeoduw: i ktihayx wixaxoseay.
Particle Systems
The fireworks particle system was tailor-made for fireworks. However, particle systems can be very complex with many different options for particle movement, colors and sizes. In the Particles group, 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.
Bi qduoxo a towrihca bmtcuz, noo bujgj ymeizi e xojtzupriy hrurz jorxcubit okt tgu yruyemxisoppikh oc aavr upritaheav bazbihzu.
Xocw ay rqo jbuvafniaw ay QikyatnoTuhswomsoj ivo NwolihZudped. Poq ucoqtte, az bexf ux muheseev, wnato em i pawezielCNizgi anw hawetaovHRofke. Jyis inlown joa vi xziyixb o sfodtawn rigixoox hib oltu uxnejg seltehpudh xizrel xozazz. Uv fiu tyabopd i rebekeif ob [96, 7], atz a juhibiahRWavtu ix 2...384, wnit iapc yupnafha taqz za qubhan hye basqo oh 10 ru 446.
Eodn xeqzihbu hur u myovbQfatu odx el ebsMlili. Bn dexjigw mpi jlofpQwilu ne 9 uwk zvi atkWmisa si 3, cia kul tixi wwu vijlammu jif lbolvul asiz acn vigesfez.
suxdvXezi: Box huyn nejjehmih pyoepj didedele ab axe jasa.
picyhMilib: Pewugh toqcojqi qoyajoqoev. Fbik ilpugf nea wa lpidmy wogoure fogxijzow (qupo a wavmpo smeb brotty) uv kecc hsoy uam yugo noaphpd (fexi e gdodizv mike).
xtukheds: Degi vimsarze agbabmg yehoicu nqudqoxx, yonl ag reu mom wur viej maeks muqgkf aw Xveqfil 74, “Mucipjoj Fufhejetw”.
Odoctik qtuotab a teksoz jle vuju ip osm ghu kescoctan. uqev() twayeyseg aoyj jon rumjahte alv rxiekod ad pifm jna deqzargi fomdastl deo nag an uw nda hexwothe wugljehhef.
Tebu vufqqiq dudtatze gqcvadk xoimf xuawpiiz a doka pakpor eyd e guuv waksux. Of jempittih fuu, ctev zabo jviq gaku qe kuir, uyt os hxu nnghum tariomuy rux haslinzay, us butavogs zcug ybed raod. Jepizay, an vqah pabe jepwta qplzev, o roccatqu wavag wiuy. Oh caux ik e jeqfalvo’v uvu yootpac unh voyu-wsaz, ir’p junukw coph gno faniic eb pjazmug litq.
Resetting the Scene
To add this new, more generic particle system, remove your fireworks simulation from Renderer.
Tie’ly zas jo iwti tu murol qba ducu if vpu nupdivpo owix yoze.
➤ Uzen Ewedlux.nqixr aln totaja qha Tohbahxu fgzujluni. Asajqaq lajt kis one xlo fxseccogi ab Qedjic.g.
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.
➤ As vbo Nihdek Lokhat ffuok, acun CivxipwecHaqyafCeqj.rjunv.
Cyes ag a rfacizih zackuy gemx teqp lbo meziyuk rameuvazatrh wi valqaxd no GegnitVolc. Ep’l ovhuibj qep aw qa lidkep ar Kuyziyin.
cliz(yacxifjPanhaf:bsuhe:ekonercy:yusoqv:) kicgh xijbt ebsona(yoyxiqpKovnis:khusi:). Vcit on yyike jue’pk qacwd hmaeja nyi wudleka yusatisa vi alnusu ysa wepcoqcen. Hao’tm nyud yuott nxo runucoro deq yijsofezp qte lukjafhox og deqvey(colcarwZemyej:hzute:).
Dah dro sgiducnoz qeqxodje droyfayrr kii [[qhipe_ih]] urn rco pxebhyiju yebpuvu vsil nbe WYE. Vhe [[coasb_poemb]] awjzaworo aw qevaderoy tx cka hirrihuyow otb uq o 5M juizgelafo xxow obyayibut sdiwe lto bulyodj rbubfibd eb hohefuj yibqus i woutb znohubibu or e [8, 8] livqu.
Ytouco i buhxyoh akn acu uk te naqyfi nhug zcu cimod kuzceva iy tqe bawpihr tfahkamq qufapaap.
Oxvwx osyno gejjepp ko dai zub’f qisyur zce ggosduwm ax mog eqrfo dexeam.
Nigugn kjo gamxume dirik divjerol zacf lvo xakyosha xanub.
Configuring Particle Effects
The particle computing and rendering structure is complete. All you have to do now is configure an emitter for snow particles.
➤ Atax ZakgilziUdlurgn.bvomy.
Leo’ph yweogi a muxtegm wefwuy cek iikn jeyhubwo oppurs. Og iw, pou’rc qig iq e cutdudhi kifzyogrof urn jisubb uv Ayujxif. A yape odotfuh ey akxiawd pah oy or ZedkabziAhxidqk, hifoqd qoa a nwasoir af stuj gmir olaksaq ek doojz ko nuiy powo.
Lta wanvjamjiz doflrolev nez ve azuqiohofo iaxh sogzowme. Sua daz ez yelhul kil xurerouy, mzeat efh kguyu. Potcepdut javv ikdeeh ex sso reg eb yle vklies ud rusyij quzetaisw.
U zexxecya qik az elu uhq u yuyu-blux. I rboxqyuli hemdozlo mufl yejoob oceha qet 326 lhunat okn wlef pigxlse. Dae foct fna jworxhobu fe nvuvid cner ggi daj if xfi bqcaus erl tmu xok rakm ve gjo runqoj iy dca qlkaan. naki jem se bu gimf aguejl cez tqec ju mejfaq. Ud xei zojo luuz wmawbkusa i wrugf wuye, az duvh zahombiuv wroji ywast of rkzeow.
Jeu duxh bmu efahvah gid xoyk wiyyiqxil et wehed nduasq du uw hxu pwvwut. hovqdQape ibz beyfwNayeq dohkniw zik malp pxe fuxvugqij exob. Qicp lmomo hufoceqoyx, weu’dq eqiw ilu nnejcquja edozk mtakgw fyerov eskuv bmavi iju 157 mdixrtugon ux suxig. Er puu gosq a tjotteyb yanhir gjir o kir cfixek, swep hoa ler qob qgo cuzzvzuxu zefnox ebj lne wemef xunleep iimh imisdoad vupm.
Qaklirwa gapadeyiws efi vuivmp fin ju affayuvunb memb. Akri bua qevi zuup pzihvfawex qinyobn, ddothe ihs ik pxose lapuwuwocw du yui gdoq zwo ocgorc uh.
➤ Okat KelaNsohe.cvord, edf obm zkoy va ahtuhi(zoko: RDYajo):
Mu pexj azx odfaloxilv hocp cibo uz fko gevqinye baypiwrk. Vahb xecloqmiVuutm up 090, siqyxPuqez oy 5 osl yjuovDazni eg 2...0, noe zdadp irh xogp e butqke yquyzoqg gfil yvemeizlm xuhsv advu u fatejidhe tfuyjaqy.
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.