需要改两个地方:
1. 新建一个 Spine3DDepth.vs 文件,放在引擎 spine/shader/files/ 目录下(和 Spine3D.vs 同级),内容是 ShadowCaster 专用的顶点着色器:
#define SHADER_NAME Spine3DDepthVS
#include "Math.glsl";
#include "Scene.glsl";
#include "Camera.glsl";
#include "Spine3DVertex.glsl";
#if defined(SHADOW) || defined(SHADOW_SPOT)
#ifndef DEPTHPASS
#include "ShadowCommon.glsl"
vec3 applyShadowBias(vec3 positionWS, vec3 normalWS, vec3 lightDirection)
{
float invNdotL = 1.0 - clamp(dot(-lightDirection, normalWS), 0.0, 1.0);
float scale = invNdotL * u_ShadowBias.y;
positionWS += -lightDirection * u_ShadowBias.xxx;
positionWS += normalWS * vec3(scale);
return positionWS;
}
#endif
#endif
vec4 DepthPositionCS(in vec3 positionWS, in vec3 normalWS)
{
#ifdef DEPTHPASS
vec4 positionCS = u_ViewProjection * vec4(positionWS, 1.0);
#endif
#ifdef SHADOW
#ifndef DEPTHPASS
positionWS = applyShadowBias(positionWS, normalWS, u_ShadowLightDirection);
vec4 positionCS = u_ViewProjection * vec4(positionWS, 1.0);
positionCS.z = max(positionCS.z, 0.0);
#endif
#endif
#ifdef SHADOW_SPOT
#ifndef DEPTHPASS
vec4 positionCS = u_ViewProjection * vec4(positionWS, 1.0);
positionCS.z = positionCS.z - u_ShadowBias.x / positionCS.w;
positionCS.z = max(positionCS.z, 0.0);
#endif
#endif
return positionCS;
}
#ifdef SPINE_BILLBOARD
uniform mat4 u_spineBillboardMatrix;
#endif
mat4 getWorldMatrix(){
#ifdef SPINE_BILLBOARD
return u_spineBillboardMatrix;
#else
#ifdef GPU_INSTANCE
mat4 worldMat = a_WorldMat;
#else
mat4 worldMat = u_WorldMat;
#endif
return worldMat;
#endif
}
void main()
{
Vertex vertex;
getVertexParams(vertex);
mat4 worldMat = getWorldMatrix();
vec4 pos = (worldMat * vec4(vertex.positionOS, 1.0));
vec3 positionWS = pos.xyz / pos.w;
vec3 normalWS = normalize((worldMat * vec4(vertex.normalOS, 0.0)).xyz);
vec4 positionCS = DepthPositionCS(positionWS, normalWS);
gl_Position = remapPositionZ(positionCS);
}
2. 修改 Spine3DShaderInit.ts,在 init() 方法里引入这个 vs 和引擎自带的 Depth.fs,然后注册 ShadowCaster pass:
// 文件顶部新增两行 import
import spine3DDepthVS from "./files/Spine3DDepth.vs";
import DepthFS from "../../d3/shader/depth/Depth.fs";
// init() 方法末尾,在 addShaderPass(spine3DVS, spine3DFS) 后面加一行
subShader3D.addShaderPass(spine3DDepthVS, DepthFS, "ShadowCaster");
改完重新编译引擎,勾选 Cast Shadow 就能投射阴影了。整体逻辑就是给 Spine3D shader 补一个 ShadowCaster 渲染 pass,和引擎其他 3D 对象的阴影实现方式一致。
以上由 AI 自动回复,仅供参考。若无异议将不再人工审查;如需人工介入请回帖说明。