|
| 1 | +using System; |
| 2 | + |
| 3 | +namespace SharpGLTF.Schema2 |
| 4 | +{ |
| 5 | + public partial class GaussianSplatting |
| 6 | + { |
| 7 | + #region lifecycle |
| 8 | + |
| 9 | + internal GaussianSplatting(MeshPrimitive primitive) |
| 10 | + { |
| 11 | + _Owner = primitive; |
| 12 | + } |
| 13 | + |
| 14 | + #endregion |
| 15 | + |
| 16 | + #region constants |
| 17 | + |
| 18 | + public const string KernelEllipse = "ellipse"; |
| 19 | + |
| 20 | + public const string ColorSpaceSrgbRec709Display = "srgb_rec709_display"; |
| 21 | + public const string ColorSpaceLinRec709Display = "lin_rec709_display"; |
| 22 | + |
| 23 | + public const string ProjectionPerspective = "perspective"; |
| 24 | + public const string SortingMethodCameraDistance = "cameraDistance"; |
| 25 | + |
| 26 | + public const string AttributeRotation = "KHR_gaussian_splatting:ROTATION"; |
| 27 | + public const string AttributeScale = "KHR_gaussian_splatting:SCALE"; |
| 28 | + public const string AttributeOpacity = "KHR_gaussian_splatting:OPACITY"; |
| 29 | + public const string AttributeSHDegree0Coef0 = "KHR_gaussian_splatting:SH_DEGREE_0_COEF_0"; |
| 30 | + |
| 31 | + #endregion |
| 32 | + |
| 33 | + #region data (not serializable) |
| 34 | + |
| 35 | + private readonly MeshPrimitive _Owner; |
| 36 | + |
| 37 | + #endregion |
| 38 | + |
| 39 | + #region properties |
| 40 | + |
| 41 | + public MeshPrimitive LogicalParent => _Owner; |
| 42 | + |
| 43 | + public string Kernel |
| 44 | + { |
| 45 | + get => _kernel; |
| 46 | + set |
| 47 | + { |
| 48 | + Guard.NotNullOrEmpty(value, nameof(value)); |
| 49 | + _kernel = value; |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + public string ColorSpace |
| 54 | + { |
| 55 | + get => _colorSpace; |
| 56 | + set |
| 57 | + { |
| 58 | + Guard.NotNullOrEmpty(value, nameof(value)); |
| 59 | + _colorSpace = value; |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + public string Projection |
| 64 | + { |
| 65 | + get => _projection ?? _projectionDefault; |
| 66 | + set |
| 67 | + { |
| 68 | + value = value.AsEmptyNullable(); |
| 69 | + _projection = value == _projectionDefault ? null : value; |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + public string SortingMethod |
| 74 | + { |
| 75 | + get => _sortingMethod ?? _sortingMethodDefault; |
| 76 | + set |
| 77 | + { |
| 78 | + value = value.AsEmptyNullable(); |
| 79 | + _sortingMethod = value == _sortingMethodDefault ? null : value; |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + #endregion |
| 84 | + |
| 85 | + #region API |
| 86 | + |
| 87 | + public static string GetSphericalHarmonicsAttribute(int degree, int coefficient) |
| 88 | + { |
| 89 | + Guard.MustBeBetweenOrEqualTo(degree, 0, 3, nameof(degree)); |
| 90 | + |
| 91 | + var coefficientCount = 2 * degree + 1; |
| 92 | + Guard.MustBeBetweenOrEqualTo(coefficient, 0, coefficientCount - 1, nameof(coefficient)); |
| 93 | + |
| 94 | + return $"KHR_gaussian_splatting:SH_DEGREE_{degree}_COEF_{coefficient}"; |
| 95 | + } |
| 96 | + |
| 97 | + #endregion |
| 98 | + } |
| 99 | + |
| 100 | + public sealed partial class MeshPrimitive |
| 101 | + { |
| 102 | + private static readonly string[] _GaussianRequiredAttributes = |
| 103 | + { |
| 104 | + GaussianSplatting.AttributeRotation, |
| 105 | + GaussianSplatting.AttributeScale, |
| 106 | + GaussianSplatting.AttributeOpacity, |
| 107 | + GaussianSplatting.AttributeSHDegree0Coef0 |
| 108 | + }; |
| 109 | + |
| 110 | + public GaussianSplatting GetGaussianSplatting() |
| 111 | + { |
| 112 | + return this.GetExtension<GaussianSplatting>(); |
| 113 | + } |
| 114 | + |
| 115 | + public GaussianSplatting UseGaussianSplatting() |
| 116 | + { |
| 117 | + var ext = GetGaussianSplatting(); |
| 118 | + if (ext == null) |
| 119 | + { |
| 120 | + ext = new GaussianSplatting(this); |
| 121 | + this.SetExtension(ext); |
| 122 | + } |
| 123 | + |
| 124 | + return ext; |
| 125 | + } |
| 126 | + |
| 127 | + public void RemoveGaussianSplatting() |
| 128 | + { |
| 129 | + this.RemoveExtensions<GaussianSplatting>(); |
| 130 | + } |
| 131 | + |
| 132 | + private void _ValidateGaussianSplatting(Validation.ValidationContext validate) |
| 133 | + { |
| 134 | + var gaussian = GetGaussianSplatting(); |
| 135 | + if (gaussian == null) return; |
| 136 | + |
| 137 | + validate.EnumsAreEqual(nameof(DrawPrimitiveType), DrawPrimitiveType, PrimitiveType.POINTS); |
| 138 | + validate.IsDefined("Attributes.POSITION", GetVertexAccessor("POSITION")); |
| 139 | + |
| 140 | + foreach (var semantic in _GaussianRequiredAttributes) |
| 141 | + { |
| 142 | + validate.IsDefined($"Attributes.{semantic}", GetVertexAccessor(semantic)); |
| 143 | + } |
| 144 | + |
| 145 | + var rotation = GetVertexAccessor(GaussianSplatting.AttributeRotation); |
| 146 | + if (rotation != null) |
| 147 | + { |
| 148 | + validate.IsAnyOf |
| 149 | + ( |
| 150 | + $"Attributes.{GaussianSplatting.AttributeRotation}.Format", |
| 151 | + rotation.Format, |
| 152 | + (DimensionType.VEC4, EncodingType.FLOAT), |
| 153 | + (DimensionType.VEC4, EncodingType.BYTE, true), |
| 154 | + (DimensionType.VEC4, EncodingType.SHORT, true) |
| 155 | + ); |
| 156 | + } |
| 157 | + |
| 158 | + var scale = GetVertexAccessor(GaussianSplatting.AttributeScale); |
| 159 | + if (scale != null) |
| 160 | + { |
| 161 | + validate.IsAnyOf |
| 162 | + ( |
| 163 | + $"Attributes.{GaussianSplatting.AttributeScale}.Format", |
| 164 | + scale.Format, |
| 165 | + (DimensionType.VEC3, EncodingType.FLOAT), |
| 166 | + (DimensionType.VEC3, EncodingType.BYTE), |
| 167 | + (DimensionType.VEC3, EncodingType.BYTE, true), |
| 168 | + (DimensionType.VEC3, EncodingType.SHORT), |
| 169 | + (DimensionType.VEC3, EncodingType.SHORT, true) |
| 170 | + ); |
| 171 | + } |
| 172 | + |
| 173 | + var opacity = GetVertexAccessor(GaussianSplatting.AttributeOpacity); |
| 174 | + if (opacity != null) |
| 175 | + { |
| 176 | + validate.IsAnyOf |
| 177 | + ( |
| 178 | + $"Attributes.{GaussianSplatting.AttributeOpacity}.Format", |
| 179 | + opacity.Format, |
| 180 | + (DimensionType.SCALAR, EncodingType.FLOAT), |
| 181 | + (DimensionType.SCALAR, EncodingType.UNSIGNED_BYTE, true), |
| 182 | + (DimensionType.SCALAR, EncodingType.UNSIGNED_SHORT, true) |
| 183 | + ); |
| 184 | + } |
| 185 | + |
| 186 | + var sh0 = GetVertexAccessor(GaussianSplatting.AttributeSHDegree0Coef0); |
| 187 | + if (sh0 != null) |
| 188 | + { |
| 189 | + validate.IsAnyOf |
| 190 | + ( |
| 191 | + $"Attributes.{GaussianSplatting.AttributeSHDegree0Coef0}.Format", |
| 192 | + sh0.Format, |
| 193 | + (DimensionType.VEC3, EncodingType.FLOAT) |
| 194 | + ); |
| 195 | + } |
| 196 | + |
| 197 | + validate.IsTrue(nameof(gaussian.Kernel), !string.IsNullOrWhiteSpace(gaussian.Kernel), "must be defined."); |
| 198 | + validate.IsTrue(nameof(gaussian.ColorSpace), !string.IsNullOrWhiteSpace(gaussian.ColorSpace), "must be defined."); |
| 199 | + } |
| 200 | + } |
| 201 | +} |
0 commit comments