[LayaAir3]laya shader多pass用深度测试方式实现遮挡显示的问题
// 我这样实现的一个, 遮挡部分显示红色,未遮挡部分显示正常纹理,但是总是两个pass同时生效,遮挡和正常效果同时出现
export class CustomMaterial extends Material {
static ALBEDOTEXTURE: number;
static __init__(): void {
CustomMaterial.ALBEDOTEXTURE = Shader3D.propertyNameToID("u_AlbedoTexture");
}
get albedoTexture(): BaseTexture {
return this.getTextureByIndex(CustomMaterial.ALBEDOTEXTURE);
}
set albedoTexture(value: BaseTexture) {
this.setTextureByIndex(CustomMaterial.ALBEDOTEXTURE, value);
}
static initShader(): void {
CustomMaterial.__init__();
const uniformMap = {
'u_AlbedoTexture': ShaderDataType.Texture2D
};
const customShader: Shader3D = Shader3D.add("CustomShader");
const subShader: SubShader = new SubShader(SubShader.DefaultAttributeMap, uniformMap);
customShader.addSubShader(subShader);
// Pass 1: Normal rendering
const normalVS = `
#include "Camera.glsl";
#include "Sprite3DVertex.glsl";
#include "VertexCommon.glsl";
varying vec2 v_Texcoord0;
void main() {
Vertex vertex;
getVertexParams(vertex);
mat4 worldMat = getWorldMatrix();
vec3 positionWS = (worldMat * vec4(vertex.positionOS, 1.0)).xyz;
gl_Position = getPositionCS(positionWS);
v_Texcoord0 = vertex.texCoord0;
gl_Position = remapPositionZ(gl_Position);
}
`;
const normalFS = `
#if defined(GL_FRAGMENT_PRECISION_HIGH)
precision highp float;
#else
precision mediump float;
#endif
varying vec2 v_Texcoord0;
void main() {
vec4 color = texture2D(u_AlbedoTexture, v_Texcoord0);
gl_FragColor = color;
}
`;
const pass1: ShaderPass = subShader.addShaderPass(normalVS, normalFS);
pass1.renderState.depthTest = RenderState.DEPTHTEST_LESS;
pass1.renderState.depthWrite = true;
// Pass 2: Render occluded parts in red
const occludedVS = normalVS; // Vertex shader can be the same
const occludedFS = `
#if defined(GL_FRAGMENT_PRECISION_HIGH)
precision highp float;
#else
precision mediump float;
#endif
void main() {
gl_FragColor = vec4(1.0, 0.0, 0.0, 0.3); // Red color for occluded parts
}
`;
const pass2: ShaderPass = subShader.addShaderPass(occludedVS, occludedFS);
pass2.renderState.depthTest = RenderState.DEPTHTEST_GREATER;
pass2.renderState.depthWrite = false;
}
constructor() {
super();
this.setShaderName("CustomShader");
}
}
没有找到相关结果
已邀请:
要回复问题请先登录
0 个回复