Laya 3.0 Shader 简单介绍

Laya 3.0 + Shader 简单介绍

Laya 升级到3.0 了,通过最近一段时间的源码阅读,对Laya 渲染有一些基础了解,于是记录一下Laya 3.0 Shader的一些使用教程,当前使用的是Laya 3.0.7版本,如果有不对的地方,请大家以官网最新版本为准。

瀑布流水效果图

在这里插入图片描述

参数设置

在这里插入图片描述

着色器实现

Shader3D Start
{
    type:Shader3D,
    name:MaskAdd,
    enableInstancing:true,
    supportReflectionProbe:true,
    uniformMap:{
        u_AlphaTestValue: { type: Float, default: 0.0, range: [0.0, 1.0] },
        u_TilingOffset: { type: Vector4, default: [1, 1, 0, 0], block: unlit },
        u_AlbedoColor: { type: Color, default: [1, 1, 1, 1], block: unlit },
        u_AlbedoTexture: { type: Texture2D, options: { define: "ALBEDOTEXTURE" } },
        u_AlbedoIntensity: { type: Float, default: 0.5 },
        
        u_DistortTexture: { type: Texture2D, options: { define: "DISTORTTEXTURE" } },
        u_TilingOffsetDistort: { type: Vector4, default: [1, 1, 0, 0], block: unlit }, 
        u_DistortTexUSpeed: { type: Float, default: 0.5 },
        u_DistortTexVSpeed: { type: Float, default: 0.5 },
        u_DistortStrength: { type: Float, default: 0.5 },
        
        u_Mask: { type: Texture2D, options: { define: "MASKTEXTURE" } },
        u_TilingOffsetMask: { type: Vector4, default: [1, 1, 0, 0], block: unlit },
        u_USpeed: { type: Float, default: 0.5 },
        u_VSpeed: { type: Float, default: 0.5 },

        u_Glow: { type: Float, default: 0.5, range: [0.0, 10.0] },
        u_ZOffset: { type: Float, default: 0.5 }
    },
    defines: {
        ENABLEVERTEXCOLOR: { type: bool, default: true },
        COLOR: { type: bool, default: true },
        UV: { type: bool, default: true }, 
        ENABLE_Z_OFFSET : { type: bool, default: false }, 
    },
    shaderPass:[
        { 
            pipeline:Forward,
            VS:unlitVS,
            FS:unlitPS, 
        }
    ]
}
Shader3D End

GLSL Start
#defineGLSL unlitVS

    #define SHADER_NAME MaskAdd

    #include "Math.glsl";

    #include "Scene.glsl";
    #include "SceneFogInput.glsl";

    #include "Camera.glsl";
    #include "Sprite3DVertex.glsl";

    #include "VertexCommon.glsl";

    #ifdef UV
    varying vec4 v_Texcoord0;
    #endif // UV

    #ifdef COLOR
    varying vec4 v_VertexColor; 
    #endif // COLOR
    
    void main()
    {
        Vertex vertex;
        getVertexParams(vertex);

    #ifdef UV
        v_Texcoord0.xy = vertex.texCoord0;
        v_Texcoord0.zw = transformUV(vertex.texCoord0, u_TilingOffsetMask);
    #endif // UV

    #ifdef COLOR
        v_VertexColor = vertex.vertexColor;   
    #endif // COLOR
        
        mat4 worldMat = getWorldMatrix();
        vec4 pos = (worldMat * vec4(vertex.positionOS, 1.0));
        vec3 positionWS = pos.xyz / pos.w;

        #if defined (ENABLE_Z_OFFSET)		
		    vec3 z_dir = normalize(positionWS.xyz - u_CameraPos);
            positionWS.xyz += z_dir * u_ZOffset; 
        #endif

        gl_Position = getPositionCS(positionWS);

        gl_Position = remapPositionZ(gl_Position);

    }
#endGLSL

#defineGLSL unlitPS

    #define SHADER_NAME MaskAdd

    #include "Color.glsl";

    #include "Scene.glsl";
    #include "SceneFog.glsl";

    #include "Camera.glsl";
    #include "Sprite3DFrag.glsl";

    #ifdef COLOR
    varying vec4 v_VertexColor; 
    #endif // COLOR
    
    #ifdef UV
    varying vec4 v_Texcoord0;
    #endif // UV 

    void main()
    { 
        vec2 uv = vec2(0.0);
        vec2 uv2 = vec2(0.); 
    #ifdef UV
        uv = v_Texcoord0.xy;
        uv2 = v_Texcoord0.zw; 
    #endif // UV 

        vec2 uvDistort = uv + vec2(u_DistortTexUSpeed, u_DistortTexVSpeed) * u_Time;
        
        vec4 distortTex = vec4(1.0);

        #ifdef DISTORTTEXTURE
            distortTex = texture2D(u_DistortTexture, transformUV(uvDistort, u_TilingOffsetDistort));
        #endif

        vec2 uvMainTex = uv + u_Time * vec2(u_USpeed, u_VSpeed) + vec2(distortTex.r * u_DistortStrength);
        
        vec4 mainTex = vec4(1.0);
        #ifdef ALBEDOTEXTURE
            mainTex = texture2D(u_AlbedoTexture, transformUV(uvMainTex, u_TilingOffset));
        #endif

        vec4 maskTex = vec4(1.0);
        #ifdef ALBEDOTEXTURE
            maskTex = texture2D(u_Mask, uv2);
        #endif
        
        vec3 color =  u_AlbedoColor.rgb * mainTex.rgb  * maskTex.rgb * u_Glow * u_AlbedoIntensity ;
        float alpha = 1.0;
        
    #ifdef COLOR 
        vec4 vertexColor = v_VertexColor; 
        color *= vertexColor.rgb ;
        alpha *= vertexColor.a; 
    #endif // COLOR

    #ifdef ALPHATEST
        if (alpha < u_AlphaTestValue)
            discard;
    #endif // ALPHATEST
        
        gl_FragColor = vec4(color, alpha);
        
        gl_FragColor = outputTransform(gl_FragColor);
    }
#endGLSL
GLSL End



有什么问题欢迎大家留言。

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值