update image material to support cubemaps

This commit is contained in:
Nick Fisher
2025-06-12 08:40:28 +08:00
parent e5bcde3ade
commit f252c86152
5 changed files with 3836 additions and 3052 deletions

View File

@@ -5,6 +5,10 @@ material {
type : sampler2d,
name : image
},
{
type : samplerCubemap,
name : cubeMap
},
{
type : mat4,
name : transform,
@@ -17,6 +21,14 @@ material {
{
type : int,
name : showImage
},
{
type : int,
name : isCubeMap
},
{
type : int,
name : cubeMapFace
}
],
variables : [
@@ -36,17 +48,48 @@ vertex {
}
fragment {
vec3 getDirectionForCubeFace(int face, vec2 uv) {
vec2 st = uv * 2.0 - 1.0; // Convert [0,1] to [-1,1]
if (face == 0) { // +X
return normalize(vec3(1.0, -st.y, -st.x));
} else if (face == 1) { // -X
return normalize(vec3(-1.0, -st.y, st.x));
} else if (face == 2) { // +Y
return normalize(vec3(st.x, 1.0, st.y));
} else if (face == 3) { // -Y
return normalize(vec3(st.x, -1.0, -st.y));
} else if (face == 4) { // +Z
return normalize(vec3(st.x, -st.y, 1.0));
} else { // -Z (face == 5)
return normalize(vec3(-st.x, -st.y, -1.0));
}
}
void material(inout MaterialInputs material) {
prepareMaterial(material);
highp vec2 uv = (materialParams.transform * vec4(saturate(variable_imageUV.st), 1.0, 1.0)).st;
prepareMaterial(material);
highp vec2 uv = (materialParams.transform * vec4(saturate(variable_imageUV.st), 1.0, 1.0)).st;
if (materialParams.showImage == 0 || uv.s > 1.0 || uv.s < 0.0 || uv.t < 0.0 || uv.t > 1.0) {
material.baseColor = materialParams.backgroundColor;
} else {
uv.t = 1.0 - uv.t;
vec4 color = max(texture(materialParams_image, uv.st), 0.0);
vec4 color;
if (materialParams.isCubeMap != 0) {
// Sample cubemap using direction vector for specified face
vec2 cubeUv = uv;
cubeUv.t = 1.0 - cubeUv.t; // Flip V coordinate
vec3 direction = getDirectionForCubeFace(materialParams.cubeMapFace, cubeUv);
color = max(texture(materialParams_cubeMap, direction), 0.0);
} else {
// Regular 2D texture sampling
uv.t = 1.0 - uv.t;
color = max(texture(materialParams_image, uv.st), 0.0);
}
color.rgb *= color.a;
// Manual, pre-multiplied srcOver with opaque destination optimization
material.baseColor.rgb = color.rgb + materialParams.backgroundColor.rgb * (1.0 - color.a);
}
}
}
}

View File

@@ -8,5 +8,5 @@ IMAGE_PACKAGE:
IMAGE_IMAGE_OFFSET:
.int 0
IMAGE_IMAGE_SIZE:
.int 63850
.int 78666

View File

@@ -8,5 +8,5 @@ _IMAGE_PACKAGE:
_IMAGE_IMAGE_OFFSET:
.int 0
_IMAGE_IMAGE_SIZE:
.int 63850
.int 78666

File diff suppressed because it is too large Load Diff