zhuguoqing ff550d5d6a init
2022-05-22 10:32:02 +08:00

73 lines
2.0 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

const renderEngine = cc.renderer.renderEngine;
const renderer = renderEngine.renderer;
let ShaderMaterial = require('ShaderMaterial');
const shader = {
name: 'Grass',
params: [
{ name: 'u_cPos', type: renderer.PARAM_FLOAT2 },
// { name: 'u_mPos', type: renderer.PARAM_FLOAT2 },
// { name: 'u_width', type: renderer.PARAM_FLOAT },
// { name: 'u_height', type: renderer.PARAM_FLOAT },
{ name: 'u_dis', type: renderer.PARAM_FLOAT },
{ name: 'u_plus', type: renderer.PARAM_FLOAT },
],
start(material,params) {
material.setParamValue('u_cPos', cc.v2(0,0));
material.setParamValue('u_dis', params.distance);
material.setParamValue('u_plus', params.plus);
// material.setParamValue('u_mPos', cc.v2(params.pos.x,params.pos.y));//左下
// material.setParamValue('u_width', params.width);
// material.setParamValue('u_height', params.height);
},
defines:[],
frag:
`uniform sampler2D texture;
uniform vec4 color;
varying vec2 uv0;
varying vec2 uv1;
varying vec2 uv2;
varying vec2 uv3;
uniform vec2 u_cPos;
uniform float u_dis;
uniform float u_plus;
void main()
{
vec4 src_color = texture2D(texture, uv0).rgba;
vec2 rpos = vec2(uv2.x + uv3.x*uv1.x , uv2.y + uv3.y*uv1.y );
float dis = distance(rpos,u_cPos);
if(dis<u_dis){
// x大于等于t1=0.9时结果为0
// x小于等于t2=0.8时结果为1
float mask = smoothstep(0.8, 0.9, dis/u_dis);
if(mask < u_plus){
mask = u_plus;
}
src_color.a = src_color.a*mask;
// src_color += vec4(0.2,0.2,0.2,0.0);
}
gl_FragColor = src_color;
}`,
};
ShaderMaterial.addShader(shader);