半年多以前有个图片指定颜色变色的需求,因为不熟悉shader相关知识只能在网上找,当时用AI写的完全不能用,要么是直接报错要么就是和需求不搭边,然后通过谷歌、百度、github、论坛也没找到符合需求的,最后只能向技美朋友求助帮忙解决了,虽然需求并不复杂但是对于没有相关知识基础的人来说真是两眼一抹黑。
今天突然又想起之前的这个需求,结果用AI调教了几句就得到了符合需求的shader。这还是cocos相关的问题,对于unity和ue的匹配支持度估计更高…回头想想从ChatGPT发布到现在不过两年而已,发展速度真是日新月异。
下面提供下AI写的变色shader,tolerance为颜色匹配的容差值。
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
blendSrcAlpha: src_alpha
blendDstAlpha: one_minus_src_alpha
rasterizerState:
cullMode: none
properties:
alphaThreshold: { value: 0.5 }
targetColor: { value: [1.0, 0.0, 0.0, 1.0], editor: { type: color } }
replaceColor: { value: [0.0, 1.0, 0.0, 1.0], editor: { type: color } }
tolerance: {
value: 0.1,
editor: {
type: float,
range: [0, 1.0],
step: 0.001,
slide: true
}
}
}%
CCProgram sprite-vs %{
precision highp float;
#include <cc-global>
#if USE_LOCAL
#include <cc-local>
#endif
in vec3 a_position;
in vec2 a_texCoord;
in vec4 a_color;
out vec2 v_uv;
out vec4 v_color;
vec4 vert () {
vec4 pos = vec4(a_position, 1);
#if USE_LOCAL
pos = cc_matWorld * pos;
#endif
#if USE_PIXEL_ALIGNMENT
pos = cc_matView * pos;
pos.xyz = floor(pos.xyz);
pos = cc_matProj * pos;
#else
pos = cc_matViewProj * pos;
#endif
v_uv = a_texCoord;
v_color = a_color;
return pos;
}
}%
CCProgram sprite-fs %{
precision highp float;
#include <embedded-alpha>
#include <alpha-test>
in vec2 v_uv;
in vec4 v_color;
#pragma builtin(local)
layout(set = 2, binding = 11) uniform sampler2D cc_spriteTexture;
uniform Constant {
vec4 targetColor;
vec4 replaceColor;
float tolerance;
};
vec4 frag () {
vec4 o = texture(cc_spriteTexture, v_uv);
o *= v_color;
// 只在不透明的地方进行颜色替换
if (o.a > 0.0) {
float diff = length(o.rgb - targetColor.rgb);
if (diff < tolerance) {
float lerpFactor = 1.0 - (diff / tolerance);
o.rgb = mix(o.rgb, replaceColor.rgb, lerpFactor);
}
}
ALPHA_TEST(o);
return o;
}
}%
效果图



,希望能带动科技进一步突破,让大家都生活更便利。

当Ai程序继续变强的时候程序员就升级为代码审核,当ai能独立完成程序美术策划等任务时就升级为制作人探索独立开发3A游戏,当人人都能独立开发3A游戏就试试独立开发npu架构,当人人都能独立开发npu架构就挑战下独立完成1光年宇宙飞船设计,当人人都能造飞船可以探索独立发展星际商业,当人人都在宇宙星座开星际商场探索下能否探索四维世界,希望Ai发展的能再快一点能借Ai之力继续探索未知的未知,新的目标是星辰大海诗与远方