DRACOLoader.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. // Copyright 2016 The Draco Authors.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. //
  15. THREE.DRACOLoader = function(manager) {
  16. this.manager = (manager !== undefined) ? manager :
  17. THREE.DefaultLoadingManager;
  18. this.materials = null;
  19. };
  20. THREE.DRACOLoader.prototype = {
  21. constructor: THREE.DRACOLoader,
  22. load: function(url, onLoad, onProgress, onError) {
  23. const scope = this;
  24. const loader = new THREE.FileLoader(scope.manager);
  25. loader.setPath(this.path);
  26. loader.setResponseType('arraybuffer');
  27. loader.load(url, function(blob) {
  28. onLoad(scope.decodeDracoFile(blob));
  29. }, onProgress, onError);
  30. },
  31. setPath: function(value) {
  32. this.path = value;
  33. },
  34. decodeDracoFile: function(rawBuffer) {
  35. const scope = this;
  36. /*
  37. * Here is how to use Draco Javascript decoder and get the geometry.
  38. */
  39. const buffer = new DracoModule.DecoderBuffer();
  40. buffer.Init(new Int8Array(rawBuffer), rawBuffer.byteLength);
  41. const wrapper = new DracoModule.WebIDLWrapper();
  42. /*
  43. * Determine what type is this file, mesh or point cloud.
  44. */
  45. const geometryType = wrapper.GetEncodedGeometryType(buffer);
  46. if (geometryType == DracoModule.TRIANGULAR_MESH) {
  47. fileDisplayArea.innerText = "Loaded a mesh.\n";
  48. } else if (geometryType == DracoModule.POINT_CLOUD) {
  49. fileDisplayArea.innerText = "Loaded a point cloud.\n";
  50. } else {
  51. const errorMsg = "Error: Unknown geometry type.";
  52. fileDisplayArea.innerText = errorMsg;
  53. throw new Error(errorMsg);
  54. }
  55. return scope.convertDracoGeometryTo3JS(wrapper, geometryType, buffer);
  56. },
  57. convertDracoGeometryTo3JS: function(wrapper, geometryType, buffer) {
  58. let dracoGeometry;
  59. if (geometryType == DracoModule.TRIANGULAR_MESH) {
  60. dracoGeometry = wrapper.DecodeMeshFromBuffer(buffer);
  61. } else {
  62. dracoGeometry = wrapper.DecodePointCloudFromBuffer(buffer);
  63. }
  64. DracoModule.destroy(buffer);
  65. /*
  66. * Example on how to retrieve mesh and attributes.
  67. */
  68. let numFaces, numPoints, numVertexCoordinates, numAttributes;
  69. // For output basic geometry information.
  70. let geometryInfoStr;
  71. if (geometryType == DracoModule.TRIANGULAR_MESH) {
  72. numFaces = dracoGeometry.num_faces();
  73. geometryInfoStr += "Number of faces loaded: " + numFaces.toString()
  74. + ".\n";
  75. } else {
  76. numFaces = 0;
  77. }
  78. numPoints = dracoGeometry.num_points();
  79. numVertexCoordinates = numPoints * 3;
  80. numAttributes = dracoGeometry.num_attributes();
  81. geometryInfoStr = "Number of points loaded: " + numPoints.toString()
  82. + ".\n";
  83. geometryInfoStr += "Number of attributes loaded: " +
  84. numAttributes.toString() + ".\n";
  85. // Get position attribute. Must exists.
  86. const posAttId = wrapper.GetAttributeId(dracoGeometry,
  87. Module.POSITION);
  88. if (posAttId == -1) {
  89. const errorMsg = "No position attribute found in the mesh.";
  90. fileDisplayArea.innerText = errorMsg;
  91. DracoModule.destroy(wrapper);
  92. DracoModule.destroy(dracoGeometry);
  93. throw new Error(errorMsg);
  94. }
  95. const posAttribute = wrapper.GetAttribute(dracoGeometry, posAttId);
  96. const posAttributeData = new DracoModule.DracoFloat32Array();
  97. wrapper.GetAttributeFloatForAllPoints(
  98. dracoGeometry, posAttribute, posAttributeData);
  99. // Get color attributes if exists.
  100. const colorAttId = wrapper.GetAttributeId(dracoGeometry, Module.COLOR);
  101. let colAttributeData;
  102. if (colorAttId != -1) {
  103. geometryInfoStr += "\nLoaded color attribute.\n";
  104. const colAttribute = wrapper.GetAttribute(dracoGeometry, colorAttId);
  105. colAttributeData = new DracoModule.DracoFloat32Array();
  106. wrapper.GetAttributeFloatForAllPoints(dracoGeometry, colAttribute,
  107. colAttributeData);
  108. }
  109. // Get normal attributes if exists.
  110. const normalAttId =
  111. wrapper.GetAttributeId(dracoGeometry, Module.NORMAL);
  112. let norAttributeData;
  113. if (normalAttId != -1) {
  114. geometryInfoStr += "\nLoaded normal attribute.\n";
  115. const norAttribute = wrapper.GetAttribute(dracoGeometry, normalAttId);
  116. norAttributeData = new DracoModule.DracoFloat32Array();
  117. wrapper.GetAttributeFloatForAllPoints(dracoGeometry, norAttribute,
  118. norAttributeData);
  119. }
  120. // Structure for converting to THREEJS geometry later.
  121. const geometryBuffer = {
  122. indices: [],
  123. vertices: [],
  124. normals: [],
  125. uvs: [],
  126. colors: []
  127. };
  128. for (let i = 0; i < numVertexCoordinates; i += 3) {
  129. geometryBuffer.vertices.push(
  130. posAttributeData.GetValue(i),
  131. posAttributeData.GetValue(i + 1),
  132. posAttributeData.GetValue(i + 2));
  133. // Add color.
  134. if (colorAttId != -1) {
  135. geometryBuffer.colors.push(
  136. colAttributeData.GetValue(i),
  137. colAttributeData.GetValue(i + 1),
  138. colAttributeData.GetValue(i + 2));
  139. } else {
  140. // Default is white.
  141. geometryBuffer.colors.push(1.0, 1.0, 1.0);
  142. }
  143. // Add normal.
  144. if (normalAttId != -1) {
  145. geometryBuffer.normals.push(
  146. norAttributeData.GetValue(i),
  147. norAttributeData.GetValue(i + 1),
  148. norAttributeData.GetValue(i + 2));
  149. }
  150. }
  151. DracoModule.destroy(posAttributeData);
  152. if (colorAttId != -1)
  153. DracoModule.destroy(colAttributeData);
  154. if (normalAttId != -1)
  155. DracoModule.destroy(norAttributeData);
  156. // For mesh, we need to generate the faces.
  157. if (geometryType == DracoModule.TRIANGULAR_MESH) {
  158. const numIndices = numFaces * 3;
  159. const ia = new DracoInt32Array();
  160. for (let i = 0; i < numFaces; ++i) {
  161. wrapper.GetFaceFromMesh(dracoGeometry, i, ia);
  162. geometryBuffer.indices.push(
  163. ia.GetValue(0), ia.GetValue(1), ia.GetValue(2));
  164. }
  165. DracoModule.destroy(ia);
  166. }
  167. DracoModule.destroy(wrapper);
  168. DracoModule.destroy(dracoGeometry);
  169. fileDisplayArea.innerText += geometryInfoStr;
  170. // Import data to Three JS geometry.
  171. const geometry = new THREE.BufferGeometry();
  172. if (geometryType == DracoModule.TRIANGULAR_MESH) {
  173. geometry.setIndex(new(geometryBuffer.indices.length > 65535 ?
  174. THREE.Uint32BufferAttribute : THREE.Uint16BufferAttribute)
  175. (geometryBuffer.indices, 1));
  176. }
  177. geometry.addAttribute('position',
  178. new THREE.Float32BufferAttribute(geometryBuffer.vertices, 3));
  179. geometry.addAttribute('color',
  180. new THREE.Float32BufferAttribute(geometryBuffer.colors, 3));
  181. if (normalAttId != -1) {
  182. geometry.addAttribute('normal',
  183. new THREE.Float32BufferAttribute(geometryBuffer.normals, 3));
  184. }
  185. return geometry;
  186. }
  187. };