瀏覽代碼

MultiMaterials support with more robust way

Takahiro 6 年之前
父節點
當前提交
bd8bfd1b20
共有 1 個文件被更改,包括 59 次插入9 次删除
  1. 59 9
      examples/js/exporters/GLTFExporter.js

+ 59 - 9
examples/js/exporters/GLTFExporter.js

@@ -429,6 +429,56 @@ THREE.GLTFExporter.prototype = {
 
 		}
 
+		/**
+		 * @param  {BufferAttribute} attribute
+		 * @param  {Integer|undefined} start
+		 * @param  {Integer|undefined} count
+		 * @return {Boolean}
+		 */
+		function hasAttributeCache( attribute, start, count ) {
+
+			return cachedData.attributes.has( attribute ) &&
+				cachedData.attributes.get( attribute ).has( start ) &&
+				cachedData.attributes.get( attribute ).get( start ).has( count );
+
+		}
+
+		/**
+		 * @param  {BufferAttribute} attribute
+		 * @param  {Integer|undefined} start
+		 * @param  {Integer|undefined} count
+		 * @return {Integer}
+		 */
+		function getAttributeCache( attribute, start, count ) {
+
+			return cachedData.attributes.get( attribute ).get( start ).get( count );
+
+		}
+
+		/**
+		 * @param  {Integer} data
+		 * @param  {BufferAttribute} attribute
+		 * @param  {Integer|undefined} start
+		 * @param  {Integer|undefined} count
+		 */
+		function setAttributeCache( data, attribute, start, count ) {
+
+			if ( ! cachedData.attributes.has( attribute ) ) {
+
+				cachedData.attributes.set( attribute, new Map() );
+
+			}
+
+			if ( ! cachedData.attributes.get( attribute ).has( start ) ) {
+
+				cachedData.attributes.get( attribute ).set( start, new Map() );
+
+			}
+
+			cachedData.attributes.get( attribute ).get( start ).set( count, data );
+
+		}
+
 		/**
 		 * Process a buffer to append to the default one.
 		 * @param  {ArrayBuffer} buffer
@@ -1178,9 +1228,9 @@ THREE.GLTFExporter.prototype = {
 
 				}
 
-				if ( cachedData.attributes.has( attribute ) ) {
+				if ( hasAttributeCache( attribute ) ) {
 
-					attributes[ attributeName ] = cachedData.attributes.get( attribute );
+					attributes[ attributeName ] = getAttributeCache( attribute );
 					continue;
 
 				}
@@ -1201,7 +1251,7 @@ THREE.GLTFExporter.prototype = {
 				if ( accessor !== null ) {
 
 					attributes[ attributeName ] = accessor;
-					cachedData.attributes.set( attribute, accessor );
+					setAttributeCache( attribute, accessor );
 
 				}
 
@@ -1267,9 +1317,9 @@ THREE.GLTFExporter.prototype = {
 
 						var baseAttribute = geometry.attributes[ attributeName ];
 
-						if ( cachedData.attributes.has( attribute ) ) {
+						if ( hasAttributeCache( attribute ) ) {
 
-							target[ gltfAttributeName ] = cachedData.attributes.get( attribute );
+							target[ gltfAttributeName ] = getAttributeCache( attribute );
 							continue;
 
 						}
@@ -1289,7 +1339,7 @@ THREE.GLTFExporter.prototype = {
 						}
 
 						target[ gltfAttributeName ] = processAccessor( relativeAttribute, geometry );
-						cachedData.attributes.set( baseAttribute, target[ gltfAttributeName ] );
+						setAttributeCache( baseAttribute, target[ gltfAttributeName ] );
 
 					}
 
@@ -1358,14 +1408,14 @@ THREE.GLTFExporter.prototype = {
 
 				if ( geometry.index !== null ) {
 
-					if ( cachedData.attributes.has( geometry.index ) ) {
+					if ( hasAttributeCache( geometry.index, groups[ i ].start, groups[ i ].count ) ) {
 
-						primitive.indices = cachedData.attributes.get( geometry.index );
+						primitive.indices = getAttributeCache( geometry.index, groups[ i ].start, groups[ i ].count );
 
 					} else {
 
 						primitive.indices = processAccessor( geometry.index, geometry, groups[ i ].start, groups[ i ].count );
-						cachedData.attributes.set( geometry.index, primitive.indices );
+						setAttributeCache( primitive.indices, geometry.index, groups[ i ].start, groups[ i ].count );
 
 					}