Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into add-wmts-properties
Browse files Browse the repository at this point in the history
  • Loading branch information
kring committed Mar 29, 2024
2 parents be2333e + 354466e commit e1a0bc6
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 29 deletions.
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@
##### Additions :tada:

- Added `CesiumWebMapTileServiceRasterOverlay`, which enables Web Map Tile Service (WMTS) imagery to be draped on a `Cesium3DTileset`.
- Added support for the `KHR_texture_transform` glTF extension - including rotation - for picking with `CesiumFeatureIdTexture`.

##### Fixes :wrench:

- Normal, metallic-roughness, and occlusion textures from glTF models will now be correctly treated as linear rather than sRGB.
- Fixed a bug where UVs were not properly interpolated in `CesiumFeatureIdTexture.GetFeatureIdFromHit`, resulting in incorrect values.

### v1.8.0 - 2023-03-01

Expand Down
2 changes: 1 addition & 1 deletion Reinterop~/Methods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public static void GenerateSingleMethod(CppGenerationContext context, TypeToGene
// If this is an instance method, pass the current object as the first parameter.
if (!method.IsStatic)
{
interopParameters = new[] { (ParameterName: "thiz", CallSiteName: "(*this)", Type: result.CppDefinition.Type.AsParameterType(), InteropType: result.CppDefinition.Type.AsInteropType()) }.Concat(interopParameters);
interopParameters = new[] { (ParameterName: "thiz", CallSiteName: "(*this)", Type: result.CppDefinition.Type.AsParameterType(), InteropType: result.CppDefinition.Type.AsParameterType().AsInteropType()) }.Concat(interopParameters);
}

bool hasStructRewrite = Interop.RewriteStructReturn(ref interopParameters, ref returnType, ref interopReturnType);
Expand Down
5 changes: 4 additions & 1 deletion Runtime/ConfigureReinterop.cs
Original file line number Diff line number Diff line change
Expand Up @@ -803,10 +803,13 @@ Cesium3DTilesetLoadFailureDetails tilesetDetails
property.valueType = property.valueType;

RaycastHit hitInfo = new RaycastHit();
primitiveFeatures = hitInfo.transform.GetComponent<CesiumPrimitiveFeatures>();
int triangleIndex = hitInfo.triangleIndex;
Vector3 coordinate = hitInfo.barycentricCoordinate;
Vector3 hitPoint = hitInfo.point;

Vector2 textureCoordinate = new Vector2();
textureCoordinate.x = textureCoordinate.y;
hitPoint = m2.MultiplyPoint3x4(hitPoint);

CesiumIonServer server = CesiumIonServer.defaultServer;
server.serverUrl = "";
Expand Down
85 changes: 65 additions & 20 deletions native~/Runtime/src/CesiumFeatureIdTextureImpl.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
#include "CesiumFeatureIdTextureImpl.h"

#include <CesiumGeometry/IntersectionTests.h>
#include <CesiumGltf/AccessorUtility.h>

#include <DotNet/CesiumForUnity/CesiumFeatureIdTexture.h>
#include <DotNet/CesiumForUnity/CesiumFeatureIdTextureStatus.h>
#include <DotNet/CesiumForUnity/CesiumPrimitiveFeatures.h>
#include <DotNet/UnityEngine/Matrix4x4.h>
#include <DotNet/UnityEngine/RaycastHit.h>
#include <DotNet/UnityEngine/Transform.h>
#include <DotNet/UnityEngine/Vector2.h>
#include <DotNet/UnityEngine/Vector3.h>

Expand All @@ -23,17 +27,13 @@ CesiumFeatureIdTextureImpl::CreateTexture(
const CesiumGltf::MeshPrimitive& primitive,
const int64_t featureCount,
const CesiumGltf::FeatureIdTexture& featureIdTexture) {
CesiumGltf::TextureViewOptions options;
options.applyKhrTextureTransformExtension = true;

CesiumFeatureIdTexture texture;
CesiumFeatureIdTextureImpl& textureImpl = texture.NativeImplementation();
textureImpl._featureIdTextureView =
CesiumGltf::FeatureIdTextureView(model, featureIdTexture);
textureImpl._texCoordAccessor = CesiumGltf::getTexCoordAccessorView(
model,
primitive,
featureIdTexture.texCoord);
textureImpl._indexAccessor =
CesiumGltf::getIndexAccessorView(model, primitive);
textureImpl._primitiveMode = primitive.mode;
CesiumGltf::FeatureIdTextureView(model, featureIdTexture, options);

switch (textureImpl._featureIdTextureView.status()) {
case CesiumGltf::FeatureIdTextureViewStatus::Valid:
Expand All @@ -43,14 +43,24 @@ CesiumFeatureIdTextureImpl::CreateTexture(
case CesiumGltf::FeatureIdTextureViewStatus::ErrorInvalidChannels:
texture.status(CesiumFeatureIdTextureStatus::ErrorInvalidTextureAccess);
texture.featureCount(0);
break;
return texture;
default:
// Error with the texture or image. The status is already set by the C#
// constructor.
texture.featureCount(0);
break;
return texture;
}

textureImpl._texCoordAccessor = CesiumGltf::getTexCoordAccessorView(
model,
primitive,
textureImpl._featureIdTextureView.getTexCoordSetIndex());
textureImpl._indexAccessor =
CesiumGltf::getIndexAccessorView(model, primitive);
textureImpl._positionAccessor =
CesiumGltf::getPositionAccessorView(model, primitive);
textureImpl._primitiveMode = primitive.mode;

return texture;
}

Expand All @@ -77,6 +87,15 @@ std::int64_t CesiumFeatureIdTextureImpl::GetFeatureIdForVertex(
std::int64_t CesiumFeatureIdTextureImpl::GetFeatureIdFromRaycastHit(
const CesiumFeatureIdTexture& featureIdTexture,
const DotNet::UnityEngine::RaycastHit& hitInfo) {
if (hitInfo.transform().GetComponent<CesiumPrimitiveFeatures>() == nullptr) {
return -1;
}

if (this->_positionAccessor.status() !=
CesiumGltf::AccessorViewStatus::Valid) {
return -1;
}

int64_t vertexCount =
std::visit(CesiumGltf::CountFromAccessor{}, this->_texCoordAccessor);

Expand All @@ -87,25 +106,51 @@ std::int64_t CesiumFeatureIdTextureImpl::GetFeatureIdFromRaycastHit(
this->_primitiveMode},
this->_indexAccessor);

std::array<glm::dvec2, 3> UVs;
for (size_t i = 0; i < UVs.size(); i++) {
std::array<glm::dvec2, 3> uvs;
std::array<glm::vec3, 3> positions;

for (size_t i = 0; i < positions.size(); i++) {
int64_t index = vertexIndices[i];
if (index < 0 || index >= vertexCount) {
return -1;
}

auto maybeTexCoord = std::visit(
CesiumGltf::TexCoordFromAccessor{vertexIndices[i]},
CesiumGltf::TexCoordFromAccessor{index},
this->_texCoordAccessor);
if (!maybeTexCoord) {
return -1;
}
uvs[i] = *maybeTexCoord;

const glm::dvec2& texCoord = *maybeTexCoord;
UVs[i] = glm::dvec2(texCoord[0], texCoord[1]);
CesiumGltf::AccessorTypes::VEC3<float> position =
this->_positionAccessor[index];
positions[i] =
glm::vec3(position.value[0], position.value[1], position.value[2]);
}

DotNet::UnityEngine::Vector3 barycentricCoords =
hitInfo.barycentricCoordinate();
// The barycentric coordinates in RaycastHit don't align with the positions
// in the glTF accessor, so we manually compute barycentric coordinates here.
DotNet::UnityEngine::Vector3 worldPosition = hitInfo.point();
DotNet::UnityEngine::Matrix4x4 worldToLocal =
hitInfo.transform().worldToLocalMatrix();
DotNet::UnityEngine::Vector3 localPosition =
worldToLocal.MultiplyPoint3x4(worldPosition);

glm::dvec3 barycentricCoordinates;
bool foundIntersection = CesiumGeometry::IntersectionTests::pointInTriangle(
glm::dvec3(localPosition.x, localPosition.y, localPosition.z),
glm::dvec3(positions[0]),
glm::dvec3(positions[1]),
glm::dvec3(positions[2]),
barycentricCoordinates);
if (!foundIntersection) {
return -1;
}

glm::dvec2 UV = (static_cast<double>(barycentricCoords.x) * UVs[0]) +
(static_cast<double>(barycentricCoords.y) * UVs[1]) +
(static_cast<double>(barycentricCoords.z) * UVs[2]);
glm::dvec2 UV = (barycentricCoordinates[0] * uvs[0]) +
(barycentricCoordinates[1] * uvs[1]) +
(barycentricCoordinates[2] * uvs[2]);

return this->_featureIdTextureView.getFeatureID(UV.x, UV.y);
}
Expand Down
1 change: 1 addition & 0 deletions native~/Runtime/src/CesiumFeatureIdTextureImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ class CesiumFeatureIdTextureImpl {
CesiumGltf::FeatureIdTextureView _featureIdTextureView;
CesiumGltf::TexCoordAccessorType _texCoordAccessor;
CesiumGltf::IndexAccessorType _indexAccessor;
CesiumGltf::PositionAccessorType _positionAccessor;
int32_t _primitiveMode;
};
} // namespace CesiumForUnityNative
16 changes: 10 additions & 6 deletions native~/Runtime/src/UnityPrepareRendererResources.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -344,12 +344,14 @@ void loadPrimitive(
Unity::Collections::LowLevel::Unsafe::NativeArrayUnsafeUtility::
GetUnsafeBufferPointerWithoutChecks(dest));

if (primitive.mode == MeshPrimitive::Mode::TRIANGLES ||
primitive.mode == MeshPrimitive::Mode::POINTS) {
switch (primitive.mode) {
case MeshPrimitive::Mode::TRIANGLES:
case MeshPrimitive::Mode::POINTS:
for (int64_t i = 0; i < indicesView.size(); ++i) {
indices[i] = indicesView[i];
}
} else if (primitive.mode == MeshPrimitive::Mode::TRIANGLE_STRIP) {
break;
case MeshPrimitive::Mode::TRIANGLE_STRIP:
for (int64_t i = 0; i < indicesView.size() - 2; ++i) {
if (i % 2) {
indices[3 * i] = indicesView[i];
Expand All @@ -361,13 +363,15 @@ void loadPrimitive(
indices[3 * i + 2] = indicesView[i + 2];
}
}
} else { // MeshPrimitive::Mode::TRIANGLE_FAN
TIndex i0 = indicesView[0];
break;
case MeshPrimitive::Mode::TRIANGLE_FAN:
default:
for (int64_t i = 2; i < indicesView.size(); ++i) {
indices[3 * i] = i0;
indices[3 * i] = indicesView[0];
indices[3 * i + 1] = indicesView[i - 1];
indices[3 * i + 2] = indicesView[i];
}
break;
}

// Max attribute count supported by Unity, see VertexAttribute.
Expand Down

0 comments on commit e1a0bc6

Please sign in to comment.