[LayaAir 2.0]分享水波一个shader

最近游戏做完了,等UI出界面,所以比较清闲,分享一个练手的shader
import BaseMaterial = Laya.BaseMaterial;
import Shader3D = Laya.Shader3D;
import SubShader = Laya.SubShader;
import BaseTexture = Laya.BaseTexture;
export default class Test1Material extends BaseMaterial
{
public readonly MAIN_TEX:number = Shader3D.propertyNameToID("u_MainTex");
public readonly NOISE_TEX:number = Shader3D.propertyNameToID("u_NoiseTex");
public readonly Time:number = Shader3D.propertyNameToID("u_Time"); 
constructor()
{
super(); 
var vs:string = `
attribute vec4 a_Position;
attribute vec2 a_Textcoord;
attribute vec3 a_Normal;
uniform mat4 u_WorldMat;
uniform mat4 u_MvpMatrix;
 
varying vec2 v_textcoord;
varying vec3 v_normal;
varying vec4 v_wpos;
 
void main()
{
v_textcoord = a_Textcoord;
v_normal = a_Normal;
v_wpos = u_WorldMat * a_Position;
gl_Position = u_MvpMatrix * a_Position;
}`;
var ps:string = `
#ifdef HIGHPRECISION
precision highp float;
#else
precision mediump float;
#endif 
#include "Lighting.glsl";
 
uniform sampler2D u_MainTex;
uniform sampler2D u_NoiseTex;
uniform vec3 u_CameraPos;
uniform vec3 u_AmbientColor;
uniform DirectionLight u_DirectionLight;
uniform float u_Time; 
varying vec2 v_textcoord;
varying vec3 v_normal;
varying vec4 v_wpos;
void main()
{
float _NoiseSpeedX = 1.0;
float _NoiseSpeedY = 1.0;
float _NoiseScaleX = 0.2;
float _NoiseScaleY = 0.2;
float _SpecularGlossy = 0.16;
float _SpecularIntensity = 0.5;
float _NoiseBrightOffset = 0.25;
vec2 tuv1 = v_textcoord + vec2(u_Time * _NoiseSpeedX,0);
vec2 tuv2 = v_textcoord + vec2(0,u_Time * _NoiseSpeedY);
vec2 ouvxy = vec2(texture2D(u_NoiseTex,tuv1).r,texture2D(u_NoiseTex,tuv2).r);
ouvxy -= _NoiseBrightOffset;
ouvxy *= vec2(_NoiseScaleX, _NoiseScaleY); 
vec4 col = texture2D(u_MainTex,v_textcoord + ouvxy);
vec3 normal = v_normal;
normal.xy += ouvxy.xy; 
vec3 L = normalize(u_DirectionLight.Direction.xyz);
vec3 N = normalize(normal);
float LdotN = dot(L, N);
vec3 diffuse = col.rgb; 
 
vec3 specular;
vec3 V = normalize(u_CameraPos.xyz - v_wpos.xyz);
vec3 H = normalize(L + V);
if (LdotN > 0.0)
{
float HdotN = max(0.0, dot(H, N)); // blinn-phone
specular = u_DirectionLight.Color.rgb * pow(HdotN, _SpecularGlossy * 100.0) * _SpecularIntensity;

 
vec3 ambient = u_AmbientColor.rgb; 
gl_FragColor = vec4(diffuse + specular + ambient, 1.0);
}
`; 
var attributeMap:object =
{
"a_Position":Laya.VertexMesh.MESH_POSITION0,
"a_Normal":Laya.VertexMesh.MESH_NORMAL0,
"a_Textcoord":Laya.VertexMesh.MESH_TEXTURECOORDINATE0
}; 
var uniformMap:object =
{
"u_WorldMat":Shader3D.PERIOD_SPRITE,
'u_MvpMatrix': Shader3D.PERIOD_SPRITE,
'u_DirectionLight.Direction': Shader3D.PERIOD_SCENE,
'u_DirectionLight.Color':Shader3D.PERIOD_SCENE,
'u_AmbientColor':Shader3D.PERIOD_SCENE,
'u_CameraPos': Shader3D.PERIOD_CAMERA,
'u_MainTex':Shader3D.PERIOD_MATERIAL,
'u_NoiseTex':Shader3D.PERIOD_MATERIAL,
'u_Time':Shader3D.PERIOD_MATERIAL,
 

var shader:Shader3D = Shader3D.add("Test1",true);
var subShader:SubShader = new SubShader(attributeMap,uniformMap);
shader.addSubShader(subShader);
subShader.addShaderPass(vs,ps); 
this.setShaderName("Test1");

set mainTex(value:BaseTexture)
{
this._shaderValues.setTexture(this.MAIN_TEX,value);

set noiseTex(value:BaseTexture)
{
this._shaderValues.setTexture(this.NOISE_TEX,value);

set time(value:number)
{
this._shaderValues.setNumber(this.Time,value);
}
}
shader.gif
已邀请:

Chuan

赞同来自:

大赞

qzzz

赞同来自:

牛逼了,还能先于美工做完....我一般都是反过来

无雨之地

赞同来自:

赞,mark一下。

137*****057

赞同来自:

牛!请问运行效率怎么样,会不会卡呢?

sheen

赞同来自:

打钱打钱打钱

_Nodep_

赞同来自:

高手!!!!!!!

要回复问题请先

商务合作
商务合作