KHR_texture_transform.ts 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. import { ImageMimeType } from "babylonjs-gltf2interface";
  2. import { Tools } from "babylonjs/Misc/tools";
  3. import { Texture } from "babylonjs/Materials/Textures/texture";
  4. import { ProceduralTexture } from "babylonjs/Materials/Textures/Procedurals/proceduralTexture";
  5. import { Scene } from "babylonjs/scene";
  6. import { IGLTFExporterExtensionV2 } from "../glTFExporterExtension";
  7. import { _Exporter } from "../glTFExporter";
  8. const NAME = "KHR_texture_transform";
  9. import "../shaders/textureTransform.fragment";
  10. /**
  11. * Interface for handling KHR texture transform
  12. * @hidden
  13. */
  14. interface IKHRTextureTransform {
  15. offset?: number[];
  16. rotation?: number;
  17. scale?: number[];
  18. texCoord?: number;
  19. }
  20. /**
  21. * @hidden
  22. */
  23. export class KHR_texture_transform implements IGLTFExporterExtensionV2 {
  24. private _recordedTextures: ProceduralTexture[] = [];
  25. /** Name of this extension */
  26. public readonly name = NAME;
  27. /** Defines whether this extension is enabled */
  28. public enabled = true;
  29. /** Defines whether this extension is required */
  30. public required = false;
  31. /** Reference to the glTF exporter */
  32. private _exporter: _Exporter;
  33. constructor(exporter: _Exporter) {
  34. this._exporter = exporter;
  35. }
  36. public dispose() {
  37. for (var texture of this._recordedTextures) {
  38. texture.dispose();
  39. }
  40. delete this._exporter;
  41. }
  42. public preExportTextureAsync(context: string, babylonTexture: Texture, mimeType: ImageMimeType): Promise<Texture> {
  43. return new Promise((resolve, reject) => {
  44. const scene = babylonTexture.getScene();
  45. if (!scene) {
  46. reject(`${context}: "scene" is not defined for Babylon texture ${babylonTexture.name}!`);
  47. return;
  48. }
  49. // TODO: this doesn't take into account rotation center values
  50. const texture_transform_extension: IKHRTextureTransform = {};
  51. if (babylonTexture.uOffset !== 0 || babylonTexture.vOffset !== 0) {
  52. texture_transform_extension.offset = [babylonTexture.uOffset, babylonTexture.vOffset];
  53. }
  54. if (babylonTexture.uScale !== 1 || babylonTexture.vScale !== 1) {
  55. texture_transform_extension.scale = [babylonTexture.uScale, babylonTexture.vScale];
  56. }
  57. if (babylonTexture.wAng !== 0) {
  58. texture_transform_extension.rotation = babylonTexture.wAng;
  59. }
  60. if (babylonTexture.coordinatesIndex !== 0) {
  61. texture_transform_extension.texCoord = babylonTexture.coordinatesIndex;
  62. }
  63. if (!Object.keys(texture_transform_extension).length) {
  64. resolve(babylonTexture);
  65. return;
  66. }
  67. return this._textureTransformTextureAsync(babylonTexture, scene)
  68. .then((proceduralTexture) => {
  69. resolve(proceduralTexture);
  70. })
  71. .catch((e) => {
  72. reject(e);
  73. });
  74. });
  75. }
  76. /**
  77. * Transform the babylon texture by the offset, rotation and scale parameters using a procedural texture
  78. * @param babylonTexture
  79. * @param offset
  80. * @param rotation
  81. * @param scale
  82. * @param scene
  83. */
  84. private _textureTransformTextureAsync(babylonTexture: Texture, scene: Scene): Promise<Texture> {
  85. return new Promise((resolve) => {
  86. const proceduralTexture = new ProceduralTexture(`${babylonTexture.name}`, babylonTexture.getSize(), "textureTransform", scene);
  87. if (!proceduralTexture) {
  88. Tools.Log(`Cannot create procedural texture for ${babylonTexture.name}!`);
  89. resolve(babylonTexture);
  90. }
  91. proceduralTexture.reservedDataStore = {
  92. hidden: true,
  93. source: babylonTexture
  94. };
  95. this._recordedTextures.push(proceduralTexture);
  96. proceduralTexture.coordinatesIndex = babylonTexture.coordinatesIndex;
  97. proceduralTexture.setTexture("textureSampler", babylonTexture);
  98. proceduralTexture.setMatrix("textureTransformMat", babylonTexture.getTextureMatrix());
  99. // isReady trigger creation of effect if it doesnt exist yet
  100. if (proceduralTexture.isReady()) {
  101. proceduralTexture.render();
  102. resolve(proceduralTexture);
  103. } else {
  104. proceduralTexture.getEffect().executeWhenCompiled(() => {
  105. proceduralTexture.render();
  106. resolve(proceduralTexture);
  107. });
  108. }
  109. });
  110. }
  111. }
  112. _Exporter.RegisterExtension(NAME, (exporter) => new KHR_texture_transform(exporter));