// Copyright (c) 2017-2020 Xiamen Yaji Software Co., Ltd. CCEffect %{ techniques: - passes: - vert: sprite-vs:vert frag: sprite-fs:frag depthStencilState: depthTest: false depthWrite: false blendState: targets: - blend: true blendSrc: src_alpha blendDst: one_minus_src_alpha blendDstAlpha: one_minus_src_alpha rasterizerState: cullMode: none properties: alphaThreshold: { value: 0.5 } }% CCProgram sprite-vs %{ precision highp float; #include #if USE_LOCAL #include #endif in vec3 a_position; in vec2 a_texCoord; in vec4 a_color; out vec4 uv0; uniform sampler2D spineData; vec4 getWorldDatas( float id, vec4 pos){ float invSize = 1.0/128.0; float y = floor(id*4.0*invSize); float x = id*4.0 - y*128.0; mat4 world = mat4( texture(spineData, vec2((x + 0.5), y)*invSize), texture(spineData, vec2((x + 1.5), y)*invSize), texture(spineData, vec2((x + 2.5), y)*invSize), texture(spineData, vec2((x + 3.5), y)*invSize) ); uv0.w = world[0].w; world[0].w = 0.0; pos = world*pos; return pos; } out vec4 v_light; #if TWO_COLORED in vec4 a_color2; out vec4 v_dark; #endif vec4 vert () { vec4 pos = vec4(a_position.xy,0, 1); #if USE_LOCAL // pos = cc_matWorld * pos; #endif float id = floor(a_position.z*0.1); uv0.z = a_position.z - id*10.0; pos = getWorldDatas(id , pos); pos = cc_matViewProj * pos; uv0.xy = a_texCoord; v_light = a_color; #if TWO_COLORED v_dark = a_color2; #endif return pos; } }% CCProgram sprite-fs %{ precision highp float; #include in vec4 v_light; #if TWO_COLORED in vec4 v_dark; #endif in vec4 uv0; uniform sampler2D texture0; uniform sampler2D texture1; uniform sampler2D texture2; uniform sampler2D texture3; uniform sampler2D texture4; uniform sampler2D texture5; uniform sampler2D texture6; uniform sampler2D texture7; const vec4 HALF = vec4(0.5, 0.5, 0.5, 0.5); const vec4 ZERO = vec4(0.0, 0.0, 0.0, 0.0); const vec4 NUM03 = vec4(0.0, 1.0, 2.0, 3.0); const vec4 NUM47 = vec4(4.0, 5.0, 6.0, 7.0); vec4 multTexture(vec4 uv , vec4 o){ vec4 p0 = vec4(uv.z) - NUM03; vec4 p1 = vec4(uv.z) - NUM47; vec4 w0 = step(p0*p0,HALF); vec4 w1 = step(p1*p1,HALF); vec4 sampledColor = ZERO; if(uv.z < 1.5){ sampledColor += w0.x * texture(texture0, uv.xy*w0.x); sampledColor += w0.y * texture(texture1, uv.xy*w0.y); }else if(uv.z < 3.5){ sampledColor += w0.z * texture(texture2, uv.xy*w0.z); sampledColor += w0.w * texture(texture3, uv.xy*w0.w); }else if(uv.z < 5.5){ sampledColor += w1.x * texture(texture4, uv.xy*w1.x); sampledColor += w1.y * texture(texture5, uv.xy*w1.y); }else{ sampledColor += w1.z * texture(texture6, uv.xy*w1.z); sampledColor += w1.w * texture(texture7, uv.xy*w1.w); } return o*sampledColor; } //多选择模式(支持四种选择) vec4 mixColor(vec4 o , float shaderVar) { float type = floor(shaderVar); float scale = shaderVar - type; //插值 vec4 p0 = vec4(type) - vec4(1.0, 2.0, 3.0, 4.0); vec4 w0 = step(p0*p0,HALF); //选择分枝,权值的计算 //置灰vec4 float gray = 0.2126 * o.r + 0.7152 * o.g + 0.0722 * o.b; // 置灰*w. + 闪白*w.y + w.z待定 + w.w待定; vec3 result = vec3(gray)*w0.x + vec3(1) * w0.y; // + vec3(1.0,0.0,0.0)*w0.z + vec3(0.0,1.0,0.0)*w0.w; float w = (w0.x + w0.y + w0.z + w0.w)*scale; o.rgb = mix(o.rgb , result , w); return o; } vec4 frag () { vec4 uv = uv0; vec4 o = vec4(1, 1, 1, 1); #if TWO_COLORED vec4 texColor = vec4(1, 1, 1, 1); //texColor *= texture(cc_spriteTexture, uv0); texColor = multTexture(uv,texColor); o.a = texColor.a * v_light.a; o.rgb = ((texColor.a - 1.0) * v_dark.a + 1.0 - texColor.rgb) * v_dark.rgb + texColor.rgb * v_light.rgb; #else //o *= texture(cc_spriteTexture, uv0); o = multTexture(uv,o); o *= v_light; #endif #if USE_SHADER_VAR //自定义参数,多选择模式 o = mixColor(o,uv.w); #endif ALPHA_TEST(o); return o; } }%