This commit is contained in:
2025-11-15 16:27:19 +08:00
parent 8db6f7b115
commit a0beb9eac7
35 changed files with 886 additions and 128 deletions
+15
View File
@@ -43,6 +43,19 @@ set(DV_UTILS_CAMERA_SOURCE_FILES
)
source_group("source/camera" FILES ${DV_UTILS_CAMERA_SOURCE_FILES})
#################
# standard asserts
#################
set(DV_UTILS_STANDARD_INCLUDE_FILES
include/standard/dvu_standard_material.h
)
source_group("include/standard" FILES ${DV_UTILS_STANDARD_INCLUDE_FILES})
set(DV_UTILS_STANDARD_SOURCE_FILES
source/standard/dvu_standard_material.cpp
)
source_group("source/standard" FILES ${DV_UTILS_STANDARD_SOURCE_FILES})
include_directories(
${DV_AUTO_INCLUDE_PATH}
${DV_CORE_INCLUDE_PATH}
@@ -58,6 +71,8 @@ add_library(dv_utils SHARED
${DV_UTILS_BUFFER_SOURCE_FILES}
${DV_UTILS_CAMERA_INCLUDE_FILES}
${DV_UTILS_CAMERA_SOURCE_FILES}
${DV_UTILS_STANDARD_INCLUDE_FILES}
${DV_UTILS_STANDARD_SOURCE_FILES}
)
target_link_libraries(dv_utils
@@ -0,0 +1,11 @@
#pragma once
#include "dvu_config.h"
#include "dvc_config.h"
#include "dvc_pre_declare.h"
DV_UTILS_BEGIN_NAMESPACE
davinci::MaterialPtr DV_UTILS_API create_standard_material(davinci::Device* device, const char* name);
DV_UTILS_END_NAMESPACE
+69 -14
View File
@@ -20,6 +20,8 @@
#include "buffer/dvu_dump_buffer.h"
#include "standard/dvu_standard_material.h"
#include <tiny_gltf.h>
#include <string>
@@ -614,6 +616,49 @@ davinci::fVector4 _getVector4FromDoubleArray(const std::vector<double>& doubleAr
return davinci::fVector4((float32_t)doubleArray[0], (float32_t)doubleArray[1], (float32_t)doubleArray[2], (float32_t)doubleArray[3]);
}
davinci::fMatrix3 _getTextureTransform(const tinygltf::ExtensionMap& gltfTextureInfoExternsionMap)
{
davinci::fMatrix3 textureTransform = davinci::fMatrix3::IDENTITY;
auto transformExternsions = gltfTextureInfoExternsionMap.find("KHR_texture_transform");
if (transformExternsions != gltfTextureInfoExternsionMap.end())
{
davinci::fVector2 scale = davinci::fVector2::ONE;
const tinygltf::Value& scaleArray = transformExternsions->second.Get("scale");
if (scaleArray.IsArray() && scaleArray.ArrayLen()>=2 &&
scaleArray.Get(0).IsNumber() && scaleArray.Get(1).IsNumber())
{
scale = davinci::fVector2(
(float32_t)(scaleArray.Get(0).GetNumberAsDouble()),
(float32_t)(scaleArray.Get(1).GetNumberAsDouble())
);
}
float32_t rotate = 0.f;
const tinygltf::Value& rotationValue = transformExternsions->second.Get("rotation");
if (rotationValue.IsNumber())
{
rotate = (float32_t)rotationValue.GetNumberAsDouble();
}
davinci::fVector2 offset = davinci::fVector2::ZERO;
const tinygltf::Value& offsetArray = transformExternsions->second.Get("offset");
if (offsetArray.IsArray() && offsetArray.ArrayLen()>=2 &&
offsetArray.Get(0).IsNumber() && offsetArray.Get(1).IsNumber())
{
offset = davinci::fVector2(
(float32_t)(offsetArray.Get(0).GetNumberAsDouble()),
(float32_t)(offsetArray.Get(1).GetNumberAsDouble())
);
}
textureTransform = davinci::fMatrix3::makeTrans(offset) *
davinci::fMatrix3::makeRotate(rotate) * davinci::fMatrix3::makeScale(scale);
}
return textureTransform;
}
bool _loadMaterial(tinygltf::Model& gltfModel, davinci::Device& device, davinci::Scene& scene)
{
for (size_t i = 0; i < gltfModel.materials.size(); i++)
@@ -621,26 +666,36 @@ bool _loadMaterial(tinygltf::Model& gltfModel, davinci::Device& device, davinci:
tinygltf::Material& gltfMaterial = gltfModel.materials[i];
//create new material
davinci::MaterialPtr materialPtr = device.newMaterialAsset();
davinci::MaterialPtr materialPtr = create_standard_material(&device, "debug:geometry_normal");
//apply matellic roughness info
const tinygltf::PbrMetallicRoughness& pbrMetllicRoughness = gltfMaterial.pbrMetallicRoughness;
const tinygltf::PbrMetallicRoughness& gltfPBRMetllicRoughness = gltfMaterial.pbrMetallicRoughness;
//base color texture
const tinygltf::TextureInfo& gltfBaseColorTextureInfo = gltfPBRMetllicRoughness.baseColorTexture;
materialPtr->setBaseColorTexture(
_getTexturePtr(gltfModel, device, pbrMetllicRoughness.baseColorTexture.index),
pbrMetllicRoughness.baseColorTexture.texCoord,
_getVector4FromDoubleArray(pbrMetllicRoughness.baseColorFactor)
);
materialPtr->setMetallicRoughnessTexture(
_getTexturePtr(gltfModel, device, pbrMetllicRoughness.metallicRoughnessTexture.index),
pbrMetllicRoughness.metallicRoughnessTexture.texCoord,
pbrMetllicRoughness.metallicFactor,
pbrMetllicRoughness.roughnessFactor
_getTexturePtr(gltfModel, device, gltfBaseColorTextureInfo.index),
gltfBaseColorTextureInfo.texCoord,
_getTextureTransform(gltfBaseColorTextureInfo.extensions),
_getVector4FromDoubleArray(gltfPBRMetllicRoughness.baseColorFactor)
);
//set normal texture info
//matallic & roughness texture
const tinygltf::TextureInfo& gltfMetalicRoughnessTextureInfo = gltfPBRMetllicRoughness.metallicRoughnessTexture;
materialPtr->setMetallicRoughnessTexture(
_getTexturePtr(gltfModel, device, gltfMetalicRoughnessTextureInfo.index),
gltfMetalicRoughnessTextureInfo.texCoord,
_getTextureTransform(gltfMetalicRoughnessTextureInfo.extensions),
gltfPBRMetllicRoughness.metallicFactor,
gltfPBRMetllicRoughness.roughnessFactor
);
//normal texture
const tinygltf::NormalTextureInfo& gltfNormalTextureInfo = gltfMaterial.normalTexture;
materialPtr->setNormalTexture(
_getTexturePtr(gltfModel, device, gltfMaterial.normalTexture.index),
gltfMaterial.normalTexture.texCoord,
_getTexturePtr(gltfModel, device, gltfNormalTextureInfo.index),
gltfNormalTextureInfo.texCoord,
_getTextureTransform(gltfNormalTextureInfo.extensions),
gltfMaterial.normalTexture.scale
);
@@ -0,0 +1,40 @@
#include "standard/dvu_standard_material.h"
#include "device/dvc_device.h"
#include "asset/dvc_material.h"
DV_UTILS_BEGIN_NAMESPACE
davinci::MaterialPtr create_standard_material(davinci::Device* device, const char* name)
{
if (strcmp(name, "debug:base_color")==0)
{
davinci::MaterialPtr materialPtr = device->newMaterialAsset();
materialPtr->setVertexShader("dv_standard_shaders.dll", "standard-normal+uv");
materialPtr->setFragementShader("dv_standard_shaders.dll", "debug:base_color");
return materialPtr;
}
else if (strcmp(name, "debug:normal_texture")==0)
{
davinci::MaterialPtr materialPtr = device->newMaterialAsset();
materialPtr->setVertexShader("dv_standard_shaders.dll", "standard-normal+uv");
materialPtr->setFragementShader("dv_standard_shaders.dll", "debug:normal_texture");
return materialPtr;
}
else if (strcmp(name, "debug:texture_coordinates")==0)
{
davinci::MaterialPtr materialPtr = device->newMaterialAsset();
materialPtr->setVertexShader("dv_standard_shaders.dll", "standard-normal+uv");
materialPtr->setFragementShader("dv_standard_shaders.dll", "debug:texture_coordinates");
return materialPtr;
}
else if (strcmp(name, "debug:geometry_normal")==0)
{
davinci::MaterialPtr materialPtr = device->newMaterialAsset();
materialPtr->setVertexShader("dv_standard_shaders.dll", "standard+normal-uv");
materialPtr->setFragementShader("dv_standard_shaders.dll", "debug:geometry_normal");
return materialPtr;
}
return nullptr;
}
DV_UTILS_END_NAMESPACE