第2.1章:YAML配置详解
CCEffect是Cocos Shader系统的核心配置部分,采用YAML语法定义着色器的渲染技术、通道和属性。本章将深入讲解CCEffect配置的各个方面,帮你掌握着色器配置的精髓。
🎯 学习目标
通过本章学习,你将掌握:
- CCEffect配置的基本语法和结构
- 渲染技术(techniques)的配置方法
- 渲染通道(passes)的详细设置
- 材质属性(properties)的定义规则
- 渲染状态和混合模式的配置
💡 CCEffect基本结构
整体架构
CCEffect采用层次化的配置结构,从上到下分为:技术→通道→程序
1 2 3 4 5 6 7 8 9 10
| CCEffect: techniques: - name: opaque passes: - vert: vs-main frag: fs-main properties: migrations:
|
基础配置示例
1 2 3 4 5 6 7 8 9 10 11
| CCEffect: techniques: - name: opaque passes: - vert: standard-vs frag: standard-fs properties: &props mainTexture: { value: white } mainColor: { value: [1, 1, 1, 1] } metallic: { value: 0.0 } roughness: { value: 0.5 }
|
🛠️ 渲染技术(Techniques)配置
技术定义
渲染技术定义了不同的渲染方式,常见的技术包括:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| techniques: - name: opaque passes: - vert: standard-vs frag: opaque-fs - name: transparent passes: - vert: standard-vs frag: transparent-fs blendState: targets: - blend: true blendSrc: src_alpha blendDst: one_minus_src_alpha - name: shadow-caster passes: - vert: shadow-vs frag: shadow-fs rasterizerState: cullMode: front
|
技术选择机制
Cocos Creator会根据材质设置和渲染条件自动选择合适的技术:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
techniques: - name: opaque - name: transparent - name: cutout
|
🎨 渲染通道(Passes)配置
基本通道配置
每个渲染通道定义了一次完整的渲染过程:
1 2 3 4 5 6 7 8 9
| passes: - vert: vertex-shader-name frag: fragment-shader-name properties: &props phase: forward embeddedMacros: USE_TEXTURE: true propertyIndex: 0
|
渲染状态配置
深度状态(DepthStencilState)
1 2 3 4 5 6 7 8 9
| passes: - vert: vs-main frag: fs-main depthStencilState: depthTest: true depthWrite: true depthFunc: less stencilTestFront: false stencilTestBack: false
|
光栅化状态(RasterizerState)
1 2 3 4 5 6 7 8
| passes: - vert: vs-main frag: fs-main rasterizerState: cullMode: back polygonMode: fill shadeModel: gourand lineWidth: 1.0
|
混合状态(BlendState)
1 2 3 4 5 6 7 8 9 10 11 12 13
| passes: - vert: vs-main frag: fs-main blendState: targets: - blend: true blendSrc: src_alpha blendDst: one_minus_src_alpha blendSrcAlpha: one blendDstAlpha: one_minus_src_alpha blendEq: add blendAlphaEq: add colorMask: all
|
常用混合模式
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| blendState: targets: - blend: true blendSrc: src_alpha blendDst: one_minus_src_alpha
blendState: targets: - blend: true blendSrc: src_alpha blendDst: one
blendState: targets: - blend: true blendSrc: dst_color blendDst: zero
blendState: targets: - blend: true blendSrc: one_minus_dst_color blendDst: one
|
🔧 材质属性(Properties)定义
基本属性类型
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| properties: mainTexture: value: white editor: displayName: "主纹理" type: texture tooltip: "物体的主要纹理贴图" mainColor: value: [1, 1, 1, 1] editor: type: color tooltip: "物体的主要颜色" metallic: value: 0.0 range: [0, 1] editor: slide: true tooltip: "金属度" tilingOffset: value: [1, 1, 0, 0] editor: tooltip: "纹理的平铺和偏移"
|
高级属性配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
| properties: groupProperty: value: 1.0 editor: group: "材质参数" tooltip: "分组中的属性" normalTexture: value: normal editor: parent: USE_NORMAL_MAP tooltip: "法线贴图" blendMode: value: 0 editor: inspector: enum options: - "Normal" - "Additive" - "Multiply" - "Screen" tooltip: "混合模式选择" enableFeature: value: false editor: inspector: boolean tooltip: "启用特殊功能"
|
属性引用和继承
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| CCEffect: techniques: - name: opaque passes: - vert: vs-main frag: fs-opaque properties: &sharedProps mainTexture: { value: white } mainColor: { value: [1, 1, 1, 1] } metallic: { value: 0.0 } roughness: { value: 0.5 } - name: transparent passes: - vert: vs-main frag: fs-transparent properties: *sharedProps blendState: targets: - blend: true blendSrc: src_alpha blendDst: one_minus_src_alpha
|
📚 高级配置特性
版本迁移(Migrations)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| CCEffect: techniques: migrations: - version: "2.0" upgrade: | // 属性重命名 this.properties.mainColor = this.properties.oldColor || [1, 1, 1, 1]; delete this.properties.oldColor; // 新增属性 this.properties.roughness = this.properties.roughness || 0.5; - version: "3.0" upgrade: | // 纹理格式升级 if (this.properties.mainTexture === 'old-format') { this.properties.mainTexture = 'new-format'; }
|
动态宏定义
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| passes: - vert: vs-main frag: fs-main embeddedMacros: USE_TEXTURE: true MAX_LIGHTS: 8 QUALITY_LEVEL: 2 dynamics: - name: USE_NORMAL_MAP type: boolean value: false - name: SHADOW_QUALITY type: number value: 1 range: [0, 3]
|
条件编译配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| passes: - vert: vs-main frag: fs-main properties: mainTexture: { value: white } mainColor: { value: [1, 1, 1, 1] } normalTexture: value: normal editor: parent: USE_NORMAL_MAP normalScale: value: 1.0 range: [0, 2] editor: parent: USE_NORMAL_MAP slide: true
|
💡 实际应用示例
PBR材质配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
| CCEffect: techniques: - name: opaque passes: - vert: pbr-vs frag: pbr-fs properties: &pbrProps albedoMap: { value: white } albedoColor: { value: [1, 1, 1, 1], editor: { type: color } } metallicMap: { value: white } metallic: { value: 0.0, range: [0, 1], editor: { slide: true } } roughnessMap: { value: white } roughness: { value: 0.5, range: [0, 1], editor: { slide: true } } normalMap: { value: normal, editor: { parent: USE_NORMAL_MAP } } normalScale: { value: 1.0, range: [0, 2], editor: { parent: USE_NORMAL_MAP, slide: true } } aoMap: { value: white, editor: { parent: USE_AO_MAP } } aoIntensity: { value: 1.0, range: [0, 1], editor: { parent: USE_AO_MAP, slide: true } } dynamics: - name: USE_NORMAL_MAP type: boolean value: false - name: USE_AO_MAP type: boolean value: false - name: transparent passes: - vert: pbr-vs frag: pbr-fs properties: *pbrProps blendState: targets: - blend: true blendSrc: src_alpha blendDst: one_minus_src_alpha
|
卡通着色器配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| CCEffect: techniques: - name: opaque passes: - vert: toon-vs frag: toon-fs properties: mainTexture: { value: white } mainColor: { value: [1, 1, 1, 1], editor: { type: color } } shadowColor: { value: [0.5, 0.5, 0.5, 1], editor: { type: color } } shadowThreshold: { value: 0.5, range: [0, 1], editor: { slide: true } } shadowSmooth: { value: 0.1, range: [0, 1], editor: { slide: true } } outlineWidth: { value: 0.01, range: [0, 0.1], editor: { slide: true, parent: USE_OUTLINE } } outlineColor: { value: [0, 0, 0, 1], editor: { type: color, parent: USE_OUTLINE } } dynamics: - name: USE_OUTLINE type: boolean value: false - name: outline passes: - vert: outline-vs frag: outline-fs rasterizerState: cullMode: front properties: outlineWidth: { value: 0.01 } outlineColor: { value: [0, 0, 0, 1] }
|
📝 配置最佳实践
1. 属性组织
1 2 3 4 5 6 7 8 9 10 11 12 13
| properties: mainTexture: { value: white, editor: { group: "基础材质" } } mainColor: { value: [1, 1, 1, 1], editor: { group: "基础材质", type: color } } metallic: { value: 0.0, editor: { group: "PBR参数", slide: true } } roughness: { value: 0.5, editor: { group: "PBR参数", slide: true } } normalMap: { value: normal, editor: { group: "高级选项", parent: USE_NORMAL_MAP } } emissionMap: { value: black, editor: { group: "高级选项", parent: USE_EMISSION } }
|
2. 性能优化配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| techniques: - name: opaque-low passes: - vert: simple-vs frag: simple-fs embeddedMacros: QUALITY_LEVEL: 0 MAX_LIGHTS: 4 - name: opaque-high passes: - vert: advanced-vs frag: advanced-fs embeddedMacros: QUALITY_LEVEL: 2 MAX_LIGHTS: 16 ENABLE_SHADOWS: true
|
3. 平台适配
1 2 3 4 5 6 7 8 9 10 11
| passes: - vert: vs-main frag: fs-main embeddedMacros: PRECISION_LEVEL: | #ifdef GL_ES mediump #else highp #endif
|
📚 小结
本章深入介绍了CCEffect的YAML配置:
- CCEffect采用层次化结构:技术→通道→程序
- 渲染技术定义不同的渲染方式和条件
- 渲染通道配置具体的渲染状态和参数
- 材质属性系统支持丰富的类型和编辑器集成
- 高级特性包括版本迁移、动态宏、条件编译等
掌握YAML配置是开发高质量着色器的重要基础,接下来我们将学习GLSL语法的详细内容。
下一章: 第2.2章:GLSL基础语法
💡 学习建议
- 从简单开始:先掌握基本的technique和pass配置
- 实验属性:尝试不同的属性类型和编辑器选项
- 理解渲染状态:深入了解混合模式和深度测试
- 参考内置:查看引擎内置着色器的配置方式
🔗 参考资源
继续学习,你将成为Cocos Shader配置专家!