-
Notifications
You must be signed in to change notification settings - Fork 103
Import tangents #613
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
base: flip-textures-and-uvs
Are you sure you want to change the base?
Import tangents #613
Changes from 15 commits
7be5e40
7508ff5
491ad86
cdc5dbc
c433b0d
81b1e44
5e5a7df
206bf1b
609e788
0994280
816f48c
7013995
432ea71
d1dfd5b
f70fc44
72cb616
25d481c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -369,6 +369,15 @@ void loadPrimitive( | |
| computeFlatNormals = hasNormals = true; | ||
| } | ||
|
|
||
| bool hasTangents = false; | ||
| auto tangentAcccessorIt = primitive.attributes.find("TANGENT"); | ||
david-lively marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| AccessorView<UnityEngine::Vector4> tangentView; | ||
| if (tangentAcccessorIt != primitive.attributes.end()) { | ||
| tangentView = | ||
| AccessorView<UnityEngine::Vector4>(gltf, tangentAcccessorIt->second); | ||
| hasTangents = tangentView.status() == AccessorViewStatus::Valid; | ||
| } | ||
|
|
||
| // Check if we need to upgrade to a large index type to accommodate the | ||
| // larger number of vertices we need for flat normals. | ||
| if (computeFlatNormals && indexFormat == IndexFormat::UInt16 && | ||
|
|
@@ -450,6 +459,15 @@ void loadPrimitive( | |
| ++numberOfAttributes; | ||
| } | ||
|
|
||
| if (hasTangents) { | ||
| assert(numberOfAttributes < MAX_ATTRIBUTES); | ||
| descriptor[numberOfAttributes].attribute = VertexAttribute::Tangent; | ||
| descriptor[numberOfAttributes].format = VertexAttributeFormat::Float32; | ||
| descriptor[numberOfAttributes].dimension = 4; | ||
| descriptor[numberOfAttributes].stream = streamIndex; | ||
| ++numberOfAttributes; | ||
| } | ||
|
|
||
| // Add the COLOR_0 attribute, if it exists. | ||
| auto colorAccessorIt = primitive.attributes.find("COLOR_0"); | ||
| bool hasVertexColors = | ||
|
|
@@ -554,9 +572,9 @@ void loadPrimitive( | |
| attributes.Item(i, descriptor[i]); | ||
| } | ||
|
|
||
| int32_t vertexCount = computeFlatNormals | ||
| ? indexCount | ||
| : static_cast<int32_t>(positionView.size()); | ||
| const int32_t vertexCount = computeFlatNormals | ||
| ? indexCount | ||
| : static_cast<int32_t>(positionView.size()); | ||
| meshData.SetVertexBufferParams(vertexCount, attributes); | ||
|
|
||
| NativeArray1<uint8_t> nativeVertexBuffer = | ||
|
|
@@ -571,74 +589,76 @@ void loadPrimitive( | |
| // The vertex layout will be as follows: | ||
| // 1. position | ||
| // 2. normals (skip if N/A) | ||
| // 3. vertex colors (skip if N/A) | ||
| // 4. texcoords (first all TEXCOORD_i, then all _CESIUMOVERLAY_i) | ||
| // 3. tangents (skip if N/A) | ||
| // 4. vertex colors (skip if N/A) | ||
| // 5. texcoords (first all TEXCOORD_i, then all _CESIUMOVERLAY_i) | ||
|
|
||
| size_t stride = sizeof(Vector3); | ||
| size_t normalByteOffset, colorByteOffset; | ||
| size_t normalByteOffset; | ||
| if (hasNormals) { | ||
| normalByteOffset = stride; | ||
| stride += sizeof(Vector3); | ||
| } | ||
|
|
||
| if (hasTangents) { | ||
| stride += sizeof(Vector4); | ||
| } | ||
|
|
||
| size_t colorByteOffset; | ||
| if (hasVertexColors) { | ||
| colorByteOffset = stride; | ||
| stride += sizeof(uint32_t); | ||
| } | ||
|
|
||
| stride += numTexCoords * sizeof(Vector2); | ||
|
|
||
| const bool duplicateVertices = computeFlatNormals; | ||
| if (computeFlatNormals) { | ||
| ::computeFlatNormals( | ||
| pWritePos + normalByteOffset, | ||
| stride, | ||
| indices, | ||
| indexCount, | ||
| positionView); | ||
| for (int64_t i = 0; i < vertexCount; ++i) { | ||
| TIndex vertexIndex = indices[i]; | ||
| *reinterpret_cast<Vector3*>(pWritePos) = positionView[vertexIndex]; | ||
| // skip position and normal | ||
| pWritePos += 2 * sizeof(Vector3); | ||
| // Skip the slot allocated for vertex colors, we will fill them in | ||
| // bulk later. | ||
| if (hasVertexColors) { | ||
| pWritePos += sizeof(uint32_t); | ||
| } | ||
| for (uint32_t texCoordIndex = 0; texCoordIndex < numTexCoords; | ||
| ++texCoordIndex) { | ||
| Vector2 texCoord = texCoordViews[texCoordIndex][vertexIndex]; | ||
| // Flip Y to comply with Unity's V-up coordinate convention | ||
| texCoord.y = 1 - texCoord.y; | ||
| *reinterpret_cast<Vector2*>(pWritePos) = texCoord; | ||
| pWritePos += sizeof(Vector2); | ||
| } | ||
| } | ||
| } else { | ||
| for (int64_t i = 0; i < vertexCount; ++i) { | ||
| *reinterpret_cast<Vector3*>(pWritePos) = positionView[i]; | ||
| } | ||
|
|
||
| for (int64_t i = 0; i < vertexCount; ++i) { | ||
| const TIndex vertexIndex = duplicateVertices ? indices[i] : i; | ||
| *reinterpret_cast<Vector3*>(pWritePos) = positionView[vertexIndex]; | ||
| pWritePos += sizeof(Vector3); | ||
|
|
||
| if (computeFlatNormals) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hm I'm not sure how much of an impact it has here, but we tend to avoid putting
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @j9liu Hmm. Not sure I see the benefit here. The existing implementation already had |
||
| // skip computed normal | ||
| pWritePos += sizeof(Vector3); | ||
| } else if (hasNormals) { | ||
| *reinterpret_cast<Vector3*>(pWritePos) = normalView[vertexIndex]; | ||
| pWritePos += sizeof(Vector3); | ||
| if (hasNormals) { | ||
| *reinterpret_cast<Vector3*>(pWritePos) = normalView[i]; | ||
| pWritePos += sizeof(Vector3); | ||
| } | ||
| // Skip the slot allocated for vertex colors, we will fill them in | ||
| // bulk later. | ||
| if (hasVertexColors) { | ||
| pWritePos += sizeof(uint32_t); | ||
| } | ||
| for (uint32_t texCoordIndex = 0; texCoordIndex < numTexCoords; | ||
| ++texCoordIndex) { | ||
| Vector2 texCoord = texCoordViews[texCoordIndex][i]; | ||
| // Flip Y to comply with Unity's V-up coordinate convention | ||
| texCoord.y = 1 - texCoord.y; | ||
| *reinterpret_cast<Vector2*>(pWritePos) = texCoord; | ||
| pWritePos += sizeof(Vector2); | ||
| } | ||
| } | ||
|
|
||
| if (hasTangents) { | ||
| *reinterpret_cast<Vector4*>(pWritePos) = tangentView[vertexIndex]; | ||
| pWritePos += sizeof(Vector4); | ||
| } | ||
|
|
||
| // Skip the slot allocated for vertex colors, we will fill them in | ||
| // bulk later. | ||
| if (hasVertexColors) { | ||
| pWritePos += sizeof(uint32_t); | ||
| } | ||
|
|
||
| for (uint32_t texCoordIndex = 0; texCoordIndex < numTexCoords; | ||
| ++texCoordIndex) { | ||
| Vector2 texCoord = texCoordViews[texCoordIndex][vertexIndex]; | ||
| // Flip Y to comply with Unity's V-up coordinate convention | ||
| texCoord.y = 1 - texCoord.y; | ||
| *reinterpret_cast<Vector2*>(pWritePos) = texCoord; | ||
| pWritePos += sizeof(Vector2); | ||
| } | ||
| } | ||
|
|
||
| // Fill in vertex colors separately, if they exist. | ||
| if (hasVertexColors) { | ||
| // Color comes after position and normal. | ||
| // Color comes after position, normal and tangent | ||
| createAccessorView( | ||
| gltf, | ||
| colorAccessorIt->second, | ||
|
|
@@ -650,7 +670,7 @@ void loadPrimitive( | |
| indices}); | ||
| } | ||
|
|
||
| if (computeFlatNormals) { | ||
| if (duplicateVertices) { | ||
| // rewrite indices | ||
| for (TIndex i = 0; i < indexCount; i++) { | ||
| indices[i] = i; | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.