diff --git a/src/wkb2gltf.core.tests/GlbCreatorTests.cs b/src/wkb2gltf.core.tests/GlbCreatorTests.cs index da8f8203..1924e07e 100644 --- a/src/wkb2gltf.core.tests/GlbCreatorTests.cs +++ b/src/wkb2gltf.core.tests/GlbCreatorTests.cs @@ -194,6 +194,38 @@ public void CreateGlbWithDegeneratedTriangleShouldNotGiveException() Assert.That(bytes, Is.EqualTo(null)); } + [Test] + public void CreateGlbWithSingleColorShader() + { + // arrange + var wkt = "MULTIPOLYGON Z (((133836.921875 463013.96875 -2.280999898910522,133842.046875 463004.65625 -2.280999898910522,133833.359375 462999.84375 -2.280999898910522,133831.21875 462998.65625 -2.280999898910522,133830.484375 463000 -2.280999898910522,133826.078125 463008.03125 -2.280999898910522,133828.03125 463009.09375 -2.280999898910522,133836.921875 463013.96875 -2.280999898910522)),((133833.359375 462999.84375 2.655999898910522,133833.359375 462999.84375 -2.280999898910522,133842.046875 463004.65625 -2.280999898910522,133842.046875 463004.65625 -0.250999987125397,133833.359375 462999.84375 2.655999898910522)),((133831.21875 462998.65625 0.360000014305115,133831.21875 462998.65625 -2.280999898910522,133833.359375 462999.84375 -2.280999898910522,133833.359375 462999.84375 2.655999898910522,133833.359375 462999.84375 2.776999950408936,133831.21875 462998.65625 0.360000014305115)),((133828.03125 463009.09375 2.638000011444092,133828.03125 463009.09375 2.555000066757202,133830.1875 463005.34375 2.644999980926514,133828.03125 463009.09375 2.638000011444092)),((133833.359375 462999.84375 2.776999950408936,133833.359375 462999.84375 2.655999898910522,133830.1875 463005.34375 2.644999980926514,133833.359375 462999.84375 2.776999950408936)),((133836.921875 463013.96875 -0.331999987363815,133836.921875 463013.96875 -2.280999898910522,133828.03125 463009.09375 -2.280999898910522,133828.03125 463009.09375 2.555000066757202,133828.03125 463009.09375 2.638000011444092,133836.921875 463013.96875 -0.331999987363815)),((133830.484375 463000 0.358999997377396,133830.484375 463000 -2.280999898910522,133831.21875 462998.65625 -2.280999898910522,133831.21875 462998.65625 0.360000014305115,133830.484375 463000 0.358999997377396)),((133842.046875 463004.65625 -0.250999987125397,133842.046875 463004.65625 -2.280999898910522,133836.921875 463013.96875 -2.280999898910522,133836.921875 463013.96875 -0.331999987363815,133842.046875 463004.65625 -0.250999987125397)),((133826.078125 463008.03125 0.354000002145767,133826.078125 463008.03125 -2.280999898910522,133830.484375 463000 -2.280999898910522,133830.484375 463000 0.358999997377396,133826.078125 463008.03125 0.354000002145767)),((133828.03125 463009.09375 2.555000066757202,133828.03125 463009.09375 -2.280999898910522,133826.078125 463008.03125 -2.280999898910522,133826.078125 463008.03125 0.354000002145767,133828.03125 463009.09375 2.555000066757202)),((133842.046875 463004.65625 -0.250999987125397,133836.921875 463013.96875 -0.331999987363815,133828.03125 463009.09375 2.638000011444092,133830.1875 463005.34375 2.644999980926514,133833.359375 462999.84375 2.655999898910522,133842.046875 463004.65625 -0.250999987125397)),((133828.03125 463009.09375 2.555000066757202,133826.078125 463008.03125 0.354000002145767,133830.484375 463000 0.358999997377396,133831.21875 462998.65625 0.360000014305115,133833.359375 462999.84375 2.776999950408936,133830.1875 463005.34375 2.644999980926514,133828.03125 463009.09375 2.555000066757202)))"; + var g = Geometry.Deserialize(wkt); + var multipolygon = ((MultiPolygon)g); + var shaderColors = new ShaderColors(); + var metallicRoughness = new PbrMetallicRoughnessColors(); + + // use 1 basecolor + var random = new Random(); + var baseColors = new List(); + var color = string.Format("#{0:X6}", random.Next(0x1000000)); + baseColors.Add(color); + + metallicRoughness.BaseColors = baseColors; + + shaderColors.PbrMetallicRoughnessColors = metallicRoughness; + + // act + var triangles = GeometryProcessor.GetTriangles(multipolygon, 100, new double[] { 0, 0, 0 }, shaderColors); + var bytes = GlbCreator.GetGlb(new List>() { triangles }); + var fileName = Path.Combine(TestContext.CurrentContext.WorkDirectory, "ams_building_multiple_colors.glb"); + File.WriteAllBytes(fileName, bytes); + + var model = ModelRoot.Load(fileName); + + Assert.That(model.LogicalMeshes[0].Primitives.Count, Is.EqualTo(1)); + } + + [Test] public void CreateGlbWithShader() { diff --git a/src/wkb2gltf.core/ShaderColors.cs b/src/wkb2gltf.core/ShaderColors.cs index 4740005d..821438eb 100644 --- a/src/wkb2gltf.core/ShaderColors.cs +++ b/src/wkb2gltf.core/ShaderColors.cs @@ -57,13 +57,19 @@ public void Validate(int expectedGeometries) private void Check(List colors, int expectedGeometries, string error, List errors) { - if (colors != null && colors.Count != expectedGeometries) { + // check the amount of colors is not null, one or the same as the geometries or 1 + if (colors != null && colors.Count != expectedGeometries && colors.Count!= 1) { errors.Add(error); } } private string GetItem(List items, int i) { + // if there is only one item, always return the first + // use for having 1 shader per geometry + if(items!=null && items.Count == 1) { + i = 0; + } return items != null ? items[i] : null; } } diff --git a/styling.md b/styling.md index ceb29003..5ddf5a44 100644 --- a/styling.md +++ b/styling.md @@ -16,7 +16,7 @@ Metallic factor: 0, Roughness factor: 0.5019608 (128/255) - Alpha: 0 (hardcoded) -Alternative option is to specify a shader per triangle in the ShadersColumn. +Alternative option is to specify a shader using the ShadersColumn. Shaderscolumn is a column of type json. In this json document the shaders are defined like PbrMetallicRoughness and PbrSpecularGlossiness. Note: PbrSpecularGlossiness is deprecated by Khronos, so advise is to use PbrMetallicRoughness. @@ -39,9 +39,27 @@ The json must have the following structure: } ``` -The amount of colors in the lists must correspond to the number of triangles in the geometry, otherwise an exception is thrown. +The amount of colors in the lists -Warning: The input geometries must be triangulated for this to work. Otherwise pg2b3dm will triangulate the geometries and the number of triangles will be unknown. +- must correspond to the number of triangles in the geometry; + +- or be 1, in which case the same color is used for all triangles in the geometry; + +Example: + +``` +update delaware_buildings set simple_shader = +'{ + "PbrMetallicRoughness": { + "BaseColors": ["#ff0000"] + } +}'; +``` + +- otherwise an exception is thrown. + + +Warning: When using a shader per triangle, the input geometries must be triangulated for this to work. Otherwise pg2b3dm will triangulate the geometries and the number of triangles will be unknown. ## Sql