有偿求一个 shader,啊啊啊啊啊啊

有偿求一个方形转为圆形的shader(用于制作头像),适用于用于2.1.2版本

微信 15811337109

50元 rmb。。

新建effect:

// Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.  

// Note: Current format version is experiment, the format may be changed.
// The future format may not be compatible, you may need to update the script manually.

// 注意:当前版本的格式是实验性的,之后还会进行修改。
// 后续版本的格式不保证兼容当前格式,可能需要手动升级到最新版本。,
%{
  techniques: [
    {
      passes: [
        {
          vert: vs
          frag: fs
          cullMode: none
          blend: true
        }
      ]
      layer: 0
    }
  ]
  properties: {
    texture: {
      type: sampler2D
      value: null
    }
    alphaThreshold: {
      type: number
      value: 0.5
    }
// 纹理大小(实际纹理,合图)
    texturesize : {
      type: size,
      value: null
    }
// frame UV
    offset: {
      type: vec2,
      value: null
    },
// frame size
    framesize: {
      type: size,
      value: null,
    }
// 是否旋转
    rotate: {
      type: number,
      value: 0.0
    }

  }
%}

%% vs {

precision highp float;

uniform mat4 cc_matViewProj;

#if _USE_MODEL
  uniform mat4 cc_matWorld;
#endif

attribute vec3 a_position;
attribute lowp vec4 a_color;

#if USE_TEXTURE
  attribute mediump vec2 a_uv0;
  varying mediump vec2 v_uv0;
#endif

varying lowp vec4 v_color;

void main () {
  mat4 mvp;
  
  #if _USE_MODEL
    mvp = cc_matViewProj * cc_matWorld;
  #else
    mvp = cc_matViewProj;
  #endif

  #if USE_TEXTURE
    v_uv0 = a_uv0;
  #endif

  v_color = a_color;

  gl_Position = mvp * vec4(a_position, 1);
}

}

%% fs {

precision highp float;

#if USE_TEXTURE
  uniform sampler2D texture;
  varying mediump vec2 v_uv0;
#endif

#if USE_TEXTURE_SIZE
  uniform vec2 texturesize;
#endif

#if USE_UVCT
  uniform float rotate;
  uniform lowp vec2 offset;
  uniform lowp vec2 framesize;
#endif

uniform vec4 color;

#include <alpha-test>
varying lowp vec4 v_color;

float u_edge = 0.5;
float u_gap = u_edge * 0.02;

void main () {
  vec4 color = v_color;
  #if USE_TEXTURE
    color *= texture2D(texture, v_uv0);
    #if _USE_ETC1_TEXTURE
      color.a *= texture2D(texture, v_uv0 + vec2(0, 0.5)).r;
    #endif
  #endif
  ALPHA_TEST(color);

  float x,y, cx, cy, w, h, rw, rh;

  w = texturesize.x;
  h = texturesize.y;

  x = v_uv0.x * w;
  y = v_uv0.y * h;
  cx = (offset.x + 0.5 * framesize.x) * w;
  cy = (offset.y + 0.5 * framesize.y) * h;
  rw = framesize.x * w;
  rh = framesize.y * h;
  float minR = min(rw, rh);
  float edge = minR * u_edge;
  float gap = minR * u_gap;

  float dis = distance(vec2(x, y), vec2(cx, cy));
  if(dis > edge) {
    discard;
    
  }
  else {
      // gl_FragColor =vec4(color.rgb, framesize.x);
      float t = color.a;
      if(dis > edge - gap && t > 0.) {
        float b = smoothstep(0., gap, edge - dis);
        if(t > b) t = b;
      }
      gl_FragColor = vec4(color.rgb, t);

  }
}
}

新建材质,选择该effect.
然后自己在脚本里面给他传参数。

好人阿 ,落泪感谢

-使用xiao效果

@visow

大佬 我这么使用正确不,
为啥我给这个卡片使用这个 shader,卡片不见了。。。。

你为什么不用mask 可以实现一样的功能,性能还比shader好

@猫小小
mask 的 draw call 太多了

offset 和 framesize还是texturesize是spriteFrame的数据,主要是为了处理动态合图,

如果不考虑动态合图,可以给offset和framesize直接设置默认值 (0, 0), (1, 1), rotate false