[LayaAir 2.0]获取相机renderTexture 作为阴影 的UV

已知:使用正交相机生成阴影贴图,顶底着色器中a_Position,
有个plane接受相机生成的阴影,通过阴影相机计算矩阵乘a_Position获取阴影UV, 混合渲染在plane上
 
顶点着色器:
 #if defined(_RECEIVESHADOWS)
        vec4 uv = u_ShadowLightMatrix * position;
        uv.xy /= uv.w;
v_shadow_data = uv;
    #endif片段着色器:
    gl_FragColor.rgb=saturate(texture2D(u_ShadowMapTex, v_shadow_data1.xy).rgb);
 
 ts代码设置u_ShadowMapTex
    onUpdate(){
        this.ShadowCamera.transform.lookAt(new Vector3(0,0,0), new Vector3(0,1,0), false);
        let v:Matrix4x4 = this.ShadowCamera.viewMatrix;
        let p:Matrix4x4 = this.ShadowCamera.projectionMatrix;
        let o:Matrix4x4 = new Matrix4x4(
            0.50000 ,0.00000,0.00000,0.50000,
            0.00000,0.50000,0.00000,0.50000,
            0.00000 ,0.00000,0.50000,0.50000,
            0.00000 ,0.00000,0.00000,1.00000
        )
        let VPO = this.mulMatrix4x4(v, p, o);
        for(let i = 0; i < this.Renders.length; i++)
        {
            let render :Laya.MeshSprite3D = this.Renders[i];//render为地面,显示阴影的对象
            let mat = this.mulMatrix4x4(render.transform.worldMatrix, VPO);
            ShaderManager.Instance.SetShadowLightMatrix(render, mat);
            ShaderManager.Instance.SetShadowMapTexture(render, this.RenderTexture);//RenderTexture为阴影相机的renderTarget
            ShaderManager.Instance.SetShadowColor(render, this.ShadowColor);
            ShaderManager.Instance.SetShadowBackfaceFactor(render, this.BackfaceFactor);
        }
    }
 
Unity上计算矩阵可以这样,可以做到阴影效果,如下代码
Matrix4x4.TRS(Vector3.one * 0.5f, Quaternion.identity, Vector3.one * 0.5f) * shadowCamera.projectionMatrix * shadowCamera.worldToCameraMatrix * plane.transform.localToWorldMatrix
 
在laya上相同公式,uv计算出来后不正确,看出问题的同学,帮忙指正下,谢谢!
已邀请:

Calvin

赞同来自:

终于找到了问题,原来时material 设置问题,写material代码时给自己挖了个坑

要回复问题请先

商务合作
商务合作