Skip to content

Commit 812e474

Browse files
Thomas Ichépeeweek
authored andcommitted
Fix 1276602 incorrect uchar pcache import (#129)
* Base Commit * Updated Documentation with explicit limitations. * Fixed incorrect Merge * Add regression Test * Removed Test as It can't handle Exceptions thrown in Custom Importers * Fixed value divider Co-authored-by: Thomas ICHÉ <[email protected]>
1 parent 9df1264 commit 812e474

File tree

3 files changed

+26
-15
lines changed

3 files changed

+26
-15
lines changed

com.unity.visualeffectgraph/Documentation~/PointCaches.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ Point cache Assets follow the Open Source [Point Cache](https://github.com/peewe
1414

1515
![](Images/PointCacheImporter.png)
1616

17+
### Limitations and Caveats
18+
19+
Currently, only the `float` and `uchar` property types are supported by the Importer. Any property of other types will return an error.
20+
1721
## Point Cache Operator
1822

1923
Point cache Assets can be referenced in a Point Cache Operator so it displays its point count and the list of Attribute Maps contained in the Point Cache Asset. The Number and the Name of the Outputs will dynamically change depending on the Asset set in the settings field.

com.unity.visualeffectgraph/Editor/Utilities/pCache/Importer/PointCacheImporter.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -124,15 +124,15 @@ public override void OnImportAsset(AssetImportContext ctx)
124124
TextureFormat surfaceFormat = TextureFormat.Alpha8;
125125
switch (kvp.Value.PropertyType)
126126
{
127-
case "byte":
127+
case "uchar":
128128
if (kvp.Value.Size == 1) surfaceFormat = TextureFormat.Alpha8;
129129
else surfaceFormat = TextureFormat.RGBA32;
130130
break;
131131
case "float":
132132
if (kvp.Value.Size == 1) surfaceFormat = TextureFormat.RHalf;
133133
else surfaceFormat = TextureFormat.RGBAHalf;
134134
break;
135-
default: throw new NotImplementedException("Types other than byte/float are not supported yet");
135+
default: throw new NotImplementedException("Types other than uchar/float are not supported yet");
136136
}
137137

138138
Texture2D surface = new Texture2D(width, height, surfaceFormat, false);
@@ -155,13 +155,13 @@ public override void OnImportAsset(AssetImportContext ctx)
155155
float val = 0.0f;
156156
switch (prop.PropertyType)
157157
{
158-
case "byte":
158+
case "uchar":
159159
val = Mathf.Clamp01(((byte)pcache.buckets[idx][i]) / 255.0f);
160160
break;
161161
case "float":
162162
val = ((float)pcache.buckets[idx][i]);
163163
break;
164-
default: throw new NotImplementedException("Types other than byte/float are not supported yet");
164+
default: throw new NotImplementedException("Types other than uchar/float are not supported yet");
165165
}
166166

167167
SetPropValue(prop.Index, outValues, prop.OutProperty, val);

com.unity.visualeffectgraph/Editor/Utilities/pCache/PCache.cs

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,9 @@ public void SaveToFile(string filePath, Format format = Format.Binary)
293293
var prop = properties[j];
294294
switch (prop.Type)
295295
{
296-
case "byte":
296+
case "char":
297+
binaryWriter.Write((sbyte)buckets[j][i]); break;
298+
case "uchar":
297299
binaryWriter.Write((byte)buckets[j][i]); break;
298300
case "short":
299301
binaryWriter.Write((short)buckets[j][i]); break;
@@ -321,7 +323,9 @@ public void SaveToFile(string filePath, Format format = Format.Binary)
321323
var prop = properties[j];
322324
switch (prop.Type)
323325
{
324-
case "byte":
326+
case "char":
327+
sb.Append(((sbyte)buckets[j][i]).ToString(CultureInfo.InvariantCulture)); break;
328+
case "uchar":
325329
sb.Append(((byte)buckets[j][i]).ToString(CultureInfo.InvariantCulture)); break;
326330
case "short":
327331
sb.Append(((short)buckets[j][i]).ToString(CultureInfo.InvariantCulture)); break;
@@ -448,13 +452,14 @@ public static PCache FromFile(string filename)
448452
var prop = data.properties[j];
449453
switch (prop.Type)
450454
{
451-
case "short": data.buckets[j].Add(binaryReader.ReadInt16()); break;
452-
case "ushort": data.buckets[j].Add(binaryReader.ReadUInt16()); break;
453-
case "int": data.buckets[j].Add(binaryReader.ReadInt32()); break;
454-
case "uint": data.buckets[j].Add(binaryReader.ReadUInt32()); break;
455-
case "byte": data.buckets[j].Add(binaryReader.ReadChar()); break;
456-
case "float": data.buckets[j].Add(binaryReader.ReadSingle()); break;
457-
case "double": data.buckets[j].Add(binaryReader.ReadDouble()); break;
455+
case "short": data.buckets[j].Add(binaryReader.ReadInt16()); break;
456+
case "ushort": data.buckets[j].Add(binaryReader.ReadUInt16()); break;
457+
case "int": data.buckets[j].Add(binaryReader.ReadInt32()); break;
458+
case "uint": data.buckets[j].Add(binaryReader.ReadUInt32()); break;
459+
case "char": data.buckets[j].Add(binaryReader.ReadSByte()); break;
460+
case "uchar": data.buckets[j].Add(binaryReader.ReadByte()); break;
461+
case "float": data.buckets[j].Add(binaryReader.ReadSingle()); break;
462+
case "double": data.buckets[j].Add(binaryReader.ReadDouble()); break;
458463
}
459464
}
460465
}
@@ -486,7 +491,8 @@ public static PCache FromFile(string filename)
486491
case "ushort": data.buckets[j].Add(ushort.Parse(elements[j], CultureInfo.InvariantCulture)); break;
487492
case "int": data.buckets[j].Add(int.Parse(elements[j], CultureInfo.InvariantCulture)); break;
488493
case "uint": data.buckets[j].Add(uint.Parse(elements[j], CultureInfo.InvariantCulture)); break;
489-
case "byte": data.buckets[j].Add(byte.Parse(elements[j], CultureInfo.InvariantCulture)); break;
494+
case "char": data.buckets[j].Add(sbyte.Parse(elements[j], CultureInfo.InvariantCulture)); break;
495+
case "uchar": data.buckets[j].Add(byte.Parse(elements[j], CultureInfo.InvariantCulture)); break;
490496
case "float": data.buckets[j].Add(float.Parse(elements[j], CultureInfo.InvariantCulture)); break;
491497
case "double": data.buckets[j].Add(double.Parse(elements[j], CultureInfo.InvariantCulture)); break;
492498
}
@@ -580,7 +586,8 @@ private static int GetPropertySize(string type)
580586

581587
private static Dictionary<string, int> TypeSize = new Dictionary<string, int>()
582588
{
583-
{ "byte", 1 },
589+
{ "char", 1 },
590+
{ "uchar", 1 },
584591
{ "short", 2 },
585592
{ "ushort", 2 },
586593
{ "int", 4 },

0 commit comments

Comments
 (0)