mirror of
https://github.com/cocos/cocos4.git
synced 2026-11-21 16:12:40 +08:00
fix procedural mesh vertex layout (#6745)
This commit is contained in:
File diff suppressed because one or more lines are too long
@@ -10,6 +10,7 @@ const _defAttrs: IGFXAttribute[] = [
|
||||
{ name: GFXAttributeName.ATTR_POSITION, format: GFXFormat.RGB32F },
|
||||
{ name: GFXAttributeName.ATTR_NORMAL, format: GFXFormat.RGB32F },
|
||||
{ name: GFXAttributeName.ATTR_TEX_COORD, format: GFXFormat.RG32F },
|
||||
{ name: GFXAttributeName.ATTR_TANGENT, format: GFXFormat.RGBA32F },
|
||||
{ name: GFXAttributeName.ATTR_COLOR, format: GFXFormat.RGBA32F },
|
||||
];
|
||||
|
||||
@@ -19,7 +20,7 @@ export function createMesh (geometry: IGeometry, out?: Mesh, options?: createMes
|
||||
// Collect attributes and calculate length of result vertex buffer.
|
||||
const attributes: IGFXAttribute[] = [];
|
||||
let stride = 0;
|
||||
const channels: Array<{ offset: number; data: number[]; attribute: IGFXAttribute; }> = [];
|
||||
const channels: { offset: number; data: number[]; attribute: IGFXAttribute; }[] = [];
|
||||
let vertCount = 0;
|
||||
|
||||
let attr: IGFXAttribute | null;
|
||||
@@ -90,6 +91,28 @@ export function createMesh (geometry: IGeometry, out?: Mesh, options?: createMes
|
||||
stride += info.size;
|
||||
}
|
||||
|
||||
if (geometry.tangents && geometry.tangents.length > 0) {
|
||||
attr = null;
|
||||
if (geometry.attributes) {
|
||||
for (const att of geometry.attributes) {
|
||||
if (att.name === GFXAttributeName.ATTR_TANGENT) {
|
||||
attr = att;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!attr) {
|
||||
attr = _defAttrs[3];
|
||||
}
|
||||
|
||||
const info = GFXFormatInfos[attr.format];
|
||||
attributes.push(attr);
|
||||
vertCount = Math.max(vertCount, Math.floor(geometry.tangents.length / info.count));
|
||||
channels.push({ offset: stride, data: geometry.tangents, attribute: attr });
|
||||
stride += info.size;
|
||||
}
|
||||
|
||||
if (geometry.colors && geometry.colors.length > 0) {
|
||||
attr = null;
|
||||
if (geometry.attributes) {
|
||||
@@ -102,7 +125,7 @@ export function createMesh (geometry: IGeometry, out?: Mesh, options?: createMes
|
||||
}
|
||||
|
||||
if (!attr) {
|
||||
attr = _defAttrs[3];
|
||||
attr = _defAttrs[4];
|
||||
}
|
||||
|
||||
const info = GFXFormatInfos[attr.format];
|
||||
|
||||
@@ -518,7 +518,7 @@ export abstract class RenderPipeline {
|
||||
fv[UBOGlobal.TIME_OFFSET + 2] = cc.director.getTotalFrames();
|
||||
|
||||
fv[UBOGlobal.SCREEN_SIZE_OFFSET] = device.width;
|
||||
fv[UBOGlobal.SCREEN_SIZE_OFFSET + 1] = device.height;
|
||||
fv[UBOGlobal.SCREEN_SIZE_OFFSET + 1] = device.height * device.projectionSignY;
|
||||
fv[UBOGlobal.SCREEN_SIZE_OFFSET + 2] = 1.0 / fv[UBOGlobal.SCREEN_SIZE_OFFSET];
|
||||
fv[UBOGlobal.SCREEN_SIZE_OFFSET + 3] = 1.0 / fv[UBOGlobal.SCREEN_SIZE_OFFSET + 1];
|
||||
|
||||
|
||||
@@ -100,17 +100,27 @@ export default function box (options?: IBoxOptions): IGeometry {
|
||||
];
|
||||
|
||||
const faceNormals = [
|
||||
[0, 0, 1], // FRONT
|
||||
[0, 0, -1], // BACK
|
||||
[0, 1, 0], // TOP
|
||||
[0, -1, 0], // BOTTOM
|
||||
[1, 0, 0], // RIGHT
|
||||
[-1, 0, 0], // LEFT
|
||||
[ 0, 0, 1], // FRONT
|
||||
[ 0, 0, -1], // BACK
|
||||
[ 0, 1, 0], // TOP
|
||||
[ 0, -1, 0], // BOTTOM
|
||||
[ 1, 0, 0], // RIGHT
|
||||
[-1, 0, 0], // LEFT
|
||||
];
|
||||
|
||||
const faceTangents = [
|
||||
[-1, 0, 0, 1], // FRONT
|
||||
[-1, 0, 0, 1], // BACK
|
||||
[-1, 0, 0, 1], // TOP
|
||||
[-1, 0, 0, 1], // BOTTOM
|
||||
[ 0, 0, -1, 1], // RIGHT
|
||||
[ 0, 0, 1, 1], // LEFT
|
||||
];
|
||||
|
||||
const positions: number[] = [];
|
||||
const normals: number[] = [];
|
||||
const uvs: number[] = [];
|
||||
const tangents: number[] = [];
|
||||
const indices: number[] = [];
|
||||
const minPos = new Vec3(-hw, -hh, -hl);
|
||||
const maxPos = new Vec3(hw, hh, hl);
|
||||
@@ -124,6 +134,7 @@ export default function box (options?: IBoxOptions): IGeometry {
|
||||
const offset = positions.length / 3;
|
||||
const faceAxe = faceAxes[side];
|
||||
const faceNormal = faceNormals[side];
|
||||
const faceTangent = faceTangents[side];
|
||||
|
||||
for (iy = 0; iy <= vSegments; iy++) {
|
||||
for (ix = 0; ix <= uSegments; ix++) {
|
||||
@@ -138,6 +149,7 @@ export default function box (options?: IBoxOptions): IGeometry {
|
||||
positions.push(r.x, r.y, r.z);
|
||||
normals.push(faceNormal[0], faceNormal[1], faceNormal[2]);
|
||||
uvs.push(u, v);
|
||||
tangents.push(faceTangent[0], faceTangent[1], faceTangent[2], faceTangent[3]);
|
||||
|
||||
if ((ix < uSegments) && (iy < vSegments)) {
|
||||
const useg1 = uSegments + 1;
|
||||
@@ -164,6 +176,7 @@ export default function box (options?: IBoxOptions): IGeometry {
|
||||
positions,
|
||||
normals,
|
||||
uvs,
|
||||
tangents,
|
||||
indices,
|
||||
minPos,
|
||||
maxPos,
|
||||
|
||||
@@ -78,6 +78,14 @@ export interface IGeometry {
|
||||
*/
|
||||
uvs?: number[];
|
||||
|
||||
/**
|
||||
* @en
|
||||
* Vertex Tangents.
|
||||
* @zh
|
||||
* 顶点切线。
|
||||
*/
|
||||
tangents?: number[];
|
||||
|
||||
/**
|
||||
* @en
|
||||
* Vertex colors.
|
||||
@@ -94,10 +102,10 @@ export interface IGeometry {
|
||||
*/
|
||||
attributes?: IGFXAttribute[];
|
||||
|
||||
customAttributes?: Array<{
|
||||
customAttributes?: {
|
||||
attr: IGFXAttribute,
|
||||
values: number[],
|
||||
}>;
|
||||
}[];
|
||||
|
||||
/**
|
||||
* @en
|
||||
|
||||
Reference in New Issue
Block a user