babylon.glTFFileLoader.ts 64 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703
  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. * onBind shaderrs callback to set uniforms and matrices
  827. */
  828. var onBindShaderMaterial = (mesh: Mesh, gltfRuntime: IGLTFRuntime, unTreatedUniforms: Object, shaderMaterial: ShaderMaterial, technique: IGLTFTechnique, material: IGLTFMaterial, onSuccess: (shaderMaterial: ShaderMaterial) => void) => {
  829. for (var unif in unTreatedUniforms) {
  830. var uniform: IGLTFTechniqueParameter = unTreatedUniforms[unif];
  831. var type = uniform.type;
  832. if (type === EParameterType.FLOAT_MAT2 || type === EParameterType.FLOAT_MAT3 || type === EParameterType.FLOAT_MAT4) {
  833. if (uniform.semantic && !uniform.source && !uniform.node) {
  834. GLTFUtils.SetMatrix(gltfRuntime.scene, mesh, uniform, unif, shaderMaterial.getEffect());
  835. }
  836. else if (uniform.semantic && (uniform.source || uniform.node)) {
  837. var source = gltfRuntime.scene.getNodeByName(uniform.source || uniform.node);
  838. if (source === null) {
  839. source = gltfRuntime.scene.getNodeByID(uniform.source || uniform.node);
  840. }
  841. if (source === null) {
  842. continue;
  843. }
  844. GLTFUtils.SetMatrix(gltfRuntime.scene, source, uniform, unif, shaderMaterial.getEffect());
  845. }
  846. }
  847. else {
  848. var value = material.values[technique.uniforms[unif]];
  849. if (!value) {
  850. continue;
  851. }
  852. if (type === EParameterType.SAMPLER_2D) {
  853. var texture: Texture = gltfRuntime.textures[value].babylonTexture;
  854. if (texture === null) {
  855. continue;
  856. }
  857. shaderMaterial.getEffect().setTexture(unif, texture);
  858. }
  859. else {
  860. GLTFUtils.SetUniform(shaderMaterial.getEffect(), unif, value, type);
  861. }
  862. }
  863. }
  864. onSuccess(shaderMaterial);
  865. };
  866. /**
  867. * Prepare uniforms to send the only one time
  868. * Loads the appropriate textures
  869. */
  870. var prepareShaderMaterialUniforms = (gltfRuntime: IGLTFRuntime, shaderMaterial: ShaderMaterial, technique: IGLTFTechnique, material: IGLTFMaterial, unTreatedUniforms: Object) => {
  871. var materialValues = material.values;
  872. var techniqueUniforms = technique.uniforms;
  873. /**
  874. * Prepare values here (not matrices)
  875. */
  876. for (var unif in unTreatedUniforms) {
  877. var uniform: IGLTFTechniqueParameter = unTreatedUniforms[unif];
  878. var type = uniform.type;
  879. var value = materialValues[techniqueUniforms[unif]] || uniform.value;
  880. if (!value) {
  881. continue;
  882. }
  883. var onLoadTexture = (texture: Texture) => {
  884. if (uniform.value) {
  885. // Static uniform
  886. shaderMaterial.setTexture(unif, texture);
  887. delete unTreatedUniforms[unif];
  888. }
  889. };
  890. // Texture (sampler2D)
  891. if (type === EParameterType.SAMPLER_2D) {
  892. GLTFFileLoaderExtension.LoadTextureAsync(gltfRuntime, <string>value, onLoadTexture, () => onLoadTexture(null));
  893. }
  894. // Others
  895. else {
  896. if (uniform.value && GLTFUtils.SetUniform(shaderMaterial, unif, value, type)) {
  897. // Static uniform
  898. delete unTreatedUniforms[unif];
  899. }
  900. }
  901. }
  902. };
  903. /**
  904. * Shader compilation failed
  905. */
  906. var onShaderCompileError = (program: IGLTFProgram, shaderMaterial: ShaderMaterial, onError: () => void) => {
  907. return (effect: Effect, error: string) => {
  908. Tools.Error("Cannot compile program named " + program.name + ". Error: " + error + ". Default material will be applied");
  909. shaderMaterial.dispose(true);
  910. onError();
  911. };
  912. };
  913. /**
  914. * Shader compilation success
  915. */
  916. var onShaderCompileSuccess = (gltfRuntime: IGLTFRuntime, shaderMaterial: ShaderMaterial, technique: IGLTFTechnique, material: IGLTFMaterial, unTreatedUniforms: Object, onSuccess: (shaderMaterial: ShaderMaterial) => void) => {
  917. return (_: Effect) => {
  918. prepareShaderMaterialUniforms(gltfRuntime, shaderMaterial, technique, material, unTreatedUniforms);
  919. shaderMaterial.onBind = (mesh: Mesh) => {
  920. onBindShaderMaterial(mesh, gltfRuntime, unTreatedUniforms, shaderMaterial, technique, material, onSuccess);
  921. };
  922. };
  923. };
  924. /**
  925. * Returns the appropriate uniform if already handled by babylon
  926. */
  927. var parseShaderUniforms = (tokenizer: Tokenizer, technique: IGLTFTechnique, unTreatedUniforms: Object): string => {
  928. for (var unif in technique.uniforms) {
  929. var uniform = technique.uniforms[unif];
  930. var uniformParameter: IGLTFTechniqueParameter = technique.parameters[uniform];
  931. if (tokenizer.currentIdentifier === unif) {
  932. if (uniformParameter.semantic && !uniformParameter.source && !uniformParameter.node) {
  933. var transformIndex = glTFTransforms.indexOf(uniformParameter.semantic);
  934. if (transformIndex !== -1) {
  935. delete unTreatedUniforms[unif];
  936. return babylonTransforms[transformIndex];
  937. }
  938. }
  939. }
  940. }
  941. return tokenizer.currentIdentifier;
  942. };
  943. /**
  944. * All shaders loaded. Create materials one by one
  945. */
  946. var importMaterials = (gltfRuntime: IGLTFRuntime) => {
  947. // Create materials
  948. for (var mat in gltfRuntime.materials) {
  949. GLTFFileLoaderExtension.LoadMaterialAsync(gltfRuntime, mat, (material: Material) => { }, () => { });
  950. }
  951. };
  952. /**
  953. * Implementation of the base glTF spec
  954. */
  955. export class GLTFFileLoaderBase {
  956. public static CreateRuntime(parsedData: any, scene: Scene, rootUrl: string): IGLTFRuntime {
  957. var gltfRuntime: IGLTFRuntime = {
  958. accessors: {},
  959. buffers: {},
  960. bufferViews: {},
  961. meshes: {},
  962. lights: {},
  963. cameras: {},
  964. nodes: {},
  965. images: {},
  966. textures: {},
  967. shaders: {},
  968. programs: {},
  969. samplers: {},
  970. techniques: {},
  971. materials: {},
  972. animations: {},
  973. skins: {},
  974. currentScene: {},
  975. extensionsUsed: [],
  976. buffersCount: 0,
  977. shaderscount: 0,
  978. scene: scene,
  979. rootUrl: rootUrl,
  980. loadedBufferCount: 0,
  981. loadedBufferViews: {},
  982. loadedShaderCount: 0,
  983. importOnlyMeshes: false,
  984. dummyNodes: []
  985. }
  986. // Parse
  987. if (parsedData.extensionsUsed) {
  988. parseObject(parsedData.extensionsUsed, "extensionsUsed", gltfRuntime);
  989. }
  990. if (parsedData.buffers) {
  991. parseBuffers(parsedData.buffers, gltfRuntime);
  992. }
  993. if (parsedData.bufferViews) {
  994. parseObject(parsedData.bufferViews, "bufferViews", gltfRuntime);
  995. }
  996. if (parsedData.accessors) {
  997. parseObject(parsedData.accessors, "accessors", gltfRuntime);
  998. }
  999. if (parsedData.meshes) {
  1000. parseObject(parsedData.meshes, "meshes", gltfRuntime);
  1001. }
  1002. if (parsedData.lights) {
  1003. parseObject(parsedData.lights, "lights", gltfRuntime);
  1004. }
  1005. if (parsedData.cameras) {
  1006. parseObject(parsedData.cameras, "cameras", gltfRuntime);
  1007. }
  1008. if (parsedData.nodes) {
  1009. parseObject(parsedData.nodes, "nodes", gltfRuntime);
  1010. }
  1011. if (parsedData.images) {
  1012. parseObject(parsedData.images, "images", gltfRuntime);
  1013. }
  1014. if (parsedData.textures) {
  1015. parseObject(parsedData.textures, "textures", gltfRuntime);
  1016. }
  1017. if (parsedData.shaders) {
  1018. parseShaders(parsedData.shaders, gltfRuntime);
  1019. }
  1020. if (parsedData.programs) {
  1021. parseObject(parsedData.programs, "programs", gltfRuntime);
  1022. }
  1023. if (parsedData.samplers) {
  1024. parseObject(parsedData.samplers, "samplers", gltfRuntime);
  1025. }
  1026. if (parsedData.techniques) {
  1027. parseObject(parsedData.techniques, "techniques", gltfRuntime);
  1028. }
  1029. if (parsedData.materials) {
  1030. parseObject(parsedData.materials, "materials", gltfRuntime);
  1031. }
  1032. if (parsedData.animations) {
  1033. parseObject(parsedData.animations, "animations", gltfRuntime);
  1034. }
  1035. if (parsedData.skins) {
  1036. parseObject(parsedData.skins, "skins", gltfRuntime);
  1037. }
  1038. if (parsedData.scene && parsedData.scenes) {
  1039. gltfRuntime.currentScene = parsedData.scenes[parsedData.scene];
  1040. }
  1041. return gltfRuntime;
  1042. }
  1043. public static LoadBufferAsync(gltfRuntime: IGLTFRuntime, id: string, onSuccess: (buffer: ArrayBufferView) => void, onError: () => void): void {
  1044. var buffer: IGLTFBuffer = gltfRuntime.buffers[id];
  1045. if (GLTFUtils.IsBase64(buffer.uri)) {
  1046. onSuccess(new Uint8Array(GLTFUtils.DecodeBase64(buffer.uri)));
  1047. }
  1048. else {
  1049. Tools.LoadFile(gltfRuntime.rootUrl + buffer.uri, data => onSuccess(new Uint8Array(data)), null, null, true, onError);
  1050. }
  1051. }
  1052. public static LoadTextureBufferAsync(gltfRuntime: IGLTFRuntime, id: string, onSuccess: (buffer: ArrayBufferView) => void, onError: () => void): void {
  1053. var texture: IGLTFTexture = gltfRuntime.textures[id];
  1054. if (!texture || !texture.source) {
  1055. onError();
  1056. return;
  1057. }
  1058. if (texture.babylonTexture) {
  1059. onSuccess(null);
  1060. return;
  1061. }
  1062. var source: IGLTFImage = gltfRuntime.images[texture.source];
  1063. if (GLTFUtils.IsBase64(source.uri)) {
  1064. onSuccess(new Uint8Array(GLTFUtils.DecodeBase64(source.uri)));
  1065. }
  1066. else {
  1067. Tools.LoadFile(gltfRuntime.rootUrl + source.uri, data => onSuccess(new Uint8Array(data)), null, null, true, onError);
  1068. }
  1069. }
  1070. public static CreateTextureAsync(gltfRuntime: IGLTFRuntime, id: string, buffer: ArrayBufferView, onSuccess: (texture: Texture) => void, onError: () => void): void {
  1071. var texture: IGLTFTexture = gltfRuntime.textures[id];
  1072. if (texture.babylonTexture) {
  1073. onSuccess(texture.babylonTexture);
  1074. return;
  1075. }
  1076. var sampler: IGLTFSampler = gltfRuntime.samplers[texture.sampler];
  1077. var createMipMaps =
  1078. (sampler.minFilter === ETextureFilterType.NEAREST_MIPMAP_NEAREST) ||
  1079. (sampler.minFilter === ETextureFilterType.NEAREST_MIPMAP_LINEAR) ||
  1080. (sampler.minFilter === ETextureFilterType.LINEAR_MIPMAP_NEAREST) ||
  1081. (sampler.minFilter === ETextureFilterType.LINEAR_MIPMAP_LINEAR);
  1082. var samplingMode = Texture.BILINEAR_SAMPLINGMODE;
  1083. var blob = new Blob([buffer]);
  1084. var blobURL = URL.createObjectURL(blob);
  1085. var revokeBlobURL = () => URL.revokeObjectURL(blobURL);
  1086. var newTexture = new Texture(blobURL, gltfRuntime.scene, !createMipMaps, true, samplingMode, revokeBlobURL, revokeBlobURL);
  1087. newTexture.wrapU = GLTFUtils.GetWrapMode(sampler.wrapS);
  1088. newTexture.wrapV = GLTFUtils.GetWrapMode(sampler.wrapT);
  1089. newTexture.name = id;
  1090. texture.babylonTexture = newTexture;
  1091. onSuccess(newTexture);
  1092. }
  1093. public static LoadShaderStringAsync(gltfRuntime: IGLTFRuntime, id: string, onSuccess: (shaderString: string) => void, onError: () => void): void {
  1094. var shader: IGLTFShader = gltfRuntime.shaders[id];
  1095. if (GLTFUtils.IsBase64(shader.uri)) {
  1096. var shaderString = atob(shader.uri.split(",")[1]);
  1097. onSuccess(shaderString);
  1098. }
  1099. else {
  1100. Tools.LoadFile(gltfRuntime.rootUrl + shader.uri, onSuccess, null, null, false, onError);
  1101. }
  1102. }
  1103. public static LoadMaterialAsync(gltfRuntime: IGLTFRuntime, id: string, onSuccess: (material: Material) => void, onError: () => void): void {
  1104. var material: IGLTFMaterial = gltfRuntime.materials[id];
  1105. var technique: IGLTFTechnique = gltfRuntime.techniques[material.technique];
  1106. var program: IGLTFProgram = gltfRuntime.programs[technique.program];
  1107. var states: IGLTFTechniqueStates = technique.states;
  1108. var vertexShader: string = Effect.ShadersStore[program.vertexShader + "VertexShader"];
  1109. var pixelShader: string = Effect.ShadersStore[program.fragmentShader + "PixelShader"];
  1110. var newVertexShader = "";
  1111. var newPixelShader = "";
  1112. var vertexTokenizer = new Tokenizer(vertexShader);
  1113. var pixelTokenizer = new Tokenizer(pixelShader);
  1114. var unTreatedUniforms: Object = {};
  1115. var uniforms = [];
  1116. var attributes = [];
  1117. var samplers = [];
  1118. // Fill uniform, sampler2D and attributes
  1119. for (var unif in technique.uniforms) {
  1120. var uniform = technique.uniforms[unif];
  1121. var uniformParameter: IGLTFTechniqueParameter = technique.parameters[uniform];
  1122. unTreatedUniforms[unif] = uniformParameter;
  1123. if (uniformParameter.semantic && !uniformParameter.node && !uniformParameter.source) {
  1124. var transformIndex = glTFTransforms.indexOf(uniformParameter.semantic);
  1125. if (transformIndex !== -1) {
  1126. uniforms.push(babylonTransforms[transformIndex]);
  1127. delete unTreatedUniforms[unif];
  1128. }
  1129. else {
  1130. uniforms.push(unif);
  1131. }
  1132. }
  1133. else if (uniformParameter.type === EParameterType.SAMPLER_2D) {
  1134. samplers.push(unif);
  1135. }
  1136. else {
  1137. uniforms.push(unif);
  1138. }
  1139. }
  1140. for (var attr in technique.attributes) {
  1141. var attribute = technique.attributes[attr];
  1142. var attributeParameter: IGLTFTechniqueParameter = technique.parameters[attribute];
  1143. if (attributeParameter.semantic) {
  1144. attributes.push(getAttribute(attributeParameter));
  1145. }
  1146. }
  1147. // Configure vertex shader
  1148. while (!vertexTokenizer.isEnd() && vertexTokenizer.getNextToken()) {
  1149. var tokenType = vertexTokenizer.currentToken;
  1150. if (tokenType !== ETokenType.IDENTIFIER) {
  1151. newVertexShader += vertexTokenizer.currentString;
  1152. continue;
  1153. }
  1154. var foundAttribute = false;
  1155. for (var attr in technique.attributes) {
  1156. var attribute = technique.attributes[attr];
  1157. var attributeParameter: IGLTFTechniqueParameter = technique.parameters[attribute];
  1158. if (vertexTokenizer.currentIdentifier === attr && attributeParameter.semantic) {
  1159. newVertexShader += getAttribute(attributeParameter);
  1160. foundAttribute = true;
  1161. break;
  1162. }
  1163. }
  1164. if (foundAttribute) {
  1165. continue;
  1166. }
  1167. newVertexShader += parseShaderUniforms(vertexTokenizer, technique, unTreatedUniforms);
  1168. }
  1169. // Configure pixel shader
  1170. while (!pixelTokenizer.isEnd() && pixelTokenizer.getNextToken()) {
  1171. var tokenType = pixelTokenizer.currentToken;
  1172. if (tokenType !== ETokenType.IDENTIFIER) {
  1173. newPixelShader += pixelTokenizer.currentString;
  1174. continue;
  1175. }
  1176. newPixelShader += parseShaderUniforms(pixelTokenizer, technique, unTreatedUniforms);
  1177. }
  1178. // Create shader material
  1179. var shaderPath = {
  1180. vertex: program.vertexShader + id,
  1181. fragment: program.fragmentShader + id
  1182. };
  1183. var options = {
  1184. attributes: attributes,
  1185. uniforms: uniforms,
  1186. samplers: samplers,
  1187. needAlphaBlending: states.functions && states.functions.blendEquationSeparate
  1188. };
  1189. Effect.ShadersStore[program.vertexShader + id + "VertexShader"] = newVertexShader;
  1190. Effect.ShadersStore[program.fragmentShader + id + "PixelShader"] = newPixelShader;
  1191. var shaderMaterial = new ShaderMaterial(id, gltfRuntime.scene, shaderPath, options);
  1192. shaderMaterial.onError = onShaderCompileError(program, shaderMaterial, onError);
  1193. shaderMaterial.onCompiled = onShaderCompileSuccess(gltfRuntime, shaderMaterial, technique, material, unTreatedUniforms, onSuccess);
  1194. shaderMaterial.sideOrientation = Material.CounterClockWiseSideOrientation;
  1195. if (states.functions) {
  1196. var functions = states.functions;
  1197. if (functions.cullFace && functions.cullFace[0] !== ECullingType.BACK) {
  1198. shaderMaterial.backFaceCulling = false;
  1199. }
  1200. var blendFunc = functions.blendFuncSeparate;
  1201. if (blendFunc) {
  1202. if (blendFunc[0] === EBlendingFunction.SRC_ALPHA && blendFunc[1] === EBlendingFunction.ONE_MINUS_SRC_ALPHA && blendFunc[2] === EBlendingFunction.ONE && blendFunc[3] === EBlendingFunction.ONE) {
  1203. shaderMaterial.alphaMode = Engine.ALPHA_COMBINE;
  1204. }
  1205. else if (blendFunc[0] === EBlendingFunction.ONE && blendFunc[1] === EBlendingFunction.ONE && blendFunc[2] === EBlendingFunction.ZERO && blendFunc[3] === EBlendingFunction.ONE) {
  1206. shaderMaterial.alphaMode = Engine.ALPHA_ONEONE;
  1207. }
  1208. else if (blendFunc[0] === EBlendingFunction.SRC_ALPHA && blendFunc[1] === EBlendingFunction.ONE && blendFunc[2] === EBlendingFunction.ZERO && blendFunc[3] === EBlendingFunction.ONE) {
  1209. shaderMaterial.alphaMode = Engine.ALPHA_ADD;
  1210. }
  1211. else if (blendFunc[0] === EBlendingFunction.ZERO && blendFunc[1] === EBlendingFunction.ONE_MINUS_SRC_COLOR && blendFunc[2] === EBlendingFunction.ONE && blendFunc[3] === EBlendingFunction.ONE) {
  1212. shaderMaterial.alphaMode = Engine.ALPHA_SUBTRACT;
  1213. }
  1214. else if (blendFunc[0] === EBlendingFunction.DST_COLOR && blendFunc[1] === EBlendingFunction.ZERO && blendFunc[2] === EBlendingFunction.ONE && blendFunc[3] === EBlendingFunction.ONE) {
  1215. shaderMaterial.alphaMode = Engine.ALPHA_MULTIPLY;
  1216. }
  1217. else if (blendFunc[0] === EBlendingFunction.SRC_ALPHA && blendFunc[1] === EBlendingFunction.ONE_MINUS_SRC_COLOR && blendFunc[2] === EBlendingFunction.ONE && blendFunc[3] === EBlendingFunction.ONE) {
  1218. shaderMaterial.alphaMode = Engine.ALPHA_MAXIMIZED;
  1219. }
  1220. }
  1221. }
  1222. }
  1223. }
  1224. /**
  1225. * glTF File Loader Plugin
  1226. */
  1227. export class GLTFFileLoader implements ISceneLoaderPluginAsync {
  1228. /**
  1229. * Public members
  1230. */
  1231. public extensions: ISceneLoaderPluginExtensions = {
  1232. ".gltf": { isBinary: false },
  1233. ".glb": { isBinary: true }
  1234. };
  1235. /**
  1236. * Private members
  1237. */
  1238. // None
  1239. /**
  1240. * Static members
  1241. */
  1242. public static MakeYUP: boolean = false;
  1243. public static HomogeneousCoordinates: boolean = false;
  1244. public static Extensions: { [name: string]: GLTFFileLoaderExtension } = {};
  1245. public static RegisterExtension(extension: GLTFFileLoaderExtension): void {
  1246. if (GLTFFileLoader.Extensions[extension.name]) {
  1247. Tools.Error("Tool with the same name \"" + extension.name + "\" already exists");
  1248. return;
  1249. }
  1250. GLTFFileLoader.Extensions[extension.name] = extension;
  1251. }
  1252. /**
  1253. * Import meshes
  1254. */
  1255. public importMeshAsync(meshesNames: any, scene: Scene, data: any, rootUrl: string, onSuccess?: (meshes: AbstractMesh[], particleSystems: ParticleSystem[], skeletons: Skeleton[]) => void, onError?: () => void): boolean {
  1256. scene.useRightHandedSystem = true;
  1257. var gltfRuntime = GLTFFileLoaderExtension.LoadRuntimeAsync(scene, data, rootUrl, gltfRuntime => {
  1258. gltfRuntime.importOnlyMeshes = true;
  1259. if (meshesNames === "") {
  1260. gltfRuntime.importMeshesNames = [];
  1261. }
  1262. else if (typeof meshesNames === "string") {
  1263. gltfRuntime.importMeshesNames = [meshesNames];
  1264. }
  1265. else if (meshesNames && !(meshesNames instanceof Array)) {
  1266. gltfRuntime.importMeshesNames = [meshesNames];
  1267. }
  1268. else {
  1269. gltfRuntime.importMeshesNames = [];
  1270. Tools.Warn("Argument meshesNames must be of type string or string[]");
  1271. }
  1272. // Create nodes
  1273. this._createNodes(gltfRuntime);
  1274. var meshes = [];
  1275. var skeletons = [];
  1276. // Fill arrays of meshes and skeletons
  1277. for (var nde in gltfRuntime.nodes) {
  1278. var node: IGLTFNode = gltfRuntime.nodes[nde];
  1279. if (node.babylonNode instanceof AbstractMesh) {
  1280. meshes.push(<AbstractMesh>node.babylonNode);
  1281. }
  1282. }
  1283. for (var skl in gltfRuntime.skins) {
  1284. var skin: IGLTFSkins = gltfRuntime.skins[skl];
  1285. if (skin.babylonSkeleton instanceof Skeleton) {
  1286. skeletons.push(skin.babylonSkeleton);
  1287. }
  1288. }
  1289. // Load buffers, shaders, materials, etc.
  1290. this._loadBuffersAsync(gltfRuntime, () => {
  1291. this._loadShadersAsync(gltfRuntime, () => {
  1292. importMaterials(gltfRuntime);
  1293. onBuffersLoaded(gltfRuntime);
  1294. });
  1295. });
  1296. if (onSuccess) {
  1297. onSuccess(meshes, null, skeletons);
  1298. }
  1299. }, onError);
  1300. return true;
  1301. }
  1302. /**
  1303. * Load scene
  1304. */
  1305. public loadAsync(scene: Scene, data: string | ArrayBuffer, rootUrl: string, onSuccess: () => void, onError: () => void): boolean {
  1306. scene.useRightHandedSystem = true;
  1307. GLTFFileLoaderExtension.LoadRuntimeAsync(scene, data, rootUrl, gltfRuntime => {
  1308. // Create nodes
  1309. this._createNodes(gltfRuntime);
  1310. // Load buffers, shaders, materials, etc.
  1311. this._loadBuffersAsync(gltfRuntime, () => {
  1312. this._loadShadersAsync(gltfRuntime, () => {
  1313. importMaterials(gltfRuntime);
  1314. onBuffersLoaded(gltfRuntime);
  1315. });
  1316. });
  1317. onSuccess();
  1318. }, onError);
  1319. return true;
  1320. }
  1321. private _loadShadersAsync(gltfRuntime: IGLTFRuntime, onload: () => void): void {
  1322. var hasShaders = false;
  1323. var processShader = (sha: string, shader: IGLTFShader) => {
  1324. GLTFFileLoaderExtension.LoadShaderStringAsync(gltfRuntime, sha, shaderString => {
  1325. gltfRuntime.loadedShaderCount++;
  1326. if (shaderString) {
  1327. Effect.ShadersStore[sha + (shader.type === EShaderType.VERTEX ? "VertexShader" : "PixelShader")] = shaderString;
  1328. }
  1329. if (gltfRuntime.loadedShaderCount === gltfRuntime.shaderscount) {
  1330. onload();
  1331. }
  1332. }, () => {
  1333. Tools.Error("Error when loading shader program named " + sha + " located at " + shader.uri);
  1334. });
  1335. };
  1336. for (var sha in gltfRuntime.shaders) {
  1337. hasShaders = true;
  1338. var shader: IGLTFShader = gltfRuntime.shaders[sha];
  1339. if (shader) {
  1340. processShader.bind(this, sha, shader)();
  1341. }
  1342. else {
  1343. Tools.Error("No shader named: " + sha);
  1344. }
  1345. }
  1346. if (!hasShaders) {
  1347. onload();
  1348. }
  1349. };
  1350. private _loadBuffersAsync(gltfRuntime: IGLTFRuntime, onload: () => void): void {
  1351. var hasBuffers = false;
  1352. var processBuffer = (buf: string, buffer: IGLTFBuffer) => {
  1353. GLTFFileLoaderExtension.LoadBufferAsync(gltfRuntime, buf, bufferView => {
  1354. gltfRuntime.loadedBufferCount++;
  1355. if (bufferView) {
  1356. if (bufferView.byteLength != gltfRuntime.buffers[buf].byteLength) {
  1357. Tools.Error("Buffer named " + buf + " is length " + bufferView.byteLength + ". Expected: " + buffer.byteLength); // Improve error message
  1358. }
  1359. gltfRuntime.loadedBufferViews[buf] = bufferView;
  1360. }
  1361. if (gltfRuntime.loadedBufferCount === gltfRuntime.buffersCount) {
  1362. onload();
  1363. }
  1364. }, () => {
  1365. Tools.Error("Error when loading buffer named " + buf + " located at " + buffer.uri);
  1366. });
  1367. };
  1368. for (var buf in gltfRuntime.buffers) {
  1369. hasBuffers = true;
  1370. var buffer: IGLTFBuffer = gltfRuntime.buffers[buf];
  1371. if (buffer) {
  1372. processBuffer.bind(this, buf, buffer)();
  1373. }
  1374. else {
  1375. Tools.Error("No buffer named: " + buf);
  1376. }
  1377. }
  1378. if (!hasBuffers) {
  1379. onload();
  1380. }
  1381. }
  1382. // Creates nodes before loading buffers and shaders
  1383. private _createNodes(gltfRuntime: IGLTFRuntime): void {
  1384. var currentScene = <IGLTFScene>gltfRuntime.currentScene;
  1385. for (var i = 0; i < currentScene.nodes.length; i++) {
  1386. traverseNodes(gltfRuntime, currentScene.nodes[i], null);
  1387. }
  1388. }
  1389. };
  1390. BABYLON.SceneLoader.RegisterPlugin(new GLTFFileLoader());
  1391. }