/*
* @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);
}
}
Laya shader模板
最新推荐文章于 2023-10-25 14:46:09 发布