| 1 |
/* |
| 2 |
* Copyright (C) 2021 The Android Open Source Project |
| 3 |
* |
| 4 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
* you may not use this file except in compliance with the License. |
| 6 |
* You may obtain a copy of the License at |
| 7 |
* |
| 8 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
* |
| 10 |
* Unless required by applicable law or agreed to in writing, software |
| 11 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
* See the License for the specific language governing permissions and |
| 14 |
* limitations under the License. |
| 15 |
*/ |
| 16 |
|
| 17 |
#include "TangentsJob.h" |
| 18 |
|
| 19 |
#include <memory> |
| 20 |
|
| 21 |
#include <geometry/SurfaceOrientation.h> |
| 22 |
|
| 23 |
using namespace gltfio; |
| 24 |
using namespace filament; |
| 25 |
using namespace filament::math; |
| 26 |
|
| 27 |
// This procedure is designed to run in an isolated job. |
| 28 |
void TangentsJob::run(Params* params) { |
| 29 |
const cgltf_primitive& prim = *params->in.prim; |
| 30 |
const int morphTargetIndex = params->in.morphTargetIndex; |
| 31 |
const bool isMorphTarget = morphTargetIndex != kMorphTargetUnused; |
| 32 |
|
| 33 |
// Extract the vertex count from the first attribute. All attributes must have the same count. |
| 34 |
assert(prim.attributes_count > 0); |
| 35 |
const cgltf_size vertexCount = prim.attributes[0].data->count; |
| 36 |
params->out.vertexCount = vertexCount; |
| 37 |
if (vertexCount == 0) { |
| 38 |
return; |
| 39 |
} |
| 40 |
|
| 41 |
// Declare storage for data that has been unpacked and converted from the source buffers. |
| 42 |
// This data needs to be held until after the SurfaceOrientation helper consumes it. |
| 43 |
// Not all of these will be required, so they get allocated lazily. |
| 44 |
std::unique_ptr<float3[]> unpackedNormals; |
| 45 |
std::unique_ptr<float4[]> unpackedTangents; |
| 46 |
std::unique_ptr<float3[]> unpackedPositions; |
| 47 |
std::unique_ptr<float2[]> unpackedTexCoords; |
| 48 |
std::unique_ptr<uint3[]> unpackedTriangles; |
| 49 |
std::unique_ptr<float3[]> morphDeltas; |
| 50 |
|
| 51 |
// Build a mapping from cgltf_attribute_type to cgltf_accessor. |
| 52 |
const int NUM_ATTRIBUTES = 8; |
| 53 |
const cgltf_accessor* baseAccessors[NUM_ATTRIBUTES] = {}; |
| 54 |
const cgltf_accessor* morphTargetAccessors[NUM_ATTRIBUTES] = {}; |
| 55 |
|
| 56 |
// Collect accessors for normals, tangents, etc. Note that we skip over attributes with |
| 57 |
// non-zero set indices likes TEXCOORD_1, TEXCOORD_2 to avoid overflowing the tiny arrays. |
| 58 |
// The SurfaceOrientation helper does not need them anyway. |
| 59 |
for (cgltf_size aindex = 0; aindex < prim.attributes_count; aindex++) { |
| 60 |
const cgltf_attribute& attr = prim.attributes[aindex]; |
| 61 |
if (attr.index == 0) { |
| 62 |
baseAccessors[attr.type] = attr.data; |
| 63 |
} |
| 64 |
} |
| 65 |
if (isMorphTarget) { |
| 66 |
const cgltf_morph_target& morphTarget = prim.targets[morphTargetIndex]; |
| 67 |
for (cgltf_size aindex = 0; aindex < morphTarget.attributes_count; aindex++) { |
| 68 |
const cgltf_attribute& attr = morphTarget.attributes[aindex]; |
| 69 |
if (attr.index == 0) { |
| 70 |
assert(baseAccessors[attr.type] && |
| 71 |
"Morph target data has no corresponding base vertex data."); |
| 72 |
morphTargetAccessors[attr.type] = attr.data; |
| 73 |
} |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
geometry::SurfaceOrientation::Builder sob; |
| 78 |
sob.vertexCount(vertexCount); |
| 79 |
|
| 80 |
// Allocate scratch space to store morph deltas. |
| 81 |
if (isMorphTarget) { |
| 82 |
morphDeltas.reset(new float3[vertexCount]); |
| 83 |
} |
| 84 |
|
| 85 |
// Convert normals into packed floats. |
| 86 |
if (auto baseNormalsInfo = baseAccessors[cgltf_attribute_type_normal]; baseNormalsInfo) { |
| 87 |
assert(baseNormalsInfo->count == vertexCount); |
| 88 |
assert(baseNormalsInfo->type == cgltf_type_vec3); |
| 89 |
unpackedNormals.reset(new float3[vertexCount]); |
| 90 |
cgltf_accessor_unpack_floats(baseNormalsInfo, &unpackedNormals[0].x, vertexCount * 3); |
| 91 |
if (auto mtNormalsInfo = morphTargetAccessors[cgltf_attribute_type_normal]) { |
| 92 |
cgltf_accessor_unpack_floats(mtNormalsInfo, &morphDeltas[0].x, vertexCount * 3); |
| 93 |
for (cgltf_size i = 0; i < vertexCount; i++) { |
| 94 |
unpackedNormals[i] += morphDeltas[i]; |
| 95 |
} |
| 96 |
} |
| 97 |
sob.normals(unpackedNormals.get()); |
| 98 |
} |
| 99 |
|
| 100 |
// Convert tangents into packed floats. |
| 101 |
if (auto baseTangentsInfo = baseAccessors[cgltf_attribute_type_tangent]; baseTangentsInfo) { |
| 102 |
assert(baseTangentsInfo->count == vertexCount); |
| 103 |
unpackedTangents.reset(new float4[vertexCount]); |
| 104 |
cgltf_accessor_unpack_floats(baseTangentsInfo, &unpackedTangents[0].x, vertexCount * 4); |
| 105 |
if (auto mtTangentsInfo = morphTargetAccessors[cgltf_attribute_type_tangent]) { |
| 106 |
cgltf_accessor_unpack_floats(mtTangentsInfo, &morphDeltas[0].x, vertexCount * 3); |
| 107 |
for (cgltf_size i = 0; i < vertexCount; i++) { |
| 108 |
unpackedTangents[i].xyz += morphDeltas[i]; |
| 109 |
} |
| 110 |
} |
| 111 |
sob.tangents(unpackedTangents.get()); |
| 112 |
} |
| 113 |
|
| 114 |
if (auto basePosInfo = baseAccessors[cgltf_attribute_type_position]; basePosInfo) { |
| 115 |
assert(basePosInfo->count == vertexCount && basePosInfo->type == cgltf_type_vec3); |
| 116 |
unpackedPositions.reset(new float3[vertexCount]); |
| 117 |
cgltf_accessor_unpack_floats(basePosInfo, &unpackedPositions[0].x, vertexCount * 3); |
| 118 |
sob.positions(unpackedPositions.get()); |
| 119 |
if (auto mtPositionsInfo = morphTargetAccessors[cgltf_attribute_type_position]) { |
| 120 |
cgltf_accessor_unpack_floats(mtPositionsInfo, &morphDeltas[0].x, vertexCount * 3); |
| 121 |
for (cgltf_size i = 0; i < vertexCount; i++) { |
| 122 |
unpackedPositions[i] += morphDeltas[i]; |
| 123 |
} |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
const size_t triangleCount = prim.indices ? (prim.indices->count / 3) : (vertexCount / 3); |
| 128 |
unpackedTriangles.reset(new uint3[triangleCount]); |
| 129 |
|
| 130 |
if (prim.indices) { |
| 131 |
for (size_t tri = 0, j = 0; tri < triangleCount; ++tri) { |
| 132 |
auto& triangle = unpackedTriangles[tri]; |
| 133 |
triangle.x = cgltf_accessor_read_index(prim.indices, j++); |
| 134 |
triangle.y = cgltf_accessor_read_index(prim.indices, j++); |
| 135 |
triangle.z = cgltf_accessor_read_index(prim.indices, j++); |
| 136 |
} |
| 137 |
} else { |
| 138 |
for (size_t tri = 0, j = 0; tri < triangleCount; ++tri) { |
| 139 |
auto& triangle = unpackedTriangles[tri]; |
| 140 |
triangle.x = j++; |
| 141 |
triangle.y = j++; |
| 142 |
triangle.z = j++; |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
sob.triangleCount(triangleCount); |
| 147 |
sob.triangles(unpackedTriangles.get()); |
| 148 |
|
| 149 |
auto uvInfo = baseAccessors[cgltf_attribute_type_texcoord]; |
| 150 |
if (uvInfo && uvInfo->count == vertexCount && uvInfo->type == cgltf_type_vec2) { |
| 151 |
unpackedTexCoords.reset(new float2[vertexCount]); |
| 152 |
cgltf_accessor_unpack_floats(uvInfo, &unpackedTexCoords[0].x, vertexCount * 2); |
| 153 |
sob.uvs(unpackedTexCoords.get()); |
| 154 |
} |
| 155 |
|
| 156 |
// Compute surface orientation quaternions. |
| 157 |
params->out.results = (short4*) malloc(sizeof(short4) * vertexCount); |
| 158 |
geometry::SurfaceOrientation* helper = sob.build(); |
| 159 |
helper->getQuats(params->out.results, vertexCount); |
| 160 |
delete helper; |
| 161 |
} |