Sfoglia il codice sorgente

Merge pull request #13220 from webprofusion-chrisc/patch-1

GLTFExporter: implement 4-byte data alignment for buffer
Mr.doob 7 anni fa
parent
commit
b4c6726f46
1 ha cambiato i file con 18 aggiunte e 0 eliminazioni
  1. 18 0
      examples/js/exporters/GLTFExporter.js

+ 18 - 0
examples/js/exporters/GLTFExporter.js

@@ -180,6 +180,20 @@ THREE.GLTFExporter.prototype = {
 
 		}
 
+		/**
+		 * Get the required size + padding for a buffer, rounded to the next 4-byte boundary.
+		 * https://github.com/KhronosGroup/glTF/tree/master/specification/2.0#data-alignment
+		 *
+		 * @param {Integer} bufferSize The size the original buffer.
+		 * @returns {Integer} new buffer size with required padding.
+		 *
+		 */
+		function getPaddedBufferSize( bufferSize ) {
+
+			return Math.ceil( bufferSize / 4 ) * 4;
+
+		}
+		
 		/**
 		 * Process a buffer to append to the default one.
 		 * @param  {THREE.BufferAttribute} attribute     Attribute to store
@@ -208,6 +222,10 @@ THREE.GLTFExporter.prototype = {
 
 			// Create a new dataview and dump the attribute's array into it
 			var byteLength = count * attribute.itemSize * componentSize;
+			
+			// adjust required size of array buffer with padding 
+			// to satisfy gltf requirement that the length is divisible by 4
+			byteLength = getPaddedBufferSize( byteLength );
 
 			var dataView = new DataView( new ArrayBuffer( byteLength ) );