Skip to content

add support for single shader per geometry #147

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions src/wkb2gltf.core.tests/GlbCreatorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<WktSerializer>(wkt);
var multipolygon = ((MultiPolygon)g);
var shaderColors = new ShaderColors();
var metallicRoughness = new PbrMetallicRoughnessColors();

// use 1 basecolor
var random = new Random();
var baseColors = new List<string>();
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<List<Triangle>>() { 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()
{
Expand Down
8 changes: 7 additions & 1 deletion src/wkb2gltf.core/ShaderColors.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,19 @@ public void Validate(int expectedGeometries)

private void Check(List<string> colors, int expectedGeometries, string error, List<string> 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<string> 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;
}
}
Expand Down
24 changes: 21 additions & 3 deletions styling.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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

Expand Down