项目中遇到使用 UnityGLTF插件加载GLTF模型时报错,如下图:
根据报错信息,我们找到GLTF文件中,第[五]个材质的描述:
{ "name": "11_-_Default木质长廊1.bmp_alpha", "occlusionTexture": { "index": 0, "texCoord": 1 }, "pbrMetallicRoughness": { "baseColorFactor": [ 0.8470589, 0.43921572, 0.0, 0.7 ], "metallicFactor": 0.099999994, "roughnessFactor": 1 }, "alphaMode": "BLEND", "emissiveFactor": [ 0.799999952, 0.799999952, 0.799999952, 1.0 ] }
其中emissiveFactor的值是:
"emissiveFactor": [ 0.799999952, 0.799999952, 0.799999952, 1 ]
然后,我们再看一下插件源码:
public static Color ReadAsRGBColor(this JsonReader reader) { if (reader.Read() && reader.TokenType != JsonToken.StartArray) { throw new Exception(string.Format("Invalid vector value at: {0}", reader.Path)); } var color = new Color { R = (float) reader.ReadAsDouble().Value, G = (float) reader.ReadAsDouble().Value, B = (float) reader.ReadAsDouble().Value, A = 1.0f }; if (reader.Read() && reader.TokenType != JsonToken.EndArray) { throw new Exception(string.Format("Invalid color value at: {0}", reader.Path)); } return color; }
可以看到Alpha直接用1.0f赋值,又由于emissiveFactor中有四个分量,就会导致下一个if中的
reader.TokenType != JsonToken.EndArray 是成立的,所以就抛出了异常
修改后的代码:
public static Color ReadAsRGBColor(this JsonReader reader) { if (reader.Read() && reader.TokenType != JsonToken.StartArray) { throw new Exception(string.Format("Invalid vector value at: {0}", reader.Path)); } var channels = new float[4] { 1f, 1f, 1f, 1f }; for (int i = 0; i < 4; i++) { var readObject = reader.ReadAsDouble(); if (reader.TokenType == JsonToken.EndArray) { UnityEngine.Debug.LogError(string.Format("Irregular color value at: {0}", reader.Path)); break; } if (readObject.HasValue) { channels[i] = (float)readObject.Value; } } if (reader.TokenType != JsonToken.EndArray && reader.Read() && reader.TokenType != JsonToken.EndArray) { throw new Exception(string.Format("Invalid color value at: {0}", reader.Path)); } var color = new Color { R = channels[0], G = channels[1], B = channels[2], A = channels[3] }; return color; }
标签:插件,JsonToken,TokenType,color,UnityGLTF,报错,reader,new,emissiveFactor From: https://www.cnblogs.com/zerozabuu/p/17732323.html