babylon.glTFFileLoader.ts 64 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705
  1. module BABYLON {
  2. /**
  3. * Tokenizer. Used for shaders compatibility
  4. * Automatically map world, view, projection, worldViewProjection, attributes and so on
  5. */
  6. enum ETokenType {
  7. IDENTIFIER = 1,
  8. UNKNOWN = 2,
  9. END_OF_INPUT = 3
  10. }
  11. class Tokenizer {
  12. private _toParse: string;
  13. private _pos: number = 0;
  14. private _maxPos: number;
  15. public currentToken: ETokenType;
  16. public currentIdentifier: string;
  17. public currentString: string;
  18. public isLetterOrDigitPattern: RegExp = /^[a-zA-Z0-9]+$/;
  19. constructor(toParse: string) {
  20. this._toParse = toParse;
  21. this._maxPos = toParse.length;
  22. }
  23. public getNextToken(): ETokenType {
  24. if (this.isEnd()) return ETokenType.END_OF_INPUT;
  25. this.currentString = this.read();
  26. this.currentToken = ETokenType.UNKNOWN;
  27. if (this.currentString === "_" || this.isLetterOrDigitPattern.test(this.currentString)) {
  28. this.currentToken = ETokenType.IDENTIFIER;
  29. this.currentIdentifier = this.currentString;
  30. while (!this.isEnd() && (this.isLetterOrDigitPattern.test(this.currentString = this.peek()) || this.currentString === "_")) {
  31. this.currentIdentifier += this.currentString;
  32. this.forward();
  33. }
  34. }
  35. return this.currentToken;
  36. }
  37. public peek(): string {
  38. return this._toParse[this._pos];
  39. }
  40. public read(): string {
  41. return this._toParse[this._pos++];
  42. }
  43. public forward(): void {
  44. this._pos++;
  45. }
  46. public isEnd(): boolean {
  47. return this._pos >= this._maxPos;
  48. }
  49. }
  50. /**
  51. * Values
  52. */
  53. var glTFTransforms = ["MODEL", "VIEW", "PROJECTION", "MODELVIEW", "MODELVIEWPROJECTION", "JOINTMATRIX"];
  54. var babylonTransforms = ["world", "view", "projection", "worldView", "worldViewProjection", "mBones"];
  55. var glTFAnimationPaths = ["translation", "rotation", "scale"];
  56. var babylonAnimationPaths = ["position", "rotationQuaternion", "scaling"];
  57. /**
  58. * Parse
  59. */
  60. var parseBuffers = (parsedBuffers: any, gltfRuntime: IGLTFRuntime) => {
  61. for (var buf in parsedBuffers) {
  62. var parsedBuffer = parsedBuffers[buf];
  63. gltfRuntime.buffers[buf] = parsedBuffer;
  64. gltfRuntime.buffersCount++;
  65. }
  66. };
  67. var parseShaders = (parsedShaders: any, gltfRuntime: IGLTFRuntime) => {
  68. for (var sha in parsedShaders) {
  69. var parsedShader = parsedShaders[sha];
  70. gltfRuntime.shaders[sha] = parsedShader;
  71. gltfRuntime.shaderscount++;
  72. }
  73. };
  74. var parseObject = (parsedObjects: any, runtimeProperty: string, gltfRuntime: IGLTFRuntime) => {
  75. for (var object in parsedObjects) {
  76. var parsedObject = parsedObjects[object];
  77. gltfRuntime[runtimeProperty][object] = parsedObject;
  78. }
  79. };
  80. /**
  81. * Utils
  82. */
  83. var normalizeUVs = (buffer: any) => {
  84. if (!buffer) {
  85. return;
  86. }
  87. for (var i = 0; i < buffer.length / 2; i++) {
  88. buffer[i * 2 + 1] = 1.0 - buffer[i * 2 + 1];
  89. }
  90. };
  91. var replaceInString = (str: string, searchValue: string, replaceValue: string) => {
  92. while (str.indexOf(searchValue) !== -1) {
  93. str = str.replace(searchValue, replaceValue);
  94. }
  95. return str;
  96. };
  97. var getAttribute = (attributeParameter: IGLTFTechniqueParameter) => {
  98. if (attributeParameter.semantic === "NORMAL") {
  99. return "normal";
  100. } else if (attributeParameter.semantic === "POSITION") {
  101. return "position";
  102. } else if (attributeParameter.semantic === "JOINT") {
  103. return "matricesIndices";
  104. } else if (attributeParameter.semantic === "WEIGHT") {
  105. return "matricesWeights";
  106. } else if (attributeParameter.semantic === "COLOR") {
  107. return "color";
  108. } else if (attributeParameter.semantic.indexOf("TEXCOORD_") !== -1) {
  109. var channel = Number(attributeParameter.semantic.split("_")[1]);
  110. return "uv" + (channel === 0 ? "" : channel + 1);
  111. }
  112. };
  113. /**
  114. * Returns the animation path (glTF -> Babylon)
  115. */
  116. var getAnimationPath = (path: string): string => {
  117. var index = glTFAnimationPaths.indexOf(path);
  118. if (index !== -1) {
  119. return babylonAnimationPaths[index];
  120. }
  121. return path;
  122. };
  123. /**
  124. * Loads and creates animations
  125. */
  126. var loadAnimations = (gltfRuntime: IGLTFRuntime) => {
  127. for (var anim in gltfRuntime.animations) {
  128. var animation: IGLTFAnimation = gltfRuntime.animations[anim];
  129. var lastAnimation: Animation = null;
  130. for (var i = 0; i < animation.channels.length; i++) {
  131. // Get parameters and load buffers
  132. var channel = animation.channels[i];
  133. var sampler: IGLTFAnimationSampler = animation.samplers[channel.sampler];
  134. if (!sampler) {
  135. continue;
  136. }
  137. var inputData = animation.parameters[sampler.input];
  138. var outputData = animation.parameters[sampler.output];
  139. var bufferInput = GLTFUtils.GetBufferFromAccessor(gltfRuntime, gltfRuntime.accessors[inputData]);
  140. var bufferOutput = GLTFUtils.GetBufferFromAccessor(gltfRuntime, gltfRuntime.accessors[outputData]);
  141. var targetID = channel.target.id;
  142. var targetNode: any = gltfRuntime.scene.getNodeByID(targetID);
  143. if (targetNode === null) {
  144. Tools.Warn("Creating animation named " + anim + ". But cannot find node named " + targetID + " to attach to");
  145. continue;
  146. }
  147. var isBone = targetNode instanceof Bone;
  148. // Get target path (position, rotation or scaling)
  149. var targetPath = channel.target.path;
  150. var targetPathIndex = glTFAnimationPaths.indexOf(targetPath);
  151. if (targetPathIndex !== -1) {
  152. targetPath = babylonAnimationPaths[targetPathIndex];
  153. }
  154. // Determine animation type
  155. var animationType = Animation.ANIMATIONTYPE_MATRIX;
  156. if (!isBone) {
  157. if (targetPath === "rotationQuaternion") {
  158. animationType = Animation.ANIMATIONTYPE_QUATERNION;
  159. targetNode.rotationQuaternion = new Quaternion();
  160. }
  161. else {
  162. animationType = Animation.ANIMATIONTYPE_VECTOR3;
  163. }
  164. }
  165. // Create animation and key frames
  166. var babylonAnimation: Animation = null;
  167. var keys = [];
  168. var arrayOffset = 0;
  169. var modifyKey = false;
  170. if (isBone && lastAnimation && lastAnimation.getKeys().length === bufferInput.length) {
  171. babylonAnimation = lastAnimation;
  172. modifyKey = true;
  173. }
  174. if (!modifyKey) {
  175. babylonAnimation = new Animation(anim, isBone ? "_matrix" : targetPath, 1, animationType, Animation.ANIMATIONLOOPMODE_CYCLE);
  176. }
  177. // For each frame
  178. for (var j = 0; j < bufferInput.length; j++) {
  179. var value: any = null;
  180. if (targetPath === "rotationQuaternion") { // VEC4
  181. value = Quaternion.FromArray([bufferOutput[arrayOffset], bufferOutput[arrayOffset + 1], bufferOutput[arrayOffset + 2], bufferOutput[arrayOffset + 3]]);
  182. arrayOffset += 4;
  183. }
  184. else { // Position and scaling are VEC3
  185. value = Vector3.FromArray([bufferOutput[arrayOffset], bufferOutput[arrayOffset + 1], bufferOutput[arrayOffset + 2]]);
  186. arrayOffset += 3;
  187. }
  188. if (isBone) {
  189. var bone = <Bone>targetNode;
  190. var translation = Vector3.Zero();
  191. var rotationQuaternion = new Quaternion();
  192. var scaling = Vector3.Zero();
  193. // Warning on decompose
  194. var mat = bone.getBaseMatrix();
  195. if (modifyKey) {
  196. mat = lastAnimation.getKeys()[j].value;
  197. }
  198. mat.decompose(scaling, rotationQuaternion, translation);
  199. if (targetPath === "position") {
  200. translation = value;
  201. }
  202. else if (targetPath === "rotationQuaternion") {
  203. rotationQuaternion = value;
  204. // Y is Up
  205. if (GLTFFileLoader.MakeYUP) {
  206. rotationQuaternion = rotationQuaternion.multiply(new Quaternion(-0.707107, 0, 0, 0.707107));
  207. }
  208. }
  209. else {
  210. scaling = value;
  211. }
  212. value = Matrix.Compose(scaling, rotationQuaternion, translation);
  213. }
  214. if (!modifyKey) {
  215. keys.push({
  216. frame: bufferInput[j],
  217. value: value
  218. });
  219. }
  220. else {
  221. lastAnimation.getKeys()[j].value = value;
  222. }
  223. }
  224. // Finish
  225. if (!modifyKey) {
  226. babylonAnimation.setKeys(keys);
  227. targetNode.animations.push(babylonAnimation);
  228. }
  229. lastAnimation = babylonAnimation;
  230. gltfRuntime.scene.stopAnimation(targetNode);
  231. gltfRuntime.scene.beginAnimation(targetNode, 0, bufferInput[bufferInput.length - 1], true, 1.0);
  232. }
  233. }
  234. };
  235. /**
  236. * Returns the bones transformation matrix
  237. */
  238. var configureBoneTransformation = (node: IGLTFNode): Matrix => {
  239. var mat: Matrix = null;
  240. if (node.translation && node.rotation && node.scale) {
  241. var scale = Vector3.FromArray(node.scale);
  242. var rotation = Quaternion.FromArray(node.rotation);
  243. var position = Vector3.FromArray(node.translation);
  244. // Y is Up
  245. if (GLTFFileLoader.MakeYUP) {
  246. rotation = rotation.multiply(new Quaternion(-0.707107, 0, 0, 0.707107));
  247. }
  248. mat = Matrix.Compose(scale, rotation, position);
  249. }
  250. else {
  251. mat = Matrix.FromArray(node.matrix);
  252. }
  253. return mat;
  254. };
  255. /**
  256. * Returns the parent bone
  257. */
  258. var getParentBone = (gltfRuntime: IGLTFRuntime, skins: IGLTFSkins, jointName: string, newSkeleton: Skeleton): Bone => {
  259. // Try to find
  260. for (var i = 0; i < newSkeleton.bones.length; i++) {
  261. if (newSkeleton.bones[i].id === jointName) {
  262. return newSkeleton.bones[i];
  263. }
  264. }
  265. // Not found, search in gltf nodes
  266. var nodes = gltfRuntime.nodes;
  267. for (var nde in nodes) {
  268. var node: IGLTFNode = nodes[nde];
  269. if (!node.jointName) {
  270. continue;
  271. }
  272. var children = node.children;
  273. for (var i = 0; i < children.length; i++) {
  274. var child: IGLTFNode = gltfRuntime.nodes[children[i]];
  275. if (!child.jointName) {
  276. continue;
  277. }
  278. if (child.jointName === jointName) {
  279. var mat = configureBoneTransformation(node);
  280. var bone = new Bone(node.name, newSkeleton, getParentBone(gltfRuntime, skins, node.jointName, newSkeleton), mat);
  281. bone.id = nde;
  282. return bone;
  283. }
  284. }
  285. }
  286. return null;
  287. }
  288. /**
  289. * Returns the appropriate root node
  290. */
  291. var getNodeToRoot = (nodesToRoot: INodeToRoot[], id: string): Bone => {
  292. for (var i = 0; i < nodesToRoot.length; i++) {
  293. var nodeToRoot = nodesToRoot[i];
  294. for (var j = 0; j < nodeToRoot.node.children.length; j++) {
  295. var child = nodeToRoot.node.children[j];
  296. if (child === id) {
  297. return nodeToRoot.bone;
  298. }
  299. }
  300. }
  301. return null;
  302. };
  303. /**
  304. * Returns the node with the joint name
  305. */
  306. var getJointNode = (gltfRuntime: IGLTFRuntime, jointName: string): IJointNode => {
  307. var nodes = gltfRuntime.nodes;
  308. var node: IGLTFNode = nodes[jointName];
  309. if (node) {
  310. return {
  311. node: node,
  312. id: jointName
  313. };
  314. }
  315. for (var nde in nodes) {
  316. node = nodes[nde];
  317. if (node.jointName === jointName) {
  318. return {
  319. node: node,
  320. id: nde
  321. };
  322. }
  323. }
  324. return null;
  325. }
  326. /**
  327. * Checks if a nodes is in joints
  328. */
  329. var nodeIsInJoints = (skins: IGLTFSkins, id: string): boolean => {
  330. for (var i = 0; i < skins.jointNames.length; i++) {
  331. if (skins.jointNames[i] === id) {
  332. return true;
  333. }
  334. }
  335. return false;
  336. }
  337. /**
  338. * Fills the nodes to root for bones and builds hierarchy
  339. */
  340. var getNodesToRoot = (gltfRuntime: IGLTFRuntime, newSkeleton: Skeleton, skins: IGLTFSkins, nodesToRoot: INodeToRoot[]) => {
  341. // Creates nodes for root
  342. for (var nde in gltfRuntime.nodes) {
  343. var node: IGLTFNode = gltfRuntime.nodes[nde];
  344. var id = nde;
  345. if (!node.jointName || nodeIsInJoints(skins, node.jointName)) {
  346. continue;
  347. }
  348. // Create node to root bone
  349. var mat = configureBoneTransformation(node);
  350. var bone = new Bone(node.name, newSkeleton, null, mat);
  351. bone.id = id;
  352. nodesToRoot.push({ bone: bone, node: node, id: id });
  353. }
  354. // Parenting
  355. for (var i = 0; i < nodesToRoot.length; i++) {
  356. var nodeToRoot = nodesToRoot[i];
  357. var children = nodeToRoot.node.children;
  358. for (var j = 0; j < children.length; j++) {
  359. var child: INodeToRoot = null;
  360. for (var k = 0; k < nodesToRoot.length; k++) {
  361. if (nodesToRoot[k].id === children[j]) {
  362. child = nodesToRoot[k];
  363. break;
  364. }
  365. }
  366. if (child) {
  367. (<any>child.bone)._parent = nodeToRoot.bone;
  368. nodeToRoot.bone.children.push(child.bone);
  369. }
  370. }
  371. }
  372. };
  373. var printMat = (m: Float32Array) => {
  374. console.log(
  375. m[0] + "\t" + m[1] + "\t" + m[2] + "\t" + m[3] + "\n" +
  376. m[4] + "\t" + m[5] + "\t" + m[6] + "\t" + m[7] + "\n" +
  377. m[8] + "\t" + m[9] + "\t" + m[10] + "\t" + m[11] + "\n" +
  378. m[12] + "\t" + m[13] + "\t" + m[14] + "\t" + m[15] + "\n"
  379. );
  380. }
  381. /**
  382. * Imports a skeleton
  383. */
  384. var importSkeleton = (gltfRuntime: IGLTFRuntime, skins: IGLTFSkins, mesh: Mesh, newSkeleton: Skeleton, id: string): Skeleton => {
  385. if (!newSkeleton) {
  386. newSkeleton = new Skeleton(skins.name, "", gltfRuntime.scene);
  387. }
  388. if (!skins.babylonSkeleton) {
  389. return newSkeleton;
  390. }
  391. // Matrices
  392. var accessor = gltfRuntime.accessors[skins.inverseBindMatrices];
  393. var buffer = GLTFUtils.GetBufferFromAccessor(gltfRuntime, accessor);
  394. var bindShapeMatrix = Matrix.FromArray(skins.bindShapeMatrix);
  395. // Find the root bones
  396. var nodesToRoot: INodeToRoot[] = [];
  397. var nodesToRootToAdd: Bone[] = [];
  398. getNodesToRoot(gltfRuntime, newSkeleton, skins, nodesToRoot);
  399. newSkeleton.bones = [];
  400. if (nodesToRoot.length === 0) {
  401. newSkeleton.needInitialSkinMatrix = true;
  402. }
  403. // Joints
  404. for (var i = 0; i < skins.jointNames.length; i++) {
  405. var jointNode = getJointNode(gltfRuntime, skins.jointNames[i]);
  406. var node = jointNode.node;
  407. if (!node) {
  408. Tools.Warn("Joint named " + skins.jointNames[i] + " does not exist");
  409. continue;
  410. }
  411. var id = jointNode.id;
  412. // Optimize, if the bone already exists...
  413. var existingBone = gltfRuntime.scene.getBoneByID(id);
  414. if (existingBone) {
  415. newSkeleton.bones.push(existingBone);
  416. continue;
  417. }
  418. // Check if node already exists
  419. var foundBone = false;
  420. for (var j = 0; j < newSkeleton.bones.length; j++) {
  421. if (newSkeleton.bones[j].id === id) {
  422. foundBone = true;
  423. break;
  424. }
  425. }
  426. if (foundBone) {
  427. continue;
  428. }
  429. // Search for parent bone
  430. var parentBone: Bone = null;
  431. for (var j = 0; j < i; j++) {
  432. var joint: IGLTFNode = getJointNode(gltfRuntime, skins.jointNames[j]).node;
  433. if (!joint) {
  434. Tools.Warn("Joint named " + skins.jointNames[j] + " does not exist when looking for parent");
  435. continue;
  436. }
  437. var children = joint.children;
  438. foundBone = false;
  439. for (var k = 0; k < children.length; k++) {
  440. if (children[k] === id) {
  441. parentBone = getParentBone(gltfRuntime, skins, skins.jointNames[j], newSkeleton);
  442. foundBone = true;
  443. break;
  444. }
  445. }
  446. if (foundBone) {
  447. break;
  448. }
  449. }
  450. // Create bone
  451. var mat = configureBoneTransformation(node);
  452. if (!parentBone && nodesToRoot.length > 0) {
  453. parentBone = getNodeToRoot(nodesToRoot, id);
  454. if (parentBone) {
  455. if (nodesToRootToAdd.indexOf(parentBone) === -1) {
  456. nodesToRootToAdd.push(parentBone);
  457. }
  458. }
  459. }
  460. if (!parentBone && nodesToRoot.length === 0) {
  461. var inverseBindMatrix = Matrix.FromArray(buffer, i * 16);
  462. var invertMesh = Matrix.Invert(mesh.getWorldMatrix());
  463. mat = mat.multiply(mesh.getWorldMatrix());
  464. }
  465. var bone = new Bone(node.name, newSkeleton, parentBone, mat);
  466. bone.id = id;
  467. }
  468. // Polish
  469. var bones = newSkeleton.bones;
  470. newSkeleton.bones = [];
  471. for (var i = 0; i < skins.jointNames.length; i++) {
  472. var jointNode: IJointNode = getJointNode(gltfRuntime, skins.jointNames[i]);
  473. if (!jointNode) {
  474. continue;
  475. }
  476. for (var j = 0; j < bones.length; j++) {
  477. if (bones[j].id === jointNode.id) {
  478. newSkeleton.bones.push(bones[j]);
  479. break;
  480. }
  481. }
  482. }
  483. // Finish
  484. newSkeleton.prepare();
  485. for (var i = 0; i < nodesToRootToAdd.length; i++) {
  486. newSkeleton.bones.push(nodesToRootToAdd[i]);
  487. }
  488. return newSkeleton;
  489. };
  490. /**
  491. * Imports a mesh and its geometries
  492. */
  493. var importMesh = (gltfRuntime: IGLTFRuntime, node: IGLTFNode, meshes: string[], id: string, newMesh: Mesh): Mesh => {
  494. if (!newMesh) {
  495. newMesh = new Mesh(node.name, gltfRuntime.scene);
  496. newMesh.id = id;
  497. }
  498. if (!node.babylonNode) {
  499. return newMesh;
  500. }
  501. var multiMat = new MultiMaterial("multimat" + id, gltfRuntime.scene);
  502. newMesh.material = multiMat;
  503. var vertexData = new VertexData();
  504. var geometry = new Geometry(id, gltfRuntime.scene, vertexData, false, newMesh);
  505. var verticesStarts = [];
  506. var verticesCounts = [];
  507. var indexStarts = [];
  508. var indexCounts = [];
  509. for (var meshIndex = 0; meshIndex < meshes.length; meshIndex++) {
  510. var meshID = meshes[meshIndex];
  511. var mesh: IGLTFMesh = gltfRuntime.meshes[meshID];
  512. if (!mesh) {
  513. continue;
  514. }
  515. // Positions, normals and UVs
  516. for (var i = 0; i < mesh.primitives.length; i++) {
  517. // Temporary vertex data
  518. var tempVertexData = new VertexData();
  519. var primitive = mesh.primitives[i];
  520. if (primitive.mode !== 4) {
  521. //continue;
  522. }
  523. var attributes = primitive.attributes;
  524. var accessor: IGLTFAccessor = null;
  525. var buffer: any = null;
  526. // Set positions, normal and uvs
  527. for (var semantic in attributes) {
  528. // Link accessor and buffer view
  529. accessor = gltfRuntime.accessors[attributes[semantic]];
  530. buffer = GLTFUtils.GetBufferFromAccessor(gltfRuntime, accessor);
  531. if (semantic === "NORMAL") {
  532. tempVertexData.normals = new Float32Array(buffer.length);
  533. (<Float32Array>tempVertexData.normals).set(buffer);
  534. }
  535. else if (semantic === "POSITION") {
  536. if (GLTFFileLoader.HomogeneousCoordinates) {
  537. tempVertexData.positions = new Float32Array(buffer.length - buffer.length / 4);
  538. for (var j = 0; j < buffer.length; j += 4) {
  539. tempVertexData.positions[j] = buffer[j];
  540. tempVertexData.positions[j + 1] = buffer[j + 1];
  541. tempVertexData.positions[j + 2] = buffer[j + 2];
  542. }
  543. }
  544. else {
  545. tempVertexData.positions = new Float32Array(buffer.length);
  546. (<Float32Array>tempVertexData.positions).set(buffer);
  547. }
  548. verticesCounts.push(tempVertexData.positions.length);
  549. }
  550. else if (semantic.indexOf("TEXCOORD_") !== -1) {
  551. var channel = Number(semantic.split("_")[1]);
  552. var uvKind = VertexBuffer.UVKind + (channel === 0 ? "" : (channel + 1));
  553. var uvs = new Float32Array(buffer.length);
  554. (<Float32Array>uvs).set(buffer);
  555. normalizeUVs(uvs);
  556. tempVertexData.set(uvs, uvKind);
  557. }
  558. else if (semantic === "JOINT") {
  559. tempVertexData.matricesIndices = new Float32Array(buffer.length);
  560. (<Float32Array>tempVertexData.matricesIndices).set(buffer);
  561. }
  562. else if (semantic === "WEIGHT") {
  563. tempVertexData.matricesWeights = new Float32Array(buffer.length);
  564. (<Float32Array>tempVertexData.matricesWeights).set(buffer);
  565. }
  566. else if (semantic === "COLOR") {
  567. tempVertexData.colors = new Float32Array(buffer.length);
  568. (<Float32Array>tempVertexData.colors).set(buffer);
  569. }
  570. }
  571. // Indices
  572. accessor = gltfRuntime.accessors[primitive.indices];
  573. buffer = GLTFUtils.GetBufferFromAccessor(gltfRuntime, accessor);
  574. tempVertexData.indices = new Int32Array(buffer.length);
  575. (<Float32Array>tempVertexData.indices).set(buffer);
  576. indexCounts.push(tempVertexData.indices.length);
  577. vertexData.merge(tempVertexData);
  578. tempVertexData = undefined;
  579. // Sub material
  580. var material = gltfRuntime.scene.getMaterialByID(primitive.material);
  581. multiMat.subMaterials.push(material === null ? gltfRuntime.scene.defaultMaterial : material);
  582. // Update vertices start and index start
  583. verticesStarts.push(verticesStarts.length === 0 ? 0 : verticesStarts[verticesStarts.length - 1] + verticesCounts[verticesCounts.length - 2]);
  584. indexStarts.push(indexStarts.length === 0 ? 0 : indexStarts[indexStarts.length - 1] + indexCounts[indexCounts.length - 2]);
  585. }
  586. }
  587. // Apply geometry
  588. geometry.setAllVerticesData(vertexData, false);
  589. newMesh.computeWorldMatrix(true);
  590. // Apply submeshes
  591. newMesh.subMeshes = [];
  592. var index = 0;
  593. for (var meshIndex = 0; meshIndex < meshes.length; meshIndex++) {
  594. var meshID = meshes[meshIndex];
  595. var mesh: IGLTFMesh = gltfRuntime.meshes[meshID];
  596. if (!mesh) {
  597. continue;
  598. }
  599. for (var i = 0; i < mesh.primitives.length; i++) {
  600. if (mesh.primitives[i].mode !== 4) {
  601. //continue;
  602. }
  603. var subMesh = new SubMesh(index, verticesStarts[index], verticesCounts[index], indexStarts[index], indexCounts[index], newMesh, newMesh, true);
  604. index++;
  605. }
  606. }
  607. // Finish
  608. return newMesh;
  609. };
  610. /**
  611. * Configure node transformation from position, rotation and scaling
  612. */
  613. var configureNode = (newNode: any, position: Vector3, rotation: Quaternion, scaling: Vector3) => {
  614. if (newNode.position) {
  615. newNode.position = position;
  616. }
  617. if (newNode.rotationQuaternion || newNode.rotation) {
  618. newNode.rotationQuaternion = rotation;
  619. }
  620. if (newNode.scaling) {
  621. newNode.scaling = scaling;
  622. }
  623. };
  624. /**
  625. * Configures node from transformation matrix
  626. */
  627. var configureNodeFromMatrix = (newNode: any, node: IGLTFNode) => {
  628. if (node.matrix) {
  629. var position = new Vector3(0, 0, 0);
  630. var rotation = new Quaternion();
  631. var scaling = new Vector3(0, 0, 0);
  632. var mat = Matrix.FromArray(node.matrix);
  633. mat.decompose(scaling, rotation, position);
  634. // Y is Up
  635. if (GLTFFileLoader.MakeYUP) {
  636. rotation = rotation.multiply(new Quaternion(-0.707107, 0, 0, 0.707107));
  637. }
  638. configureNode(newNode, position, rotation, scaling);
  639. if (newNode instanceof TargetCamera) {
  640. (<TargetCamera>newNode).setTarget(Vector3.Zero());
  641. }
  642. }
  643. else {
  644. configureNode(newNode, Vector3.FromArray(node.translation), Quaternion.FromArray(node.rotation), Vector3.FromArray(node.scale));
  645. }
  646. };
  647. /**
  648. * Imports a node
  649. */
  650. var importNode = (gltfRuntime: IGLTFRuntime, node: IGLTFNode, id: string): Node => {
  651. var lastNode: Node = null;
  652. if (gltfRuntime.importOnlyMeshes && (node.skin || node.meshes)) {
  653. if (gltfRuntime.importMeshesNames.length > 0 && gltfRuntime.importMeshesNames.indexOf(node.name) === -1) {
  654. return null;
  655. }
  656. }
  657. // Meshes
  658. if (node.skin) {
  659. if (node.meshes) {
  660. var skin: IGLTFSkins = gltfRuntime.skins[node.skin];
  661. var newMesh = importMesh(gltfRuntime, node, node.meshes, id, <Mesh>node.babylonNode);
  662. newMesh.skeleton = gltfRuntime.scene.getLastSkeletonByID(node.skin);
  663. if (newMesh.skeleton === null) {
  664. newMesh.skeleton = importSkeleton(gltfRuntime, skin, newMesh, skin.babylonSkeleton, node.skin);
  665. if (!skin.babylonSkeleton) {
  666. skin.babylonSkeleton = newMesh.skeleton;
  667. }
  668. }
  669. if (newMesh.skeleton !== null) {
  670. newMesh.useBones = true;
  671. }
  672. lastNode = newMesh;
  673. }
  674. }
  675. else if (node.meshes) {
  676. /**
  677. * Improve meshes property
  678. */
  679. var newMesh = importMesh(gltfRuntime, node, node.mesh ? [node.mesh] : node.meshes, id, <Mesh>node.babylonNode);
  680. lastNode = newMesh;
  681. }
  682. // Lights
  683. else if (node.light && !node.babylonNode && !gltfRuntime.importOnlyMeshes) {
  684. var light: IGLTFLight = gltfRuntime.lights[node.light];
  685. if (light) {
  686. if (light.type === "ambient") {
  687. var ambienLight: IGLTFAmbienLight = light[light.type];
  688. var hemiLight = new HemisphericLight(node.light, Vector3.Zero(), gltfRuntime.scene);
  689. hemiLight.name = node.name;
  690. if (ambienLight.color) {
  691. hemiLight.diffuse = Color3.FromArray(ambienLight.color);
  692. }
  693. lastNode = hemiLight;
  694. }
  695. else if (light.type === "directional") {
  696. var directionalLight: IGLTFDirectionalLight = light[light.type];
  697. var dirLight = new DirectionalLight(node.light, Vector3.Zero(), gltfRuntime.scene);
  698. dirLight.name = node.name;
  699. if (directionalLight.color) {
  700. dirLight.diffuse = Color3.FromArray(directionalLight.color);
  701. }
  702. lastNode = dirLight;
  703. }
  704. else if (light.type === "point") {
  705. var pointLight: IGLTFPointLight = light[light.type];
  706. var ptLight = new PointLight(node.light, Vector3.Zero(), gltfRuntime.scene);
  707. ptLight.name = node.name;
  708. if (pointLight.color) {
  709. ptLight.diffuse = Color3.FromArray(pointLight.color);
  710. }
  711. lastNode = ptLight;
  712. }
  713. else if (light.type === "spot") {
  714. var spotLight: IGLTFSpotLight = light[light.type];
  715. var spLight = new SpotLight(node.light, Vector3.Zero(), Vector3.Zero(), 0, 0, gltfRuntime.scene);
  716. spLight.name = node.name;
  717. if (spotLight.color) {
  718. spLight.diffuse = Color3.FromArray(spotLight.color);
  719. }
  720. if (spotLight.fallOfAngle) {
  721. spLight.angle = spotLight.fallOfAngle;
  722. }
  723. if (spotLight.fallOffExponent) {
  724. spLight.exponent = spotLight.fallOffExponent;
  725. }
  726. lastNode = spLight;
  727. }
  728. }
  729. }
  730. // Cameras
  731. else if (node.camera && !node.babylonNode && !gltfRuntime.importOnlyMeshes) {
  732. var camera: IGLTFCamera = gltfRuntime.cameras[node.camera];
  733. if (camera) {
  734. if (camera.type === "orthographic") {
  735. var orthographicCamera: IGLTFCameraOrthographic = camera[camera.type];
  736. var orthoCamera = new FreeCamera(node.camera, Vector3.Zero(), gltfRuntime.scene);
  737. orthoCamera.name = node.name;
  738. orthoCamera.mode = Camera.ORTHOGRAPHIC_CAMERA;
  739. orthoCamera.attachControl(gltfRuntime.scene.getEngine().getRenderingCanvas());
  740. lastNode = orthoCamera;
  741. }
  742. else if (camera.type === "perspective") {
  743. var perspectiveCamera: IGLTFCameraPerspective = camera[camera.type];
  744. var persCamera = new FreeCamera(node.camera, Vector3.Zero(), gltfRuntime.scene);
  745. persCamera.name = node.name;
  746. persCamera.attachControl(gltfRuntime.scene.getEngine().getRenderingCanvas());
  747. if (!perspectiveCamera.aspectRatio) {
  748. perspectiveCamera.aspectRatio = gltfRuntime.scene.getEngine().getRenderWidth() / gltfRuntime.scene.getEngine().getRenderHeight();
  749. }
  750. if (perspectiveCamera.znear && perspectiveCamera.zfar) {
  751. persCamera.maxZ = perspectiveCamera.zfar;
  752. persCamera.minZ = perspectiveCamera.znear;
  753. }
  754. lastNode = persCamera;
  755. }
  756. }
  757. }
  758. // Empty node
  759. if (!node.jointName) {
  760. if (node.babylonNode) {
  761. return node.babylonNode;
  762. }
  763. else if (lastNode === null) {
  764. var dummy = new Mesh(node.name, gltfRuntime.scene);
  765. node.babylonNode = dummy;
  766. lastNode = dummy;
  767. }
  768. }
  769. if (lastNode !== null) {
  770. if (node.matrix) {
  771. configureNodeFromMatrix(lastNode, node);
  772. }
  773. else {
  774. configureNode(lastNode, Vector3.FromArray(node.translation), Quaternion.RotationAxis(Vector3.FromArray(node.rotation).normalize(), node.rotation[3]), Vector3.FromArray(node.scale));
  775. }
  776. lastNode.updateCache(true);
  777. node.babylonNode = lastNode;
  778. }
  779. return lastNode;
  780. };
  781. /**
  782. * Traverses nodes and creates them
  783. */
  784. var traverseNodes = (gltfRuntime: IGLTFRuntime, id: string, parent: Node, meshIncluded?: boolean) => {
  785. var node: IGLTFNode = gltfRuntime.nodes[id];
  786. var newNode: Node = null;
  787. if (gltfRuntime.importOnlyMeshes && !meshIncluded) {
  788. if (gltfRuntime.importMeshesNames.indexOf(node.name) !== -1 || gltfRuntime.importMeshesNames.length === 0) {
  789. meshIncluded = true;
  790. }
  791. else {
  792. meshIncluded = false;
  793. }
  794. }
  795. else {
  796. meshIncluded = true;
  797. }
  798. if (!node.jointName && meshIncluded) {
  799. newNode = importNode(gltfRuntime, node, id);
  800. if (newNode !== null) {
  801. newNode.id = id;
  802. newNode.parent = parent;
  803. }
  804. }
  805. for (var i = 0; i < node.children.length; i++) {
  806. traverseNodes(gltfRuntime, node.children[i], newNode, meshIncluded);
  807. }
  808. };
  809. /**
  810. * Buffers loaded, create nodes
  811. */
  812. var onBuffersLoaded = (gltfRuntime: IGLTFRuntime) => {
  813. // Nodes
  814. var currentScene: IGLTFScene = <IGLTFScene>gltfRuntime.currentScene;
  815. for (var i = 0; i < currentScene.nodes.length; i++) {
  816. traverseNodes(gltfRuntime, currentScene.nodes[i], null);
  817. }
  818. // Set animations
  819. loadAnimations(gltfRuntime);
  820. for (var i = 0; i < gltfRuntime.scene.skeletons.length; i++) {
  821. var skeleton = gltfRuntime.scene.skeletons[i];
  822. gltfRuntime.scene.beginAnimation(skeleton, 0, Number.MAX_VALUE, true, 1.0);
  823. }
  824. };
  825. /**
  826. * Decode array buffer from base64
  827. */
  828. var decodeArrayBuffer = (base64: string): ArrayBuffer => {
  829. var decodedString = atob(base64);
  830. var bufferLength = decodedString.length;
  831. var arraybuffer = new Uint8Array(new ArrayBuffer(bufferLength));
  832. for (var i = 0; i < bufferLength; i++) {
  833. arraybuffer[i] = decodedString.charCodeAt(i);
  834. }
  835. return arraybuffer.buffer;
  836. };
  837. /**
  838. * onBind shaderrs callback to set uniforms and matrices
  839. */
  840. var onBindShaderMaterial = (mesh: Mesh, gltfRuntime: IGLTFRuntime, unTreatedUniforms: Object, shaderMaterial: ShaderMaterial, technique: IGLTFTechnique, material: IGLTFMaterial, onSuccess: (shaderMaterial: ShaderMaterial) => void) => {
  841. for (var unif in unTreatedUniforms) {
  842. var uniform: IGLTFTechniqueParameter = unTreatedUniforms[unif];
  843. var type = uniform.type;
  844. if (type === EParameterType.FLOAT_MAT2 || type === EParameterType.FLOAT_MAT3 || type === EParameterType.FLOAT_MAT4) {
  845. if (uniform.semantic && !uniform.source && !uniform.node) {
  846. GLTFUtils.SetMatrix(gltfRuntime.scene, mesh, uniform, unif, shaderMaterial.getEffect());
  847. }
  848. else if (uniform.semantic && (uniform.source || uniform.node)) {
  849. var source = gltfRuntime.scene.getNodeByName(uniform.source || uniform.node);
  850. if (source === null) {
  851. source = gltfRuntime.scene.getNodeByID(uniform.source || uniform.node);
  852. }
  853. if (source === null) {
  854. continue;
  855. }
  856. GLTFUtils.SetMatrix(gltfRuntime.scene, source, uniform, unif, shaderMaterial.getEffect());
  857. }
  858. }
  859. else {
  860. var value = material.values[technique.uniforms[unif]];
  861. if (!value) {
  862. continue;
  863. }
  864. if (type === EParameterType.SAMPLER_2D) {
  865. var texture: Texture = gltfRuntime.textures[value].babylonTexture;
  866. if (texture === null) {
  867. continue;
  868. }
  869. shaderMaterial.getEffect().setTexture(unif, texture);
  870. }
  871. else {
  872. GLTFUtils.SetUniform(shaderMaterial.getEffect(), unif, value, type);
  873. }
  874. }
  875. }
  876. onSuccess(shaderMaterial);
  877. };
  878. /**
  879. * Prepare uniforms to send the only one time
  880. * Loads the appropriate textures
  881. */
  882. var prepareShaderMaterialUniforms = (gltfRuntime: IGLTFRuntime, shaderMaterial: ShaderMaterial, technique: IGLTFTechnique, material: IGLTFMaterial, unTreatedUniforms: Object) => {
  883. var materialValues = material.values;
  884. var techniqueUniforms = technique.uniforms;
  885. /**
  886. * Prepare values here (not matrices)
  887. */
  888. for (var unif in unTreatedUniforms) {
  889. var uniform: IGLTFTechniqueParameter = unTreatedUniforms[unif];
  890. var type = uniform.type;
  891. var value = materialValues[techniqueUniforms[unif]] || uniform.value;
  892. if (!value) {
  893. continue;
  894. }
  895. var onLoadTexture = (texture: Texture) => {
  896. if (uniform.value) {
  897. // Static uniform
  898. shaderMaterial.setTexture(unif, texture);
  899. delete unTreatedUniforms[unif];
  900. }
  901. };
  902. // Texture (sampler2D)
  903. if (type === EParameterType.SAMPLER_2D) {
  904. GLTFFileLoaderExtension.LoadTextureAsync(gltfRuntime, <string>value, onLoadTexture, () => onLoadTexture(null));
  905. }
  906. // Others
  907. else {
  908. if (uniform.value && GLTFUtils.SetUniform(shaderMaterial, unif, value, type)) {
  909. // Static uniform
  910. delete unTreatedUniforms[unif];
  911. }
  912. }
  913. }
  914. };
  915. /**
  916. * Shader compilation failed
  917. */
  918. var onShaderCompileError = (program: IGLTFProgram, shaderMaterial: ShaderMaterial, onError: () => void) => {
  919. return (effect: Effect, error: string) => {
  920. Tools.Error("Cannot compile program named " + program.name + ". Error: " + error + ". Default material will be applied");
  921. shaderMaterial.dispose(true);
  922. onError();
  923. };
  924. };
  925. /**
  926. * Shader compilation success
  927. */
  928. var onShaderCompileSuccess = (gltfRuntime: IGLTFRuntime, shaderMaterial: ShaderMaterial, technique: IGLTFTechnique, material: IGLTFMaterial, unTreatedUniforms: Object, onSuccess: (shaderMaterial: ShaderMaterial) => void) => {
  929. return (_: Effect) => {
  930. prepareShaderMaterialUniforms(gltfRuntime, shaderMaterial, technique, material, unTreatedUniforms);
  931. shaderMaterial.onBind = (mesh: Mesh) => {
  932. onBindShaderMaterial(mesh, gltfRuntime, unTreatedUniforms, shaderMaterial, technique, material, onSuccess);
  933. };
  934. };
  935. };
  936. /**
  937. * Returns the appropriate uniform if already handled by babylon
  938. */
  939. var parseShaderUniforms = (tokenizer: Tokenizer, technique: IGLTFTechnique, unTreatedUniforms: Object): string => {
  940. for (var unif in technique.uniforms) {
  941. var uniform = technique.uniforms[unif];
  942. var uniformParameter: IGLTFTechniqueParameter = technique.parameters[uniform];
  943. if (tokenizer.currentIdentifier === unif) {
  944. if (uniformParameter.semantic && !uniformParameter.source && !uniformParameter.node) {
  945. var transformIndex = glTFTransforms.indexOf(uniformParameter.semantic);
  946. if (transformIndex !== -1) {
  947. delete unTreatedUniforms[unif];
  948. return babylonTransforms[transformIndex];
  949. }
  950. }
  951. }
  952. }
  953. return tokenizer.currentIdentifier;
  954. };
  955. /**
  956. * All shaders loaded. Create materials one by one
  957. */
  958. var importMaterials = (gltfRuntime: IGLTFRuntime) => {
  959. // Create materials
  960. for (var mat in gltfRuntime.materials) {
  961. GLTFFileLoaderExtension.LoadMaterialAsync(gltfRuntime, mat, (material: Material) => { }, () => { });
  962. }
  963. };
  964. /**
  965. * Implementation of the base glTF spec
  966. */
  967. export class GLTFFileLoaderBase {
  968. public static CreateRuntime(parsedData: any, scene: Scene, rootUrl: string): IGLTFRuntime {
  969. var gltfRuntime: IGLTFRuntime = {
  970. accessors: {},
  971. buffers: {},
  972. bufferViews: {},
  973. meshes: {},
  974. lights: {},
  975. cameras: {},
  976. nodes: {},
  977. images: {},
  978. textures: {},
  979. shaders: {},
  980. programs: {},
  981. samplers: {},
  982. techniques: {},
  983. materials: {},
  984. animations: {},
  985. skins: {},
  986. currentScene: {},
  987. extensionsUsed: [],
  988. buffersCount: 0,
  989. shaderscount: 0,
  990. scene: scene,
  991. rootUrl: rootUrl,
  992. loadedBufferCount: 0,
  993. loadedBufferViews: {},
  994. loadedShaderCount: 0,
  995. importOnlyMeshes: false,
  996. dummyNodes: []
  997. }
  998. // Parse
  999. if (parsedData.extensionsUsed) {
  1000. parseObject(parsedData.extensionsUsed, "extensionsUsed", gltfRuntime);
  1001. }
  1002. if (parsedData.buffers) {
  1003. parseBuffers(parsedData.buffers, gltfRuntime);
  1004. }
  1005. if (parsedData.bufferViews) {
  1006. parseObject(parsedData.bufferViews, "bufferViews", gltfRuntime);
  1007. }
  1008. if (parsedData.accessors) {
  1009. parseObject(parsedData.accessors, "accessors", gltfRuntime);
  1010. }
  1011. if (parsedData.meshes) {
  1012. parseObject(parsedData.meshes, "meshes", gltfRuntime);
  1013. }
  1014. if (parsedData.lights) {
  1015. parseObject(parsedData.lights, "lights", gltfRuntime);
  1016. }
  1017. if (parsedData.cameras) {
  1018. parseObject(parsedData.cameras, "cameras", gltfRuntime);
  1019. }
  1020. if (parsedData.nodes) {
  1021. parseObject(parsedData.nodes, "nodes", gltfRuntime);
  1022. }
  1023. if (parsedData.images) {
  1024. parseObject(parsedData.images, "images", gltfRuntime);
  1025. }
  1026. if (parsedData.textures) {
  1027. parseObject(parsedData.textures, "textures", gltfRuntime);
  1028. }
  1029. if (parsedData.shaders) {
  1030. parseShaders(parsedData.shaders, gltfRuntime);
  1031. }
  1032. if (parsedData.programs) {
  1033. parseObject(parsedData.programs, "programs", gltfRuntime);
  1034. }
  1035. if (parsedData.samplers) {
  1036. parseObject(parsedData.samplers, "samplers", gltfRuntime);
  1037. }
  1038. if (parsedData.techniques) {
  1039. parseObject(parsedData.techniques, "techniques", gltfRuntime);
  1040. }
  1041. if (parsedData.materials) {
  1042. parseObject(parsedData.materials, "materials", gltfRuntime);
  1043. }
  1044. if (parsedData.animations) {
  1045. parseObject(parsedData.animations, "animations", gltfRuntime);
  1046. }
  1047. if (parsedData.skins) {
  1048. parseObject(parsedData.skins, "skins", gltfRuntime);
  1049. }
  1050. if (parsedData.scene && parsedData.scenes) {
  1051. gltfRuntime.currentScene = parsedData.scenes[parsedData.scene];
  1052. }
  1053. return gltfRuntime;
  1054. }
  1055. public static LoadBufferAsync(gltfRuntime: IGLTFRuntime, id: string, onSuccess: (bufferView: ArrayBufferView) => void, onError: () => void): void {
  1056. var buffer: IGLTFBuffer = gltfRuntime.buffers[id];
  1057. if (GLTFUtils.IsBase64(buffer.uri)) {
  1058. var decodedBuffer = decodeArrayBuffer(buffer.uri.split(",")[1]);
  1059. onSuccess(new Uint8Array(decodedBuffer));
  1060. }
  1061. else {
  1062. Tools.LoadFile(gltfRuntime.rootUrl + buffer.uri, data => onSuccess(new Uint8Array(data)), null, null, true, onError);
  1063. }
  1064. }
  1065. public static LoadTextureAsync(gltfRuntime: IGLTFRuntime, id: string, onSuccess: (texture: Texture) => void, onError: () => void): void {
  1066. var texture: IGLTFTexture = gltfRuntime.textures[id];
  1067. if (!texture || !texture.source) {
  1068. onError();
  1069. return;
  1070. }
  1071. if (texture.babylonTexture) {
  1072. onSuccess(texture.babylonTexture);
  1073. return;
  1074. }
  1075. var sampler: IGLTFSampler = gltfRuntime.samplers[texture.sampler];
  1076. var source: IGLTFImage = gltfRuntime.images[texture.source];
  1077. var newTexture: Texture;
  1078. var createMipMaps =
  1079. (sampler.minFilter === ETextureFilterType.NEAREST_MIPMAP_NEAREST) ||
  1080. (sampler.minFilter === ETextureFilterType.NEAREST_MIPMAP_LINEAR) ||
  1081. (sampler.minFilter === ETextureFilterType.LINEAR_MIPMAP_NEAREST) ||
  1082. (sampler.minFilter === ETextureFilterType.LINEAR_MIPMAP_LINEAR);
  1083. var samplingMode = Texture.BILINEAR_SAMPLINGMODE;
  1084. if (GLTFUtils.IsBase64(source.uri)) {
  1085. newTexture = new Texture(source.uri, gltfRuntime.scene, !createMipMaps, true, samplingMode, () => onSuccess(newTexture), onError, source.uri, true);
  1086. }
  1087. else {
  1088. newTexture = new Texture(gltfRuntime.rootUrl + source.uri, gltfRuntime.scene, !createMipMaps, true, samplingMode, () => onSuccess(newTexture), onError);
  1089. }
  1090. newTexture.wrapU = GLTFUtils.GetWrapMode(sampler.wrapS);
  1091. newTexture.wrapV = GLTFUtils.GetWrapMode(sampler.wrapT);
  1092. newTexture.name = id;
  1093. texture.babylonTexture = newTexture;
  1094. }
  1095. public static LoadShaderDataAsync(gltfRuntime: IGLTFRuntime, id: string, onSuccess: (shaderData: string) => void, onError: () => void): void {
  1096. var shader: IGLTFShader = gltfRuntime.shaders[id];
  1097. if (GLTFUtils.IsBase64(shader.uri)) {
  1098. var shaderData = atob(shader.uri.split(",")[1]);
  1099. onSuccess(shaderData);
  1100. }
  1101. else {
  1102. Tools.LoadFile(gltfRuntime.rootUrl + shader.uri, onSuccess, null, null, false, onError);
  1103. }
  1104. }
  1105. public static LoadMaterialAsync(gltfRuntime: IGLTFRuntime, id: string, onSuccess: (material: Material) => void, onError: () => void): void {
  1106. var material: IGLTFMaterial = gltfRuntime.materials[id];
  1107. var technique: IGLTFTechnique = gltfRuntime.techniques[material.technique];
  1108. var program: IGLTFProgram = gltfRuntime.programs[technique.program];
  1109. var states: IGLTFTechniqueStates = technique.states;
  1110. var vertexShader: string = Effect.ShadersStore[program.vertexShader + "VertexShader"];
  1111. var pixelShader: string = Effect.ShadersStore[program.fragmentShader + "PixelShader"];
  1112. var newVertexShader = "";
  1113. var newPixelShader = "";
  1114. var vertexTokenizer = new Tokenizer(vertexShader);
  1115. var pixelTokenizer = new Tokenizer(pixelShader);
  1116. var unTreatedUniforms: Object = {};
  1117. var uniforms = [];
  1118. var attributes = [];
  1119. var samplers = [];
  1120. // Fill uniform, sampler2D and attributes
  1121. for (var unif in technique.uniforms) {
  1122. var uniform = technique.uniforms[unif];
  1123. var uniformParameter: IGLTFTechniqueParameter = technique.parameters[uniform];
  1124. unTreatedUniforms[unif] = uniformParameter;
  1125. if (uniformParameter.semantic && !uniformParameter.node && !uniformParameter.source) {
  1126. var transformIndex = glTFTransforms.indexOf(uniformParameter.semantic);
  1127. if (transformIndex !== -1) {
  1128. uniforms.push(babylonTransforms[transformIndex]);
  1129. delete unTreatedUniforms[unif];
  1130. }
  1131. else {
  1132. uniforms.push(unif);
  1133. }
  1134. }
  1135. else if (uniformParameter.type === EParameterType.SAMPLER_2D) {
  1136. samplers.push(unif);
  1137. }
  1138. else {
  1139. uniforms.push(unif);
  1140. }
  1141. }
  1142. for (var attr in technique.attributes) {
  1143. var attribute = technique.attributes[attr];
  1144. var attributeParameter: IGLTFTechniqueParameter = technique.parameters[attribute];
  1145. if (attributeParameter.semantic) {
  1146. attributes.push(getAttribute(attributeParameter));
  1147. }
  1148. }
  1149. // Configure vertex shader
  1150. while (!vertexTokenizer.isEnd() && vertexTokenizer.getNextToken()) {
  1151. var tokenType = vertexTokenizer.currentToken;
  1152. if (tokenType !== ETokenType.IDENTIFIER) {
  1153. newVertexShader += vertexTokenizer.currentString;
  1154. continue;
  1155. }
  1156. var foundAttribute = false;
  1157. for (var attr in technique.attributes) {
  1158. var attribute = technique.attributes[attr];
  1159. var attributeParameter: IGLTFTechniqueParameter = technique.parameters[attribute];
  1160. if (vertexTokenizer.currentIdentifier === attr && attributeParameter.semantic) {
  1161. newVertexShader += getAttribute(attributeParameter);
  1162. foundAttribute = true;
  1163. break;
  1164. }
  1165. }
  1166. if (foundAttribute) {
  1167. continue;
  1168. }
  1169. newVertexShader += parseShaderUniforms(vertexTokenizer, technique, unTreatedUniforms);
  1170. }
  1171. // Configure pixel shader
  1172. while (!pixelTokenizer.isEnd() && pixelTokenizer.getNextToken()) {
  1173. var tokenType = pixelTokenizer.currentToken;
  1174. if (tokenType !== ETokenType.IDENTIFIER) {
  1175. newPixelShader += pixelTokenizer.currentString;
  1176. continue;
  1177. }
  1178. newPixelShader += parseShaderUniforms(pixelTokenizer, technique, unTreatedUniforms);
  1179. }
  1180. // Create shader material
  1181. var shaderPath = {
  1182. vertex: program.vertexShader + id,
  1183. fragment: program.fragmentShader + id
  1184. };
  1185. var options = {
  1186. attributes: attributes,
  1187. uniforms: uniforms,
  1188. samplers: samplers,
  1189. needAlphaBlending: states.functions && states.functions.blendEquationSeparate
  1190. };
  1191. Effect.ShadersStore[program.vertexShader + id + "VertexShader"] = newVertexShader;
  1192. Effect.ShadersStore[program.fragmentShader + id + "PixelShader"] = newPixelShader;
  1193. var shaderMaterial = new ShaderMaterial(id, gltfRuntime.scene, shaderPath, options);
  1194. shaderMaterial.onError = onShaderCompileError(program, shaderMaterial, onError);
  1195. shaderMaterial.onCompiled = onShaderCompileSuccess(gltfRuntime, shaderMaterial, technique, material, unTreatedUniforms, onSuccess);
  1196. shaderMaterial.sideOrientation = Material.CounterClockWiseSideOrientation;
  1197. if (states.functions) {
  1198. var functions = states.functions;
  1199. if (functions.cullFace && functions.cullFace[0] !== ECullingType.BACK) {
  1200. shaderMaterial.backFaceCulling = false;
  1201. }
  1202. var blendFunc = functions.blendFuncSeparate;
  1203. if (blendFunc) {
  1204. if (blendFunc[0] === EBlendingFunction.SRC_ALPHA && blendFunc[1] === EBlendingFunction.ONE_MINUS_SRC_ALPHA && blendFunc[2] === EBlendingFunction.ONE && blendFunc[3] === EBlendingFunction.ONE) {
  1205. shaderMaterial.alphaMode = Engine.ALPHA_COMBINE;
  1206. }
  1207. else if (blendFunc[0] === EBlendingFunction.ONE && blendFunc[1] === EBlendingFunction.ONE && blendFunc[2] === EBlendingFunction.ZERO && blendFunc[3] === EBlendingFunction.ONE) {
  1208. shaderMaterial.alphaMode = Engine.ALPHA_ONEONE;
  1209. }
  1210. else if (blendFunc[0] === EBlendingFunction.SRC_ALPHA && blendFunc[1] === EBlendingFunction.ONE && blendFunc[2] === EBlendingFunction.ZERO && blendFunc[3] === EBlendingFunction.ONE) {
  1211. shaderMaterial.alphaMode = Engine.ALPHA_ADD;
  1212. }
  1213. else if (blendFunc[0] === EBlendingFunction.ZERO && blendFunc[1] === EBlendingFunction.ONE_MINUS_SRC_COLOR && blendFunc[2] === EBlendingFunction.ONE && blendFunc[3] === EBlendingFunction.ONE) {
  1214. shaderMaterial.alphaMode = Engine.ALPHA_SUBTRACT;
  1215. }
  1216. else if (blendFunc[0] === EBlendingFunction.DST_COLOR && blendFunc[1] === EBlendingFunction.ZERO && blendFunc[2] === EBlendingFunction.ONE && blendFunc[3] === EBlendingFunction.ONE) {
  1217. shaderMaterial.alphaMode = Engine.ALPHA_MULTIPLY;
  1218. }
  1219. else if (blendFunc[0] === EBlendingFunction.SRC_ALPHA && blendFunc[1] === EBlendingFunction.ONE_MINUS_SRC_COLOR && blendFunc[2] === EBlendingFunction.ONE && blendFunc[3] === EBlendingFunction.ONE) {
  1220. shaderMaterial.alphaMode = Engine.ALPHA_MAXIMIZED;
  1221. }
  1222. }
  1223. }
  1224. }
  1225. }
  1226. /**
  1227. * glTF File Loader Plugin
  1228. */
  1229. export class GLTFFileLoader implements ISceneLoaderPluginAsync {
  1230. /**
  1231. * Public members
  1232. */
  1233. public extensions: ISceneLoaderPluginExtensions = {
  1234. ".gltf": { isBinary: false },
  1235. ".glb": { isBinary: true }
  1236. };
  1237. /**
  1238. * Private members
  1239. */
  1240. // None
  1241. /**
  1242. * Static members
  1243. */
  1244. public static MakeYUP: boolean = false;
  1245. public static HomogeneousCoordinates: boolean = false;
  1246. public static Extensions: { [name: string]: GLTFFileLoaderExtension } = {};
  1247. public static RegisterExtension(extension: GLTFFileLoaderExtension): void {
  1248. if (GLTFFileLoader.Extensions[extension.name]) {
  1249. Tools.Error("Tool with the same name \"" + extension.name + "\" already exists");
  1250. return;
  1251. }
  1252. GLTFFileLoader.Extensions[extension.name] = extension;
  1253. }
  1254. /**
  1255. * Import meshes
  1256. */
  1257. public importMeshAsync(meshesNames: any, scene: Scene, data: any, rootUrl: string, onSuccess?: (meshes: AbstractMesh[], particleSystems: ParticleSystem[], skeletons: Skeleton[]) => void, onError?: () => void): boolean {
  1258. scene.useRightHandedSystem = true;
  1259. var gltfRuntime = GLTFFileLoaderExtension.LoadRuntimeAsync(scene, data, rootUrl, gltfRuntime => {
  1260. gltfRuntime.importOnlyMeshes = true;
  1261. if (meshesNames === "") {
  1262. gltfRuntime.importMeshesNames = [];
  1263. }
  1264. else if (typeof meshesNames === "string") {
  1265. gltfRuntime.importMeshesNames = [meshesNames];
  1266. }
  1267. else if (meshesNames && !(meshesNames instanceof Array)) {
  1268. gltfRuntime.importMeshesNames = [meshesNames];
  1269. }
  1270. else {
  1271. gltfRuntime.importMeshesNames = [];
  1272. Tools.Warn("Argument meshesNames must be of type string or string[]");
  1273. }
  1274. // Create nodes
  1275. this._createNodes(gltfRuntime);
  1276. var meshes = [];
  1277. var skeletons = [];
  1278. // Fill arrays of meshes and skeletons
  1279. for (var nde in gltfRuntime.nodes) {
  1280. var node: IGLTFNode = gltfRuntime.nodes[nde];
  1281. if (node.babylonNode instanceof AbstractMesh) {
  1282. meshes.push(<AbstractMesh>node.babylonNode);
  1283. }
  1284. }
  1285. for (var skl in gltfRuntime.skins) {
  1286. var skin: IGLTFSkins = gltfRuntime.skins[skl];
  1287. if (skin.babylonSkeleton instanceof Skeleton) {
  1288. skeletons.push(skin.babylonSkeleton);
  1289. }
  1290. }
  1291. // Load shaders and then buffers
  1292. this._loadShadersAsync(gltfRuntime, () => {
  1293. importMaterials(gltfRuntime);
  1294. this._loadBuffersAsync(gltfRuntime, () => {
  1295. onBuffersLoaded(gltfRuntime);
  1296. });
  1297. });
  1298. if (onSuccess) {
  1299. onSuccess(meshes, null, skeletons);
  1300. }
  1301. }, onError);
  1302. return true;
  1303. }
  1304. /**
  1305. * Load scene
  1306. */
  1307. public loadAsync(scene: Scene, data: string | ArrayBuffer, rootUrl: string, onSuccess: () => void, onError: () => void): boolean {
  1308. scene.useRightHandedSystem = true;
  1309. GLTFFileLoaderExtension.LoadRuntimeAsync(scene, data, rootUrl, gltfRuntime => {
  1310. // Create nodes
  1311. this._createNodes(gltfRuntime);
  1312. // Load shaders and then buffers
  1313. this._loadShadersAsync(gltfRuntime, () => {
  1314. importMaterials(gltfRuntime);
  1315. this._loadBuffersAsync(gltfRuntime, () => {
  1316. onBuffersLoaded(gltfRuntime);
  1317. });
  1318. });
  1319. onSuccess();
  1320. }, onError);
  1321. return true;
  1322. }
  1323. private _loadShadersAsync(gltfRuntime: IGLTFRuntime, onload: () => void): void {
  1324. var hasShaders = false;
  1325. var processShader = (sha: string, shader: IGLTFShader) => {
  1326. GLTFFileLoaderExtension.LoadShaderDataAsync(gltfRuntime, sha, shaderData => {
  1327. gltfRuntime.loadedShaderCount++;
  1328. if (shaderData) {
  1329. Effect.ShadersStore[sha + (shader.type === EShaderType.VERTEX ? "VertexShader" : "PixelShader")] = shaderData;
  1330. }
  1331. if (gltfRuntime.loadedShaderCount === gltfRuntime.shaderscount) {
  1332. onload();
  1333. }
  1334. }, () => {
  1335. Tools.Error("Error when loading shader program named " + sha + " located at " + shader.uri);
  1336. });
  1337. };
  1338. for (var sha in gltfRuntime.shaders) {
  1339. hasShaders = true;
  1340. var shader: IGLTFShader = gltfRuntime.shaders[sha];
  1341. if (shader) {
  1342. processShader.bind(this, sha, shader)();
  1343. }
  1344. else {
  1345. Tools.Error("No shader named: " + sha);
  1346. }
  1347. }
  1348. if (!hasShaders) {
  1349. onload();
  1350. }
  1351. };
  1352. private _loadBuffersAsync(gltfRuntime: IGLTFRuntime, onload: () => void): void {
  1353. var hasBuffers = false;
  1354. var processBuffer = (buf: string, buffer: IGLTFBuffer) => {
  1355. GLTFFileLoaderExtension.LoadBufferAsync(gltfRuntime, buf, bufferView => {
  1356. gltfRuntime.loadedBufferCount++;
  1357. if (bufferView) {
  1358. if (bufferView.byteLength != gltfRuntime.buffers[buf].byteLength) {
  1359. Tools.Error("Buffer named " + buf + " is length " + bufferView.byteLength + ". Expected: " + buffer.byteLength); // Improve error message
  1360. }
  1361. gltfRuntime.loadedBufferViews[buf] = bufferView;
  1362. }
  1363. if (gltfRuntime.loadedBufferCount === gltfRuntime.buffersCount) {
  1364. onload();
  1365. }
  1366. }, () => {
  1367. Tools.Error("Error when loading buffer named " + buf + " located at " + buffer.uri);
  1368. });
  1369. };
  1370. for (var buf in gltfRuntime.buffers) {
  1371. hasBuffers = true;
  1372. var buffer: IGLTFBuffer = gltfRuntime.buffers[buf];
  1373. if (buffer) {
  1374. processBuffer.bind(this, buf, buffer)();
  1375. }
  1376. else {
  1377. Tools.Error("No buffer named: " + buf);
  1378. }
  1379. }
  1380. if (!hasBuffers) {
  1381. onload();
  1382. }
  1383. }
  1384. // Creates nodes before loading buffers and shaders
  1385. private _createNodes(gltfRuntime: IGLTFRuntime): void {
  1386. var currentScene = <IGLTFScene>gltfRuntime.currentScene;
  1387. for (var i = 0; i < currentScene.nodes.length; i++) {
  1388. traverseNodes(gltfRuntime, currentScene.nodes[i], null);
  1389. }
  1390. }
  1391. };
  1392. BABYLON.SceneLoader.RegisterPlugin(new GLTFFileLoader());
  1393. }