Files
cup_edit/materials/vdtm.mat
2025-03-28 10:01:00 +08:00

99 lines
2.7 KiB
Plaintext

material {
name : ViewDependentTexture,
parameters : [
{
type : sampler3d,
name : perspectives,
precision: high
},
{
type : float3[3],
name : cameraForwardVectors
},
{
type : bool,
name : flipUVs
}
],
variables : [
{
type : float,
name : samplerCoord,
}
],
requires : [ position, uv0 ],
shadingModel : unlit,
doubleSided : false,
interpolation: flat,
blending: opaque,
depthWrite : true,
depthCulling : true,
culling: none,
vertexDomain: object
}
// View-Dependent Texture Mapping Shader
//
// This material projects a 3D texture containing multiple perspectives onto a mesh,
// blending between different views based on the current camera position.
//
// The 3D texture contains different renderings of the object from various camera positions.
// At runtime, we calculate the best blend of perspectives to use based on the current view.
//
// Parameters:
// - perspectives: 3D texture containing different perspective views of the object
// - cameraPositions: Array of camera positions used to render each perspective slice
vertex {
void materialVertex(inout MaterialVertexInputs material) {
vec3 forward = -(normalize(getWorldFromViewMatrix()[2].xyz));
vec3 weights = vec3(0.0, 0.0, 0.0);
int idxMax = 0;
float weightMax = 0.0;
for(int i = 0; i < 3; i++) {
weights[i] = dot(forward, materialParams.cameraForwardVectors[i]);
if(weights[i] > weightMax) {
weightMax = weights[i];
idxMax = i;
}
}
if(idxMax == 0) {
float z = (weights.y * 0.5) + (weights.z * 1.0);
}
//weights /= (weights.x + weights.y + weights.z);
material.samplerCoord.x = z;
}
}
fragment {
void useMax(vec3 forward) {
float maxIdx = 0.0f;
float maxWeight = 0.0f;
for(int i =0; i < 3; i++) {
float weight = dot(forward, materialParams.cameraForwardVectors[i]);
if(weight > maxWeight) {
maxWeight = weight;
maxIdx = float(i) / 2.0f;
}
}
float z = maxIdx;
}
void material(inout MaterialInputs material) {
prepareMaterial(material);
vec2 uv = getUV0();
if(materialParams.flipUVs) {
uv = uvToRenderTargetUV(uv);
}
vec3 texCoord = vec3(uv, variable_samplerCoord.x);
material.baseColor = texture(materialParams_perspectives, texCoord);
}
}