| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609 |
- // gltfpack is part of meshoptimizer library; see meshoptimizer.h for version/license details
- //
- // gltfpack is a command-line tool that takes a glTF file as an input and can produce two types of files:
- // - regular glb/gltf files that use data that has been optimized for GPU consumption using various cache optimizers
- // and quantization
- // - packed glb/gltf files that additionally use meshoptimizer codecs to reduce the size of vertex/index data; these
- // files can be further compressed with deflate/etc.
- //
- // To load regular glb files, it should be sufficient to use a standard glTF loader (although note that these files
- // use quantized position/texture coordinates that require support for KHR_mesh_quantization; THREE.js and BabylonJS
- // support these files out of the box).
- // To load packed glb files, meshoptimizer vertex decoder needs to be integrated into the loader; demo/GLTFLoader.js
- // contains a work-in-progress loader - please note that the extension specification isn't ready yet so the format
- // will change!
- #ifndef _CRT_SECURE_NO_WARNINGS
- #define _CRT_SECURE_NO_WARNINGS
- #endif
- #ifndef _CRT_NONSTDC_NO_WARNINGS
- #define _CRT_NONSTDC_NO_WARNINGS
- #endif
- #include "../src/meshoptimizer.h"
- #include <algorithm>
- #include <string>
- #include <vector>
- #include <float.h>
- #include <limits.h>
- #include <math.h>
- #include <stdint.h>
- #include <stdio.h>
- #include <string.h>
- #ifdef _WIN32
- #include <io.h>
- #define popen _popen
- #define pclose _pclose
- #else
- #include <unistd.h>
- #endif
- #include "cgltf.h"
- #include "fast_obj.h"
- struct Attr
- {
- float f[4];
- };
- struct Stream
- {
- cgltf_attribute_type type;
- int index;
- int target; // 0 = base mesh, 1+ = morph target
- std::vector<Attr> data;
- };
- struct Mesh
- {
- cgltf_node* node;
- cgltf_material* material;
- cgltf_skin* skin;
- cgltf_primitive_type type;
- std::vector<Stream> streams;
- std::vector<unsigned int> indices;
- size_t targets;
- std::vector<float> target_weights;
- std::vector<const char*> target_names;
- };
- struct Track
- {
- cgltf_node* node;
- cgltf_animation_path_type path;
- bool dummy;
- size_t components; // 1 unless path is cgltf_animation_path_type_weights
- cgltf_interpolation_type interpolation;
- std::vector<float> time; // empty for resampled or constant animations
- std::vector<Attr> data;
- };
- struct Animation
- {
- const char* name;
- float start;
- int frames;
- std::vector<Track> tracks;
- };
- struct Settings
- {
- int pos_bits;
- int tex_bits;
- int nrm_bits;
- bool nrm_unnormalized;
- int anim_freq;
- bool anim_const;
- bool keep_named;
- float simplify_threshold;
- bool simplify_aggressive;
- bool texture_embed;
- bool texture_basis;
- bool texture_ktx2;
- int texture_quality;
- bool compress;
- bool fallback;
- int verbose;
- };
- struct QuantizationParams
- {
- float pos_offset[3];
- float pos_scale;
- int pos_bits;
- float uv_offset[2];
- float uv_scale[2];
- int uv_bits;
- };
- struct StreamFormat
- {
- cgltf_type type;
- cgltf_component_type component_type;
- bool normalized;
- size_t stride;
- };
- struct NodeInfo
- {
- bool keep;
- bool animated;
- unsigned int animated_paths;
- int remap;
- std::vector<size_t> meshes;
- };
- struct MaterialInfo
- {
- bool keep;
- int remap;
- };
- struct ImageInfo
- {
- bool normal_map;
- bool srgb;
- };
- struct ExtensionInfo
- {
- const char* name;
- bool used;
- bool required;
- };
- struct BufferView
- {
- enum Kind
- {
- Kind_Vertex,
- Kind_Index,
- Kind_Skin,
- Kind_Time,
- Kind_Keyframe,
- Kind_Image,
- Kind_Count
- };
- Kind kind;
- int variant;
- size_t stride;
- bool compressed;
- std::string data;
- size_t bytes;
- };
- const char* getError(cgltf_result result, cgltf_data* data)
- {
- switch (result)
- {
- case cgltf_result_file_not_found:
- return data ? "resource not found" : "file not found";
- case cgltf_result_io_error:
- return "I/O error";
- case cgltf_result_invalid_json:
- return "invalid JSON";
- case cgltf_result_invalid_gltf:
- return "invalid GLTF";
- case cgltf_result_out_of_memory:
- return "out of memory";
- case cgltf_result_legacy_gltf:
- return "legacy GLTF";
- case cgltf_result_data_too_short:
- return data ? "buffer too short" : "not a GLTF file";
- case cgltf_result_unknown_format:
- return data ? "unknown resource format" : "not a GLTF file";
- default:
- return "unknown error";
- }
- }
- cgltf_accessor* getAccessor(const cgltf_attribute* attributes, size_t attribute_count, cgltf_attribute_type type, int index = 0)
- {
- for (size_t i = 0; i < attribute_count; ++i)
- if (attributes[i].type == type && attributes[i].index == index)
- return attributes[i].data;
- return 0;
- }
- void readAccessor(std::vector<float>& data, const cgltf_accessor* accessor)
- {
- assert(accessor->type == cgltf_type_scalar);
- data.resize(accessor->count);
- cgltf_accessor_unpack_floats(accessor, &data[0], data.size());
- }
- void readAccessor(std::vector<Attr>& data, const cgltf_accessor* accessor)
- {
- size_t components = cgltf_num_components(accessor->type);
- std::vector<float> temp(accessor->count * components);
- cgltf_accessor_unpack_floats(accessor, &temp[0], temp.size());
- data.resize(accessor->count);
- for (size_t i = 0; i < accessor->count; ++i)
- {
- for (size_t k = 0; k < components && k < 4; ++k)
- data[i].f[k] = temp[i * components + k];
- }
- }
- void transformPosition(float* ptr, const float* transform)
- {
- float x = ptr[0] * transform[0] + ptr[1] * transform[4] + ptr[2] * transform[8] + transform[12];
- float y = ptr[0] * transform[1] + ptr[1] * transform[5] + ptr[2] * transform[9] + transform[13];
- float z = ptr[0] * transform[2] + ptr[1] * transform[6] + ptr[2] * transform[10] + transform[14];
- ptr[0] = x;
- ptr[1] = y;
- ptr[2] = z;
- }
- void transformNormal(float* ptr, const float* transform)
- {
- float x = ptr[0] * transform[0] + ptr[1] * transform[4] + ptr[2] * transform[8];
- float y = ptr[0] * transform[1] + ptr[1] * transform[5] + ptr[2] * transform[9];
- float z = ptr[0] * transform[2] + ptr[1] * transform[6] + ptr[2] * transform[10];
- float l = sqrtf(x * x + y * y + z * z);
- float s = (l == 0.f) ? 0.f : 1 / l;
- ptr[0] = x * s;
- ptr[1] = y * s;
- ptr[2] = z * s;
- }
- void transformMesh(Mesh& mesh, const cgltf_node* node)
- {
- float transform[16];
- cgltf_node_transform_world(node, transform);
- for (size_t si = 0; si < mesh.streams.size(); ++si)
- {
- Stream& stream = mesh.streams[si];
- if (stream.type == cgltf_attribute_type_position)
- {
- for (size_t i = 0; i < stream.data.size(); ++i)
- transformPosition(stream.data[i].f, transform);
- }
- else if (stream.type == cgltf_attribute_type_normal || stream.type == cgltf_attribute_type_tangent)
- {
- for (size_t i = 0; i < stream.data.size(); ++i)
- transformNormal(stream.data[i].f, transform);
- }
- }
- }
- void parseMeshes(cgltf_data* data, std::vector<Mesh>& meshes)
- {
- for (size_t ni = 0; ni < data->nodes_count; ++ni)
- {
- cgltf_node& node = data->nodes[ni];
- if (!node.mesh)
- continue;
- const cgltf_mesh& mesh = *node.mesh;
- int mesh_id = int(&mesh - data->meshes);
- for (size_t pi = 0; pi < mesh.primitives_count; ++pi)
- {
- const cgltf_primitive& primitive = mesh.primitives[pi];
- if (primitive.type != cgltf_primitive_type_triangles && primitive.type != cgltf_primitive_type_points)
- {
- fprintf(stderr, "Warning: ignoring primitive %d of mesh %d because type %d is not supported\n", int(pi), mesh_id, primitive.type);
- continue;
- }
- if (primitive.type == cgltf_primitive_type_points && primitive.indices)
- {
- fprintf(stderr, "Warning: ignoring primitive %d of mesh %d because indexed points are not supported\n", int(pi), mesh_id);
- continue;
- }
- Mesh result = {};
- result.node = &node;
- result.material = primitive.material;
- result.skin = node.skin;
- result.type = primitive.type;
- if (primitive.indices)
- {
- result.indices.resize(primitive.indices->count);
- for (size_t i = 0; i < primitive.indices->count; ++i)
- result.indices[i] = unsigned(cgltf_accessor_read_index(primitive.indices, i));
- }
- else if (primitive.type != cgltf_primitive_type_points)
- {
- size_t count = primitive.attributes ? primitive.attributes[0].data->count : 0;
- // note, while we could generate a good index buffer, reindexMesh will take care of this
- result.indices.resize(count);
- for (size_t i = 0; i < count; ++i)
- result.indices[i] = unsigned(i);
- }
- for (size_t ai = 0; ai < primitive.attributes_count; ++ai)
- {
- const cgltf_attribute& attr = primitive.attributes[ai];
- if (attr.type == cgltf_attribute_type_invalid)
- {
- fprintf(stderr, "Warning: ignoring unknown attribute %s in primitive %d of mesh %d\n", attr.name, int(pi), mesh_id);
- continue;
- }
- Stream s = {attr.type, attr.index};
- readAccessor(s.data, attr.data);
- if (attr.type == cgltf_attribute_type_color && attr.data->type == cgltf_type_vec3)
- {
- for (size_t i = 0; i < s.data.size(); ++i)
- s.data[i].f[3] = 1.0f;
- }
- result.streams.push_back(s);
- }
- for (size_t ti = 0; ti < primitive.targets_count; ++ti)
- {
- const cgltf_morph_target& target = primitive.targets[ti];
- for (size_t ai = 0; ai < target.attributes_count; ++ai)
- {
- const cgltf_attribute& attr = target.attributes[ai];
- if (attr.type == cgltf_attribute_type_invalid)
- {
- fprintf(stderr, "Warning: ignoring unknown attribute %s in morph target %d of primitive %d of mesh %d\n", attr.name, int(ti), int(pi), mesh_id);
- continue;
- }
- Stream s = {attr.type, attr.index, int(ti + 1)};
- readAccessor(s.data, attr.data);
- result.streams.push_back(s);
- }
- }
- result.targets = primitive.targets_count;
- result.target_weights.assign(mesh.weights, mesh.weights + mesh.weights_count);
- result.target_names.assign(mesh.target_names, mesh.target_names + mesh.target_names_count);
- meshes.push_back(result);
- }
- }
- }
- void defaultFree(void*, void* p)
- {
- free(p);
- }
- int textureIndex(const std::vector<std::string>& textures, const char* name)
- {
- for (size_t i = 0; i < textures.size(); ++i)
- if (textures[i] == name)
- return int(i);
- return -1;
- }
- cgltf_data* parseSceneObj(fastObjMesh* obj)
- {
- cgltf_data* data = (cgltf_data*)calloc(1, sizeof(cgltf_data));
- data->memory_free = defaultFree;
- std::vector<std::string> textures;
- for (unsigned int mi = 0; mi < obj->material_count; ++mi)
- {
- fastObjMaterial& om = obj->materials[mi];
- if (om.map_Kd.name && textureIndex(textures, om.map_Kd.name) < 0)
- textures.push_back(om.map_Kd.name);
- }
- data->images = (cgltf_image*)calloc(textures.size(), sizeof(cgltf_image));
- data->images_count = textures.size();
- for (size_t i = 0; i < textures.size(); ++i)
- {
- data->images[i].uri = strdup(textures[i].c_str());
- }
- data->textures = (cgltf_texture*)calloc(textures.size(), sizeof(cgltf_texture));
- data->textures_count = textures.size();
- for (size_t i = 0; i < textures.size(); ++i)
- {
- data->textures[i].image = &data->images[i];
- }
- data->materials = (cgltf_material*)calloc(obj->material_count, sizeof(cgltf_material));
- data->materials_count = obj->material_count;
- for (unsigned int mi = 0; mi < obj->material_count; ++mi)
- {
- cgltf_material& gm = data->materials[mi];
- fastObjMaterial& om = obj->materials[mi];
- gm.has_pbr_metallic_roughness = true;
- gm.pbr_metallic_roughness.base_color_factor[0] = 1.0f;
- gm.pbr_metallic_roughness.base_color_factor[1] = 1.0f;
- gm.pbr_metallic_roughness.base_color_factor[2] = 1.0f;
- gm.pbr_metallic_roughness.base_color_factor[3] = 1.0f;
- gm.pbr_metallic_roughness.metallic_factor = 0.0f;
- gm.pbr_metallic_roughness.roughness_factor = 1.0f;
- gm.alpha_cutoff = 0.5f;
- if (om.map_Kd.name)
- {
- gm.pbr_metallic_roughness.base_color_texture.texture = &data->textures[textureIndex(textures, om.map_Kd.name)];
- gm.pbr_metallic_roughness.base_color_texture.scale = 1.0f;
- gm.alpha_mode = (om.illum == 4 || om.illum == 6 || om.illum == 7 || om.illum == 9) ? cgltf_alpha_mode_mask : cgltf_alpha_mode_opaque;
- }
- if (om.map_d.name)
- {
- gm.alpha_mode = cgltf_alpha_mode_blend;
- }
- }
- return data;
- }
- void parseMeshesObj(fastObjMesh* obj, cgltf_data* data, std::vector<Mesh>& meshes)
- {
- unsigned int material_count = std::max(obj->material_count, 1u);
- std::vector<size_t> vertex_count(material_count);
- std::vector<size_t> index_count(material_count);
- for (unsigned int fi = 0; fi < obj->face_count; ++fi)
- {
- unsigned int mi = obj->face_materials[fi];
- vertex_count[mi] += obj->face_vertices[fi];
- index_count[mi] += (obj->face_vertices[fi] - 2) * 3;
- }
- std::vector<size_t> mesh_index(material_count);
- for (unsigned int mi = 0; mi < material_count; ++mi)
- {
- if (index_count[mi] == 0)
- continue;
- mesh_index[mi] = meshes.size();
- meshes.push_back(Mesh());
- Mesh& mesh = meshes.back();
- if (data->materials_count)
- {
- assert(mi < data->materials_count);
- mesh.material = &data->materials[mi];
- }
- mesh.type = cgltf_primitive_type_triangles;
- mesh.streams.resize(3);
- mesh.streams[0].type = cgltf_attribute_type_position;
- mesh.streams[0].data.resize(vertex_count[mi]);
- mesh.streams[1].type = cgltf_attribute_type_normal;
- mesh.streams[1].data.resize(vertex_count[mi]);
- mesh.streams[2].type = cgltf_attribute_type_texcoord;
- mesh.streams[2].data.resize(vertex_count[mi]);
- mesh.indices.resize(index_count[mi]);
- mesh.targets = 0;
- }
- std::vector<size_t> vertex_offset(material_count);
- std::vector<size_t> index_offset(material_count);
- size_t group_offset = 0;
- for (unsigned int fi = 0; fi < obj->face_count; ++fi)
- {
- unsigned int mi = obj->face_materials[fi];
- Mesh& mesh = meshes[mesh_index[mi]];
- size_t vo = vertex_offset[mi];
- size_t io = index_offset[mi];
- for (unsigned int vi = 0; vi < obj->face_vertices[fi]; ++vi)
- {
- fastObjIndex ii = obj->indices[group_offset + vi];
- Attr p = {{obj->positions[ii.p * 3 + 0], obj->positions[ii.p * 3 + 1], obj->positions[ii.p * 3 + 2]}};
- Attr n = {{obj->normals[ii.n * 3 + 0], obj->normals[ii.n * 3 + 1], obj->normals[ii.n * 3 + 2]}};
- Attr t = {{obj->texcoords[ii.t * 2 + 0], 1.f - obj->texcoords[ii.t * 2 + 1]}};
- mesh.streams[0].data[vo + vi] = p;
- mesh.streams[1].data[vo + vi] = n;
- mesh.streams[2].data[vo + vi] = t;
- }
- for (unsigned int vi = 2; vi < obj->face_vertices[fi]; ++vi)
- {
- size_t to = io + (vi - 2) * 3;
- mesh.indices[to + 0] = unsigned(vo);
- mesh.indices[to + 1] = unsigned(vo + vi - 1);
- mesh.indices[to + 2] = unsigned(vo + vi);
- }
- vertex_offset[mi] += obj->face_vertices[fi];
- index_offset[mi] += (obj->face_vertices[fi] - 2) * 3;
- group_offset += obj->face_vertices[fi];
- }
- }
- void parseAnimations(cgltf_data* data, std::vector<Animation>& animations)
- {
- for (size_t i = 0; i < data->animations_count; ++i)
- {
- const cgltf_animation& animation = data->animations[i];
- Animation result = {};
- result.name = animation.name;
- for (size_t j = 0; j < animation.channels_count; ++j)
- {
- const cgltf_animation_channel& channel = animation.channels[j];
- if (!channel.target_node)
- {
- fprintf(stderr, "Warning: ignoring channel %d of animation %d because it has no target node\n", int(j), int(i));
- continue;
- }
- Track track = {};
- track.node = channel.target_node;
- track.path = channel.target_path;
- track.components = (channel.target_path == cgltf_animation_path_type_weights) ? track.node->mesh->primitives[0].targets_count : 1;
- track.interpolation = channel.sampler->interpolation;
- readAccessor(track.time, channel.sampler->input);
- readAccessor(track.data, channel.sampler->output);
- result.tracks.push_back(track);
- }
- if (result.tracks.empty())
- {
- fprintf(stderr, "Warning: ignoring animation %d because it has no valid tracks\n", int(i));
- continue;
- }
- animations.push_back(result);
- }
- }
- bool areTextureViewsEqual(const cgltf_texture_view& lhs, const cgltf_texture_view& rhs)
- {
- if (lhs.has_transform != rhs.has_transform)
- return false;
- if (lhs.has_transform)
- {
- const cgltf_texture_transform& lt = lhs.transform;
- const cgltf_texture_transform& rt = rhs.transform;
- if (memcmp(lt.offset, rt.offset, sizeof(cgltf_float) * 2) != 0)
- return false;
- if (lt.rotation != rt.rotation)
- return false;
- if (memcmp(lt.scale, rt.scale, sizeof(cgltf_float) * 2) != 0)
- return false;
- if (lt.texcoord != rt.texcoord)
- return false;
- }
- if (lhs.texture != rhs.texture)
- return false;
- if (lhs.texcoord != rhs.texcoord)
- return false;
- if (lhs.scale != rhs.scale)
- return false;
- return true;
- }
- bool areMaterialsEqual(const cgltf_material& lhs, const cgltf_material& rhs)
- {
- if (lhs.has_pbr_metallic_roughness != rhs.has_pbr_metallic_roughness)
- return false;
- if (lhs.has_pbr_metallic_roughness)
- {
- const cgltf_pbr_metallic_roughness& lpbr = lhs.pbr_metallic_roughness;
- const cgltf_pbr_metallic_roughness& rpbr = rhs.pbr_metallic_roughness;
- if (!areTextureViewsEqual(lpbr.base_color_texture, rpbr.base_color_texture))
- return false;
- if (!areTextureViewsEqual(lpbr.metallic_roughness_texture, rpbr.metallic_roughness_texture))
- return false;
- if (memcmp(lpbr.base_color_factor, rpbr.base_color_factor, sizeof(cgltf_float) * 4) != 0)
- return false;
- if (lpbr.metallic_factor != rpbr.metallic_factor)
- return false;
- if (lpbr.roughness_factor != rpbr.roughness_factor)
- return false;
- }
- if (lhs.has_pbr_specular_glossiness != rhs.has_pbr_specular_glossiness)
- return false;
- if (lhs.has_pbr_specular_glossiness)
- {
- const cgltf_pbr_specular_glossiness& lpbr = lhs.pbr_specular_glossiness;
- const cgltf_pbr_specular_glossiness& rpbr = rhs.pbr_specular_glossiness;
- if (!areTextureViewsEqual(lpbr.diffuse_texture, rpbr.diffuse_texture))
- return false;
- if (!areTextureViewsEqual(lpbr.specular_glossiness_texture, rpbr.specular_glossiness_texture))
- return false;
- if (memcmp(lpbr.diffuse_factor, rpbr.diffuse_factor, sizeof(cgltf_float) * 4) != 0)
- return false;
- if (memcmp(lpbr.specular_factor, rpbr.specular_factor, sizeof(cgltf_float) * 3) != 0)
- return false;
- if (lpbr.glossiness_factor != rpbr.glossiness_factor)
- return false;
- }
- if (!areTextureViewsEqual(lhs.normal_texture, rhs.normal_texture))
- return false;
- if (!areTextureViewsEqual(lhs.occlusion_texture, rhs.occlusion_texture))
- return false;
- if (!areTextureViewsEqual(lhs.emissive_texture, rhs.emissive_texture))
- return false;
- if (memcmp(lhs.emissive_factor, rhs.emissive_factor, sizeof(cgltf_float) * 3) != 0)
- return false;
- if (lhs.alpha_mode != rhs.alpha_mode)
- return false;
- if (lhs.alpha_cutoff != rhs.alpha_cutoff)
- return false;
- if (lhs.double_sided != rhs.double_sided)
- return false;
- if (lhs.unlit != rhs.unlit)
- return false;
- return true;
- }
- bool usesTextureSet(const cgltf_material& material, int set)
- {
- if (material.has_pbr_metallic_roughness)
- {
- const cgltf_pbr_metallic_roughness& pbr = material.pbr_metallic_roughness;
- if (pbr.base_color_texture.texture && pbr.base_color_texture.texcoord == set)
- return true;
- if (pbr.metallic_roughness_texture.texture && pbr.metallic_roughness_texture.texcoord == set)
- return true;
- }
- if (material.has_pbr_specular_glossiness)
- {
- const cgltf_pbr_specular_glossiness& pbr = material.pbr_specular_glossiness;
- if (pbr.diffuse_texture.texture && pbr.diffuse_texture.texcoord == set)
- return true;
- if (pbr.specular_glossiness_texture.texture && pbr.specular_glossiness_texture.texcoord == set)
- return true;
- }
- if (material.normal_texture.texture && material.normal_texture.texcoord == set)
- return true;
- if (material.occlusion_texture.texture && material.occlusion_texture.texcoord == set)
- return true;
- if (material.emissive_texture.texture && material.emissive_texture.texcoord == set)
- return true;
- return false;
- }
- void mergeMeshMaterials(cgltf_data* data, std::vector<Mesh>& meshes)
- {
- for (size_t i = 0; i < meshes.size(); ++i)
- {
- Mesh& mesh = meshes[i];
- if (!mesh.material)
- continue;
- for (int j = 0; j < mesh.material - data->materials; ++j)
- {
- if (areMaterialsEqual(*mesh.material, data->materials[j]))
- {
- mesh.material = &data->materials[j];
- break;
- }
- }
- }
- }
- bool compareMeshTargets(const Mesh& lhs, const Mesh& rhs)
- {
- if (lhs.targets != rhs.targets)
- return false;
- if (lhs.target_weights.size() != rhs.target_weights.size())
- return false;
- for (size_t i = 0; i < lhs.target_weights.size(); ++i)
- if (lhs.target_weights[i] != rhs.target_weights[i])
- return false;
- if (lhs.target_names.size() != rhs.target_names.size())
- return false;
- for (size_t i = 0; i < lhs.target_names.size(); ++i)
- if (strcmp(lhs.target_names[i], rhs.target_names[i]) != 0)
- return false;
- return true;
- }
- bool canMergeMeshes(const Mesh& lhs, const Mesh& rhs, const Settings& settings)
- {
- if (lhs.node != rhs.node)
- {
- if (!lhs.node || !rhs.node)
- return false;
- if (lhs.node->parent != rhs.node->parent)
- return false;
- bool lhs_transform = lhs.node->has_translation | lhs.node->has_rotation | lhs.node->has_scale | lhs.node->has_matrix | (!!lhs.node->weights);
- bool rhs_transform = rhs.node->has_translation | rhs.node->has_rotation | rhs.node->has_scale | rhs.node->has_matrix | (!!rhs.node->weights);
- if (lhs_transform || rhs_transform)
- return false;
- if (settings.keep_named)
- {
- if (lhs.node->name && *lhs.node->name)
- return false;
- if (rhs.node->name && *rhs.node->name)
- return false;
- }
- // we can merge nodes that don't have transforms of their own and have the same parent
- // this is helpful when instead of splitting mesh into primitives, DCCs split mesh into mesh nodes
- }
- if (lhs.material != rhs.material)
- return false;
- if (lhs.skin != rhs.skin)
- return false;
- if (lhs.type != rhs.type)
- return false;
- if (!compareMeshTargets(lhs, rhs))
- return false;
- if (lhs.indices.empty() != rhs.indices.empty())
- return false;
- if (lhs.streams.size() != rhs.streams.size())
- return false;
- for (size_t i = 0; i < lhs.streams.size(); ++i)
- if (lhs.streams[i].type != rhs.streams[i].type || lhs.streams[i].index != rhs.streams[i].index || lhs.streams[i].target != rhs.streams[i].target)
- return false;
- return true;
- }
- void mergeMeshes(Mesh& target, const Mesh& mesh)
- {
- assert(target.streams.size() == mesh.streams.size());
- size_t vertex_offset = target.streams[0].data.size();
- size_t index_offset = target.indices.size();
- for (size_t i = 0; i < target.streams.size(); ++i)
- target.streams[i].data.insert(target.streams[i].data.end(), mesh.streams[i].data.begin(), mesh.streams[i].data.end());
- target.indices.resize(target.indices.size() + mesh.indices.size());
- size_t index_count = mesh.indices.size();
- for (size_t i = 0; i < index_count; ++i)
- target.indices[index_offset + i] = unsigned(vertex_offset + mesh.indices[i]);
- }
- void mergeMeshes(std::vector<Mesh>& meshes, const Settings& settings)
- {
- for (size_t i = 0; i < meshes.size(); ++i)
- {
- Mesh& target = meshes[i];
- if (target.streams.empty())
- continue;
- size_t target_vertices = target.streams[0].data.size();
- size_t target_indices = target.indices.size();
- size_t last_merged = i;
- for (size_t j = i + 1; j < meshes.size(); ++j)
- {
- Mesh& mesh = meshes[j];
- if (!mesh.streams.empty() && canMergeMeshes(target, mesh, settings))
- {
- target_vertices += mesh.streams[0].data.size();
- target_indices += mesh.indices.size();
- last_merged = j;
- }
- }
- for (size_t j = 0; j < target.streams.size(); ++j)
- target.streams[j].data.reserve(target_vertices);
- target.indices.reserve(target_indices);
- for (size_t j = i + 1; j <= last_merged; ++j)
- {
- Mesh& mesh = meshes[j];
- if (!mesh.streams.empty() && canMergeMeshes(target, mesh, settings))
- {
- mergeMeshes(target, mesh);
- mesh.streams.clear();
- mesh.indices.clear();
- }
- }
- assert(target.streams[0].data.size() == target_vertices);
- assert(target.indices.size() == target_indices);
- }
- }
- void filterEmptyMeshes(std::vector<Mesh>& meshes)
- {
- size_t write = 0;
- for (size_t i = 0; i < meshes.size(); ++i)
- {
- Mesh& mesh = meshes[i];
- if (mesh.streams.empty())
- continue;
- if (mesh.streams[0].data.empty())
- continue;
- if (mesh.type == cgltf_primitive_type_triangles && mesh.indices.empty())
- continue;
- // the following code is roughly equivalent to meshes[write] = std::move(mesh)
- std::vector<Stream> streams;
- streams.swap(mesh.streams);
- std::vector<unsigned int> indices;
- indices.swap(mesh.indices);
- meshes[write] = mesh;
- meshes[write].streams.swap(streams);
- meshes[write].indices.swap(indices);
- write++;
- }
- meshes.resize(write);
- }
- bool hasColorData(const std::vector<Attr>& data)
- {
- const float threshold = 0.99f;
- for (size_t i = 0; i < data.size(); ++i)
- {
- const Attr& a = data[i];
- if (a.f[0] < threshold || a.f[1] < threshold || a.f[2] < threshold || a.f[3] < threshold)
- return true;
- }
- return false;
- }
- void filterStreams(Mesh& mesh)
- {
- size_t write = 0;
- for (size_t i = 0; i < mesh.streams.size(); ++i)
- {
- Stream& stream = mesh.streams[i];
- if (stream.type == cgltf_attribute_type_texcoord && (!mesh.material || !usesTextureSet(*mesh.material, stream.index)))
- continue;
- if (stream.type == cgltf_attribute_type_tangent && (!mesh.material || !mesh.material->normal_texture.texture))
- continue;
- if ((stream.type == cgltf_attribute_type_joints || stream.type == cgltf_attribute_type_weights) && !mesh.skin)
- continue;
- if (stream.type == cgltf_attribute_type_color && !hasColorData(stream.data))
- continue;
- // the following code is roughly equivalent to streams[write] = std::move(stream)
- std::vector<Attr> data;
- data.swap(stream.data);
- mesh.streams[write] = stream;
- mesh.streams[write].data.swap(data);
- write++;
- }
- mesh.streams.resize(write);
- }
- void reindexMesh(Mesh& mesh)
- {
- size_t total_vertices = mesh.streams[0].data.size();
- size_t total_indices = mesh.indices.size();
- std::vector<meshopt_Stream> streams;
- for (size_t i = 0; i < mesh.streams.size(); ++i)
- {
- if (mesh.streams[i].target)
- continue;
- assert(mesh.streams[i].data.size() == total_vertices);
- meshopt_Stream stream = {&mesh.streams[i].data[0], sizeof(Attr), sizeof(Attr)};
- streams.push_back(stream);
- }
- std::vector<unsigned int> remap(total_vertices);
- size_t unique_vertices = meshopt_generateVertexRemapMulti(&remap[0], &mesh.indices[0], total_indices, total_vertices, &streams[0], streams.size());
- assert(unique_vertices <= total_vertices);
- meshopt_remapIndexBuffer(&mesh.indices[0], &mesh.indices[0], total_indices, &remap[0]);
- for (size_t i = 0; i < mesh.streams.size(); ++i)
- {
- assert(mesh.streams[i].data.size() == total_vertices);
- meshopt_remapVertexBuffer(&mesh.streams[i].data[0], &mesh.streams[i].data[0], total_vertices, sizeof(Attr), &remap[0]);
- mesh.streams[i].data.resize(unique_vertices);
- }
- }
- void filterTriangles(Mesh& mesh)
- {
- unsigned int* indices = &mesh.indices[0];
- size_t total_indices = mesh.indices.size();
- size_t write = 0;
- for (size_t i = 0; i < total_indices; i += 3)
- {
- unsigned int a = indices[i + 0], b = indices[i + 1], c = indices[i + 2];
- if (a != b && a != c && b != c)
- {
- indices[write + 0] = a;
- indices[write + 1] = b;
- indices[write + 2] = c;
- write += 3;
- }
- }
- mesh.indices.resize(write);
- }
- Stream* getStream(Mesh& mesh, cgltf_attribute_type type, int index = 0)
- {
- for (size_t i = 0; i < mesh.streams.size(); ++i)
- if (mesh.streams[i].type == type && mesh.streams[i].index == index)
- return &mesh.streams[i];
- return 0;
- }
- void simplifyMesh(Mesh& mesh, float threshold, bool aggressive)
- {
- if (threshold >= 1)
- return;
- const Stream* positions = getStream(mesh, cgltf_attribute_type_position);
- if (!positions)
- return;
- size_t vertex_count = mesh.streams[0].data.size();
- size_t target_index_count = size_t(double(mesh.indices.size() / 3) * threshold) * 3;
- float target_error = 1e-2f;
- if (target_index_count < 1)
- return;
- std::vector<unsigned int> indices(mesh.indices.size());
- indices.resize(meshopt_simplify(&indices[0], &mesh.indices[0], mesh.indices.size(), positions->data[0].f, vertex_count, sizeof(Attr), target_index_count, target_error));
- mesh.indices.swap(indices);
- // Note: if the simplifier got stuck, we can try to reindex without normals/tangents and retry
- // For now we simply fall back to aggressive simplifier instead
- // if the mesh is complex enough and the precise simplifier got "stuck", we'll try to simplify using the sloppy simplifier which is guaranteed to reach the target count
- if (aggressive && target_index_count > 50 * 3 && mesh.indices.size() > target_index_count)
- {
- indices.resize(meshopt_simplifySloppy(&indices[0], &mesh.indices[0], mesh.indices.size(), positions->data[0].f, vertex_count, sizeof(Attr), target_index_count));
- mesh.indices.swap(indices);
- }
- }
- void optimizeMesh(Mesh& mesh)
- {
- size_t vertex_count = mesh.streams[0].data.size();
- meshopt_optimizeVertexCache(&mesh.indices[0], &mesh.indices[0], mesh.indices.size(), vertex_count);
- std::vector<unsigned int> remap(vertex_count);
- size_t unique_vertices = meshopt_optimizeVertexFetchRemap(&remap[0], &mesh.indices[0], mesh.indices.size(), vertex_count);
- assert(unique_vertices <= vertex_count);
- meshopt_remapIndexBuffer(&mesh.indices[0], &mesh.indices[0], mesh.indices.size(), &remap[0]);
- for (size_t i = 0; i < mesh.streams.size(); ++i)
- {
- assert(mesh.streams[i].data.size() == vertex_count);
- meshopt_remapVertexBuffer(&mesh.streams[i].data[0], &mesh.streams[i].data[0], vertex_count, sizeof(Attr), &remap[0]);
- mesh.streams[i].data.resize(unique_vertices);
- }
- }
- struct BoneInfluence
- {
- float i;
- float w;
- };
- struct BoneInfluenceIndexPredicate
- {
- bool operator()(const BoneInfluence& lhs, const BoneInfluence& rhs) const
- {
- return lhs.i < rhs.i;
- }
- };
- struct BoneInfluenceWeightPredicate
- {
- bool operator()(const BoneInfluence& lhs, const BoneInfluence& rhs) const
- {
- return lhs.w > rhs.w;
- }
- };
- void filterBones(Mesh& mesh)
- {
- const int kMaxGroups = 8;
- std::pair<Stream*, Stream*> groups[kMaxGroups];
- int group_count = 0;
- // gather all joint/weight groups; each group contains 4 bone influences
- for (int i = 0; i < kMaxGroups; ++i)
- {
- Stream* jg = getStream(mesh, cgltf_attribute_type_joints, int(i));
- Stream* wg = getStream(mesh, cgltf_attribute_type_weights, int(i));
- if (!jg || !wg)
- break;
- groups[group_count++] = std::make_pair(jg, wg);
- }
- if (group_count == 0)
- return;
- // weights below cutoff can't be represented in quantized 8-bit storage
- const float weight_cutoff = 0.5f / 255.f;
- size_t vertex_count = mesh.streams[0].data.size();
- BoneInfluence inf[kMaxGroups * 4] = {};
- for (size_t i = 0; i < vertex_count; ++i)
- {
- int count = 0;
- // gather all bone influences for this vertex
- for (int j = 0; j < group_count; ++j)
- {
- const Attr& ja = groups[j].first->data[i];
- const Attr& wa = groups[j].second->data[i];
- for (int k = 0; k < 4; ++k)
- if (wa.f[k] > weight_cutoff)
- {
- inf[count].i = ja.f[k];
- inf[count].w = wa.f[k];
- count++;
- }
- }
- // pick top 4 influences - could use partial_sort but it is slower on small sets
- std::sort(inf, inf + count, BoneInfluenceWeightPredicate());
- // now sort top 4 influences by bone index - this improves compression ratio
- std::sort(inf, inf + std::min(4, count), BoneInfluenceIndexPredicate());
- // copy the top 4 influences back into stream 0 - we will remove other streams at the end
- Attr& ja = groups[0].first->data[i];
- Attr& wa = groups[0].second->data[i];
- for (int k = 0; k < 4; ++k)
- {
- if (k < count)
- {
- ja.f[k] = inf[k].i;
- wa.f[k] = inf[k].w;
- }
- else
- {
- ja.f[k] = 0.f;
- wa.f[k] = 0.f;
- }
- }
- }
- // remove redundant weight/joint streams
- for (size_t i = 0; i < mesh.streams.size();)
- {
- Stream& s = mesh.streams[i];
- if ((s.type == cgltf_attribute_type_joints || s.type == cgltf_attribute_type_weights) && s.index > 0)
- mesh.streams.erase(mesh.streams.begin() + i);
- else
- ++i;
- }
- }
- void simplifyPointMesh(Mesh& mesh, float threshold)
- {
- if (threshold >= 1)
- return;
- const Stream* positions = getStream(mesh, cgltf_attribute_type_position);
- if (!positions)
- return;
- size_t vertex_count = mesh.streams[0].data.size();
- size_t target_vertex_count = size_t(double(vertex_count) * threshold);
- if (target_vertex_count < 1)
- return;
- std::vector<unsigned int> indices(target_vertex_count);
- indices.resize(meshopt_simplifyPoints(&indices[0], positions->data[0].f, vertex_count, sizeof(Attr), target_vertex_count));
- std::vector<Attr> scratch(indices.size());
- for (size_t i = 0; i < mesh.streams.size(); ++i)
- {
- std::vector<Attr>& data = mesh.streams[i].data;
- assert(data.size() == vertex_count);
- for (size_t j = 0; j < indices.size(); ++j)
- scratch[j] = data[indices[j]];
- data = scratch;
- }
- }
- void sortPointMesh(Mesh& mesh)
- {
- const Stream* positions = getStream(mesh, cgltf_attribute_type_position);
- if (!positions)
- return;
- size_t vertex_count = mesh.streams[0].data.size();
- std::vector<unsigned int> remap(vertex_count);
- meshopt_spatialSortRemap(&remap[0], positions->data[0].f, vertex_count, sizeof(Attr));
- for (size_t i = 0; i < mesh.streams.size(); ++i)
- {
- assert(mesh.streams[i].data.size() == vertex_count);
- meshopt_remapVertexBuffer(&mesh.streams[i].data[0], &mesh.streams[i].data[0], vertex_count, sizeof(Attr), &remap[0]);
- }
- }
- void processMesh(Mesh& mesh, const Settings& settings)
- {
- filterStreams(mesh);
- switch (mesh.type)
- {
- case cgltf_primitive_type_points:
- assert(mesh.indices.empty());
- simplifyPointMesh(mesh, settings.simplify_threshold);
- sortPointMesh(mesh);
- break;
- case cgltf_primitive_type_triangles:
- filterBones(mesh);
- reindexMesh(mesh);
- filterTriangles(mesh);
- simplifyMesh(mesh, settings.simplify_threshold, settings.simplify_aggressive);
- optimizeMesh(mesh);
- break;
- default:
- assert(!"Unknown primitive type");
- }
- }
- bool getAttributeBounds(const std::vector<Mesh>& meshes, cgltf_attribute_type type, Attr& min, Attr& max)
- {
- min.f[0] = min.f[1] = min.f[2] = min.f[3] = +FLT_MAX;
- max.f[0] = max.f[1] = max.f[2] = max.f[3] = -FLT_MAX;
- Attr pad = {};
- bool valid = false;
- for (size_t i = 0; i < meshes.size(); ++i)
- {
- const Mesh& mesh = meshes[i];
- for (size_t j = 0; j < mesh.streams.size(); ++j)
- {
- const Stream& s = mesh.streams[j];
- if (s.type == type)
- {
- if (s.target == 0)
- {
- for (size_t k = 0; k < s.data.size(); ++k)
- {
- const Attr& a = s.data[k];
- min.f[0] = std::min(min.f[0], a.f[0]);
- min.f[1] = std::min(min.f[1], a.f[1]);
- min.f[2] = std::min(min.f[2], a.f[2]);
- min.f[3] = std::min(min.f[3], a.f[3]);
- max.f[0] = std::max(max.f[0], a.f[0]);
- max.f[1] = std::max(max.f[1], a.f[1]);
- max.f[2] = std::max(max.f[2], a.f[2]);
- max.f[3] = std::max(max.f[3], a.f[3]);
- valid = true;
- }
- }
- else
- {
- for (size_t k = 0; k < s.data.size(); ++k)
- {
- const Attr& a = s.data[k];
- pad.f[0] = std::max(pad.f[0], fabsf(a.f[0]));
- pad.f[1] = std::max(pad.f[1], fabsf(a.f[1]));
- pad.f[2] = std::max(pad.f[2], fabsf(a.f[2]));
- pad.f[3] = std::max(pad.f[3], fabsf(a.f[3]));
- }
- }
- }
- }
- }
- if (valid)
- {
- for (int k = 0; k < 4; ++k)
- {
- min.f[k] -= pad.f[k];
- max.f[k] += pad.f[k];
- }
- }
- return valid;
- }
- QuantizationParams prepareQuantization(const std::vector<Mesh>& meshes, const Settings& settings)
- {
- QuantizationParams result = {};
- result.pos_bits = settings.pos_bits;
- Attr pos_min, pos_max;
- if (getAttributeBounds(meshes, cgltf_attribute_type_position, pos_min, pos_max))
- {
- result.pos_offset[0] = pos_min.f[0];
- result.pos_offset[1] = pos_min.f[1];
- result.pos_offset[2] = pos_min.f[2];
- result.pos_scale = std::max(pos_max.f[0] - pos_min.f[0], std::max(pos_max.f[1] - pos_min.f[1], pos_max.f[2] - pos_min.f[2]));
- }
- result.uv_bits = settings.tex_bits;
- Attr uv_min, uv_max;
- if (getAttributeBounds(meshes, cgltf_attribute_type_texcoord, uv_min, uv_max))
- {
- result.uv_offset[0] = uv_min.f[0];
- result.uv_offset[1] = uv_min.f[1];
- result.uv_scale[0] = uv_max.f[0] - uv_min.f[0];
- result.uv_scale[1] = uv_max.f[1] - uv_min.f[1];
- }
- return result;
- }
- void rescaleNormal(float& nx, float& ny, float& nz)
- {
- // scale the normal to make sure the largest component is +-1.0
- // this reduces the entropy of the normal by ~1.5 bits without losing precision
- // it's better to use octahedral encoding but that requires special shader support
- float nm = std::max(fabsf(nx), std::max(fabsf(ny), fabsf(nz)));
- float ns = nm == 0.f ? 0.f : 1 / nm;
- nx *= ns;
- ny *= ns;
- nz *= ns;
- }
- void renormalizeWeights(uint8_t (&w)[4])
- {
- int sum = w[0] + w[1] + w[2] + w[3];
- if (sum == 255)
- return;
- // we assume that the total error is limited to 0.5/component = 2
- // this means that it's acceptable to adjust the max. component to compensate for the error
- int max = 0;
- for (int k = 1; k < 4; ++k)
- if (w[k] > w[max])
- max = k;
- w[max] += uint8_t(255 - sum);
- }
- StreamFormat writeVertexStream(std::string& bin, const Stream& stream, const QuantizationParams& params, const Settings& settings, bool has_targets)
- {
- if (stream.type == cgltf_attribute_type_position)
- {
- if (stream.target == 0)
- {
- float pos_rscale = params.pos_scale == 0.f ? 0.f : 1.f / params.pos_scale;
- for (size_t i = 0; i < stream.data.size(); ++i)
- {
- const Attr& a = stream.data[i];
- uint16_t v[4] = {
- uint16_t(meshopt_quantizeUnorm((a.f[0] - params.pos_offset[0]) * pos_rscale, params.pos_bits)),
- uint16_t(meshopt_quantizeUnorm((a.f[1] - params.pos_offset[1]) * pos_rscale, params.pos_bits)),
- uint16_t(meshopt_quantizeUnorm((a.f[2] - params.pos_offset[2]) * pos_rscale, params.pos_bits)),
- 0};
- bin.append(reinterpret_cast<const char*>(v), sizeof(v));
- }
- StreamFormat format = {cgltf_type_vec3, cgltf_component_type_r_16u, false, 8};
- return format;
- }
- else
- {
- float pos_rscale = params.pos_scale == 0.f ? 0.f : 1.f / params.pos_scale;
- int maxv = 0;
- for (size_t i = 0; i < stream.data.size(); ++i)
- {
- const Attr& a = stream.data[i];
- maxv = std::max(maxv, meshopt_quantizeUnorm(fabsf(a.f[0]) * pos_rscale, params.pos_bits));
- maxv = std::max(maxv, meshopt_quantizeUnorm(fabsf(a.f[1]) * pos_rscale, params.pos_bits));
- maxv = std::max(maxv, meshopt_quantizeUnorm(fabsf(a.f[2]) * pos_rscale, params.pos_bits));
- }
- if (maxv <= 127)
- {
- for (size_t i = 0; i < stream.data.size(); ++i)
- {
- const Attr& a = stream.data[i];
- int8_t v[4] = {
- int8_t((a.f[0] >= 0.f ? 1 : -1) * meshopt_quantizeUnorm(fabsf(a.f[0]) * pos_rscale, params.pos_bits)),
- int8_t((a.f[1] >= 0.f ? 1 : -1) * meshopt_quantizeUnorm(fabsf(a.f[1]) * pos_rscale, params.pos_bits)),
- int8_t((a.f[2] >= 0.f ? 1 : -1) * meshopt_quantizeUnorm(fabsf(a.f[2]) * pos_rscale, params.pos_bits)),
- 0};
- bin.append(reinterpret_cast<const char*>(v), sizeof(v));
- }
- StreamFormat format = {cgltf_type_vec3, cgltf_component_type_r_8, false, 4};
- return format;
- }
- else
- {
- for (size_t i = 0; i < stream.data.size(); ++i)
- {
- const Attr& a = stream.data[i];
- int16_t v[4] = {
- int16_t((a.f[0] >= 0.f ? 1 : -1) * meshopt_quantizeUnorm(fabsf(a.f[0]) * pos_rscale, params.pos_bits)),
- int16_t((a.f[1] >= 0.f ? 1 : -1) * meshopt_quantizeUnorm(fabsf(a.f[1]) * pos_rscale, params.pos_bits)),
- int16_t((a.f[2] >= 0.f ? 1 : -1) * meshopt_quantizeUnorm(fabsf(a.f[2]) * pos_rscale, params.pos_bits)),
- 0};
- bin.append(reinterpret_cast<const char*>(v), sizeof(v));
- }
- StreamFormat format = {cgltf_type_vec3, cgltf_component_type_r_16, false, 8};
- return format;
- }
- }
- }
- else if (stream.type == cgltf_attribute_type_texcoord)
- {
- float uv_rscale[2] = {
- params.uv_scale[0] == 0.f ? 0.f : 1.f / params.uv_scale[0],
- params.uv_scale[1] == 0.f ? 0.f : 1.f / params.uv_scale[1],
- };
- for (size_t i = 0; i < stream.data.size(); ++i)
- {
- const Attr& a = stream.data[i];
- uint16_t v[2] = {
- uint16_t(meshopt_quantizeUnorm((a.f[0] - params.uv_offset[0]) * uv_rscale[0], params.uv_bits)),
- uint16_t(meshopt_quantizeUnorm((a.f[1] - params.uv_offset[1]) * uv_rscale[1], params.uv_bits)),
- };
- bin.append(reinterpret_cast<const char*>(v), sizeof(v));
- }
- StreamFormat format = {cgltf_type_vec2, cgltf_component_type_r_16u, false, 4};
- return format;
- }
- else if (stream.type == cgltf_attribute_type_normal)
- {
- bool unnormalized = settings.nrm_unnormalized && !has_targets;
- int bits = unnormalized ? settings.nrm_bits : (settings.nrm_bits > 8 ? 16 : 8);
- for (size_t i = 0; i < stream.data.size(); ++i)
- {
- const Attr& a = stream.data[i];
- float nx = a.f[0], ny = a.f[1], nz = a.f[2];
- if (unnormalized)
- rescaleNormal(nx, ny, nz);
- if (bits > 8)
- {
- int16_t v[4] = {
- int16_t(meshopt_quantizeSnorm(nx, bits)),
- int16_t(meshopt_quantizeSnorm(ny, bits)),
- int16_t(meshopt_quantizeSnorm(nz, bits)),
- 0};
- bin.append(reinterpret_cast<const char*>(v), sizeof(v));
- }
- else
- {
- int8_t v[4] = {
- int8_t(meshopt_quantizeSnorm(nx, bits)),
- int8_t(meshopt_quantizeSnorm(ny, bits)),
- int8_t(meshopt_quantizeSnorm(nz, bits)),
- 0};
- bin.append(reinterpret_cast<const char*>(v), sizeof(v));
- }
- }
- if (bits > 8)
- {
- StreamFormat format = {cgltf_type_vec3, cgltf_component_type_r_16, true, 8};
- return format;
- }
- else
- {
- StreamFormat format = {cgltf_type_vec3, cgltf_component_type_r_8, true, 4};
- return format;
- }
- }
- else if (stream.type == cgltf_attribute_type_tangent)
- {
- bool unnormalized = settings.nrm_unnormalized && !has_targets;
- int bits = unnormalized ? settings.nrm_bits : (settings.nrm_bits > 8 ? 16 : 8);
- for (size_t i = 0; i < stream.data.size(); ++i)
- {
- const Attr& a = stream.data[i];
- float nx = a.f[0], ny = a.f[1], nz = a.f[2], nw = a.f[3];
- if (unnormalized)
- rescaleNormal(nx, ny, nz);
- if (bits > 8)
- {
- int16_t v[4] = {
- int16_t(meshopt_quantizeSnorm(nx, bits)),
- int16_t(meshopt_quantizeSnorm(ny, bits)),
- int16_t(meshopt_quantizeSnorm(nz, bits)),
- int16_t(meshopt_quantizeSnorm(nw, 8))};
- bin.append(reinterpret_cast<const char*>(v), sizeof(v));
- }
- else
- {
- int8_t v[4] = {
- int8_t(meshopt_quantizeSnorm(nx, bits)),
- int8_t(meshopt_quantizeSnorm(ny, bits)),
- int8_t(meshopt_quantizeSnorm(nz, bits)),
- int8_t(meshopt_quantizeSnorm(nw, 8))};
- bin.append(reinterpret_cast<const char*>(v), sizeof(v));
- }
- }
- cgltf_type type = (stream.target == 0) ? cgltf_type_vec4 : cgltf_type_vec3;
- if (bits > 8)
- {
- StreamFormat format = {type, cgltf_component_type_r_16, true, 8};
- return format;
- }
- else
- {
- StreamFormat format = {type, cgltf_component_type_r_8, true, 4};
- return format;
- }
- }
- else if (stream.type == cgltf_attribute_type_color)
- {
- for (size_t i = 0; i < stream.data.size(); ++i)
- {
- const Attr& a = stream.data[i];
- uint8_t v[4] = {
- uint8_t(meshopt_quantizeUnorm(a.f[0], 8)),
- uint8_t(meshopt_quantizeUnorm(a.f[1], 8)),
- uint8_t(meshopt_quantizeUnorm(a.f[2], 8)),
- uint8_t(meshopt_quantizeUnorm(a.f[3], 8))};
- bin.append(reinterpret_cast<const char*>(v), sizeof(v));
- }
- StreamFormat format = {cgltf_type_vec4, cgltf_component_type_r_8u, true, 4};
- return format;
- }
- else if (stream.type == cgltf_attribute_type_weights)
- {
- for (size_t i = 0; i < stream.data.size(); ++i)
- {
- const Attr& a = stream.data[i];
- float ws = a.f[0] + a.f[1] + a.f[2] + a.f[3];
- float wsi = (ws == 0.f) ? 0.f : 1.f / ws;
- uint8_t v[4] = {
- uint8_t(meshopt_quantizeUnorm(a.f[0] * wsi, 8)),
- uint8_t(meshopt_quantizeUnorm(a.f[1] * wsi, 8)),
- uint8_t(meshopt_quantizeUnorm(a.f[2] * wsi, 8)),
- uint8_t(meshopt_quantizeUnorm(a.f[3] * wsi, 8))};
- if (wsi != 0.f)
- renormalizeWeights(v);
- bin.append(reinterpret_cast<const char*>(v), sizeof(v));
- }
- StreamFormat format = {cgltf_type_vec4, cgltf_component_type_r_8u, true, 4};
- return format;
- }
- else if (stream.type == cgltf_attribute_type_joints)
- {
- unsigned int maxj = 0;
- for (size_t i = 0; i < stream.data.size(); ++i)
- maxj = std::max(maxj, unsigned(stream.data[i].f[0]));
- assert(maxj <= 65535);
- if (maxj <= 255)
- {
- for (size_t i = 0; i < stream.data.size(); ++i)
- {
- const Attr& a = stream.data[i];
- uint8_t v[4] = {
- uint8_t(a.f[0]),
- uint8_t(a.f[1]),
- uint8_t(a.f[2]),
- uint8_t(a.f[3])};
- bin.append(reinterpret_cast<const char*>(v), sizeof(v));
- }
- StreamFormat format = {cgltf_type_vec4, cgltf_component_type_r_8u, false, 4};
- return format;
- }
- else
- {
- for (size_t i = 0; i < stream.data.size(); ++i)
- {
- const Attr& a = stream.data[i];
- uint16_t v[4] = {
- uint16_t(a.f[0]),
- uint16_t(a.f[1]),
- uint16_t(a.f[2]),
- uint16_t(a.f[3])};
- bin.append(reinterpret_cast<const char*>(v), sizeof(v));
- }
- StreamFormat format = {cgltf_type_vec4, cgltf_component_type_r_16u, false, 8};
- return format;
- }
- }
- else
- {
- for (size_t i = 0; i < stream.data.size(); ++i)
- {
- const Attr& a = stream.data[i];
- float v[4] = {a.f[0], a.f[1], a.f[2], a.f[3]};
- bin.append(reinterpret_cast<const char*>(v), sizeof(v));
- }
- StreamFormat format = {cgltf_type_vec4, cgltf_component_type_r_32f, false, 16};
- return format;
- }
- }
- void getPositionBounds(int min[3], int max[3], const Stream& stream, const QuantizationParams& params)
- {
- assert(stream.type == cgltf_attribute_type_position);
- assert(stream.data.size() > 0);
- min[0] = min[1] = min[2] = INT_MAX;
- max[0] = max[1] = max[2] = INT_MIN;
- float pos_rscale = params.pos_scale == 0.f ? 0.f : 1.f / params.pos_scale;
- if (stream.target == 0)
- {
- for (size_t i = 0; i < stream.data.size(); ++i)
- {
- const Attr& a = stream.data[i];
- for (int k = 0; k < 3; ++k)
- {
- int v = meshopt_quantizeUnorm((a.f[k] - params.pos_offset[k]) * pos_rscale, params.pos_bits);
- min[k] = std::min(min[k], v);
- max[k] = std::max(max[k], v);
- }
- }
- }
- else
- {
- for (size_t i = 0; i < stream.data.size(); ++i)
- {
- const Attr& a = stream.data[i];
- for (int k = 0; k < 3; ++k)
- {
- int v = (a.f[k] >= 0.f ? 1 : -1) * meshopt_quantizeUnorm(fabsf(a.f[k]) * pos_rscale, params.pos_bits);
- min[k] = std::min(min[k], v);
- max[k] = std::max(max[k], v);
- }
- }
- }
- }
- StreamFormat writeIndexStream(std::string& bin, const std::vector<unsigned int>& stream)
- {
- unsigned int maxi = 0;
- for (size_t i = 0; i < stream.size(); ++i)
- maxi = std::max(maxi, stream[i]);
- // save 16-bit indices if we can; note that we can't use restart index (65535)
- if (maxi < 65535)
- {
- for (size_t i = 0; i < stream.size(); ++i)
- {
- uint16_t v[1] = {uint16_t(stream[i])};
- bin.append(reinterpret_cast<const char*>(v), sizeof(v));
- }
- StreamFormat format = {cgltf_type_scalar, cgltf_component_type_r_16u, false, 2};
- return format;
- }
- else
- {
- for (size_t i = 0; i < stream.size(); ++i)
- {
- uint32_t v[1] = {stream[i]};
- bin.append(reinterpret_cast<const char*>(v), sizeof(v));
- }
- StreamFormat format = {cgltf_type_scalar, cgltf_component_type_r_32u, false, 4};
- return format;
- }
- }
- StreamFormat writeTimeStream(std::string& bin, const std::vector<float>& data)
- {
- for (size_t i = 0; i < data.size(); ++i)
- {
- float v[1] = {data[i]};
- bin.append(reinterpret_cast<const char*>(v), sizeof(v));
- }
- StreamFormat format = {cgltf_type_scalar, cgltf_component_type_r_32f, false, 4};
- return format;
- }
- StreamFormat writeKeyframeStream(std::string& bin, cgltf_animation_path_type type, const std::vector<Attr>& data)
- {
- if (type == cgltf_animation_path_type_rotation)
- {
- for (size_t i = 0; i < data.size(); ++i)
- {
- const Attr& a = data[i];
- int16_t v[4] = {
- int16_t(meshopt_quantizeSnorm(a.f[0], 16)),
- int16_t(meshopt_quantizeSnorm(a.f[1], 16)),
- int16_t(meshopt_quantizeSnorm(a.f[2], 16)),
- int16_t(meshopt_quantizeSnorm(a.f[3], 16)),
- };
- bin.append(reinterpret_cast<const char*>(v), sizeof(v));
- }
- StreamFormat format = {cgltf_type_vec4, cgltf_component_type_r_16, true, 8};
- return format;
- }
- else if (type == cgltf_animation_path_type_weights)
- {
- for (size_t i = 0; i < data.size(); ++i)
- {
- const Attr& a = data[i];
- uint8_t v[1] = {uint8_t(meshopt_quantizeUnorm(a.f[0], 8))};
- bin.append(reinterpret_cast<const char*>(v), sizeof(v));
- }
- StreamFormat format = {cgltf_type_scalar, cgltf_component_type_r_8u, true, 1};
- return format;
- }
- else if (type == cgltf_animation_path_type_translation || type == cgltf_animation_path_type_scale)
- {
- int bits = 15;
- for (size_t i = 0; i < data.size(); ++i)
- {
- const Attr& a = data[i];
- float v[3] = {
- meshopt_quantizeFloat(a.f[0], bits),
- meshopt_quantizeFloat(a.f[1], bits),
- meshopt_quantizeFloat(a.f[2], bits)};
- bin.append(reinterpret_cast<const char*>(v), sizeof(v));
- }
- StreamFormat format = {cgltf_type_vec3, cgltf_component_type_r_32f, false, 12};
- return format;
- }
- else
- {
- for (size_t i = 0; i < data.size(); ++i)
- {
- const Attr& a = data[i];
- float v[4] = {a.f[0], a.f[1], a.f[2], a.f[3]};
- bin.append(reinterpret_cast<const char*>(v), sizeof(v));
- }
- StreamFormat format = {cgltf_type_vec4, cgltf_component_type_r_32f, false, 16};
- return format;
- }
- }
- void compressVertexStream(std::string& bin, const std::string& data, size_t count, size_t stride)
- {
- assert(data.size() == count * stride);
- std::vector<unsigned char> compressed(meshopt_encodeVertexBufferBound(count, stride));
- size_t size = meshopt_encodeVertexBuffer(&compressed[0], compressed.size(), data.c_str(), count, stride);
- bin.append(reinterpret_cast<const char*>(&compressed[0]), size);
- }
- void compressIndexStream(std::string& bin, const std::string& data, size_t count, size_t stride)
- {
- assert(stride == 2 || stride == 4);
- assert(data.size() == count * stride);
- std::vector<unsigned char> compressed(meshopt_encodeIndexBufferBound(count, count * 3));
- size_t size = 0;
- if (stride == 2)
- size = meshopt_encodeIndexBuffer(&compressed[0], compressed.size(), reinterpret_cast<const uint16_t*>(data.c_str()), count);
- else
- size = meshopt_encodeIndexBuffer(&compressed[0], compressed.size(), reinterpret_cast<const uint32_t*>(data.c_str()), count);
- bin.append(reinterpret_cast<const char*>(&compressed[0]), size);
- }
- void comma(std::string& s)
- {
- char ch = s.empty() ? 0 : s[s.size() - 1];
- if (ch != 0 && ch != '[' && ch != '{')
- s += ",";
- }
- void append(std::string& s, size_t v)
- {
- char buf[32];
- sprintf(buf, "%zu", v);
- s += buf;
- }
- void append(std::string& s, float v)
- {
- char buf[512];
- sprintf(buf, "%.9g", v);
- s += buf;
- }
- void append(std::string& s, const char* v)
- {
- s += v;
- }
- void append(std::string& s, const std::string& v)
- {
- s += v;
- }
- const char* componentType(cgltf_component_type type)
- {
- switch (type)
- {
- case cgltf_component_type_r_8:
- return "5120";
- case cgltf_component_type_r_8u:
- return "5121";
- case cgltf_component_type_r_16:
- return "5122";
- case cgltf_component_type_r_16u:
- return "5123";
- case cgltf_component_type_r_32u:
- return "5125";
- case cgltf_component_type_r_32f:
- return "5126";
- default:
- return "0";
- }
- }
- const char* shapeType(cgltf_type type)
- {
- switch (type)
- {
- case cgltf_type_scalar:
- return "SCALAR";
- case cgltf_type_vec2:
- return "VEC2";
- case cgltf_type_vec3:
- return "VEC3";
- case cgltf_type_vec4:
- return "VEC4";
- case cgltf_type_mat2:
- return "MAT2";
- case cgltf_type_mat3:
- return "MAT3";
- case cgltf_type_mat4:
- return "MAT4";
- default:
- return "";
- }
- }
- const char* attributeType(cgltf_attribute_type type)
- {
- switch (type)
- {
- case cgltf_attribute_type_position:
- return "POSITION";
- case cgltf_attribute_type_normal:
- return "NORMAL";
- case cgltf_attribute_type_tangent:
- return "TANGENT";
- case cgltf_attribute_type_texcoord:
- return "TEXCOORD";
- case cgltf_attribute_type_color:
- return "COLOR";
- case cgltf_attribute_type_joints:
- return "JOINTS";
- case cgltf_attribute_type_weights:
- return "WEIGHTS";
- default:
- return "ATTRIBUTE";
- }
- }
- const char* animationPath(cgltf_animation_path_type type)
- {
- switch (type)
- {
- case cgltf_animation_path_type_translation:
- return "translation";
- case cgltf_animation_path_type_rotation:
- return "rotation";
- case cgltf_animation_path_type_scale:
- return "scale";
- case cgltf_animation_path_type_weights:
- return "weights";
- default:
- return "";
- }
- }
- const char* lightType(cgltf_light_type type)
- {
- switch (type)
- {
- case cgltf_light_type_directional:
- return "directional";
- case cgltf_light_type_point:
- return "point";
- case cgltf_light_type_spot:
- return "spot";
- default:
- return "";
- }
- }
- void writeTextureInfo(std::string& json, const cgltf_data* data, const cgltf_texture_view& view, const QuantizationParams& qp)
- {
- assert(view.texture);
- cgltf_texture_transform transform = {};
- if (view.has_transform)
- {
- transform = view.transform;
- }
- else
- {
- transform.scale[0] = transform.scale[1] = 1.f;
- }
- transform.offset[0] += qp.uv_offset[0];
- transform.offset[1] += qp.uv_offset[1];
- transform.scale[0] *= qp.uv_scale[0] / float((1 << qp.uv_bits) - 1);
- transform.scale[1] *= qp.uv_scale[1] / float((1 << qp.uv_bits) - 1);
- append(json, "{\"index\":");
- append(json, size_t(view.texture - data->textures));
- append(json, ",\"texCoord\":");
- append(json, size_t(view.texcoord));
- append(json, ",\"extensions\":{\"KHR_texture_transform\":{");
- append(json, "\"offset\":[");
- append(json, transform.offset[0]);
- append(json, ",");
- append(json, transform.offset[1]);
- append(json, "],\"scale\":[");
- append(json, transform.scale[0]);
- append(json, ",");
- append(json, transform.scale[1]);
- append(json, "]");
- if (transform.rotation != 0.f)
- {
- append(json, ",\"rotation\":");
- append(json, transform.rotation);
- }
- append(json, "}}}");
- }
- void writeMaterialInfo(std::string& json, const cgltf_data* data, const cgltf_material& material, const QuantizationParams& qp)
- {
- static const float white[4] = {1, 1, 1, 1};
- static const float black[4] = {0, 0, 0, 0};
- if (material.name && *material.name)
- {
- comma(json);
- append(json, "\"name\":\"");
- append(json, material.name);
- append(json, "\"");
- }
- if (material.has_pbr_metallic_roughness)
- {
- const cgltf_pbr_metallic_roughness& pbr = material.pbr_metallic_roughness;
- comma(json);
- append(json, "\"pbrMetallicRoughness\":{");
- if (memcmp(pbr.base_color_factor, white, 16) != 0)
- {
- comma(json);
- append(json, "\"baseColorFactor\":[");
- append(json, pbr.base_color_factor[0]);
- append(json, ",");
- append(json, pbr.base_color_factor[1]);
- append(json, ",");
- append(json, pbr.base_color_factor[2]);
- append(json, ",");
- append(json, pbr.base_color_factor[3]);
- append(json, "]");
- }
- if (pbr.base_color_texture.texture)
- {
- comma(json);
- append(json, "\"baseColorTexture\":");
- writeTextureInfo(json, data, pbr.base_color_texture, qp);
- }
- if (pbr.metallic_factor != 1)
- {
- comma(json);
- append(json, "\"metallicFactor\":");
- append(json, pbr.metallic_factor);
- }
- if (pbr.roughness_factor != 1)
- {
- comma(json);
- append(json, "\"roughnessFactor\":");
- append(json, pbr.roughness_factor);
- }
- if (pbr.metallic_roughness_texture.texture)
- {
- comma(json);
- append(json, "\"metallicRoughnessTexture\":");
- writeTextureInfo(json, data, pbr.metallic_roughness_texture, qp);
- }
- append(json, "}");
- }
- if (material.normal_texture.texture)
- {
- comma(json);
- append(json, "\"normalTexture\":");
- writeTextureInfo(json, data, material.normal_texture, qp);
- }
- if (material.occlusion_texture.texture)
- {
- comma(json);
- append(json, "\"occlusionTexture\":");
- writeTextureInfo(json, data, material.occlusion_texture, qp);
- }
- if (material.emissive_texture.texture)
- {
- comma(json);
- append(json, "\"emissiveTexture\":");
- writeTextureInfo(json, data, material.emissive_texture, qp);
- }
- if (memcmp(material.emissive_factor, black, 12) != 0)
- {
- comma(json);
- append(json, "\"emissiveFactor\":[");
- append(json, material.emissive_factor[0]);
- append(json, ",");
- append(json, material.emissive_factor[1]);
- append(json, ",");
- append(json, material.emissive_factor[2]);
- append(json, "]");
- }
- if (material.alpha_mode != cgltf_alpha_mode_opaque)
- {
- comma(json);
- append(json, "\"alphaMode\":");
- append(json, (material.alpha_mode == cgltf_alpha_mode_blend) ? "\"BLEND\"" : "\"MASK\"");
- }
- if (material.alpha_cutoff != 0.5f)
- {
- comma(json);
- append(json, "\"alphaCutoff\":");
- append(json, material.alpha_cutoff);
- }
- if (material.double_sided)
- {
- comma(json);
- append(json, "\"doubleSided\":true");
- }
- if (material.has_pbr_specular_glossiness || material.unlit)
- {
- comma(json);
- append(json, "\"extensions\":{");
- if (material.has_pbr_specular_glossiness)
- {
- const cgltf_pbr_specular_glossiness& pbr = material.pbr_specular_glossiness;
- comma(json);
- append(json, "\"KHR_materials_pbrSpecularGlossiness\":{");
- if (pbr.diffuse_texture.texture)
- {
- comma(json);
- append(json, "\"diffuseTexture\":");
- writeTextureInfo(json, data, pbr.diffuse_texture, qp);
- }
- if (pbr.specular_glossiness_texture.texture)
- {
- comma(json);
- append(json, "\"specularGlossinessTexture\":");
- writeTextureInfo(json, data, pbr.specular_glossiness_texture, qp);
- }
- if (memcmp(pbr.diffuse_factor, white, 16) != 0)
- {
- comma(json);
- append(json, "\"diffuseFactor\":[");
- append(json, pbr.diffuse_factor[0]);
- append(json, ",");
- append(json, pbr.diffuse_factor[1]);
- append(json, ",");
- append(json, pbr.diffuse_factor[2]);
- append(json, ",");
- append(json, pbr.diffuse_factor[3]);
- append(json, "]");
- }
- if (memcmp(pbr.specular_factor, white, 12) != 0)
- {
- comma(json);
- append(json, "\"specularFactor\":[");
- append(json, pbr.specular_factor[0]);
- append(json, ",");
- append(json, pbr.specular_factor[1]);
- append(json, ",");
- append(json, pbr.specular_factor[2]);
- append(json, "]");
- }
- if (pbr.glossiness_factor != 1)
- {
- comma(json);
- append(json, "\"glossinessFactor\":");
- append(json, pbr.glossiness_factor);
- }
- append(json, "}");
- }
- if (material.unlit)
- {
- comma(json);
- append(json, "\"KHR_materials_unlit\":{}");
- }
- append(json, "}");
- }
- }
- size_t getBufferView(std::vector<BufferView>& views, BufferView::Kind kind, int variant, size_t stride, bool compressed)
- {
- if (variant >= 0)
- {
- for (size_t i = 0; i < views.size(); ++i)
- if (views[i].kind == kind && views[i].variant == variant && views[i].stride == stride && views[i].compressed == compressed)
- return i;
- }
- BufferView view = {kind, variant, stride, compressed};
- views.push_back(view);
- return views.size() - 1;
- }
- void writeBufferView(std::string& json, BufferView::Kind kind, size_t count, size_t stride, size_t bin_offset, size_t bin_size, int compression, size_t compressed_offset, size_t compressed_size)
- {
- assert(bin_size == count * stride);
- // when compression is enabled, we store uncompressed data in buffer 1 and compressed data in buffer 0
- // when compression is disabled, we store uncompressed data in buffer 0
- size_t buffer = compression >= 0 ? 1 : 0;
- append(json, "{\"buffer\":");
- append(json, buffer);
- append(json, ",\"byteOffset\":");
- append(json, bin_offset);
- append(json, ",\"byteLength\":");
- append(json, bin_size);
- if (kind == BufferView::Kind_Vertex)
- {
- append(json, ",\"byteStride\":");
- append(json, stride);
- }
- if (kind == BufferView::Kind_Vertex || kind == BufferView::Kind_Index)
- {
- append(json, ",\"target\":");
- append(json, (kind == BufferView::Kind_Vertex) ? "34962" : "34963");
- }
- if (compression >= 0)
- {
- append(json, ",\"extensions\":{");
- append(json, "\"MESHOPT_compression\":{");
- append(json, "\"buffer\":0");
- append(json, ",\"byteOffset\":");
- append(json, size_t(compressed_offset));
- append(json, ",\"byteLength\":");
- append(json, size_t(compressed_size));
- append(json, ",\"byteStride\":");
- append(json, stride);
- append(json, ",\"mode\":");
- append(json, size_t(compression));
- append(json, ",\"count\":");
- append(json, count);
- append(json, "}}");
- }
- append(json, "}");
- }
- void writeAccessor(std::string& json, size_t view, size_t offset, cgltf_type type, cgltf_component_type component_type, bool normalized, size_t count, const float* min = 0, const float* max = 0, size_t numminmax = 0)
- {
- append(json, "{\"bufferView\":");
- append(json, view);
- append(json, ",\"byteOffset\":");
- append(json, offset);
- append(json, ",\"componentType\":");
- append(json, componentType(component_type));
- append(json, ",\"count\":");
- append(json, count);
- append(json, ",\"type\":\"");
- append(json, shapeType(type));
- append(json, "\"");
- if (normalized)
- {
- append(json, ",\"normalized\":true");
- }
- if (min && max)
- {
- assert(numminmax);
- append(json, ",\"min\":[");
- for (size_t k = 0; k < numminmax; ++k)
- {
- comma(json);
- append(json, min[k]);
- }
- append(json, "],\"max\":[");
- for (size_t k = 0; k < numminmax; ++k)
- {
- comma(json);
- append(json, max[k]);
- }
- append(json, "]");
- }
- append(json, "}");
- }
- float getDelta(const Attr& l, const Attr& r, cgltf_animation_path_type type)
- {
- switch (type)
- {
- case cgltf_animation_path_type_translation:
- return std::max(std::max(fabsf(l.f[0] - r.f[0]), fabsf(l.f[1] - r.f[1])), fabsf(l.f[2] - r.f[2]));
- case cgltf_animation_path_type_rotation:
- return acosf(std::min(1.f, fabsf(l.f[0] * r.f[0] + l.f[1] * r.f[1] + l.f[2] * r.f[2] + l.f[3] * r.f[3])));
- case cgltf_animation_path_type_scale:
- return std::max(std::max(fabsf(l.f[0] / r.f[0] - 1), fabsf(l.f[1] / r.f[1] - 1)), fabsf(l.f[2] / r.f[2] - 1));
- case cgltf_animation_path_type_weights:
- return fabsf(l.f[0] - r.f[0]);
- default:
- assert(!"Uknown animation path");
- return 0;
- }
- }
- float getDeltaTolerance(cgltf_animation_path_type type)
- {
- switch (type)
- {
- case cgltf_animation_path_type_translation:
- return 0.001f; // linear
- case cgltf_animation_path_type_rotation:
- return 0.001f; // radians
- case cgltf_animation_path_type_scale:
- return 0.001f; // ratio
- case cgltf_animation_path_type_weights:
- return 0.001f; // linear
- default:
- assert(!"Uknown animation path");
- return 0;
- }
- }
- Attr interpolateLinear(const Attr& l, const Attr& r, float t, cgltf_animation_path_type type)
- {
- if (type == cgltf_animation_path_type_rotation)
- {
- // Approximating slerp, https://zeux.io/2015/07/23/approximating-slerp/
- // We also handle quaternion double-cover
- float ca = l.f[0] * r.f[0] + l.f[1] * r.f[1] + l.f[2] * r.f[2] + l.f[3] * r.f[3];
- float d = fabsf(ca);
- float A = 1.0904f + d * (-3.2452f + d * (3.55645f - d * 1.43519f));
- float B = 0.848013f + d * (-1.06021f + d * 0.215638f);
- float k = A * (t - 0.5f) * (t - 0.5f) + B;
- float ot = t + t * (t - 0.5f) * (t - 1) * k;
- float t0 = 1 - ot;
- float t1 = ca > 0 ? ot : -ot;
- Attr lerp = {{
- l.f[0] * t0 + r.f[0] * t1,
- l.f[1] * t0 + r.f[1] * t1,
- l.f[2] * t0 + r.f[2] * t1,
- l.f[3] * t0 + r.f[3] * t1,
- }};
- float len = sqrtf(lerp.f[0] * lerp.f[0] + lerp.f[1] * lerp.f[1] + lerp.f[2] * lerp.f[2] + lerp.f[3] * lerp.f[3]);
- if (len > 0.f)
- {
- lerp.f[0] /= len;
- lerp.f[1] /= len;
- lerp.f[2] /= len;
- lerp.f[3] /= len;
- }
- return lerp;
- }
- else
- {
- Attr lerp = {{
- l.f[0] * (1 - t) + r.f[0] * t,
- l.f[1] * (1 - t) + r.f[1] * t,
- l.f[2] * (1 - t) + r.f[2] * t,
- l.f[3] * (1 - t) + r.f[3] * t,
- }};
- return lerp;
- }
- }
- Attr interpolateHermite(const Attr& v0, const Attr& t0, const Attr& v1, const Attr& t1, float t, float dt, cgltf_animation_path_type type)
- {
- float s0 = 1 + t * t * (2 * t - 3);
- float s1 = t + t * t * (t - 2);
- float s2 = 1 - s0;
- float s3 = t * t * (t - 1);
- float ts1 = dt * s1;
- float ts3 = dt * s3;
- Attr lerp = {{
- s0 * v0.f[0] + ts1 * t0.f[0] + s2 * v1.f[0] + ts3 * t1.f[0],
- s0 * v0.f[1] + ts1 * t0.f[1] + s2 * v1.f[1] + ts3 * t1.f[1],
- s0 * v0.f[2] + ts1 * t0.f[2] + s2 * v1.f[2] + ts3 * t1.f[2],
- s0 * v0.f[3] + ts1 * t0.f[3] + s2 * v1.f[3] + ts3 * t1.f[3],
- }};
- if (type == cgltf_animation_path_type_rotation)
- {
- float len = sqrtf(lerp.f[0] * lerp.f[0] + lerp.f[1] * lerp.f[1] + lerp.f[2] * lerp.f[2] + lerp.f[3] * lerp.f[3]);
- if (len > 0.f)
- {
- lerp.f[0] /= len;
- lerp.f[1] /= len;
- lerp.f[2] /= len;
- lerp.f[3] /= len;
- }
- }
- return lerp;
- }
- void resampleKeyframes(std::vector<Attr>& data, const std::vector<float>& input, const std::vector<Attr>& output, cgltf_animation_path_type type, cgltf_interpolation_type interpolation, size_t components, int frames, float mint, int freq)
- {
- size_t cursor = 0;
- for (int i = 0; i < frames; ++i)
- {
- float time = mint + float(i) / freq;
- while (cursor + 1 < input.size())
- {
- float next_time = input[cursor + 1];
- if (next_time > time)
- break;
- cursor++;
- }
- if (cursor + 1 < input.size())
- {
- float cursor_time = input[cursor + 0];
- float next_time = input[cursor + 1];
- float range = next_time - cursor_time;
- float inv_range = (range == 0.f) ? 0.f : 1.f / (next_time - cursor_time);
- float t = std::max(0.f, std::min(1.f, (time - cursor_time) * inv_range));
- for (size_t j = 0; j < components; ++j)
- {
- switch (interpolation)
- {
- case cgltf_interpolation_type_linear:
- {
- const Attr& v0 = output[(cursor + 0) * components + j];
- const Attr& v1 = output[(cursor + 1) * components + j];
- data.push_back(interpolateLinear(v0, v1, t, type));
- }
- break;
- case cgltf_interpolation_type_step:
- {
- const Attr& v = output[cursor * components + j];
- data.push_back(v);
- }
- break;
- case cgltf_interpolation_type_cubic_spline:
- {
- const Attr& v0 = output[(cursor * 3 + 1) * components + j];
- const Attr& b0 = output[(cursor * 3 + 2) * components + j];
- const Attr& a1 = output[(cursor * 3 + 3) * components + j];
- const Attr& v1 = output[(cursor * 3 + 4) * components + j];
- data.push_back(interpolateHermite(v0, b0, v1, a1, t, range, type));
- }
- break;
- default:
- assert(!"Unknown interpolation type");
- }
- }
- }
- else
- {
- size_t offset = (interpolation == cgltf_interpolation_type_cubic_spline) ? cursor * 3 + 1 : cursor;
- for (size_t j = 0; j < components; ++j)
- {
- const Attr& v = output[offset * components + j];
- data.push_back(v);
- }
- }
- }
- }
- bool isTrackEqual(const std::vector<Attr>& data, cgltf_animation_path_type type, int frames, const Attr* value, size_t components)
- {
- assert(data.size() == frames * components);
- float tolerance = getDeltaTolerance(type);
- for (int i = 0; i < frames; ++i)
- {
- for (size_t j = 0; j < components; ++j)
- {
- float delta = getDelta(value[j], data[i * components + j], type);
- if (delta > tolerance)
- return false;
- }
- }
- return true;
- }
- void getBaseTransform(Attr* result, size_t components, cgltf_animation_path_type type, cgltf_node* node)
- {
- switch (type)
- {
- case cgltf_animation_path_type_translation:
- memcpy(result->f, node->translation, 3 * sizeof(float));
- break;
- case cgltf_animation_path_type_rotation:
- memcpy(result->f, node->rotation, 4 * sizeof(float));
- break;
- case cgltf_animation_path_type_scale:
- memcpy(result->f, node->scale, 3 * sizeof(float));
- break;
- case cgltf_animation_path_type_weights:
- if (node->weights_count)
- {
- assert(node->weights_count == components);
- memcpy(result->f, node->weights, components * sizeof(float));
- }
- else if (node->mesh && node->mesh->weights_count)
- {
- assert(node->mesh->weights_count == components);
- memcpy(result->f, node->mesh->weights, components * sizeof(float));
- }
- break;
- default:
- assert(!"Unknown animation path");
- }
- }
- void processAnimation(Animation& animation, const Settings& settings)
- {
- float mint = 0, maxt = 0;
- for (size_t i = 0; i < animation.tracks.size(); ++i)
- {
- const Track& track = animation.tracks[i];
- assert(!track.time.empty());
- mint = std::min(mint, track.time.front());
- maxt = std::max(maxt, track.time.back());
- }
- // round the number of frames to nearest but favor the "up" direction
- // this means that at 10 Hz resampling, we will try to preserve the last frame <10ms
- // but if the last frame is <2ms we favor just removing this data
- int frames = 1 + int((maxt - mint) * settings.anim_freq + 0.8f);
- animation.start = mint;
- animation.frames = frames;
- std::vector<Attr> base;
- for (size_t i = 0; i < animation.tracks.size(); ++i)
- {
- Track& track = animation.tracks[i];
- std::vector<Attr> result;
- resampleKeyframes(result, track.time, track.data, track.path, track.interpolation, track.components, frames, mint, settings.anim_freq);
- track.time.clear();
- track.data.swap(result);
- if (isTrackEqual(track.data, track.path, frames, &track.data[0], track.components))
- {
- // track is constant (equal to first keyframe), we only need the first keyframe
- track.data.resize(track.components);
- // track.dummy is true iff track redundantly sets up the value to be equal to default node transform
- base.resize(track.components);
- getBaseTransform(&base[0], track.components, track.path, track.node);
- track.dummy = isTrackEqual(track.data, track.path, 1, &base[0], track.components);
- }
- }
- }
- void markAnimated(cgltf_data* data, std::vector<NodeInfo>& nodes, const std::vector<Animation>& animations)
- {
- for (size_t i = 0; i < animations.size(); ++i)
- {
- const Animation& animation = animations[i];
- for (size_t j = 0; j < animation.tracks.size(); ++j)
- {
- const Track& track = animation.tracks[j];
- // mark nodes that have animation tracks that change their base transform as animated
- if (!track.dummy)
- {
- NodeInfo& ni = nodes[track.node - data->nodes];
- ni.animated_paths |= (1 << track.path);
- }
- }
- }
- for (size_t i = 0; i < data->nodes_count; ++i)
- {
- NodeInfo& ni = nodes[i];
- for (cgltf_node* node = &data->nodes[i]; node; node = node->parent)
- ni.animated |= nodes[node - data->nodes].animated_paths != 0;
- }
- }
- void markNeededNodes(cgltf_data* data, std::vector<NodeInfo>& nodes, const std::vector<Mesh>& meshes, const std::vector<Animation>& animations, const Settings& settings)
- {
- // mark all joints as kept
- for (size_t i = 0; i < data->skins_count; ++i)
- {
- const cgltf_skin& skin = data->skins[i];
- // for now we keep all joints directly referenced by the skin and the entire ancestry tree; we keep names for joints as well
- for (size_t j = 0; j < skin.joints_count; ++j)
- {
- NodeInfo& ni = nodes[skin.joints[j] - data->nodes];
- ni.keep = true;
- }
- }
- // mark all animated nodes as kept
- for (size_t i = 0; i < animations.size(); ++i)
- {
- const Animation& animation = animations[i];
- for (size_t j = 0; j < animation.tracks.size(); ++j)
- {
- const Track& track = animation.tracks[j];
- if (settings.anim_const || !track.dummy)
- {
- NodeInfo& ni = nodes[track.node - data->nodes];
- ni.keep = true;
- }
- }
- }
- // mark all mesh nodes as kept
- for (size_t i = 0; i < meshes.size(); ++i)
- {
- const Mesh& mesh = meshes[i];
- if (mesh.node)
- {
- NodeInfo& ni = nodes[mesh.node - data->nodes];
- ni.keep = true;
- }
- }
- // mark all light/camera nodes as kept
- for (size_t i = 0; i < data->nodes_count; ++i)
- {
- const cgltf_node& node = data->nodes[i];
- if (node.light || node.camera)
- {
- nodes[i].keep = true;
- }
- }
- // mark all named nodes as needed (if -kn is specified)
- if (settings.keep_named)
- {
- for (size_t i = 0; i < data->nodes_count; ++i)
- {
- const cgltf_node& node = data->nodes[i];
- if (node.name && *node.name)
- {
- nodes[i].keep = true;
- }
- }
- }
- }
- void markNeededMaterials(cgltf_data* data, std::vector<MaterialInfo>& materials, const std::vector<Mesh>& meshes)
- {
- // mark all used materials as kept
- for (size_t i = 0; i < meshes.size(); ++i)
- {
- const Mesh& mesh = meshes[i];
- if (mesh.material)
- {
- MaterialInfo& mi = materials[mesh.material - data->materials];
- mi.keep = true;
- }
- }
- }
- void remapNodes(cgltf_data* data, std::vector<NodeInfo>& nodes, size_t& node_offset)
- {
- // to keep a node, we currently need to keep the entire ancestry chain
- for (size_t i = 0; i < data->nodes_count; ++i)
- {
- if (!nodes[i].keep)
- continue;
- for (cgltf_node* node = &data->nodes[i]; node; node = node->parent)
- nodes[node - data->nodes].keep = true;
- }
- // generate sequential indices for all nodes; they aren't sorted topologically
- for (size_t i = 0; i < data->nodes_count; ++i)
- {
- NodeInfo& ni = nodes[i];
- if (ni.keep)
- {
- ni.remap = int(node_offset);
- node_offset++;
- }
- }
- }
- void analyzeImages(cgltf_data* data, std::vector<ImageInfo>& images)
- {
- for (size_t i = 0; i < data->materials_count; ++i)
- {
- const cgltf_material& material = data->materials[i];
- if (material.has_pbr_metallic_roughness)
- {
- const cgltf_pbr_metallic_roughness& pbr = material.pbr_metallic_roughness;
- if (pbr.base_color_texture.texture && pbr.base_color_texture.texture->image)
- images[pbr.base_color_texture.texture->image - data->images].srgb = true;
- }
- if (material.has_pbr_specular_glossiness)
- {
- const cgltf_pbr_specular_glossiness& pbr = material.pbr_specular_glossiness;
- if (pbr.diffuse_texture.texture && pbr.diffuse_texture.texture->image)
- images[pbr.diffuse_texture.texture->image - data->images].srgb = true;
- }
- if (material.emissive_texture.texture && material.emissive_texture.texture->image)
- images[material.emissive_texture.texture->image - data->images].srgb = true;
- if (material.normal_texture.texture && material.normal_texture.texture->image)
- images[material.normal_texture.texture->image - data->images].normal_map = true;
- }
- }
- bool parseDataUri(const char* uri, std::string& mime_type, std::string& result)
- {
- if (strncmp(uri, "data:", 5) == 0)
- {
- const char* comma = strchr(uri, ',');
- if (comma && comma - uri >= 7 && strncmp(comma - 7, ";base64", 7) == 0)
- {
- const char* base64 = comma + 1;
- size_t base64_size = strlen(base64);
- size_t size = base64_size - base64_size / 4;
- if (base64_size >= 2)
- {
- size -= base64[base64_size - 2] == '=';
- size -= base64[base64_size - 1] == '=';
- }
- void* data = 0;
- cgltf_options options = {};
- cgltf_result res = cgltf_load_buffer_base64(&options, size, base64, &data);
- if (res != cgltf_result_success)
- return false;
- mime_type = std::string(uri + 5, comma - 7);
- result = std::string(static_cast<const char*>(data), size);
- free(data);
- return true;
- }
- }
- return false;
- }
- void writeEmbeddedImage(std::string& json, std::vector<BufferView>& views, const char* data, size_t size, const char* mime_type)
- {
- size_t view = getBufferView(views, BufferView::Kind_Image, -1, 1, false);
- assert(views[view].data.empty());
- views[view].data.assign(data, size);
- append(json, "\"bufferView\":");
- append(json, view);
- append(json, ",\"mimeType\":\"");
- append(json, mime_type);
- append(json, "\"");
- }
- std::string inferMimeType(const char* path)
- {
- const char* ext = strrchr(path, '.');
- if (!ext)
- return "";
- std::string extl = ext + 1;
- for (size_t i = 0; i < extl.length(); ++i)
- extl[i] = char(tolower(extl[i]));
- if (extl == "jpg")
- return "image/jpeg";
- else
- return "image/" + extl;
- }
- std::string getFullPath(const char* path, const char* base_path)
- {
- std::string result = base_path;
- std::string::size_type slash = result.find_last_of("/\\");
- result.erase(slash == std::string::npos ? 0 : slash + 1);
- result += path;
- return result;
- }
- std::string getFileName(const char* path)
- {
- std::string result = path;
- std::string::size_type slash = result.find_last_of("/\\");
- if (slash != std::string::npos)
- result.erase(0, slash + 1);
- std::string::size_type dot = result.find_last_of('.');
- if (dot != std::string::npos)
- result.erase(dot);
- return result;
- }
- bool readFile(const char* path, std::string& data)
- {
- FILE* file = fopen(path, "rb");
- if (!file)
- return false;
- fseek(file, 0, SEEK_END);
- long length = ftell(file);
- fseek(file, 0, SEEK_SET);
- if (length <= 0)
- {
- fclose(file);
- return false;
- }
- data.resize(length);
- size_t result = fread(&data[0], 1, data.size(), file);
- fclose(file);
- return result == data.size();
- }
- bool writeFile(const char* path, const std::string& data)
- {
- FILE* file = fopen(path, "wb");
- if (!file)
- return false;
- size_t result = fwrite(&data[0], 1, data.size(), file);
- fclose(file);
- return result == data.size();
- }
- struct TempFile
- {
- std::string path;
- int fd;
- TempFile(const char* suffix)
- : fd(-1)
- {
- #ifdef _WIN32
- const char* temp_dir = getenv("TEMP");
- path = temp_dir ? temp_dir : ".";
- path += "\\gltfpack-XXXXXX";
- (void)_mktemp(&path[0]);
- path += suffix;
- #else
- path = "/tmp/gltfpack-XXXXXX";
- path += suffix;
- fd = mkstemps(&path[0], strlen(suffix));
- #endif
- }
- ~TempFile()
- {
- unlink(path.c_str());
- #ifndef _WIN32
- close(fd);
- #endif
- }
- };
- bool checkBasis()
- {
- const char* basisu_path = getenv("BASISU_PATH");
- std::string cmd = basisu_path ? basisu_path : "basisu";
- #ifdef _WIN32
- cmd += " 2>nul";
- #else
- cmd += " 2>/dev/null";
- #endif
- FILE* pipe = popen(cmd.c_str(), "r");
- if (!pipe)
- return false;
- char buf[15];
- size_t read = fread(buf, 1, sizeof(buf), pipe);
- pclose(pipe);
- return read == sizeof(buf) && memcmp(buf, "Basis Universal", sizeof(buf)) == 0;
- }
- bool encodeBasis(const std::string& data, std::string& result, bool normal_map, bool srgb, int quality)
- {
- TempFile temp_input(".raw");
- TempFile temp_output(".basis");
- if (!writeFile(temp_input.path.c_str(), data))
- return false;
- const char* basisu_path = getenv("BASISU_PATH");
- std::string cmd = basisu_path ? basisu_path : "basisu";
- char ql[16];
- sprintf(ql, "%d", (quality * 255 + 50) / 100);
- cmd += " -q ";
- cmd += ql;
- cmd += " -mipmap";
- if (normal_map)
- {
- cmd += " -normal_map";
- // for optimal quality we should specify seperate_rg_to_color_alpha but this requires renderer awareness
- }
- else if (!srgb)
- {
- cmd += " -linear";
- }
- cmd += " -file ";
- cmd += temp_input.path;
- cmd += " -output_file ";
- cmd += temp_output.path;
- #ifdef _WIN32
- cmd += " >nul";
- #else
- cmd += " >/dev/null";
- #endif
- int rc = system(cmd.c_str());
- return rc == 0 && readFile(temp_output.path.c_str(), result);
- }
- // basistoktx.cpp
- extern std::string basisToKtx(const std::string& basis, bool srgb);
- void writeImage(std::string& json, std::vector<BufferView>& views, const cgltf_image& image, const ImageInfo& info, size_t index, const char* input_path, const char* output_path, const Settings& settings)
- {
- std::string img_data;
- std::string mime_type;
- if (image.uri && parseDataUri(image.uri, mime_type, img_data))
- {
- // we will re-embed img_data below
- }
- else if (image.buffer_view && image.buffer_view->buffer->data && image.mime_type)
- {
- const cgltf_buffer_view* view = image.buffer_view;
- img_data.assign(static_cast<const char*>(view->buffer->data) + view->offset, view->size);
- mime_type = image.mime_type;
- }
- else if (image.uri && settings.texture_embed)
- {
- std::string full_path = getFullPath(image.uri, input_path);
- if (!readFile(full_path.c_str(), img_data))
- {
- fprintf(stderr, "Warning: unable to read image %s, skipping\n", image.uri);
- }
- mime_type = inferMimeType(image.uri);
- }
- if (!img_data.empty())
- {
- if (settings.texture_basis)
- {
- std::string encoded;
- if (encodeBasis(img_data, encoded, info.normal_map, info.srgb, settings.texture_quality))
- {
- if (settings.texture_ktx2)
- encoded = basisToKtx(encoded, info.srgb);
- writeEmbeddedImage(json, views, encoded.c_str(), encoded.size(), settings.texture_ktx2 ? "image/ktx2" : "image/basis");
- }
- else
- {
- fprintf(stderr, "Warning: unable to encode image %d with Basis, skipping\n", int(index));
- }
- }
- else
- {
- writeEmbeddedImage(json, views, img_data.c_str(), img_data.size(), mime_type.c_str());
- }
- }
- else if (image.uri)
- {
- if (settings.texture_basis)
- {
- std::string full_path = getFullPath(image.uri, input_path);
- std::string basis_path = getFileName(image.uri) + (settings.texture_ktx2 ? ".ktx2" : ".basis");
- std::string basis_full_path = getFullPath(basis_path.c_str(), output_path);
- if (readFile(full_path.c_str(), img_data))
- {
- std::string encoded;
- if (encodeBasis(img_data, encoded, info.normal_map, info.srgb, settings.texture_quality))
- {
- if (settings.texture_ktx2)
- encoded = basisToKtx(encoded, info.srgb);
- if (writeFile(basis_full_path.c_str(), encoded))
- {
- append(json, "\"uri\":\"");
- append(json, basis_path);
- append(json, "\"");
- }
- else
- {
- fprintf(stderr, "Warning: unable to save Basis image %s, skipping\n", image.uri);
- }
- }
- else
- {
- fprintf(stderr, "Warning: unable to encode image %s with Basis, skipping\n", image.uri);
- }
- }
- else
- {
- fprintf(stderr, "Warning: unable to read image %s, skipping\n", image.uri);
- }
- }
- else
- {
- append(json, "\"uri\":\"");
- append(json, image.uri);
- append(json, "\"");
- }
- }
- else
- {
- fprintf(stderr, "Warning: ignoring image %d since it has no URI and no valid buffer data\n", int(index));
- }
- }
- void writeTexture(std::string& json, const cgltf_texture& texture, cgltf_data* data, const Settings& settings)
- {
- if (texture.image)
- {
- if (settings.texture_ktx2)
- {
- append(json, "\"extensions\":{\"KHR_texture_basisu\":{\"source\":");
- append(json, size_t(texture.image - data->images));
- append(json, "}}");
- }
- else
- {
- append(json, "\"source\":");
- append(json, size_t(texture.image - data->images));
- }
- }
- }
- void writeMeshAttributes(std::string& json, std::vector<BufferView>& views, std::string& json_accessors, size_t& accr_offset, const Mesh& mesh, int target, const QuantizationParams& qp, const Settings& settings)
- {
- std::string scratch;
- for (size_t j = 0; j < mesh.streams.size(); ++j)
- {
- const Stream& stream = mesh.streams[j];
- if (stream.target != target)
- continue;
- scratch.clear();
- StreamFormat format = writeVertexStream(scratch, stream, qp, settings, mesh.targets > 0);
- size_t view = getBufferView(views, BufferView::Kind_Vertex, stream.type, format.stride, settings.compress);
- size_t offset = views[view].data.size();
- views[view].data += scratch;
- comma(json_accessors);
- if (stream.type == cgltf_attribute_type_position)
- {
- int min[3] = {};
- int max[3] = {};
- getPositionBounds(min, max, stream, qp);
- float minf[3] = {float(min[0]), float(min[1]), float(min[2])};
- float maxf[3] = {float(max[0]), float(max[1]), float(max[2])};
- writeAccessor(json_accessors, view, offset, format.type, format.component_type, format.normalized, stream.data.size(), minf, maxf, 3);
- }
- else
- {
- writeAccessor(json_accessors, view, offset, format.type, format.component_type, format.normalized, stream.data.size());
- }
- size_t vertex_accr = accr_offset++;
- comma(json);
- append(json, "\"");
- append(json, attributeType(stream.type));
- if (stream.type != cgltf_attribute_type_position && stream.type != cgltf_attribute_type_normal && stream.type != cgltf_attribute_type_tangent)
- {
- append(json, "_");
- append(json, size_t(stream.index));
- }
- append(json, "\":");
- append(json, vertex_accr);
- }
- }
- size_t writeMeshIndices(std::vector<BufferView>& views, std::string& json_accessors, size_t& accr_offset, const Mesh& mesh, const Settings& settings)
- {
- std::string scratch;
- StreamFormat format = writeIndexStream(scratch, mesh.indices);
- // note: we prefer to merge all index streams together; however, index codec currently doesn't handle concatenated index streams well and loses compression ratio
- int variant = settings.compress ? -1 : 0;
- size_t view = getBufferView(views, BufferView::Kind_Index, variant, format.stride, settings.compress);
- size_t offset = views[view].data.size();
- views[view].data += scratch;
- comma(json_accessors);
- writeAccessor(json_accessors, view, offset, format.type, format.component_type, format.normalized, mesh.indices.size());
- size_t index_accr = accr_offset++;
- return index_accr;
- }
- size_t writeAnimationTime(std::vector<BufferView>& views, std::string& json_accessors, size_t& accr_offset, float mint, int frames, const Settings& settings)
- {
- std::vector<float> time(frames);
- for (int j = 0; j < frames; ++j)
- time[j] = mint + float(j) / settings.anim_freq;
- std::string scratch;
- StreamFormat format = writeTimeStream(scratch, time);
- size_t view = getBufferView(views, BufferView::Kind_Time, 0, format.stride, settings.compress);
- size_t offset = views[view].data.size();
- views[view].data += scratch;
- comma(json_accessors);
- writeAccessor(json_accessors, view, offset, cgltf_type_scalar, format.component_type, format.normalized, frames, &time.front(), &time.back(), 1);
- size_t time_accr = accr_offset++;
- return time_accr;
- }
- size_t writeJointBindMatrices(std::vector<BufferView>& views, std::string& json_accessors, size_t& accr_offset, const cgltf_skin& skin, const QuantizationParams& qp, const Settings& settings)
- {
- std::string scratch;
- for (size_t j = 0; j < skin.joints_count; ++j)
- {
- float transform[16] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};
- if (skin.inverse_bind_matrices)
- {
- cgltf_accessor_read_float(skin.inverse_bind_matrices, j, transform, 16);
- }
- float node_scale = qp.pos_scale / float((1 << qp.pos_bits) - 1);
- // pos_offset has to be applied first, thus it results in an offset rotated by the bind matrix
- transform[12] += qp.pos_offset[0] * transform[0] + qp.pos_offset[1] * transform[4] + qp.pos_offset[2] * transform[8];
- transform[13] += qp.pos_offset[0] * transform[1] + qp.pos_offset[1] * transform[5] + qp.pos_offset[2] * transform[9];
- transform[14] += qp.pos_offset[0] * transform[2] + qp.pos_offset[1] * transform[6] + qp.pos_offset[2] * transform[10];
- // node_scale will be applied before the rotation/scale from transform
- for (int k = 0; k < 12; ++k)
- transform[k] *= node_scale;
- scratch.append(reinterpret_cast<const char*>(transform), sizeof(transform));
- }
- size_t view = getBufferView(views, BufferView::Kind_Skin, 0, 64, settings.compress);
- size_t offset = views[view].data.size();
- views[view].data += scratch;
- comma(json_accessors);
- writeAccessor(json_accessors, view, offset, cgltf_type_mat4, cgltf_component_type_r_32f, false, skin.joints_count);
- size_t matrix_accr = accr_offset++;
- return matrix_accr;
- }
- void writeMeshNode(std::string& json, size_t mesh_offset, const Mesh& mesh, cgltf_data* data, const QuantizationParams& qp)
- {
- float node_scale = qp.pos_scale / float((1 << qp.pos_bits) - 1);
- comma(json);
- append(json, "{\"mesh\":");
- append(json, mesh_offset);
- if (mesh.skin)
- {
- comma(json);
- append(json, "\"skin\":");
- append(json, size_t(mesh.skin - data->skins));
- }
- append(json, ",\"translation\":[");
- append(json, qp.pos_offset[0]);
- append(json, ",");
- append(json, qp.pos_offset[1]);
- append(json, ",");
- append(json, qp.pos_offset[2]);
- append(json, "],\"scale\":[");
- append(json, node_scale);
- append(json, ",");
- append(json, node_scale);
- append(json, ",");
- append(json, node_scale);
- append(json, "]");
- if (mesh.node && mesh.node->weights_count)
- {
- append(json, ",\"weights\":[");
- for (size_t j = 0; j < mesh.node->weights_count; ++j)
- {
- comma(json);
- append(json, mesh.node->weights[j]);
- }
- append(json, "]");
- }
- append(json, "}");
- }
- void writeNode(std::string& json, const cgltf_node& node, const std::vector<NodeInfo>& nodes, cgltf_data* data)
- {
- const NodeInfo& ni = nodes[&node - data->nodes];
- comma(json);
- append(json, "{");
- if (node.name && *node.name)
- {
- comma(json);
- append(json, "\"name\":\"");
- append(json, node.name);
- append(json, "\"");
- }
- if (node.has_translation)
- {
- comma(json);
- append(json, "\"translation\":[");
- append(json, node.translation[0]);
- append(json, ",");
- append(json, node.translation[1]);
- append(json, ",");
- append(json, node.translation[2]);
- append(json, "]");
- }
- if (node.has_rotation)
- {
- comma(json);
- append(json, "\"rotation\":[");
- append(json, node.rotation[0]);
- append(json, ",");
- append(json, node.rotation[1]);
- append(json, ",");
- append(json, node.rotation[2]);
- append(json, ",");
- append(json, node.rotation[3]);
- append(json, "]");
- }
- if (node.has_scale)
- {
- comma(json);
- append(json, "\"scale\":[");
- append(json, node.scale[0]);
- append(json, ",");
- append(json, node.scale[1]);
- append(json, ",");
- append(json, node.scale[2]);
- append(json, "]");
- }
- if (node.has_matrix)
- {
- comma(json);
- append(json, "\"matrix\":[");
- for (int k = 0; k < 16; ++k)
- {
- comma(json);
- append(json, node.matrix[k]);
- }
- append(json, "]");
- }
- if (node.children_count || !ni.meshes.empty())
- {
- comma(json);
- append(json, "\"children\":[");
- for (size_t j = 0; j < node.children_count; ++j)
- {
- const NodeInfo& ci = nodes[node.children[j] - data->nodes];
- if (ci.keep)
- {
- comma(json);
- append(json, size_t(ci.remap));
- }
- }
- for (size_t j = 0; j < ni.meshes.size(); ++j)
- {
- comma(json);
- append(json, ni.meshes[j]);
- }
- append(json, "]");
- }
- if (node.camera)
- {
- comma(json);
- append(json, "\"camera\":");
- append(json, size_t(node.camera - data->cameras));
- }
- if (node.light)
- {
- comma(json);
- append(json, "\"extensions\":{\"KHR_lights_punctual\":{\"light\":");
- append(json, size_t(node.light - data->lights));
- append(json, "}}");
- }
- append(json, "}");
- }
- void writeAnimation(std::string& json, std::vector<BufferView>& views, std::string& json_accessors, size_t& accr_offset, const Animation& animation, size_t i, cgltf_data* data, const std::vector<NodeInfo>& nodes, const Settings& settings)
- {
- std::vector<const Track*> tracks;
- for (size_t j = 0; j < animation.tracks.size(); ++j)
- {
- const Track& track = animation.tracks[j];
- const NodeInfo& ni = nodes[track.node - data->nodes];
- if (!ni.keep)
- continue;
- if (!settings.anim_const && (ni.animated_paths & (1 << track.path)) == 0)
- continue;
- tracks.push_back(&track);
- }
- if (tracks.empty())
- {
- fprintf(stderr, "Warning: ignoring animation %d because it has no valid tracks\n", int(i));
- return;
- }
- bool needs_time = false;
- bool needs_pose = false;
- for (size_t j = 0; j < tracks.size(); ++j)
- {
- const Track& track = *tracks[j];
- bool tc = track.data.size() == track.components;
- needs_time = needs_time || !tc;
- needs_pose = needs_pose || tc;
- }
- size_t time_accr = needs_time ? writeAnimationTime(views, json_accessors, accr_offset, animation.start, animation.frames, settings) : 0;
- size_t pose_accr = needs_pose ? writeAnimationTime(views, json_accessors, accr_offset, animation.start, 1, settings) : 0;
- std::string json_samplers;
- std::string json_channels;
- size_t track_offset = 0;
- for (size_t j = 0; j < tracks.size(); ++j)
- {
- const Track& track = *tracks[j];
- bool tc = track.data.size() == track.components;
- std::string scratch;
- StreamFormat format = writeKeyframeStream(scratch, track.path, track.data);
- size_t view = getBufferView(views, BufferView::Kind_Keyframe, track.path, format.stride, settings.compress && track.path != cgltf_animation_path_type_weights);
- size_t offset = views[view].data.size();
- views[view].data += scratch;
- comma(json_accessors);
- writeAccessor(json_accessors, view, offset, format.type, format.component_type, format.normalized, track.data.size());
- size_t data_accr = accr_offset++;
- comma(json_samplers);
- append(json_samplers, "{\"input\":");
- append(json_samplers, tc ? pose_accr : time_accr);
- append(json_samplers, ",\"output\":");
- append(json_samplers, data_accr);
- append(json_samplers, "}");
- const NodeInfo& tni = nodes[track.node - data->nodes];
- size_t target_node = size_t(tni.remap);
- if (track.path == cgltf_animation_path_type_weights)
- {
- assert(tni.meshes.size() == 1);
- target_node = tni.meshes[0];
- }
- comma(json_channels);
- append(json_channels, "{\"sampler\":");
- append(json_channels, track_offset);
- append(json_channels, ",\"target\":{\"node\":");
- append(json_channels, target_node);
- append(json_channels, ",\"path\":\"");
- append(json_channels, animationPath(track.path));
- append(json_channels, "\"}}");
- track_offset++;
- }
- comma(json);
- append(json, "{");
- if (animation.name && *animation.name)
- {
- append(json, "\"name\":\"");
- append(json, animation.name);
- append(json, "\",");
- }
- append(json, "\"samplers\":[");
- append(json, json_samplers);
- append(json, "],\"channels\":[");
- append(json, json_channels);
- append(json, "]}");
- }
- void writeCamera(std::string& json, const cgltf_camera& camera)
- {
- comma(json);
- append(json, "{");
- switch (camera.type)
- {
- case cgltf_camera_type_perspective:
- append(json, "\"type\":\"perspective\",\"perspective\":{");
- append(json, "\"yfov\":");
- append(json, camera.data.perspective.yfov);
- append(json, ",\"znear\":");
- append(json, camera.data.perspective.znear);
- if (camera.data.perspective.aspect_ratio != 0.f)
- {
- append(json, ",\"aspectRatio\":");
- append(json, camera.data.perspective.aspect_ratio);
- }
- if (camera.data.perspective.zfar != 0.f)
- {
- append(json, ",\"zfar\":");
- append(json, camera.data.perspective.zfar);
- }
- append(json, "}");
- break;
- case cgltf_camera_type_orthographic:
- append(json, "\"type\":\"orthographic\",\"orthographic\":{");
- append(json, "\"xmag\":");
- append(json, camera.data.orthographic.xmag);
- append(json, ",\"ymag\":");
- append(json, camera.data.orthographic.ymag);
- append(json, ",\"znear\":");
- append(json, camera.data.orthographic.znear);
- append(json, ",\"zfar\":");
- append(json, camera.data.orthographic.zfar);
- append(json, "}");
- break;
- default:
- fprintf(stderr, "Warning: skipping camera of unknown type\n");
- }
- append(json, "}");
- }
- void writeLight(std::string& json, const cgltf_light& light)
- {
- static const float white[3] = {1, 1, 1};
- comma(json);
- append(json, "{\"type\":\"");
- append(json, lightType(light.type));
- append(json, "\"");
- if (memcmp(light.color, white, sizeof(white)) != 0)
- {
- comma(json);
- append(json, "\"color\":[");
- append(json, light.color[0]);
- append(json, ",");
- append(json, light.color[1]);
- append(json, ",");
- append(json, light.color[2]);
- append(json, "]");
- }
- if (light.intensity != 1.f)
- {
- comma(json);
- append(json, "\"intensity\":");
- append(json, light.intensity);
- }
- if (light.range != 0.f)
- {
- comma(json);
- append(json, "\"range\":");
- append(json, light.range);
- }
- if (light.type == cgltf_light_type_spot)
- {
- comma(json);
- append(json, "\"spot\":{");
- append(json, "\"innerConeAngle\":");
- append(json, light.spot_inner_cone_angle);
- append(json, ",\"outerConeAngle\":");
- append(json, light.spot_outer_cone_angle == 0.f ? 0.78539816339f : light.spot_outer_cone_angle);
- append(json, "}");
- }
- append(json, "}");
- }
- void writeArray(std::string& json, const char* name, const std::string& contents)
- {
- if (contents.empty())
- return;
- comma(json);
- append(json, "\"");
- append(json, name);
- append(json, "\":[");
- append(json, contents);
- append(json, "]");
- }
- void writeExtensions(std::string& json, const ExtensionInfo* extensions, size_t count)
- {
- comma(json);
- append(json, "\"extensionsUsed\":[");
- for (size_t i = 0; i < count; ++i)
- if (extensions[i].used)
- {
- comma(json);
- append(json, "\"");
- append(json, extensions[i].name);
- append(json, "\"");
- }
- append(json, "]");
- comma(json);
- append(json, "\"extensionsRequired\":[");
- for (size_t i = 0; i < count; ++i)
- if (extensions[i].used && extensions[i].required)
- {
- comma(json);
- append(json, "\"");
- append(json, extensions[i].name);
- append(json, "\"");
- }
- append(json, "]");
- }
- void finalizeBufferViews(std::string& json, std::vector<BufferView>& views, std::string& bin, std::string& fallback)
- {
- for (size_t i = 0; i < views.size(); ++i)
- {
- BufferView& view = views[i];
- size_t bin_offset = bin.size();
- size_t fallback_offset = fallback.size();
- size_t count = view.data.size() / view.stride;
- int compression = -1;
- if (view.compressed)
- {
- if (view.kind == BufferView::Kind_Index)
- {
- compressIndexStream(bin, view.data, count, view.stride);
- compression = 1;
- }
- else
- {
- compressVertexStream(bin, view.data, count, view.stride);
- compression = 0;
- }
- fallback += view.data;
- }
- else
- {
- bin += view.data;
- }
- size_t raw_offset = (compression >= 0) ? fallback_offset : bin_offset;
- comma(json);
- writeBufferView(json, view.kind, count, view.stride, raw_offset, view.data.size(), compression, bin_offset, bin.size() - bin_offset);
- // record written bytes for statistics
- view.bytes = bin.size() - bin_offset;
- // align each bufferView by 4 bytes
- bin.resize((bin.size() + 3) & ~3);
- fallback.resize((fallback.size() + 3) & ~3);
- }
- }
- void printMeshStats(const std::vector<Mesh>& meshes, const char* name)
- {
- size_t triangles = 0;
- size_t vertices = 0;
- for (size_t i = 0; i < meshes.size(); ++i)
- {
- const Mesh& mesh = meshes[i];
- triangles += mesh.indices.size() / 3;
- vertices += mesh.streams.empty() ? 0 : mesh.streams[0].data.size();
- }
- printf("%s: %d triangles, %d vertices\n", name, int(triangles), int(vertices));
- }
- void printSceneStats(const std::vector<BufferView>& views, const std::vector<Mesh>& meshes, size_t node_offset, size_t mesh_offset, size_t material_offset, size_t json_size, size_t bin_size)
- {
- size_t bytes[BufferView::Kind_Count] = {};
- for (size_t i = 0; i < views.size(); ++i)
- {
- const BufferView& view = views[i];
- bytes[view.kind] += view.bytes;
- }
- printf("output: %d nodes, %d meshes (%d primitives), %d materials\n", int(node_offset), int(mesh_offset), int(meshes.size()), int(material_offset));
- printf("output: JSON %d bytes, buffers %d bytes\n", int(json_size), int(bin_size));
- printf("output: buffers: vertex %d bytes, index %d bytes, skin %d bytes, time %d bytes, keyframe %d bytes, image %d bytes\n",
- int(bytes[BufferView::Kind_Vertex]), int(bytes[BufferView::Kind_Index]), int(bytes[BufferView::Kind_Skin]),
- int(bytes[BufferView::Kind_Time]), int(bytes[BufferView::Kind_Keyframe]), int(bytes[BufferView::Kind_Image]));
- }
- void printAttributeStats(const std::vector<BufferView>& views, BufferView::Kind kind, const char* name)
- {
- for (size_t i = 0; i < views.size(); ++i)
- {
- const BufferView& view = views[i];
- if (view.kind != kind)
- continue;
- const char* variant = "unknown";
- switch (kind)
- {
- case BufferView::Kind_Vertex:
- variant = attributeType(cgltf_attribute_type(view.variant));
- break;
- case BufferView::Kind_Index:
- variant = "index";
- break;
- case BufferView::Kind_Keyframe:
- variant = animationPath(cgltf_animation_path_type(view.variant));
- break;
- default:;
- }
- size_t count = view.data.size() / view.stride;
- printf("stats: %s %s: compressed %d bytes (%.1f bits), raw %d bytes (%d bits)\n",
- name,
- variant,
- int(view.bytes),
- double(view.bytes) / double(count) * 8,
- int(view.data.size()),
- int(view.stride * 8));
- }
- }
- void process(cgltf_data* data, const char* input_path, const char* output_path, std::vector<Mesh>& meshes, std::vector<Animation>& animations, const Settings& settings, std::string& json, std::string& bin, std::string& fallback)
- {
- if (settings.verbose)
- {
- printf("input: %d nodes, %d meshes (%d primitives), %d materials, %d skins, %d animations\n",
- int(data->nodes_count), int(data->meshes_count), int(meshes.size()), int(data->materials_count), int(data->skins_count), int(animations.size()));
- printMeshStats(meshes, "input");
- }
- for (size_t i = 0; i < animations.size(); ++i)
- {
- processAnimation(animations[i], settings);
- }
- std::vector<NodeInfo> nodes(data->nodes_count);
- markAnimated(data, nodes, animations);
- for (size_t i = 0; i < meshes.size(); ++i)
- {
- Mesh& mesh = meshes[i];
- // note: when -kn is specified, we keep mesh-node attachment so that named nodes can be transformed
- if (mesh.node && !settings.keep_named)
- {
- NodeInfo& ni = nodes[mesh.node - data->nodes];
- // we transform all non-skinned non-animated meshes to world space
- // this makes sure that quantization doesn't introduce gaps if the original scene was watertight
- if (!ni.animated && !mesh.skin && mesh.targets == 0)
- {
- transformMesh(mesh, mesh.node);
- mesh.node = 0;
- }
- // skinned and animated meshes will be anchored to the same node that they used to be in
- // for animated meshes, this is important since they need to be transformed by the same animation
- // for skinned meshes, in theory this isn't important since the transform of the skinned node doesn't matter; in practice this affects generated bounding box in three.js
- }
- }
- mergeMeshMaterials(data, meshes);
- mergeMeshes(meshes, settings);
- filterEmptyMeshes(meshes);
- markNeededNodes(data, nodes, meshes, animations, settings);
- std::vector<MaterialInfo> materials(data->materials_count);
- markNeededMaterials(data, materials, meshes);
- for (size_t i = 0; i < meshes.size(); ++i)
- {
- processMesh(meshes[i], settings);
- }
- filterEmptyMeshes(meshes); // some meshes may become empty after processing
- std::vector<ImageInfo> images(data->images_count);
- analyzeImages(data, images);
- QuantizationParams qp = prepareQuantization(meshes, settings);
- std::string json_images;
- std::string json_textures;
- std::string json_materials;
- std::string json_accessors;
- std::string json_meshes;
- std::string json_nodes;
- std::string json_skins;
- std::string json_roots;
- std::string json_animations;
- std::string json_cameras;
- std::string json_lights;
- std::vector<BufferView> views;
- bool ext_pbr_specular_glossiness = false;
- bool ext_unlit = false;
- size_t accr_offset = 0;
- size_t node_offset = 0;
- size_t mesh_offset = 0;
- size_t material_offset = 0;
- for (size_t i = 0; i < data->images_count; ++i)
- {
- const cgltf_image& image = data->images[i];
- if (settings.verbose && settings.texture_basis)
- {
- const char* uri = image.uri;
- bool embedded = !uri || strncmp(uri, "data:", 5) == 0;
- printf("image %d (%s) is being encoded with Basis\n", int(i), embedded ? "embedded" : uri);
- }
- comma(json_images);
- append(json_images, "{");
- writeImage(json_images, views, image, images[i], i, input_path, output_path, settings);
- append(json_images, "}");
- }
- for (size_t i = 0; i < data->textures_count; ++i)
- {
- const cgltf_texture& texture = data->textures[i];
- comma(json_textures);
- append(json_textures, "{");
- writeTexture(json_textures, texture, data, settings);
- append(json_textures, "}");
- }
- for (size_t i = 0; i < data->materials_count; ++i)
- {
- MaterialInfo& mi = materials[i];
- if (!mi.keep)
- continue;
- const cgltf_material& material = data->materials[i];
- comma(json_materials);
- append(json_materials, "{");
- writeMaterialInfo(json_materials, data, material, qp);
- append(json_materials, "}");
- mi.remap = int(material_offset);
- material_offset++;
- ext_pbr_specular_glossiness = ext_pbr_specular_glossiness || material.has_pbr_specular_glossiness;
- ext_unlit = ext_unlit || material.unlit;
- }
- for (size_t i = 0; i < meshes.size(); ++i)
- {
- const Mesh& mesh = meshes[i];
- comma(json_meshes);
- append(json_meshes, "{\"primitives\":[");
- size_t pi = i;
- for (; pi < meshes.size(); ++pi)
- {
- const Mesh& prim = meshes[pi];
- if (prim.node != mesh.node || prim.skin != mesh.skin || prim.targets != mesh.targets)
- break;
- if (!compareMeshTargets(mesh, prim))
- break;
- comma(json_meshes);
- append(json_meshes, "{\"attributes\":{");
- writeMeshAttributes(json_meshes, views, json_accessors, accr_offset, prim, 0, qp, settings);
- append(json_meshes, "}");
- append(json_meshes, ",\"mode\":");
- append(json_meshes, size_t(prim.type));
- if (mesh.targets)
- {
- append(json_meshes, ",\"targets\":[");
- for (size_t j = 0; j < mesh.targets; ++j)
- {
- comma(json_meshes);
- append(json_meshes, "{");
- writeMeshAttributes(json_meshes, views, json_accessors, accr_offset, prim, int(1 + j), qp, settings);
- append(json_meshes, "}");
- }
- append(json_meshes, "]");
- }
- if (!prim.indices.empty())
- {
- size_t index_accr = writeMeshIndices(views, json_accessors, accr_offset, prim, settings);
- append(json_meshes, ",\"indices\":");
- append(json_meshes, index_accr);
- }
- if (prim.material)
- {
- MaterialInfo& mi = materials[prim.material - data->materials];
- assert(mi.keep);
- append(json_meshes, ",\"material\":");
- append(json_meshes, size_t(mi.remap));
- }
- append(json_meshes, "}");
- }
- append(json_meshes, "]");
- if (mesh.target_weights.size())
- {
- append(json_meshes, ",\"weights\":[");
- for (size_t j = 0; j < mesh.target_weights.size(); ++j)
- {
- comma(json_meshes);
- append(json_meshes, mesh.target_weights[j]);
- }
- append(json_meshes, "]");
- }
- if (mesh.target_names.size())
- {
- append(json_meshes, ",\"extras\":{\"targetNames\":[");
- for (size_t j = 0; j < mesh.target_names.size(); ++j)
- {
- comma(json_meshes);
- append(json_meshes, "\"");
- append(json_meshes, mesh.target_names[j]);
- append(json_meshes, "\"");
- }
- append(json_meshes, "]}");
- }
- append(json_meshes, "}");
- writeMeshNode(json_nodes, mesh_offset, mesh, data, qp);
- if (mesh.node)
- {
- NodeInfo& ni = nodes[mesh.node - data->nodes];
- assert(ni.keep);
- ni.meshes.push_back(node_offset);
- }
- else
- {
- comma(json_roots);
- append(json_roots, node_offset);
- }
- node_offset++;
- mesh_offset++;
- // skip all meshes that we've written in this iteration
- assert(pi > i);
- i = pi - 1;
- }
- remapNodes(data, nodes, node_offset);
- for (size_t i = 0; i < data->nodes_count; ++i)
- {
- NodeInfo& ni = nodes[i];
- if (!ni.keep)
- continue;
- const cgltf_node& node = data->nodes[i];
- if (!node.parent)
- {
- comma(json_roots);
- append(json_roots, size_t(ni.remap));
- }
- writeNode(json_nodes, node, nodes, data);
- }
- for (size_t i = 0; i < data->skins_count; ++i)
- {
- const cgltf_skin& skin = data->skins[i];
- size_t matrix_accr = writeJointBindMatrices(views, json_accessors, accr_offset, skin, qp, settings);
- comma(json_skins);
- append(json_skins, "{");
- append(json_skins, "\"joints\":[");
- for (size_t j = 0; j < skin.joints_count; ++j)
- {
- comma(json_skins);
- append(json_skins, size_t(nodes[skin.joints[j] - data->nodes].remap));
- }
- append(json_skins, "]");
- append(json_skins, ",\"inverseBindMatrices\":");
- append(json_skins, matrix_accr);
- if (skin.skeleton)
- {
- comma(json_skins);
- append(json_skins, "\"skeleton\":");
- append(json_skins, size_t(nodes[skin.skeleton - data->nodes].remap));
- }
- append(json_skins, "}");
- }
- for (size_t i = 0; i < animations.size(); ++i)
- {
- const Animation& animation = animations[i];
- writeAnimation(json_animations, views, json_accessors, accr_offset, animation, i, data, nodes, settings);
- }
- for (size_t i = 0; i < data->cameras_count; ++i)
- {
- const cgltf_camera& camera = data->cameras[i];
- writeCamera(json_cameras, camera);
- }
- for (size_t i = 0; i < data->lights_count; ++i)
- {
- const cgltf_light& light = data->lights[i];
- writeLight(json_lights, light);
- }
- char version[32];
- sprintf(version, "%d.%d", MESHOPTIMIZER_VERSION / 1000, (MESHOPTIMIZER_VERSION % 1000) / 10);
- append(json, "\"asset\":{");
- append(json, "\"version\":\"2.0\",\"generator\":\"gltfpack ");
- append(json, version);
- append(json, "\"");
- if (data->asset.extras.start_offset)
- {
- append(json, ",\"extras\":");
- json.append(data->json + data->asset.extras.start_offset, data->json + data->asset.extras.end_offset);
- }
- append(json, "}");
- const ExtensionInfo extensions[] = {
- {"KHR_mesh_quantization", true, true},
- {"MESHOPT_compression", settings.compress, !settings.fallback},
- {"KHR_texture_transform", !json_textures.empty(), false},
- {"KHR_materials_pbrSpecularGlossiness", ext_pbr_specular_glossiness, false},
- {"KHR_materials_unlit", ext_unlit, false},
- {"KHR_lights_punctual", data->lights_count > 0, false},
- {"KHR_image_ktx2", !json_textures.empty() && settings.texture_ktx2, true},
- {"KHR_texture_basisu", !json_textures.empty() && settings.texture_ktx2, true},
- };
- writeExtensions(json, extensions, sizeof(extensions) / sizeof(extensions[0]));
- std::string json_views;
- finalizeBufferViews(json_views, views, bin, fallback);
- writeArray(json, "bufferViews", json_views);
- writeArray(json, "accessors", json_accessors);
- writeArray(json, "images", json_images);
- writeArray(json, "textures", json_textures);
- writeArray(json, "materials", json_materials);
- writeArray(json, "meshes", json_meshes);
- writeArray(json, "skins", json_skins);
- writeArray(json, "animations", json_animations);
- writeArray(json, "nodes", json_nodes);
- if (!json_roots.empty())
- {
- append(json, ",\"scenes\":[");
- append(json, "{\"nodes\":[");
- append(json, json_roots);
- append(json, "]}]");
- }
- writeArray(json, "cameras", json_cameras);
- if (!json_lights.empty())
- {
- append(json, ",\"extensions\":{\"KHR_lights_punctual\":{\"lights\":[");
- append(json, json_lights);
- append(json, "]}}");
- }
- if (!json_roots.empty())
- {
- append(json, ",\"scene\":0");
- }
- if (settings.verbose)
- {
- printMeshStats(meshes, "output");
- printSceneStats(views, meshes, node_offset, mesh_offset, material_offset, json.size(), bin.size());
- }
- if (settings.verbose > 1)
- {
- printAttributeStats(views, BufferView::Kind_Vertex, "vertex");
- printAttributeStats(views, BufferView::Kind_Index, "index");
- printAttributeStats(views, BufferView::Kind_Keyframe, "keyframe");
- }
- }
- void writeU32(FILE* out, uint32_t data)
- {
- fwrite(&data, 4, 1, out);
- }
- bool requiresExtension(cgltf_data* data, const char* name)
- {
- for (size_t i = 0; i < data->extensions_required_count; ++i)
- if (strcmp(data->extensions_required[i], name) == 0)
- return true;
- return false;
- }
- const char* getBaseName(const char* path)
- {
- const char* slash = strrchr(path, '/');
- const char* backslash = strrchr(path, '\\');
- const char* rs = slash ? slash + 1 : path;
- const char* bs = backslash ? backslash + 1 : path;
- return std::max(rs, bs);
- }
- std::string getBufferSpec(const char* bin_path, size_t bin_size, const char* fallback_path, size_t fallback_size, bool fallback_ref)
- {
- std::string json;
- append(json, "\"buffers\":[");
- append(json, "{");
- if (bin_path)
- {
- append(json, "\"uri\":\"");
- append(json, bin_path);
- append(json, "\"");
- }
- comma(json);
- append(json, "\"byteLength\":");
- append(json, bin_size);
- append(json, "}");
- if (fallback_ref)
- {
- comma(json);
- append(json, "{");
- if (fallback_path)
- {
- append(json, "\"uri\":\"");
- append(json, fallback_path);
- append(json, "\"");
- }
- comma(json);
- append(json, "\"byteLength\":");
- append(json, fallback_size);
- append(json, ",\"extensions\":{");
- append(json, "\"MESHOPT_compression\":{");
- append(json, "\"fallback\":true");
- append(json, "}}");
- append(json, "}");
- }
- append(json, "]");
- return json;
- }
- bool needsDummyBuffers(cgltf_data* data)
- {
- for (size_t i = 0; i < data->accessors_count; ++i)
- {
- cgltf_accessor* accessor = &data->accessors[i];
- if (accessor->buffer_view && accessor->buffer_view->buffer->data == NULL)
- return true;
- if (accessor->is_sparse)
- {
- cgltf_accessor_sparse* sparse = &accessor->sparse;
- if (sparse->indices_buffer_view->buffer->data == NULL)
- return true;
- if (sparse->values_buffer_view->buffer->data == NULL)
- return true;
- }
- }
- return false;
- }
- int gltfpack(const char* input, const char* output, const Settings& settings)
- {
- cgltf_data* data = 0;
- std::vector<Mesh> meshes;
- std::vector<Animation> animations;
- const char* iext = strrchr(input, '.');
- if (iext && (strcmp(iext, ".gltf") == 0 || strcmp(iext, ".GLTF") == 0 || strcmp(iext, ".glb") == 0 || strcmp(iext, ".GLB") == 0))
- {
- cgltf_options options = {};
- cgltf_result result = cgltf_parse_file(&options, input, &data);
- result = (result == cgltf_result_success) ? cgltf_load_buffers(&options, data, input) : result;
- result = (result == cgltf_result_success) ? cgltf_validate(data) : result;
- const char* error = NULL;
- if (result != cgltf_result_success)
- error = getError(result, data);
- else if (requiresExtension(data, "KHR_draco_mesh_compression"))
- error = "file requires Draco mesh compression support";
- else if (requiresExtension(data, "MESHOPT_compression"))
- error = "file has already been compressed using gltfpack";
- else if (needsDummyBuffers(data))
- error = "buffer has no data";
- if (error)
- {
- fprintf(stderr, "Error loading %s: %s\n", input, error);
- cgltf_free(data);
- return 2;
- }
- parseMeshes(data, meshes);
- parseAnimations(data, animations);
- }
- else if (iext && (strcmp(iext, ".obj") == 0 || strcmp(iext, ".OBJ") == 0))
- {
- fastObjMesh* obj = fast_obj_read(input);
- if (!obj)
- {
- fprintf(stderr, "Error loading %s: file not found\n", input);
- cgltf_free(data);
- return 2;
- }
- data = parseSceneObj(obj);
- parseMeshesObj(obj, data, meshes);
- fast_obj_destroy(obj);
- }
- else
- {
- fprintf(stderr, "Error loading %s: unknown extension (expected .gltf or .glb or .obj)\n", input);
- return 2;
- }
- if (data->images_count && settings.texture_basis)
- {
- if (!checkBasis())
- {
- fprintf(stderr, "Error: basisu is not present in PATH or BASISU_PATH is not set\n");
- return 3;
- }
- }
- std::string json, bin, fallback;
- process(data, input, output, meshes, animations, settings, json, bin, fallback);
- cgltf_free(data);
- if (!output)
- {
- return 0;
- }
- const char* oext = strrchr(output, '.');
- if (oext && (strcmp(oext, ".gltf") == 0 || strcmp(oext, ".GLTF") == 0))
- {
- std::string binpath = output;
- binpath.replace(binpath.size() - 5, 5, ".bin");
- std::string fbpath = output;
- fbpath.replace(fbpath.size() - 5, 5, ".fallback.bin");
- FILE* outjson = fopen(output, "wb");
- FILE* outbin = fopen(binpath.c_str(), "wb");
- FILE* outfb = settings.fallback ? fopen(fbpath.c_str(), "wb") : NULL;
- if (!outjson || !outbin || (!outfb && settings.fallback))
- {
- fprintf(stderr, "Error saving %s\n", output);
- return 4;
- }
- std::string bufferspec = getBufferSpec(getBaseName(binpath.c_str()), bin.size(), settings.fallback ? getBaseName(fbpath.c_str()) : NULL, fallback.size(), settings.compress);
- fprintf(outjson, "{");
- fwrite(bufferspec.c_str(), bufferspec.size(), 1, outjson);
- fprintf(outjson, ",");
- fwrite(json.c_str(), json.size(), 1, outjson);
- fprintf(outjson, "}");
- fwrite(bin.c_str(), bin.size(), 1, outbin);
- if (settings.fallback)
- fwrite(fallback.c_str(), fallback.size(), 1, outfb);
- fclose(outjson);
- fclose(outbin);
- if (outfb)
- fclose(outfb);
- }
- else if (oext && (strcmp(oext, ".glb") == 0 || strcmp(oext, ".GLB") == 0))
- {
- std::string fbpath = output;
- fbpath.replace(fbpath.size() - 4, 4, ".fallback.bin");
- FILE* out = fopen(output, "wb");
- FILE* outfb = settings.fallback ? fopen(fbpath.c_str(), "wb") : NULL;
- if (!out || (!outfb && settings.fallback))
- {
- fprintf(stderr, "Error saving %s\n", output);
- return 4;
- }
- std::string bufferspec = getBufferSpec(NULL, bin.size(), settings.fallback ? getBaseName(fbpath.c_str()) : NULL, fallback.size(), settings.compress);
- json.insert(0, "{" + bufferspec + ",");
- json.push_back('}');
- while (json.size() % 4)
- json.push_back(' ');
- while (bin.size() % 4)
- bin.push_back('\0');
- writeU32(out, 0x46546C67);
- writeU32(out, 2);
- writeU32(out, uint32_t(12 + 8 + json.size() + 8 + bin.size()));
- writeU32(out, uint32_t(json.size()));
- writeU32(out, 0x4E4F534A);
- fwrite(json.c_str(), json.size(), 1, out);
- writeU32(out, uint32_t(bin.size()));
- writeU32(out, 0x004E4942);
- fwrite(bin.c_str(), bin.size(), 1, out);
- if (settings.fallback)
- fwrite(fallback.c_str(), fallback.size(), 1, outfb);
- fclose(out);
- if (outfb)
- fclose(outfb);
- }
- else
- {
- fprintf(stderr, "Error saving %s: unknown extension (expected .gltf or .glb)\n", output);
- return 4;
- }
- return 0;
- }
- int main(int argc, char** argv)
- {
- Settings settings = {};
- settings.pos_bits = 14;
- settings.tex_bits = 12;
- settings.nrm_bits = 8;
- settings.anim_freq = 30;
- settings.simplify_threshold = 1.f;
- settings.texture_quality = 50;
- const char* input = 0;
- const char* output = 0;
- bool help = false;
- int test = 0;
- for (int i = 1; i < argc; ++i)
- {
- const char* arg = argv[i];
- if (strcmp(arg, "-vp") == 0 && i + 1 < argc && isdigit(argv[i + 1][0]))
- {
- settings.pos_bits = atoi(argv[++i]);
- }
- else if (strcmp(arg, "-vt") == 0 && i + 1 < argc && isdigit(argv[i + 1][0]))
- {
- settings.tex_bits = atoi(argv[++i]);
- }
- else if (strcmp(arg, "-vn") == 0 && i + 1 < argc && isdigit(argv[i + 1][0]))
- {
- settings.nrm_bits = atoi(argv[++i]);
- }
- else if (strcmp(arg, "-vu") == 0)
- {
- settings.nrm_unnormalized = true;
- }
- else if (strcmp(arg, "-af") == 0 && i + 1 < argc && isdigit(argv[i + 1][0]))
- {
- settings.anim_freq = atoi(argv[++i]);
- }
- else if (strcmp(arg, "-ac") == 0)
- {
- settings.anim_const = true;
- }
- else if (strcmp(arg, "-kn") == 0)
- {
- settings.keep_named = true;
- }
- else if (strcmp(arg, "-si") == 0 && i + 1 < argc && isdigit(argv[i + 1][0]))
- {
- settings.simplify_threshold = float(atof(argv[++i]));
- }
- else if (strcmp(arg, "-sa") == 0)
- {
- settings.simplify_aggressive = true;
- }
- else if (strcmp(arg, "-te") == 0)
- {
- settings.texture_embed = true;
- }
- else if (strcmp(arg, "-tb") == 0)
- {
- settings.texture_basis = true;
- }
- else if (strcmp(arg, "-tc") == 0)
- {
- settings.texture_basis = true;
- settings.texture_ktx2 = true;
- }
- else if (strcmp(arg, "-tq") == 0 && i + 1 < argc && isdigit(argv[i + 1][0]))
- {
- settings.texture_quality = atoi(argv[++i]);
- }
- else if (strcmp(arg, "-i") == 0 && i + 1 < argc && !input)
- {
- input = argv[++i];
- }
- else if (strcmp(arg, "-o") == 0 && i + 1 < argc && !output)
- {
- output = argv[++i];
- }
- else if (strcmp(arg, "-c") == 0)
- {
- settings.compress = true;
- }
- else if (strcmp(arg, "-cf") == 0)
- {
- settings.compress = true;
- settings.fallback = true;
- }
- else if (strcmp(arg, "-v") == 0)
- {
- settings.verbose = 1;
- }
- else if (strcmp(arg, "-vv") == 0)
- {
- settings.verbose = 2;
- }
- else if (strcmp(arg, "-h") == 0)
- {
- help = true;
- }
- else if (strcmp(arg, "-test") == 0)
- {
- test = i + 1;
- break;
- }
- else
- {
- fprintf(stderr, "Unrecognized option %s\n", arg);
- return 1;
- }
- }
- if (test)
- {
- for (int i = test; i < argc; ++i)
- {
- printf("%s\n", argv[i]);
- gltfpack(argv[i], NULL, settings);
- }
- return 0;
- }
- if (!input || !output || help)
- {
- fprintf(stderr, "Usage: gltfpack [options] -i input -o output\n");
- fprintf(stderr, "\n");
- fprintf(stderr, "Options:\n");
- fprintf(stderr, "-i file: input file to process, .obj/.gltf/.glb\n");
- fprintf(stderr, "-o file: output file path, .gltf/.glb\n");
- fprintf(stderr, "-vp N: use N-bit quantization for positions (default: 14; N should be between 1 and 16)\n");
- fprintf(stderr, "-vt N: use N-bit quantization for texture corodinates (default: 12; N should be between 1 and 16)\n");
- fprintf(stderr, "-vn N: use N-bit quantization for normals and tangents (default: 8; N should be between 1 and 16)\n");
- fprintf(stderr, "-vu: use unnormalized normal/tangent vectors to improve compression (default: off)\n");
- fprintf(stderr, "-af N: resample animations at N Hz (default: 30)\n");
- fprintf(stderr, "-ac: keep constant animation tracks even if they don't modify the node transform\n");
- fprintf(stderr, "-kn: keep named nodes and meshes attached to named nodes so that named nodes can be transformed externally\n");
- fprintf(stderr, "-si R: simplify meshes to achieve the ratio R (default: 1; R should be between 0 and 1)\n");
- fprintf(stderr, "-sa: aggressively simplify to the target ratio disregarding quality\n");
- fprintf(stderr, "-te: embed all textures into main buffer\n");
- fprintf(stderr, "-tb: convert all textures to Basis Universal format (with basisu executable)\n");
- fprintf(stderr, "-tc: convert all textures to KTX2 with BasisU supercompression (using basisu executable)\n");
- fprintf(stderr, "-tq N: set texture encoding quality (default: 50; N should be between 1 and 100\n");
- fprintf(stderr, "-c: produce compressed gltf/glb files\n");
- fprintf(stderr, "-cf: produce compressed gltf/glb files with fallback for loaders that don't support compression\n");
- fprintf(stderr, "-v: verbose output\n");
- fprintf(stderr, "-h: display this help and exit\n");
- return 1;
- }
- return gltfpack(input, output, settings);
- }
|