// Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd. CCEffect %{ techniques: - passes: - vert: vs frag: fs blendState: targets: - blend: true rasterizerState: cullMode: none properties: texture: { value: white } amplitude: { value: 0.05, range: [0.0, 0.5], editor: { tooltip: '振幅' } } angularVelocity: { value: 10, editor: { tooltip: '角速度' } } frequency: { value: 10, editor: { tooltip: '频率' } } offset: { value: 0.5, range: [0.0, 1.0], editor: { tooltip: '偏距' } } toLeft: { value: true, editor: { type: boolean, tooltip: '向左(方向)' } } }% CCProgram vs %{ precision highp float; #include #include in vec3 a_position; in vec4 a_color; out vec4 v_color; #if USE_TEXTURE in vec2 a_uv0; out vec2 v_uv0; #endif void main () { vec4 pos = vec4(a_position, 1); #if CC_USE_MODEL pos = cc_matViewProj * cc_matWorld * pos; #else pos = cc_matViewProj * pos; #endif #if USE_TEXTURE v_uv0 = a_uv0; #endif v_color = a_color; gl_Position = pos; } }% CCProgram fs %{ precision highp float; // 引入 Cocos Creator 内置的全部变量 #include #include // 顶点颜色(来自顶点着色器) in vec4 v_color; // 纹理 #if USE_TEXTURE in vec2 v_uv0; uniform sampler2D texture; #endif // 自定义属性 uniform Properties { float amplitude; // 振幅 float angularVelocity; // 角速度 float frequency; // 频率 float offset; // 偏距 bool toLeft; // 是否向左 }; void main () { // 保存顶点颜色 vec4 color = v_color; // 叠加纹理颜色 color *= texture(texture, v_uv0); // 直接丢弃原本就透明的像素 if (color.a == 0.0) discard; // 初相位(正值表现为向左移动,负值则表现为向右移动) // cc_time 是 Cocos Creator 提供的运行时间全局变量(类型:vec4) //float initiaPhase = frequency * cc_time.x; // 代入正弦曲线公式计算 y 值 // y = Asin(ωx ± φt) + k float y = amplitude * sin((angularVelocity * v_uv0.x) + ((frequency * cc_time.x) * (toLeft ? 1. : -1.))) + offset; //float y = amplitude * sin(angularVelocity * v_uv0.x + initiaPhase) + offset; // 丢弃 y 值以上的像素(左上角为原点 [0.0, 0.0]) if (v_uv0.y < y) discard; // 输出颜色 gl_FragColor = color; } }%