A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.
TypeScript 2d-game-engine3d3d-game-engineandroidcppecsgame-editorgame-enginegamedeviostypescriptwebassemblywebglwebgpuwechat-minigame
feat(pipeline): a hair material's flow map, shift map and tip softness are read from the model's material extras
yhh8e1f2921e759
diff --git a/docs/astro/src/content/docs/assets/models.mdx b/docs/astro/src/content/docs/assets/models.mdxindex c2948ae9a..3c8735d44 100644--- a/docs/astro/src/content/docs/assets/models.mdx+++ b/docs/astro/src/content/docs/assets/models.mdx@@ -214,7 +214,7 @@ replaces them — a model told to occlude itself keeps doing so through its mate## Character materials-A model's materials import on the Model shader. Turn on **Character Materials** (`characterMaterials: auto`) in the model's import settings and a material whose name says skin, face, head, hair or eye gets the Skin, Hair or Eye shader instead, with its normal map and its occlusion/roughness map bound to them. The hair's triangles are also put in the order a blended draw wants, the cards nearest the middle of the hair first, which is back to front from any side for a shell of cards around a head (a ponytail that leaves the shell is not ordered by it). A hair material is cut out by its alpha (`u_alphaCutoff`, 0.5 unless the source says otherwise) and drawn without blending, so its edges are feathered by coverage. The name is read word by word, so `BodySkin` and `Hair_A` match while `Eyelashes` and `Headband` do not, and the colour texture's file name counts too. A name is a convention, not a fact: look at what it chose in the materials the import writes, and change a shader by hand where it guessed wrong. Eyes need their iris maps (`u_depthMap`, `u_irisNormalMap`) assigned by hand, as the source has no slot for them. It is off by default, so re-importing never changes a material you did not ask about.+A model's materials import on the Model shader. Turn on **Character Materials** (`characterMaterials: auto`) in the model's import settings and a material whose name says skin, face, head, hair or eye gets the Skin, Hair or Eye shader instead, with its normal map and its occlusion/roughness map bound to them. The hair's triangles are also put in the order a blended draw wants, the cards nearest the middle of the hair first, which is back to front from any side for a shell of cards around a head (a ponytail that leaves the shell is not ordered by it). A hair material is cut out by its alpha (`u_alphaCutoff`, 0.5 unless the source says otherwise) and drawn without blending, so its edges are feathered by coverage. The name is read word by word, so `BodySkin` and `Hair_A` match while `Eyelashes` and `Headband` do not, and the colour texture's file name counts too. A name is a convention, not a fact: look at what it chose in the materials the import writes, and change a shader by hand where it guessed wrong. Eyes need their iris maps (`u_depthMap`, `u_irisNormalMap`) assigned by hand, as the source has no slot for them. Hair's strand-direction and highlight-shift maps have no glTF slot either, so a hair material names them in its extras (`flow_map` and `shift_map`, paths from the model, and `suggested_tipSoftness`) and the import binds them as `u_flowMap`, `u_shiftMap` and `u_tipSoftness`. It is off by default, so re-importing never changes a material you did not ask about.## Animationsdiff --git a/docs/astro/src/content/docs/zh-cn/assets/models.mdx b/docs/astro/src/content/docs/zh-cn/assets/models.mdxindex ba87bcab0..e1da609c8 100644--- a/docs/astro/src/content/docs/zh-cn/assets/models.mdx+++ b/docs/astro/src/content/docs/zh-cn/assets/models.mdx@@ -185,7 +185,7 @@ chart,每片投影到自己的平面,再按**同一个**世界-纹素比打包## 角色材质-模型的材质默认导入到 Model 着色器。在模型的导入设置里打开**角色材质**(`characterMaterials: auto`)后,名字里带 skin、face、head、hair 或 eye 的材质会改用 Skin、Hair 或 Eye 着色器,并绑上它的法线贴图和遮蔽/粗糙度贴图。头发的三角形还会按混合绘制需要的顺序重排,最靠近头发中心的发片排在最前,对围绕头部的一圈发片来说,从任何一侧看这都是由后向前(离开这一圈的马尾不受这个排序约束)。头发材质按 alpha 裁剪(`u_alphaCutoff`,源文件没说就取 0.5),并且不做混合绘制,边缘由覆盖率来柔化。名字是按单词读的,所以 `BodySkin`、`Hair_A` 会命中,`Eyelashes`、`Headband` 不会,颜色贴图的文件名也算。名字只是约定,不是事实:请看导入写出的材质选了什么,猜错的地方手动改着色器。眼睛的虹膜贴图(`u_depthMap`、`u_irisNormalMap`)需要手动指定,源文件里没有对应的位置。默认关闭,所以重新导入不会改动你没要求改的材质。+模型的材质默认导入到 Model 着色器。在模型的导入设置里打开**角色材质**(`characterMaterials: auto`)后,名字里带 skin、face、head、hair 或 eye 的材质会改用 Skin、Hair 或 Eye 着色器,并绑上它的法线贴图和遮蔽/粗糙度贴图。头发的三角形还会按混合绘制需要的顺序重排,最靠近头发中心的发片排在最前,对围绕头部的一圈发片来说,从任何一侧看这都是由后向前(离开这一圈的马尾不受这个排序约束)。头发材质按 alpha 裁剪(`u_alphaCutoff`,源文件没说就取 0.5),并且不做混合绘制,边缘由覆盖率来柔化。名字是按单词读的,所以 `BodySkin`、`Hair_A` 会命中,`Eyelashes`、`Headband` 不会,颜色贴图的文件名也算。名字只是约定,不是事实:请看导入写出的材质选了什么,猜错的地方手动改着色器。眼睛的虹膜贴图(`u_depthMap`、`u_irisNormalMap`)需要手动指定,源文件里没有对应的位置。头发的流向图和高光偏移图在 glTF 里同样没有位置,所以头发材质在它的 extras 里写出它们(`flow_map`、`shift_map`,路径相对于模型,以及 `suggested_tipSoftness`),导入时绑定为 `u_flowMap`、`u_shiftMap` 和 `u_tipSoftness`。默认关闭,所以重新导入不会改动你没要求改的材质。## 动画diff --git a/pipeline/src/assets/gltfImport.ts b/pipeline/src/assets/gltfImport.tsindex 32136bf24..57bf11c10 100644--- a/pipeline/src/assets/gltfImport.ts+++ b/pipeline/src/assets/gltfImport.ts@@ -143,6 +143,7 @@ interface GltfJson {emissiveTexture?: GltfTextureRef; emissiveFactor?: number[];alphaMode?: string; alphaCutoff?: number; doubleSided?: boolean;extensions?: Record<string, unknown>;+ extras?: Record<string, unknown>;}[];textures?: { source?: number; sampler?: number }[];samplers?: { magFilter?: number; minFilter?: number; wrapS?: number; wrapT?: number }[];@@ -617,6 +618,14 @@ function readMaterial(ctx: MaterialContext, textureCache: Map<number, ImportedIm// rough metal, and writing that out is what makes a product say so.const metallic = pbr.metallicFactor ?? 1;const roughness = pbr.roughnessFactor ?? 1;+ // Data maps a material names in its extras: files on disk, read as linear data and referenced where they lie.+ const extraMap = (key: string): ImportedImageRef | null => {+ const uri = src.extras?.[key];+ return typeof uri === 'string' && uri !== '' && !uri.startsWith('data:')+ ? { file: decodeURIComponent(uri), external: true, settings: { sRGB: false } } : null;+ };+ const flow = extraMap('flow_map'), shift = extraMap('shift_map');+ const tipSoftness = src.extras?.suggested_tipSoftness;return {index,name: src.name ?? `material_${index}`,@@ -641,6 +650,9 @@ function readMaterial(ctx: MaterialContext, textureCache: Map<number, ImportedImroughness,...(metalRoughMap ? { metallicRoughnessTexture: metalRoughMap } : {}),...(src.alphaMode === 'MASK' ? { alphaCutoff: src.alphaCutoff ?? 0.5 } : {}),+ ...(flow ? { flowTexture: flow } : {}),+ ...(shift ? { shiftTexture: shift } : {}),+ ...(typeof tipSoftness === 'number' && tipSoftness >= 0 ? { tipSoftness } : {}),...layers,};}diff --git a/pipeline/src/assets/modelImport.ts b/pipeline/src/assets/modelImport.tsindex 1ae5282fa..03c85f958 100644--- a/pipeline/src/assets/modelImport.ts+++ b/pipeline/src/assets/modelImport.ts@@ -78,6 +78,11 @@ export interface ImportedMaterial {emissiveTexture?: ImportedImageRef;/** Ambient occlusion in the map's red channel, scaled by @ref occlusionStrength. */occlusionTexture?: ImportedImageRef;+ /** Hair's strand-direction and highlight-shift maps, which glTF has no slot for: a material states them in its extras (`flow_map`, `shift_map`, paths from the model). */+ flowTexture?: ImportedImageRef;+ shiftTexture?: ImportedImageRef;+ /** How far in from the cutoff a hair card's edge fades (`extras.suggested_tipSoftness`). */+ tipSoftness?: number;occlusionStrength?: number;/** How metal and how rough the surface is, as constants. */metallic?: number;@@ -709,8 +714,12 @@ function characterProperties(shading: CharacterShading, material: ImportedMateribind('u_ormMap', material.metallicRoughnessTexture);if (rough !== undefined && !material.metallicRoughnessTexture) properties.u_roughness = rough;}- if (shading === 'hair') properties.u_alphaCutoff = material.alphaCutoff ?? 0.5;- else delete properties.u_alphaCutoff;+ if (shading === 'hair') {+ properties.u_alphaCutoff = material.alphaCutoff ?? 0.5;+ bind('u_flowMap', material.flowTexture);+ bind('u_shiftMap', material.shiftTexture);+ if (material.tipSoftness !== undefined) properties.u_tipSoftness = material.tipSoftness;+ } else delete properties.u_alphaCutoff;if (shading === 'eye') delete properties.u_normalMap;}@@ -737,7 +746,7 @@ export function importedImageSettings(meshes: ImportedMesh[]):for (const mesh of meshes) {const m = mesh.material;for (const image of [m?.baseColorTexture, m?.normalTexture, m?.emissiveTexture,- m?.occlusionTexture, m?.metallicRoughnessTexture]) {+ m?.occlusionTexture, m?.metallicRoughnessTexture, m?.flowTexture, m?.shiftTexture]) {if (!image) continue;const seen = out.get(image.file);out.set(image.file, { external: image.external, settings: { ...image.settings, ...seen?.settings } });diff --git a/pipeline/tests/character-materials.test.ts b/pipeline/tests/character-materials.test.tsindex dfdaf464c..7a18e99a1 100644--- a/pipeline/tests/character-materials.test.ts+++ b/pipeline/tests/character-materials.test.ts@@ -55,6 +55,28 @@ describe('materialProducts with character materials', () => {});});+describe('hair maps a material states in its extras', () => {+ const hair = material(0, 'hair_cards', {+ normalTexture: image('n.png', true), metallicRoughnessTexture: image('orm.png', true), alphaCutoff: 0.45,+ flowTexture: image('textures/flow.png', true), shiftTexture: image('textures/shift.png', true), tipSoftness: 0.1,+ });++ it('binds the strand-direction and shift maps and the tip softness of a hair material', () => {+ const [product] = materialProducts([meshOf(hair)], 'girl', { characters: true });+ expect(product!.data.shader).toBe('builtin:hair');+ expect(product!.data.properties).toMatchObject({+ u_flowMap: expect.stringContaining('flow.png'), u_shiftMap: expect.stringContaining('shift.png'), u_tipSoftness: 0.1, u_alphaCutoff: 0.45,+ });+ });++ it('does not put them on a material that is not hair', () => {+ const skin = material(0, 'skin_face', { normalTexture: image('n.png', true), metallicRoughnessTexture: image('orm.png', true), flowTexture: image('f.png', true), tipSoftness: 0.2 });+ const [product] = materialProducts([meshOf(skin)], 'girl', { characters: true });+ expect(product!.data.properties).not.toHaveProperty('u_flowMap');+ expect(product!.data.properties).not.toHaveProperty('u_tipSoftness');+ });+});+describe('orderHairTriangles', () => {// Three triangles at distances 1, 3 and 2 from the middle of the box, written outermost-first.function hairMesh(name: string): ImportedMesh {