分享一个Blinn-phong

自定义材质需要更新到 3.0 最新的版本,内部一些代码可以对比一下进行调整

3.0没mac版本吗?

有啊,直接用 dashboard 下载就好

大佬帮忙看下,我这边看了,看不出3.0要怎么改呢?

我调了一个版本,但是还是不行,Blinn-phong.rar (2.0 KB) ,麻烦帮忙看一下:
// Copyright © 2017-2018 Xiamen Yaji Software Co., Ltd.
CCEffect %{
techniques:

  • name: opaque
    passes:
    • vert: unlit-vs:vert
      frag: unlit-fs:frag
      properties: &props
      mainTexture: { value: grey }
      tilingOffset: { value: [1.0, 1.0, 0.0, 0.0] }
      mainColor: { value: [1.0, 1.0, 1.0, 1.0], editor: { type: color } }
      colorScale: { value: [1.0, 1.0, 1.0], target: colorScaleAndCutoff.xyz }
      alphaThreshold: { value: 0.5, target: colorScaleAndCutoff.w, editor: { parent: USE_ALPHA_TEST } }
      color: { target: mainColor, editor: { visible: false } } # backward compability
      specularGloss: { value: 20.0 }
      ambientColor: { value: [0.1, 0.1, 0.1, 1.0], editor: { type: color } }
      diffuseColor: { value: [1.0, 1.0, 1.0, 1.0], editor: { type: color } }
      specularColor: { value: [1.0, 1.0, 1.0, 1.0], editor: { type: color } }
      migrations: &migs
      properties:
      mainColor: { formerlySerializedAs: color }
    • vert: shadow-caster-vs:vert
      frag: shadow-caster-fs:frag
      phase: shadow-add
      propertyIndex: 0
      rasterizerState:
      cullMode: back
  • name: transparent
    passes:
    • vert: unlit-vs:vert
      frag: unlit-fs:frag
      depthStencilState: &d1
      depthTest: true
      depthWrite: false
      blendState:
      targets:
      • blend: true
        blendSrc: src_alpha
        blendDst: one_minus_src_alpha
        blendDstAlpha: one_minus_src_alpha
        properties: *props
        migrations: *migs
  • name: add
    passes:
    • vert: unlit-vs:vert
      frag: unlit-fs:frag
      rasterizerState: &r1 { cullMode: none }
      depthStencilState: *d1
      blendState:
      targets:
      • blend: true
        blendSrc: src_alpha
        blendDst: one
        blendSrcAlpha: src_alpha
        blendDstAlpha: one
        properties: *props
        migrations: *migs
  • name: alpha-blend
    passes:
    • vert: unlit-vs:vert
      frag: unlit-fs:frag
      rasterizerState: *r1
      depthStencilState: *d1
      blendState:
      targets:
      • blend: true
        blendSrc: src_alpha
        blendDst: one_minus_src_alpha
        blendSrcAlpha: src_alpha
        blendDstAlpha: one_minus_src_alpha
        properties: *props
        migrations: *migs
        }%

CCProgram unlit-vs %{
precision highp float;
#include
#include
#include

#if USE_VERTEX_COLOR
in vec4 a_color;
out vec4 v_color;
#endif

#if USE_TEXTURE

out vec2 v_uv;
uniform TexCoords {
  vec4 tilingOffset;
};

#endif

out vec3 v_worldNormal;
out vec3 v_worldPos;

vec4 vert () {
StandardVertInput In;
CCVertInput(In);

mat4 matWorld, matWorldIT;
CCGetWorldMatrixFull(matWorld, matWorldIT);

vec4 pos = matWorld * In.position;
v_worldPos = pos.xyz;
v_worldNormal = normalize((matWorldIT * vec4(In.normal, 0.0)).xyz);

#if USE_TEXTURE
  v_uv = a_texCoord;
  #if FLIP_UV
    v_uv.y = 1.0 - v_uv.y;
  #endif
  v_uv = v_uv * tilingOffset.xy + tilingOffset.zw;
#endif

#if USE_VERTEX_COLOR
  v_color = a_color;
#endif

// return cc_matProj * (cc_matView * matWorld) * In.position;
return cc_matProj * (cc_matView * (matWorld * In.position));

}
}%

CCProgram unlit-fs %{
precision mediump float;
#include

#if USE_ALPHA_TEST
#pragma define ALPHA_TEST_CHANNEL options([a, r, g, b])
#endif

#if USE_TEXTURE
in vec2 v_uv;
uniform sampler2D mainTexture;
#endif

uniform Constant {
vec4 mainColor;
vec4 colorScaleAndCutoff;
vec4 ambientColor;
vec4 diffuseColor;
vec4 specularColor;
float specularGloss;
};

#if USE_VERTEX_COLOR
in vec4 v_color;
#endif

in vec3 v_worldNormal;
in vec3 v_worldPos;

vec4 frag () {
vec4 o = mainColor;
o.rgb *= colorScaleAndCutoff.xyz;

#if USE_VERTEX_COLOR
  o *= v_color;
#endif

#if USE_TEXTURE
  o *= texture(mainTexture, v_uv);
#endif

#if USE_ALPHA_TEST
  if (o.ALPHA_TEST_CHANNEL < colorScaleAndCutoff.w) discard;
#endif

// 1.ambientColor
vec3 ambient = ambientColor.rgb;
// 2.diffuseColor
vec3 N =  normalize(v_worldNormal);
vec3 L = normalize(cc_mainLitDir.xyz * -1.0);
vec3 diffuse = cc_mainLitColor.rgb * diffuseColor.rgb * max(0.0, dot(N,L));
// 3.specularColor
vec3 V = normalize(cc_cameraPos.xyz - v_worldPos.xyz);
// H替代了Phong中的reflectDic = normalize(reflect(-L,N));
vec3 H = normalize(L+V);
// 这里需要注意的是计算高光反射的时候使用的是【半角向量 H】和法向量的点积
vec3 specular = cc_mainLitColor.rgb * specularColor.rgb * pow(max(0.0, dot(H,N)), specularGloss);

o.rgb *= (ambient + diffuse + specular);

return CCFragOutput(o);

}
}%

CCProgram shared-ubos { uniform Constants { vec4 tilingOffset; vec4 albedo; vec4 albedoScaleAndCutoff; vec4 pbrParams; vec4 emissive; vec4 emissiveScaleParam; }; }

CCProgram shadow-caster-vs %{
precision highp float;
#include
#include
#include
#include

#if HAS_SECOND_UV || USE_LIGHTMAP
in vec2 a_texCoord1;
#endif

out vec2 v_uv;
out vec2 v_uv1;

out float v_clip_depth;

vec4 vert () {
StandardVertInput In;
CCVertInput(In);

mat4 matWorld, matWorldIT;
CCGetWorldMatrixFull(matWorld, matWorldIT);

vec4 worldPos = matWorld * In.position;
vec4 clipPos = cc_matLightViewProj * worldPos;

v_uv = a_texCoord * tilingOffset.xy + tilingOffset.zw;
#if HAS_SECOND_UV
  v_uv1 = a_texCoord1 * tilingOffset.xy + tilingOffset.zw;
#endif

v_clip_depth = clipPos.z / clipPos.w * 0.5 + 0.5;

return clipPos;

}
}%

CCProgram shadow-caster-fs %{
precision highp float;
#include
#include

in vec2 v_uv;
in vec2 v_uv1;
in float v_clip_depth;

#if USE_ALBEDO_MAP
uniform sampler2D albedoMap;
#pragma define ALBEDO_UV options([v_uv, v_uv1])
#endif

#if USE_ALPHA_TEST
#pragma define ALPHA_TEST_CHANNEL options([a, r])
#endif

vec4 frag () {
vec4 baseColor = albedo;

#if USE_ALBEDO_MAP
  baseColor *= texture(albedoMap, ALBEDO_UV);
#endif

#if USE_ALPHA_TEST
  if (baseColor.ALPHA_TEST_CHANNEL < albedoScaleAndCutoff.w) discard;
#endif

return packDepthToRGBA(v_clip_depth);

}
}%


临时改了一下
Blinn-phong.effect.zip (2.4 KB)

1赞


这里的blinn-phong也是支持阴影的

好的,谢谢,大佬!

这个发现一个问题,地板接受阴影的无法使用Blinn-phong,不然阴影会无法显示。切换到standard就可以显示阴影,用1.2的那个Blinn-phong也是无法显示阴影~

这个还没处理,过几天我再研究一下。

好的,麻烦你了~

Blinn-phong.effect.zip (2.5 KB)

OK了试试吧Blinn-phong.effect.zip (2.5 KB)

好的,感谢大佬~

研究一下呢。

BlinnPhong.zip (2.6 KB)
Techniques只有opaque, 更新一个, 支持opaque, transparent, add, alpha-blend

顶下,希望官方考虑加入多一些适用于小游戏性能的基本shader。

楼主,有时间能更新下支持cocos 3d 3.1版本吗?目前发现不兼容

我这边还没升级到3.1,想等到版本相对稳定再升级

Blinn-phong.zip (2.1 KB)
我丢到3.1没看到报错呀