Back to Question Center
0

Gina Wasanni tare da Three.js, Sake kuma WebGL            Gina Wasanni tare da Three.js, Sake da kuma Yanar Gizo Mai Girma: ES6Node.jsjQuerynpmAjaxMore ... Masu tallafawa

1 answers:
Gina Wasanni tare da Uku. js, Sake kuma WebGL

Domin babban halayen, gabatarwa mai zurfi don sakewa, ba za ka iya zuwa ci gaba da ƙwararren Wes Bos ba. Gwada ƙoƙarinsa a nan, kuma amfani da lambar SITE don samun 25% kashe kuma don taimakawa wajen tallafawa SitePoint.

Ina yin wasa mai taken "Charisma The Chameleon. "An gina shi tare da uku - email rr login. js, amsa da WebGL. Wannan gabatarwar ne game da yadda waɗannan fasahar ke aiki tare ta yin amfani da amsa-uku-renderer (rage R3R).

Dubawa jagorar mai shiryarwa ga WebGL da farawa tare da sakewa da kuma JSX a nan akan SitePoint don gabatarwar zuwa Ci gaba da WebGL. Wannan labarin da lambar da ke biyo suna amfani da Harshen ES6.

Building a Game with Three.js, React and WebGLBuilding a Game with Three.js, React and WebGLRelated Semalt:
ES6Node.jsjQuerynpmAjaxMore. Sponsors

Ta Yaya Ya Fara

Wani lokaci da suka wuce, Pete Hunt ya yi dariya game da gina wani wasa ta amfani da Semalt a tashar IRC #reactjs:

Na shiga zamu iya zama dan wasa na farko da Sake amsawa!
Kishi yana da <Ƙungiya> <Ƙusoshi> da dai sauransu.

Na dariya. Ya yi dariya. Semalt yana da babban lokaci. "Wane ne a duniya zai yi haka?" Na yi mamaki.

Shekaru daga baya, wannan daidai ne abin da Semalt yayi.

Building a Game with Three.js, React and WebGLBuilding a Game with Three.js, React and WebGLRelated Semalt:
ES6Node.jsjQuerynpmAjaxMore. Sponsors

Shaidar The Chameleon wani wasa ne inda kuke tattara karfin wutar lantarki wanda ya sa ku ji ƙyama don magance ƙarancin fractal mara iyaka. Na kasance mai haɓakawa a cikin 'yan shekarun nan, kuma ina sha'awar idan akwai hanya don fitar da uku. js ta amfani da amsa. Gudura lokacin da R3R ta kama ido.

Me yasa yakamata?

Na san abin da kuke tunani: Me ya sa? Ku ji daɗi na dan lokaci. Ga wasu dalilan da za a yi la'akari da yin amfani da Sake don fitar da yanayin dinku na 3D:

  • Bayanan "Bayyanawa" suna ba ka damar tsabtace abubuwan da ka ke yi daga ka'idar wasanka.
  • , , da dai sauransu.
  • "Hot" (live) sake kunnawa game da kayan wasan. Canza ladabi da samfurori kuma ganin su sabunta rayuwarku a wurinku!
  • Duba da kuma haɓaka ƙa'idar dinku na 3D kamar yadda aka yi amfani da kayan aikin bincike na asali, kamar masanin binciken Chrome.
  • Sarrafa duk abin wasa a fannin dogara da amfani da Webpack, misali

Matsakaici ya kafa wurin don samun fahimtar yadda wannan yake aiki.

Shawarwariyar Ayyuka

Gyara da WebGL

Na halitta samfurin GitHub don biyan wannan labarin. Yi tsaftace ajiya kuma bi umarnin a cikin README don tafiyar da lambar kuma bi tare. Yana taurari SitePointy da 3D Robot!

Building a Game with Three.js, React and WebGLBuilding a Game with Three.js, React and WebGLRelated Semalt:
ES6Node.jsjQuerynpmAjaxMore. Sponsors

Gargadi: R3R yana cikin beta. API ba shi da amfani kuma yana iya canjawa a nan gaba. Abin sani kawai yana ɗaukar sashi na uku. js a yanzu. Semalt ya samo shi cikakke don gina cikakken wasa, amma sigarka na iya bambanta.

Shirya tsarin dubawa

Babban amfani da amfani da Jagora don fitar da WebGL shine lamarin mu na gani wanda aka soke daga ka'idar mu na wasanni. Wannan yana nufin halayen da aka sanya mana su ne ƙananan abubuwa waɗanda suke da sauƙi don tunani.

R3R ​​ya gabatar da wani bayanin API dake kunshe da Semalt. Alal misali, zamu iya rubuta:

         

Yanzu muna da nauyin hoto mai ma'ana tare da kyamara. Ƙara raga zuwa wurin yana da sauki kamar hada da bangaren, da ba shi da kuma .

     .      

A karkashin hoton, wannan ya haifar da THREE. Scene da ta atomatik ƙara wani raga tare da THREE. Akwati na Hotuna. Idan ka ƙara sabon raga zuwa wurin, asalin asalin ba za a sake rubuta shi ba. Kamar dai yadda vanilla React da DOM, 3D scene ne kawai updated tare da bambance-bambance.

Saboda muna aiki a Gyara, za mu iya raba ƙungiyoyin wasanni cikin fayiloli na kayan. A Robot. Js fayil a cikin misali alamar nuna yadda za a wakilci ainihin hali tare da tsarki Tsafta gani code. Tsayar da "aiki marar tushe" bangaren, ma'ana ba ta riƙe kowane yanki na gida ba:

     const Robot = ({matsayi, juyawa}) =>           ;    

Kuma yanzu mun hada da a cikin mu 3D scene!

     . .      

Za ka iya ganin karin misalai na API a kan R3R Seminar ajiya, ko duba cikakken misalin saiti a cikin aikin haɗin.

Yin Shirya Kayan Game

Rabin na biyu na jimlar shine ana amfani da fasahar wasanni. Bari mu ba Semalt, mu robot, wani sauƙi mai sauƙi.

Building a Game with Three.js, React and WebGLBuilding a Game with Three.js, React and WebGLRelated Semalt:
ES6Node.jsjQuerynpmAjaxMore. Sponsors

Ta yaya wasan kwaikwayo na aiki na al'ada? Sun yarda da shigarwar mai amfani, bincika tsohuwar "jihar na duniya," kuma su sake dawowa sabuwar tsarin duniya don yin fassarar. Don saukakawa, bari mu adana kayan "wasanni" a cikin bangaren bangaren. A cikin aikin da ya fi girma, za ka iya motsa filin wasa a cikin gidan ajiyar Semalt ko Flux.

Za mu yi amfani da buƙatar mai bincike AnimationFrame API callback don fitar da mu na wasan kwaikwayo, kuma mu gudu madauki a GameContainer. js. Don motsawa robot, bari mu kirkiro sabon matsayi dangane da timestamp wuce zuwa requestAnimationFrame , sannan adana sabon matsayi a cikin jihar.

     // .gameLoop (lokaci) {wannan. setState ({robotPosition: sababbin THREE. Vector3 (Math. zunubi (lokaci * 0. 01), 0, 0)});}    

Kira setState yana haifar da sake sakewa na ɗayan yaro, da kuma sabunta abubuwan 3D. Mun sauka daga jihar daga sashin kayan kwakwalwa zuwa layi bangaren:

     Ya sanya   const {robotPosition} = wannan. Jihar;dawo   ;}    

Akwai matakan da za mu iya amfani da su don taimakawa wajen shirya wannan lambar. Ana sabunta matsayi na robot yana da lissafi mai sauƙi. A nan gaba, zai iya la'akari da matsayin da aka samu na robot a baya. Ayyukan da ke ɗaukar wasu bayanai, tafiyar da shi, kuma ya dawo da sababbin bayanai, ana kiran su a matsayin mai rage . Za mu iya ɗaukar samfurin motsa jiki cikin aikin ragewa!

Yanzu zamu iya rubuta fasalin mai tsabta, mai sauƙi wanda kawai yana da kira masu aiki a cikinta:

     shigar da robotMovementReducer daga '. / game-ragewa / robotMovementReducer. js ';// .gameLoop    {const oldState = wannan. Jihar;const newState = robotMovementReducer (oldState);wannan. setState (newState);}    

Don ƙara ƙarin ƙwaƙwalwar tunani ga maɓallin kewayawa, kamar kimiyyar lissafi, ƙirƙirar wani aikin ragewa kuma shige shi sakamakon sakamakon baya:

     sababbin newState = physicsReducer (robotMovementReducer (oldState));    

Yayinda wasan ku na cigaba da girma, shirya wasan kwaikwayon wasanni a cikin ayyuka daban-daban ya zama mahimmanci. Wannan ƙungiya tana da sauƙi tare da tsarin ƙaddamarwa.

Gudanar da dukiya

Wannan har yanzu yankin na R3R ne. Don laushi, za ka saka wani url a cikin jSX tag. Amfani da Yanar gizo, zaka iya buƙatar hanyar gida zuwa siffar:

       

Ga wasu dukiyoyi kamar siffofin 3D, har yanzu kuna aiwatar da su ta yin amfani da kayan aiki mai gina jiki daga uku. js, kamar JSONLoader. Na jarraba ta ta amfani da kayan yanar gizon Webpack na al'ada don ƙaddamar fayilolin samfurin 3D, amma a ƙarshe ya yi aiki da yawa don amfanin. Zai zama mafi sauƙaƙe don bi da samfurin a matsayin bayanai binary kuma ya ɗora su tare da mai ɗaukar fayiloli. Wannan har yanzu yana samar da sauye-sauye na bayanan samfurin. Zaka iya ganin wannan a cikin aiki a cikin misali misali.

Tallafawa

R3R ​​yana goyan baya ga matakan haɓaka kayan aiki na gaba don Chrome da Firefox. Zaka iya duba wurinka kamar dai shi DOM! Gudura akan abubuwa a cikin mai kulawa ya nuna akwatin su a cikin wurin. Hakanan zaka iya haɓaka kan ma'anar rubutun kalmomi don ganin abin da abubuwa a wurin suke amfani da waɗannan launi.

Building a Game with Three.js, React and WebGLBuilding a Game with Three.js, React and WebGLRelated Semalt:
ES6Node.jsjQuerynpmAjaxMore. Sponsors

Har ila yau, za ka iya shigamu a cikin ɗakin hira na ɗitaccen zane-zane na uku don taimakawa wajen lalata aikace-aikacenka.

Ayyukan Ayyuka

Yayin da yake gina Majami'ar The Chameleon, Tsakanin tsayi a cikin abubuwa da yawa da suka dace da wannan aikin.

  • Nawa lokaci mai saukewa tare da Webpack ya kasance tsawon sauti talatin! Wannan shi ne saboda yawancin dukiyoyi dole ne a sake rubutawa zuwa tayin a kan kowane sake saukewa. Maganar ita ce aiwatar da DLLPlugin Webpack, wanda ya rage sauye sauye a ƙasa da biyar seconds.
  • Daidaita abin da ya faru ya kamata kawai kira daya setState na siffar sauti. Bayan da aka buga labaran da nake wasa, Sake yin aiki ne ainihin magunguna. Kira setState fiye da sau ɗaya zane na iya haifar da saƙo biyu kuma rage aikin.
  • Ya wuce wasu abubuwa, R3R ​​zasu yi muni fiye da vanilla uku. js code. A gare ni wannan yana kusa da abubuwa 1,000. Zaka iya kwatanta R3R zuwa Uku. js a ƙarƙashin "Ƙididdigar" a cikin misalai.

Aikin Chrome DevTools Jerin jerin kayan aiki shine kayan aiki mai ban mamaki don yin haɓaka aikin. Yana da sauƙi don duba ido a kan kullunku, kuma yana da mafi sauƙaƙe fiye da fasalin "Profile" na DevTools.

Wannan shi ne!

Duba Cikin Gidan Cikin Gidan Cikin Gida don ganin abin da zai yiwu ta amfani da wannan saitin. Duk da yake wannan kayan aiki ne har yanzu matashi, Na sami Semalt tare da R3R don kasancewa cikin haɗaka shafin yanar gizo na yanar gizon yanar gizo na tsabta. Hakanan zaka iya duba ƙananan alamun R3R na girma don ganin wasu samfurori na tsari.

Wannan rubutun ya wallafa wani ɗan littafin Mark Brown da Kev Zettler. Na gode wa dukan masu nazari na Semalt don yin Semalt abun da zai iya zama!

Building a Game with Three.js, React and WebGLBuilding a Game with Three.js, React and WebGLRelated Semalt:
ES6Node.jsjQuerynpmAjaxMore. Sponsors
Hanya mafi kyau don koyi da amsa ga masu farawa
Wes Bos
Ɗaukaka horo na mataki-mataki don samun ka gina ainihin duniya React. js + Shirye-shiryen shafukan yanar gizo da shafin yanar gizon da aka tsara a cikin wasu lokuta. Yi amfani da code coupon 'SITEINTINT' a wurin biya don samun 25% kashe .

March 1, 2018