mirror of
https://github.com/cocos/cocos4.git
synced 2026-12-03 09:32:38 +08:00
Add adv mesh functionalities. (#16009)
* stage * update * update * fix physics & ray cast problem * fix default value, clean unused code, fix lint errors * clean code * update meta file * update external, license * fix tidy, circular dep * fix type cast * rm unit test * fix unit test * update comment
This commit is contained in:
@@ -26,6 +26,7 @@
|
||||
#include "3d/assets/Morph.h"
|
||||
#include "3d/assets/Skeleton.h"
|
||||
#include "3d/misc/BufferBlob.h"
|
||||
#include "3d/misc/CreateMesh.h"
|
||||
#include "base/std/hash/hash.h"
|
||||
#include "core/DataView.h"
|
||||
#include "core/assets/RenderingSubMesh.h"
|
||||
@@ -102,7 +103,13 @@ DataReaderCallback getReader(const DataView &dataView, gfx::Format format) {
|
||||
break;
|
||||
}
|
||||
case gfx::FormatType::FLOAT: {
|
||||
return [&](uint32_t offset) -> TypedArrayElementType { return dataView.getFloat32(offset); };
|
||||
switch (stride) {
|
||||
case 2: return [&](uint32_t offset) -> TypedArrayElementType { return dataView.getUint16(offset); };
|
||||
case 4: return [&](uint32_t offset) -> TypedArrayElementType { return dataView.getFloat32(offset); };
|
||||
default:
|
||||
break;
|
||||
}
|
||||
break;
|
||||
}
|
||||
default:
|
||||
break;
|
||||
@@ -159,7 +166,13 @@ DataWritterCallback getWriter(DataView &dataView, gfx::Format format) {
|
||||
break;
|
||||
}
|
||||
case gfx::FormatType::FLOAT: {
|
||||
return [&](uint32_t offset, const TypedArrayElementType &value) { dataView.setFloat32(offset, ccstd::get<float>(value)); };
|
||||
switch (stride) {
|
||||
case 2: return [&](uint32_t offset, const TypedArrayElementType &value) { dataView.setUint16(offset, ccstd::get<uint16_t>(value)); };
|
||||
case 4: return [&](uint32_t offset, const TypedArrayElementType &value) { dataView.setFloat32(offset, ccstd::get<float>(value)); };
|
||||
default:
|
||||
break;
|
||||
}
|
||||
break;
|
||||
}
|
||||
default:
|
||||
break;
|
||||
@@ -293,6 +306,15 @@ void Mesh::initialize() {
|
||||
|
||||
_initialized = true;
|
||||
|
||||
if (_struct.compressed) {
|
||||
// decompress
|
||||
MeshUtils::inflateMesh(_struct, _data);
|
||||
}
|
||||
if (_struct.encoded) {
|
||||
// decode
|
||||
MeshUtils::decodeMesh(_struct, _data);
|
||||
}
|
||||
|
||||
if (_struct.dynamic.has_value()) {
|
||||
auto *device = gfx::Device::getInstance();
|
||||
gfx::BufferList vertexBuffers;
|
||||
@@ -1301,7 +1323,13 @@ TypedArray Mesh::createTypedArrayWithGFXFormat(gfx::Format format, uint32_t coun
|
||||
break;
|
||||
}
|
||||
case gfx::FormatType::FLOAT: {
|
||||
return Float32Array(count);
|
||||
switch (stride) {
|
||||
case 2: return Uint16Array(count);
|
||||
case 4: return Float32Array(count);
|
||||
default:
|
||||
break;
|
||||
}
|
||||
break;
|
||||
}
|
||||
default:
|
||||
break;
|
||||
|
||||
@@ -196,6 +196,12 @@ public:
|
||||
* @zh 动态网格特有数据
|
||||
*/
|
||||
ccstd::optional<IDynamicStruct> dynamic;
|
||||
|
||||
ccstd::optional<bool> encoded;
|
||||
|
||||
ccstd::optional<bool> compressed;
|
||||
|
||||
ccstd::optional<bool> quantized;
|
||||
};
|
||||
|
||||
struct ICreateInfo {
|
||||
|
||||
@@ -23,12 +23,14 @@
|
||||
****************************************************************************/
|
||||
|
||||
#include "3d/misc/CreateMesh.h"
|
||||
#include <zlib.h>
|
||||
#include <algorithm>
|
||||
#include "3d/misc/Buffer.h"
|
||||
#include "3d/misc/BufferBlob.h"
|
||||
#include "core/ArrayBuffer.h"
|
||||
#include "core/DataView.h"
|
||||
#include "core/assets/RenderingSubMesh.h"
|
||||
#include "meshopt/meshoptimizer.h"
|
||||
#include "renderer/gfx-base/GFXDef-common.h"
|
||||
|
||||
namespace cc {
|
||||
@@ -404,4 +406,70 @@ Mesh::ICreateInfo MeshUtils::createDynamicMeshInfo(const IDynamicGeometry &geome
|
||||
return createInfo;
|
||||
}
|
||||
|
||||
void MeshUtils::inflateMesh(const Mesh::IStruct &structInfo, Uint8Array &data) {
|
||||
uLongf uncompressedSize = 0U;
|
||||
for (const auto &prim : structInfo.primitives) {
|
||||
uncompressedSize += prim.indexView->length;
|
||||
}
|
||||
for (const auto &vb : structInfo.vertexBundles) {
|
||||
uncompressedSize += vb.view.length;
|
||||
}
|
||||
auto uncompressedData = Uint8Array(static_cast<uint32_t>(uncompressedSize));
|
||||
auto res = uncompress(uncompressedData.buffer()->getData(), &uncompressedSize, data.buffer()->getData(), data.byteLength());
|
||||
data = uncompressedData;
|
||||
}
|
||||
|
||||
void MeshUtils::decodeMesh(Mesh::IStruct &structInfo, Uint8Array &data) {
|
||||
BufferBlob bufferBlob;
|
||||
|
||||
for (auto &bundle : structInfo.vertexBundles) {
|
||||
auto &view = bundle.view;
|
||||
auto bound = view.count * view.stride;
|
||||
auto *buffer = ccnew ArrayBuffer(bound);
|
||||
auto vertex = Uint8Array(data.buffer(), view.offset, view.length);
|
||||
int res = meshopt_decodeVertexBuffer(buffer->getData(), view.count, view.stride, vertex.buffer()->getData() + vertex.byteOffset(), view.length);
|
||||
if (res < 0) {
|
||||
assert(false && "failed to decode vertex buffer");
|
||||
}
|
||||
|
||||
bufferBlob.setNextAlignment(view.stride);
|
||||
Mesh::IVertexBundle vertexBundle;
|
||||
Mesh::IBufferView buffferView;
|
||||
buffferView.offset = bufferBlob.getLength();
|
||||
buffferView.length = bound;
|
||||
buffferView.count = view.count;
|
||||
buffferView.stride = view.stride;
|
||||
bufferBlob.addBuffer(buffer);
|
||||
|
||||
bundle.view = buffferView;
|
||||
}
|
||||
|
||||
for (auto &primitive : structInfo.primitives) {
|
||||
if (!primitive.indexView.has_value()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
auto view = *primitive.indexView;
|
||||
auto bound = view.count * view.stride;
|
||||
auto *buffer = ccnew ArrayBuffer(bound);
|
||||
auto index = DataView(data.buffer(), view.offset, view.length);
|
||||
int res = meshopt_decodeIndexBuffer(buffer->getData(), view.count, view.stride, index.buffer()->getData() + index.byteOffset(), view.length);
|
||||
if (res < 0) {
|
||||
assert(false && "failed to decode index buffer");
|
||||
}
|
||||
|
||||
bufferBlob.setNextAlignment(view.stride);
|
||||
Mesh::IBufferView buffferView;
|
||||
buffferView.offset = bufferBlob.getLength();
|
||||
buffferView.length = bound;
|
||||
buffferView.count = view.count;
|
||||
buffferView.stride = view.stride;
|
||||
bufferBlob.addBuffer(buffer);
|
||||
|
||||
primitive.indexView = buffferView;
|
||||
}
|
||||
|
||||
data = Uint8Array(bufferBlob.getCombined());
|
||||
}
|
||||
|
||||
} // namespace cc
|
||||
|
||||
@@ -87,6 +87,13 @@ public:
|
||||
* @zh 创建一个动态网格ICreateInfo。
|
||||
*/
|
||||
static Mesh::ICreateInfo createDynamicMeshInfo(const IDynamicGeometry &geometry, const ICreateDynamicMeshOptions &options = {});
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
static void inflateMesh(const Mesh::IStruct &structInfo, Uint8Array &data);
|
||||
|
||||
static void decodeMesh(Mesh::IStruct &structInfo, Uint8Array &data);
|
||||
};
|
||||
|
||||
} // namespace cc
|
||||
|
||||
@@ -28,6 +28,7 @@
|
||||
#include "3d/misc/Buffer.h"
|
||||
#include "core/DataView.h"
|
||||
#include "core/TypedArray.h"
|
||||
#include "math/Utils.h"
|
||||
#include "math/Vec3.h"
|
||||
#include "renderer/gfx-base/GFXBuffer.h"
|
||||
#include "renderer/gfx-base/GFXDevice.h"
|
||||
@@ -91,59 +92,118 @@ const IGeometricInfo &RenderingSubMesh::getGeometricInfo() {
|
||||
return EMPTY_GEOMETRIC_INFO;
|
||||
}
|
||||
|
||||
auto iter = std::find_if(_attributes.cbegin(), _attributes.cend(), [](const gfx::Attribute &element) -> bool {
|
||||
return element.name == gfx::ATTR_NAME_POSITION;
|
||||
});
|
||||
if (iter != _attributes.end()) {
|
||||
return EMPTY_GEOMETRIC_INFO;
|
||||
}
|
||||
|
||||
const auto &attri = *iter;
|
||||
const uint32_t count = gfx::GFX_FORMAT_INFOS[static_cast<uint32_t>(attri.format)].count;
|
||||
|
||||
auto index = static_cast<index_t>(_subMeshIdx.value());
|
||||
|
||||
const auto &positionsVar = _mesh->readAttribute(index, gfx::ATTR_NAME_POSITION);
|
||||
const auto *pPositions = ccstd::get_if<Float32Array>(&positionsVar);
|
||||
if (pPositions != nullptr) {
|
||||
const auto &positions = *pPositions;
|
||||
const auto &indicesVar = _mesh->readIndices(index);
|
||||
|
||||
Vec3 max;
|
||||
Vec3 min;
|
||||
|
||||
auto iter = std::find_if(_attributes.cbegin(), _attributes.cend(), [](const gfx::Attribute &element) -> bool {
|
||||
return element.name == gfx::ATTR_NAME_POSITION;
|
||||
});
|
||||
if (iter != _attributes.cend()) {
|
||||
const auto &attri = *iter;
|
||||
const uint32_t count = gfx::GFX_FORMAT_INFOS[static_cast<uint32_t>(attri.format)].count;
|
||||
if (count == 2) {
|
||||
max.set(positions[0], positions[1], 0);
|
||||
min.set(positions[0], positions[1], 0);
|
||||
} else {
|
||||
max.set(positions[0], positions[1], positions[2]);
|
||||
min.set(positions[0], positions[1], positions[2]);
|
||||
Float32Array const *pPositions = nullptr;
|
||||
switch (attri.format) {
|
||||
case gfx::Format::RG32F:
|
||||
case gfx::Format::RGB32F: {
|
||||
pPositions = ccstd::get_if<Float32Array>(&positionsVar);
|
||||
if (pPositions == nullptr) {
|
||||
return EMPTY_GEOMETRIC_INFO;
|
||||
}
|
||||
|
||||
for (int i = 0; i < positions.length(); i += static_cast<int>(count)) {
|
||||
if (count == 2) {
|
||||
max.x = positions[i] > max.x ? positions[i] : max.x;
|
||||
max.y = positions[i + 1] > max.y ? positions[i + 1] : max.y;
|
||||
min.x = positions[i] < min.x ? positions[i] : min.x;
|
||||
min.y = positions[i + 1] < min.y ? positions[i + 1] : min.y;
|
||||
} else {
|
||||
max.x = positions[i] > max.x ? positions[i] : max.x;
|
||||
max.y = positions[i + 1] > max.y ? positions[i + 1] : max.y;
|
||||
max.z = positions[i + 2] > max.z ? positions[i + 2] : max.z;
|
||||
min.x = positions[i] < min.x ? positions[i] : min.x;
|
||||
min.y = positions[i + 1] < min.y ? positions[i + 1] : min.y;
|
||||
min.z = positions[i + 2] < min.z ? positions[i + 2] : min.z;
|
||||
}
|
||||
break;
|
||||
}
|
||||
case gfx::Format::RGBA32F: {
|
||||
const auto *data = ccstd::get_if<Float32Array>(&positionsVar);
|
||||
if (data == nullptr) {
|
||||
return EMPTY_GEOMETRIC_INFO;
|
||||
}
|
||||
const auto count = data->length() / 4;
|
||||
auto *pos = ccnew Float32Array(count * 3);
|
||||
for (uint32_t i = 0; i < count; i++) {
|
||||
const auto dstPtr = i * 3;
|
||||
const auto srcPtr = i * 4;
|
||||
(*pos)[dstPtr] = (*data)[srcPtr];
|
||||
(*pos)[dstPtr + 1] = (*data)[srcPtr + 1];
|
||||
(*pos)[dstPtr + 2] = (*data)[srcPtr + 2];
|
||||
}
|
||||
pPositions = pos;
|
||||
break;
|
||||
}
|
||||
case gfx::Format::RG16F:
|
||||
case gfx::Format::RGB16F: {
|
||||
const auto *data = ccstd::get_if<Uint16Array>(&positionsVar);
|
||||
if (data == nullptr) {
|
||||
return EMPTY_GEOMETRIC_INFO;
|
||||
}
|
||||
auto *pos = ccnew Float32Array(data->length());
|
||||
for (uint32_t i = 0; i < data->length(); ++i) {
|
||||
(*pos)[i] = mathutils::halfToFloat((*data)[i]);
|
||||
}
|
||||
pPositions = pos;
|
||||
break;
|
||||
}
|
||||
case gfx::Format::RGBA16F: {
|
||||
const auto *data = ccstd::get_if<Uint16Array>(&positionsVar);
|
||||
if (data == nullptr) {
|
||||
return EMPTY_GEOMETRIC_INFO;
|
||||
}
|
||||
const auto count = data->length() / 4;
|
||||
auto *pos = ccnew Float32Array(count * 3);
|
||||
for (uint32_t i = 0; i < count; i++) {
|
||||
const auto dstPtr = i * 3;
|
||||
const auto srcPtr = i * 4;
|
||||
(*pos)[dstPtr] = mathutils::halfToFloat((*data)[srcPtr]);
|
||||
(*pos)[dstPtr + 1] = mathutils::halfToFloat((*data)[srcPtr + 1]);
|
||||
(*pos)[dstPtr + 2] = mathutils::halfToFloat((*data)[srcPtr + 2]);
|
||||
}
|
||||
pPositions = pos;
|
||||
break;
|
||||
}
|
||||
default:
|
||||
return EMPTY_GEOMETRIC_INFO;
|
||||
};
|
||||
|
||||
IGeometricInfo info;
|
||||
info.positions = positions;
|
||||
info.indices = indicesVar;
|
||||
info.boundingBox.max = max;
|
||||
info.boundingBox.min = min;
|
||||
const auto &positions = *pPositions;
|
||||
const auto &indicesVar = _mesh->readIndices(index);
|
||||
|
||||
_geometricInfo = info;
|
||||
return _geometricInfo.value();
|
||||
Vec3 max;
|
||||
Vec3 min;
|
||||
|
||||
if (count == 2) {
|
||||
max.set(positions[0], positions[1], 0);
|
||||
min.set(positions[0], positions[1], 0);
|
||||
} else {
|
||||
max.set(positions[0], positions[1], positions[2]);
|
||||
min.set(positions[0], positions[1], positions[2]);
|
||||
}
|
||||
|
||||
for (int i = 0; i < positions.length(); i += static_cast<int>(count)) {
|
||||
if (count == 2) {
|
||||
max.x = positions[i] > max.x ? positions[i] : max.x;
|
||||
max.y = positions[i + 1] > max.y ? positions[i + 1] : max.y;
|
||||
min.x = positions[i] < min.x ? positions[i] : min.x;
|
||||
min.y = positions[i + 1] < min.y ? positions[i + 1] : min.y;
|
||||
} else {
|
||||
max.x = positions[i] > max.x ? positions[i] : max.x;
|
||||
max.y = positions[i + 1] > max.y ? positions[i + 1] : max.y;
|
||||
max.z = positions[i + 2] > max.z ? positions[i + 2] : max.z;
|
||||
min.x = positions[i] < min.x ? positions[i] : min.x;
|
||||
min.y = positions[i + 1] < min.y ? positions[i + 1] : min.y;
|
||||
min.z = positions[i + 2] < min.z ? positions[i + 2] : min.z;
|
||||
}
|
||||
}
|
||||
|
||||
return EMPTY_GEOMETRIC_INFO;
|
||||
IGeometricInfo info;
|
||||
info.positions = positions;
|
||||
info.indices = indicesVar;
|
||||
info.boundingBox.max = max;
|
||||
info.boundingBox.min = min;
|
||||
|
||||
_geometricInfo = info;
|
||||
return _geometricInfo.value();
|
||||
}
|
||||
|
||||
void RenderingSubMesh::genFlatBuffers() {
|
||||
|
||||
@@ -50,6 +50,63 @@ float maxComponent(const Vec3 &v) {
|
||||
return std::max(std::max(v.x, v.y), v.z);
|
||||
}
|
||||
|
||||
uint16_t floatToHalf(float fval) {
|
||||
union {
|
||||
float f;
|
||||
unsigned int ui;
|
||||
} u = {fval};
|
||||
unsigned int ui = u.ui;
|
||||
|
||||
int s = (ui >> 16) & 0x8000; // NOLINT
|
||||
int em = ui & 0x7fffffff; // NOLINT
|
||||
|
||||
/* bias exponent and round to nearest; 112 is relative exponent bias (127-15) */
|
||||
int h = (em - (112 << 23) + (1 << 12)) >> 13;
|
||||
|
||||
/* underflow: flush to zero; 113 encodes exponent -14 */
|
||||
h = (em < (113 << 23)) ? 0 : h;
|
||||
|
||||
/* overflow: infinity; 143 encodes exponent 16 */
|
||||
h = (em >= (143 << 23)) ? 0x7c00 : h;
|
||||
|
||||
/* NaN; note that we convert all types of NaN to qNaN */
|
||||
h = (em > (255 << 23)) ? 0x7e00 : h;
|
||||
|
||||
return static_cast<uint16_t>(s | h);
|
||||
}
|
||||
|
||||
float halfToFloat(uint16_t hval) {
|
||||
union {
|
||||
float f;
|
||||
unsigned int ui;
|
||||
} u;
|
||||
|
||||
int s = (hval >> 15) & 0x00000001;
|
||||
int em = hval & 0x00007fff;
|
||||
int m = 0;
|
||||
|
||||
if (em > 0) {
|
||||
/* normalized */
|
||||
if (em > 30 << 10) {
|
||||
/* overflow: infinity */
|
||||
em = 255 << 23;
|
||||
} else {
|
||||
em = (em + (112 << 10)) << 13;
|
||||
}
|
||||
} else {
|
||||
/* denormalized */
|
||||
if (em > 25 << 10) {
|
||||
/* underflow: flush to zero */
|
||||
em = 0;
|
||||
} else {
|
||||
em = (em + (113 << 10)) >> 1;
|
||||
}
|
||||
}
|
||||
|
||||
u.ui = ((uint16_t)(s << 31)) | em | m; // NOLINT
|
||||
return u.f;
|
||||
}
|
||||
|
||||
} // namespace mathutils
|
||||
|
||||
} // namespace cc
|
||||
|
||||
@@ -284,5 +284,9 @@ auto absMax(F a, F b) {
|
||||
return std::fabs(a) > std::fabs(b) ? a : b;
|
||||
}
|
||||
|
||||
uint16_t floatToHalf(float fval);
|
||||
|
||||
float halfToFloat(uint16_t hval);
|
||||
|
||||
} // namespace mathutils
|
||||
} // namespace cc
|
||||
|
||||
@@ -3,6 +3,6 @@
|
||||
"type": "github",
|
||||
"owner": "cocos-creator",
|
||||
"name": "engine-native-external",
|
||||
"checkout": "v3.8.1-11"
|
||||
"checkout": "v3.8.1-12"
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -22,6 +22,7 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
****************************************************************************/
|
||||
#include <math.h>
|
||||
#include <stdlib.h>
|
||||
#include <vector>
|
||||
#include "cocos/math/Math.h"
|
||||
#include "cocos/math/MathUtil.h"
|
||||
|
||||
Reference in New Issue
Block a user