| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497 |
- // 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 are technically invalid per spec; 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>
- #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;
- std::vector<Stream> streams;
- std::vector<unsigned int> indices;
- size_t targets;
- std::vector<float> weights;
- };
- struct Settings
- {
- int pos_bits;
- int tex_bits;
- int nrm_bits;
- bool nrm_unit;
- int anim_freq;
- bool anim_const;
- bool keep_named;
- bool compress;
- 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 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)
- {
- switch (result)
- {
- case cgltf_result_file_not_found:
- return "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";
- 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 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 parseMeshesGltf(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.indices || !primitive.indices->buffer_view)
- {
- fprintf(stderr, "Warning: ignoring primitive %d of mesh %d because it has no index data\n", int(pi), mesh_id);
- continue;
- }
- if (primitive.type != cgltf_primitive_type_triangles)
- {
- fprintf(stderr, "Warning: ignoring primitive %d of mesh %d because type %d is not supported\n", int(pi), mesh_id, primitive.type);
- continue;
- }
- Mesh result;
- result.node = &node;
- result.material = primitive.material;
- result.skin = node.skin;
- 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));
- 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};
- s.data.resize(attr.data->count);
- for (size_t i = 0; i < attr.data->count; ++i)
- cgltf_accessor_read_float(attr.data, i, s.data[i].f, 4);
- 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)};
- s.data.resize(attr.data->count);
- for (size_t i = 0; i < attr.data->count; ++i)
- cgltf_accessor_read_float(attr.data, i, s.data[i].f, 4);
- result.streams.push_back(s);
- }
- }
- result.targets = primitive.targets_count;
- result.weights.assign(mesh.weights, mesh.weights + mesh.weights_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.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];
- }
- }
- 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;
- }
- void mergeMeshMaterials(cgltf_data* data, std::vector<Mesh>& meshes)
- {
- for (size_t i = 0; i < meshes.size(); ++i)
- {
- Mesh& mesh = meshes[i];
- if (mesh.indices.empty())
- continue;
- 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 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.targets != rhs.targets)
- return false;
- if (lhs.weights.size() != rhs.weights.size())
- return false;
- for (size_t i = 0; i < lhs.weights.size(); ++i)
- if (lhs.weights[i] != rhs.weights[i])
- 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& mesh = meshes[i];
- for (size_t j = 0; j < i; ++j)
- {
- Mesh& target = meshes[j];
- if (target.indices.size() && canMergeMeshes(mesh, target, settings))
- {
- mergeMeshes(target, mesh);
- mesh.streams.clear();
- mesh.indices.clear();
- break;
- }
- }
- }
- }
- 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 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);
- (void)unique_vertices;
- meshopt_remapIndexBuffer(&mesh.indices[0], &mesh.indices[0], mesh.indices.size(), &remap[0]);
- for (size_t i = 0; i < mesh.streams.size(); ++i)
- meshopt_remapVertexBuffer(&mesh.streams[i].data[0], &mesh.streams[i].data[0], vertex_count, sizeof(Attr), &remap[0]);
- }
- 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;
- 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 nrm_unit = has_targets || settings.nrm_unit;
- int bits = nrm_unit ? 8 : settings.nrm_bits;
- 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 (!nrm_unit)
- rescaleNormal(nx, ny, nz);
- 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));
- }
- StreamFormat format = {cgltf_type_vec3, cgltf_component_type_r_8, true, 4};
- return format;
- }
- else if (stream.type == cgltf_attribute_type_tangent)
- {
- bool nrm_unit = has_targets || settings.nrm_unit;
- int bits = nrm_unit ? 8 : settings.nrm_bits;
- 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 (!nrm_unit)
- rescaleNormal(nx, ny, nz);
- 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));
- }
- if (stream.target == 0)
- {
- StreamFormat format = {cgltf_type_vec4, cgltf_component_type_r_8, true, 4};
- return format;
- }
- else
- {
- StreamFormat format = {cgltf_type_vec3, 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];
- 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))};
- 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
- {
- for (size_t i = 0; i < data.size(); ++i)
- {
- const Attr& a = data[i];
- float v[3] = {a.f[0], a.f[1], a.f[2]};
- bin.append(reinterpret_cast<const char*>(v), sizeof(v));
- }
- StreamFormat format = {cgltf_type_vec3, cgltf_component_type_r_32f, false, 12};
- 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.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, "}");
- }
- }
- 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;
- }
- 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)
- {
- append(json, "{\"buffer\":0");
- append(json, ",\"byteLength\":");
- append(json, bin_size);
- append(json, ",\"byteOffset\":");
- append(json, bin_offset);
- 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, "\"mode\":");
- append(json, size_t(compression));
- append(json, ",\"count\":");
- append(json, count);
- append(json, ",\"byteStride\":");
- append(json, stride);
- 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)
- {
- if (type == cgltf_animation_path_type_rotation)
- {
- float error = 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]);
- return error;
- }
- else
- {
- float error = 0;
- for (int k = 0; k < 4; ++k)
- error += fabsf(r.f[k] - l.f[k]);
- return error;
- }
- }
- bool isTrackConstant(const cgltf_animation_sampler& sampler, cgltf_animation_path_type type, cgltf_node* target_node)
- {
- const float tolerance = 1e-3f;
- size_t value_stride = (sampler.interpolation == cgltf_interpolation_type_cubic_spline) ? 3 : 1;
- size_t value_offset = (sampler.interpolation == cgltf_interpolation_type_cubic_spline) ? 1 : 0;
- size_t components = (type == cgltf_animation_path_type_weights) ? target_node->mesh->primitives[0].targets_count : 1;
- assert(sampler.input->count * value_stride * components == sampler.output->count);
- for (size_t j = 0; j < components; ++j)
- {
- Attr first = {};
- cgltf_accessor_read_float(sampler.output, j * value_stride + value_offset, first.f, 4);
- for (size_t i = 1; i < sampler.input->count; ++i)
- {
- Attr attr = {};
- cgltf_accessor_read_float(sampler.output, (i * components + j) * value_stride + value_offset, attr.f, 4);
- if (getDelta(first, attr, type) > tolerance)
- return false;
- }
- if (sampler.interpolation == cgltf_interpolation_type_cubic_spline)
- {
- for (size_t i = 0; i < sampler.input->count; ++i)
- {
- for (int k = 0; k < 2; ++k)
- {
- Attr t = {};
- cgltf_accessor_read_float(sampler.output, (i * components + j) * 3 + k * 2, t.f, 4);
- float error = fabsf(t.f[0]) + fabsf(t.f[1]) + fabsf(t.f[2]) + fabsf(t.f[3]);
- if (error > tolerance)
- return false;
- }
- }
- }
- }
- return true;
- }
- 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 cgltf_animation_sampler& sampler, cgltf_animation_path_type type, cgltf_node* target_node, int frames, float mint, int freq)
- {
- size_t components = (type == cgltf_animation_path_type_weights) ? target_node->mesh->primitives[0].targets_count : 1;
- size_t cursor = 0;
- for (int i = 0; i < frames; ++i)
- {
- float time = mint + float(i) / freq;
- while (cursor + 1 < sampler.input->count)
- {
- float next_time = 0;
- cgltf_accessor_read_float(sampler.input, cursor + 1, &next_time, 1);
- if (next_time > time)
- break;
- cursor++;
- }
- if (cursor + 1 < sampler.input->count)
- {
- float cursor_time = 0;
- float next_time = 0;
- cgltf_accessor_read_float(sampler.input, cursor + 0, &cursor_time, 1);
- cgltf_accessor_read_float(sampler.input, cursor + 1, &next_time, 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 (sampler.interpolation)
- {
- case cgltf_interpolation_type_linear:
- {
- Attr v0 = {};
- Attr v1 = {};
- cgltf_accessor_read_float(sampler.output, (cursor + 0) * components + j, v0.f, 4);
- cgltf_accessor_read_float(sampler.output, (cursor + 1) * components + j, v1.f, 4);
- data.push_back(interpolateLinear(v0, v1, t, type));
- }
- break;
- case cgltf_interpolation_type_step:
- {
- Attr v = {};
- cgltf_accessor_read_float(sampler.output, cursor * components + j, v.f, 4);
- data.push_back(v);
- }
- break;
- case cgltf_interpolation_type_cubic_spline:
- {
- Attr v0 = {};
- Attr b0 = {};
- Attr a1 = {};
- Attr v1 = {};
- cgltf_accessor_read_float(sampler.output, (cursor * 3 + 1) * components + j, v0.f, 4);
- cgltf_accessor_read_float(sampler.output, (cursor * 3 + 2) * components + j, b0.f, 4);
- cgltf_accessor_read_float(sampler.output, (cursor * 3 + 3) * components + j, a1.f, 4);
- cgltf_accessor_read_float(sampler.output, (cursor * 3 + 4) * components + j, v1.f, 4);
- data.push_back(interpolateHermite(v0, b0, v1, a1, t, range, type));
- }
- break;
- default:
- assert(!"Unknown interpolation type");
- }
- }
- }
- else
- {
- size_t offset = (sampler.interpolation == cgltf_interpolation_type_cubic_spline) ? cursor * 3 + 1 : cursor;
- for (size_t j = 0; j < components; ++j)
- {
- Attr v = {};
- cgltf_accessor_read_float(sampler.output, offset * components + j, v.f, 4);
- data.push_back(v);
- }
- }
- }
- }
- void markAnimated(cgltf_data* data, std::vector<NodeInfo>& nodes)
- {
- for (size_t i = 0; i < data->animations_count; ++i)
- {
- const cgltf_animation& animation = data->animations[i];
- for (size_t j = 0; j < animation.channels_count; ++j)
- {
- const cgltf_animation_channel& channel = animation.channels[j];
- const cgltf_animation_sampler& sampler = *channel.sampler;
- if (!channel.target_node)
- continue;
- NodeInfo& ni = nodes[channel.target_node - data->nodes];
- // mark nodes that have animation tracks that change their base transform as animated
- if (!isTrackConstant(sampler, channel.target_path, channel.target_node))
- {
- ni.animated_paths |= (1 << channel.target_path);
- }
- else
- {
- Attr base = {};
- switch (channel.target_path)
- {
- case cgltf_animation_path_type_translation:
- memcpy(base.f, channel.target_node->translation, 3 * sizeof(float));
- break;
- case cgltf_animation_path_type_rotation:
- memcpy(base.f, channel.target_node->rotation, 4 * sizeof(float));
- break;
- case cgltf_animation_path_type_scale:
- memcpy(base.f, channel.target_node->scale, 3 * sizeof(float));
- break;
- default:;
- }
- Attr first = {};
- cgltf_accessor_read_float(sampler.output, 0, first.f, 4);
- const float tolerance = 1e-3f;
- if (getDelta(base, first, channel.target_path) > tolerance)
- {
- ni.animated_paths |= (1 << channel.target_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 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 < data->animations_count; ++i)
- {
- const cgltf_animation& animation = data->animations[i];
- for (size_t j = 0; j < animation.channels_count; ++j)
- {
- const cgltf_animation_channel& channel = animation.channels[j];
- if (channel.target_node)
- {
- NodeInfo& ni = nodes[channel.target_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.indices.empty())
- continue;
- 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.indices.empty())
- continue;
- 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++;
- }
- }
- }
- 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.append(data, size);
- // each chunk must be aligned to 4 bytes
- views[view].data.resize((views[view].data.size() + 3) & ~3);
- append(json, "\"bufferView\":");
- append(json, view);
- append(json, ",\"mimeType\":\"");
- append(json, mime_type);
- append(json, "\"");
- }
- 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;
- if (stream.type == cgltf_attribute_type_texcoord && (!mesh.material || !usesTextureSet(*mesh.material, stream.index)))
- continue;
- if ((stream.type == cgltf_attribute_type_joints || stream.type == cgltf_attribute_type_weights) && !mesh.skin)
- 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 cgltf_animation& animation, cgltf_data* data, const std::vector<NodeInfo>& nodes, const Settings& settings)
- {
- std::vector<const cgltf_animation_channel*> tracks;
- 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(&animation - data->animations));
- continue;
- }
- const NodeInfo& ni = nodes[channel.target_node - data->nodes];
- if (!ni.keep)
- continue;
- if (!settings.anim_const && (ni.animated_paths & (1 << channel.target_path)) == 0)
- continue;
- tracks.push_back(&channel);
- }
- if (tracks.empty())
- {
- fprintf(stderr, "Warning: ignoring animation %d because it has no valid tracks\n", int(&animation - data->animations));
- return;
- }
- float mint = 0, maxt = 0;
- bool needs_time = false;
- bool needs_pose = false;
- for (size_t j = 0; j < tracks.size(); ++j)
- {
- const cgltf_animation_channel& channel = *tracks[j];
- const cgltf_animation_sampler& sampler = *channel.sampler;
- mint = std::min(mint, sampler.input->min[0]);
- maxt = std::max(maxt, sampler.input->max[0]);
- bool tc = isTrackConstant(sampler, channel.target_path, channel.target_node);
- needs_time = needs_time || !tc;
- needs_pose = needs_pose || tc;
- }
- // 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);
- size_t time_accr = needs_time ? writeAnimationTime(views, json_accessors, accr_offset, mint, frames, settings) : 0;
- size_t pose_accr = needs_pose ? writeAnimationTime(views, json_accessors, accr_offset, mint, 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 cgltf_animation_channel& channel = *tracks[j];
- const cgltf_animation_sampler& sampler = *channel.sampler;
- bool tc = isTrackConstant(sampler, channel.target_path, channel.target_node);
- std::vector<Attr> track;
- resampleKeyframes(track, sampler, channel.target_path, channel.target_node, tc ? 1 : frames, mint, settings.anim_freq);
- std::string scratch;
- StreamFormat format = writeKeyframeStream(scratch, channel.target_path, track);
- size_t view = getBufferView(views, BufferView::Kind_Keyframe, channel.target_path, format.stride, settings.compress && channel.target_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.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[channel.target_node - data->nodes];
- size_t target_node = size_t(tni.remap);
- if (channel.target_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(channel.target_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.perspective.yfov);
- append(json, ",\"znear\":");
- append(json, camera.perspective.znear);
- if (camera.perspective.aspect_ratio != 0.f)
- {
- append(json, ",\"aspectRatio\":");
- append(json, camera.perspective.aspect_ratio);
- }
- if (camera.perspective.zfar != 0.f)
- {
- append(json, ",\"zfar\":");
- append(json, camera.perspective.zfar);
- }
- append(json, "}");
- break;
- case cgltf_camera_type_orthographic:
- append(json, "\"type\":\"orthographic\",\"orthographic\":{");
- append(json, "\"xmag\":");
- append(json, camera.orthographic.xmag);
- append(json, ",\"ymag\":");
- append(json, camera.orthographic.ymag);
- append(json, ",\"znear\":");
- append(json, camera.orthographic.znear);
- append(json, ",\"zfar\":");
- append(json, camera.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 printStats(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));
- }
- }
- bool process(cgltf_data* data, std::vector<Mesh>& meshes, const Settings& settings, std::string& json, std::string& bin)
- {
- 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(data->animations_count));
- }
- std::vector<NodeInfo> nodes(data->nodes_count);
- markAnimated(data, nodes);
- 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);
- markNeededNodes(data, nodes, meshes, settings);
- std::vector<MaterialInfo> materials(data->materials_count);
- markNeededMaterials(data, materials, meshes);
- for (size_t i = 0; i < meshes.size(); ++i)
- {
- Mesh& mesh = meshes[i];
- if (mesh.indices.empty())
- continue;
- reindexMesh(mesh);
- optimizeMesh(mesh);
- }
- if (settings.verbose)
- {
- 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("meshes: %d triangles, %d vertices\n", int(triangles), int(vertices));
- }
- 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];
- comma(json_images);
- append(json_images, "{");
- if (image.uri)
- {
- std::string mime_type;
- std::string img;
- if (parseDataUri(image.uri, mime_type, img))
- {
- writeEmbeddedImage(json_images, views, img.c_str(), img.size(), mime_type.c_str());
- }
- else
- {
- append(json_images, "\"uri\":\"");
- append(json_images, image.uri);
- append(json_images, "\"");
- }
- }
- else if (image.buffer_view && image.buffer_view->buffer->data && image.mime_type)
- {
- const char* img = static_cast<const char*>(image.buffer_view->buffer->data) + image.buffer_view->offset;
- size_t size = image.buffer_view->size;
- writeEmbeddedImage(json_images, views, img, size, image.mime_type);
- }
- else
- {
- fprintf(stderr, "Warning: ignoring image %d since it has no URI and no valid buffer data\n", int(i));
- }
- 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, "{");
- if (texture.image)
- {
- append(json_textures, "\"source\":");
- append(json_textures, size_t(texture.image - data->images));
- }
- 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];
- if (mesh.indices.empty())
- continue;
- comma(json_meshes);
- append(json_meshes, "{\"primitives\":[");
- size_t pi = i;
- for (; pi < meshes.size(); ++pi)
- {
- const Mesh& prim = meshes[pi];
- if (prim.indices.empty())
- continue;
- if (prim.node != mesh.node || prim.skin != mesh.skin || prim.targets != mesh.targets)
- break;
- if (mesh.weights.size() && (prim.weights.size() != mesh.weights.size() || memcmp(&mesh.weights[0], &prim.weights[0], mesh.weights.size() * sizeof(float)) != 0))
- break;
- comma(json_meshes);
- append(json_meshes, "{\"attributes\":{");
- writeMeshAttributes(json_meshes, views, json_accessors, accr_offset, prim, 0, qp, settings);
- append(json_meshes, "}");
- 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, "]");
- }
- 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.weights.size())
- {
- append(json_meshes, ",\"weights\":[");
- for (size_t j = 0; j < mesh.weights.size(); ++j)
- {
- comma(json_meshes);
- append(json_meshes, mesh.weights[j]);
- }
- 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 < data->animations_count; ++i)
- {
- const cgltf_animation& animation = data->animations[i];
- writeAnimation(json_animations, views, json_accessors, accr_offset, animation, 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, "}");
- append(json, ",\"extensionsUsed\":[");
- append(json, "\"MESHOPT_quantized_geometry\"");
- if (settings.compress)
- {
- comma(json);
- append(json, "\"MESHOPT_compression\"");
- }
- if (!json_textures.empty())
- {
- comma(json);
- append(json, "\"KHR_texture_transform\"");
- }
- if (ext_pbr_specular_glossiness)
- {
- comma(json);
- append(json, "\"KHR_materials_pbrSpecularGlossiness\"");
- }
- if (ext_unlit)
- {
- comma(json);
- append(json, "\"KHR_materials_unlit\"");
- }
- if (data->lights_count)
- {
- comma(json);
- append(json, "\"KHR_lights_punctual\"");
- }
- append(json, "]");
- if (settings.compress)
- {
- append(json, ",\"extensionsRequired\":[");
- // Note: ideally we should include MESHOPT_quantized_geometry in the required extension list (regardless of compression)
- // This extension *only* allows the use of quantized attributes for positions/normals/etc. This happens to be supported
- // by popular JS frameworks, however, Babylon.JS refuses to load files with unsupported required extensions.
- // For now we don't include it in the list, which will be fixed at some point once this extension becomes official.
- append(json, "\"MESHOPT_compression\"");
- append(json, "]");
- }
- size_t bytes[BufferView::Kind_Count] = {};
- if (!views.empty())
- {
- append(json, ",\"bufferViews\":[");
- for (size_t i = 0; i < views.size(); ++i)
- {
- BufferView& view = views[i];
- size_t offset = bin.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;
- }
- }
- else
- {
- bin += view.data;
- }
- comma(json);
- writeBufferView(json, view.kind, count, view.stride, offset, bin.size() - offset, compression);
- view.bytes = bin.size() - offset;
- bytes[view.kind] += view.bytes;
- // align each bufferView by 4 bytes
- bin.resize((bin.size() + 3) & ~3);
- }
- append(json, "]");
- }
- if (!json_accessors.empty())
- {
- append(json, ",\"accessors\":[");
- append(json, json_accessors);
- append(json, "]");
- }
- if (!json_images.empty())
- {
- append(json, ",\"images\":[");
- append(json, json_images);
- append(json, "]");
- }
- if (!json_textures.empty())
- {
- append(json, ",\"textures\":[");
- append(json, json_textures);
- append(json, "]");
- }
- if (!json_materials.empty())
- {
- append(json, ",\"materials\":[");
- append(json, json_materials);
- append(json, "]");
- }
- if (!json_meshes.empty())
- {
- append(json, ",\"meshes\":[");
- append(json, json_meshes);
- append(json, "]");
- }
- if (!json_skins.empty())
- {
- append(json, ",\"skins\":[");
- append(json, json_skins);
- append(json, "]");
- }
- if (!json_animations.empty())
- {
- append(json, ",\"animations\":[");
- append(json, json_animations);
- append(json, "]");
- }
- if (!json_roots.empty())
- {
- append(json, ",\"nodes\":[");
- append(json, json_nodes);
- append(json, "],\"scenes\":[");
- append(json, "{\"nodes\":[");
- append(json, json_roots);
- append(json, "]}]");
- }
- if (!json_cameras.empty())
- {
- append(json, ",\"cameras\":[");
- append(json, json_cameras);
- append(json, "]");
- }
- 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)
- {
- size_t primitives = 0;
- for (size_t i = 0; i < meshes.size(); ++i)
- primitives += !meshes[i].indices.empty();
- printf("output: %d nodes, %d meshes (%d primitives), %d materials\n", int(node_offset), int(mesh_offset), int(primitives), 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]));
- }
- if (settings.verbose > 1)
- {
- printStats(views, BufferView::Kind_Vertex, "vertex");
- printStats(views, BufferView::Kind_Index, "index");
- printStats(views, BufferView::Kind_Keyframe, "keyframe");
- }
- return true;
- }
- void writeU32(FILE* out, uint32_t data)
- {
- fwrite(&data, 4, 1, out);
- }
- int gltfpack(const char* input, const char* output, const Settings& settings)
- {
- cgltf_data* data = 0;
- std::vector<Mesh> meshes;
- 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_validate(data) : result;
- result = (result == cgltf_result_success) ? cgltf_load_buffers(&options, data, input) : result;
- if (result != cgltf_result_success)
- {
- fprintf(stderr, "Error loading %s: %s\n", input, getError(result));
- cgltf_free(data);
- return 2;
- }
- parseMeshesGltf(data, meshes);
- }
- 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;
- }
- std::string json, bin;
- if (!process(data, meshes, settings, json, bin))
- {
- fprintf(stderr, "Error processing %s\n", input);
- cgltf_free(data);
- return 3;
- }
- 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 binname = binpath;
- std::string::size_type slash = binname.find_last_of("/\\");
- if (slash != std::string::npos)
- binname.erase(0, slash + 1);
- FILE* outjson = fopen(output, "wb");
- FILE* outbin = fopen(binpath.c_str(), "wb");
- if (!outjson || !outbin)
- {
- fprintf(stderr, "Error saving %s\n", output);
- return 4;
- }
- fprintf(outjson, "{\"buffers\":[{\"uri\":\"%s\",\"byteLength\":%zu}],", binname.c_str(), bin.size());
- fwrite(json.c_str(), json.size(), 1, outjson);
- fprintf(outjson, "}");
- fwrite(bin.c_str(), bin.size(), 1, outbin);
- fclose(outjson);
- fclose(outbin);
- }
- else if (oext && (strcmp(oext, ".glb") == 0 || strcmp(oext, ".GLB") == 0))
- {
- FILE* out = fopen(output, "wb");
- if (!out)
- {
- fprintf(stderr, "Error saving %s\n", output);
- return 4;
- }
- char bufferspec[64];
- sprintf(bufferspec, "{\"buffers\":[{\"byteLength\":%zu}],", bin.size());
- 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);
- fclose(out);
- }
- 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;
- 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_unit = 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, "-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, "-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)
- 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 8)\n");
- fprintf(stderr, "-vu: use unit-length normal/tangent vectors (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, "-c: produce compressed glb files\n");
- fprintf(stderr, "-v: verbose output\n");
- fprintf(stderr, "-h: display this help and exit\n");
- return 1;
- }
- return gltfpack(input, output, settings);
- }
|