Laya shader模板

/*
 * @Description: 
 * @Version: 2.0
 * @Autor: lincon-kong
 * @Date: 2021-10-12 15:35:41
 */
export class WaterMaterial extends Laya.Material {
    public readonly MAIN_TEX: number = Laya.Shader3D.propertyNameToID("u_AlbedoTexture");
    constructor() {
        super();
        this.setShaderName("Water");
        this.albedoColor = new Laya.Vector4(1.0, 1.0, 1.0, 1.0);
        Laya.loader.load("res/images/water/water.jpg", Laya.Handler.create(this, () => {
            this.albedoTexture = Laya.loader.getRes("res/images/water/water.jpg");
        }));
    }
    set albedoTexture(value) {
        if (value)
            this._shaderValues.addDefine(Laya.UnlitMaterial.SHADERDEFINE_ALBEDOTEXTURE);
        else
            this._shaderValues.removeDefine(Laya.UnlitMaterial.SHADERDEFINE_ALBEDOTEXTURE);
        this._shaderValues.setTexture(Laya.UnlitMaterial.ALBEDOTEXTURE, value);
    }

    set albedoColor(value) {
        this._shaderValues.setVector(Laya.UnlitMaterial.ALBEDOCOLOR, value);
    }

    static initShader() {
        let attributeMap = {
            'a_Position': Laya.VertexMesh.MESH_POSITION0,
            'a_Texcoord0': Laya.VertexMesh.MESH_TEXTURECOORDINATE0,
            'a_MvpMatrix': Laya.VertexMesh.MESH_MVPMATRIX_ROW0
        };
        let uniformMap = {
            'u_AlbedoTexture': Laya.Shader3D.PERIOD_MATERIAL,
            'u_AlbedoColor': Laya.Shader3D.PERIOD_MATERIAL,
            'u_MvpMatrix': Laya.Shader3D.PERIOD_SPRITE,
        };

        let vs = `
        #include "Lighting.glsl";
        attribute vec4 a_Position;
        attribute vec2 a_Texcoord0;
        varying vec2 v_Texcoord0;

        #ifdef GPU_INSTANCE
            attribute mat4 a_MvpMatrix;
        #else
            uniform mat4 u_MvpMatrix;
        #endif
        void main() {
            v_Texcoord0=a_Texcoord0;

            #ifdef GPU_INSTANCE
                gl_Position = a_MvpMatrix * a_Position;
            #else
                gl_Position = u_MvpMatrix * a_Position;
            #endif

            gl_Position=remapGLPositionZ(gl_Position);
        }
        `

        let ps = `
        #ifdef GL_FRAGMENT_PRECISION_HIGH
        precision highp float;
        #else
        precision mediump float;
        #endif
        
        uniform sampler2D u_AlbedoTexture;
        varying vec2 v_Texcoord0;
        uniform vec4 u_AlbedoColor;
        void main()
        {
            vec4 color =  u_AlbedoColor;
            color *= texture2D(u_AlbedoTexture, v_Texcoord0);
            gl_FragColor = color;
        }
        `

        var shader: Laya.Shader3D = Laya.Shader3D.add("Water", null, null, true);
        var subShader: Laya.SubShader = new Laya.SubShader(attributeMap, uniformMap);
        shader.addSubShader(subShader);
        subShader.addShaderPass(vs, ps);
    }
}






  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值