custom vertex attribute when create mesh (#4113)

This commit is contained in:
Yun Hsiao Wu
2019-03-28 15:13:45 +08:00
committed by Weiliang Wang
parent 39a404c71c
commit 605d1a0586
5 changed files with 53 additions and 50 deletions
+3 -3
View File
File diff suppressed because one or more lines are too long
+42 -46
View File
@@ -5,14 +5,50 @@ import { AttributeBaseType, AttributeType,
IMeshStruct, IndexUnit, IPrimitive, IVertexAttribute, IVertexBundle, Mesh } from '../assets/mesh';
import { IGeometry } from '../primitive/define';
const copyAttribute = (attribute: IVertexAttribute) => Object.assign({}, attribute);
/**
* save a color buffer to a PPM file
*/
export function toPPM (buffer: Uint8Array, w: number, h: number) {
return `P3 ${w} ${h} 255\n${buffer.filter((e, i) => i % 4 < 3).toString()}\n`;
}
const defaultAttributes: Record<string, IVertexAttribute> = {
positions: {
name: GFXAttributeName.ATTR_POSITION,
baseType: AttributeBaseType.FLOAT32,
type: AttributeType.VEC3,
normalize: false,
},
normals: {
name: GFXAttributeName.ATTR_NORMAL,
baseType: AttributeBaseType.FLOAT32,
type: AttributeType.VEC3,
normalize: false,
},
uvs: {
name: GFXAttributeName.ATTR_TEX_COORD,
baseType: AttributeBaseType.FLOAT32,
type: AttributeType.VEC2,
normalize: false,
},
colors: {
name: GFXAttributeName.ATTR_COLOR,
baseType: AttributeBaseType.FLOAT32,
type: AttributeType.VEC4,
normalize: false,
},
};
export function createMesh (geometry: IGeometry, out?: Mesh) {
// Collect attributes and calculate length of result vertex buffer.
const attributes: IVertexAttribute[] = [];
let stride = 0;
const channels: Array<{ offset: number; data: number[]; attribute: IVertexAttribute; }> = [];
let verticesCount = 0;
const addAttribute = (attribute: IVertexAttribute, data: number[]) => {
const addAttribute = (name: string) => {
const data = geometry[name]; if (!data) { return; }
let attribute = geometry.attributes && geometry.attributes[name];
if (!attribute) { attribute = Object.assign({}, defaultAttributes[name]); }
const componentCount = _getComponentCount(attribute);
const componentBytesLength = _getComponentByteLength(attribute);
const bytesLength = componentBytesLength * componentCount;
@@ -21,16 +57,10 @@ export function createMesh (geometry: IGeometry, out?: Mesh) {
stride += bytesLength;
verticesCount = Math.max(verticesCount, Math.floor(data.length / componentCount));
};
addAttribute(copyAttribute(predefinedAttributes.positions), geometry.positions);
if (geometry.normals) {
addAttribute(copyAttribute(predefinedAttributes.normals), geometry.normals);
}
if (geometry.uvs) {
addAttribute(copyAttribute(predefinedAttributes.uvs), geometry.uvs);
}
if (geometry.colors) {
addAttribute(copyAttribute(predefinedAttributes.colors), geometry.colors);
}
addAttribute('positions');
addAttribute('normals');
addAttribute('uvs');
addAttribute('colors');
// Use this to generate final merged buffer.
const bufferBlob = new BufferBlob();
@@ -109,40 +139,6 @@ export function createMesh (geometry: IGeometry, out?: Mesh) {
return out;
}
/**
*
*/
export function toPPM (buffer: Uint8Array, w: number, h: number) {
return `P3 ${w} ${h} 255\n${buffer.filter((e, i) => i % 4 < 3).toString()}\n`;
}
const predefinedAttributes = {
positions: {
name: GFXAttributeName.ATTR_POSITION,
baseType: AttributeBaseType.FLOAT32,
type: AttributeType.VEC3,
normalize: false,
},
normals: {
name: GFXAttributeName.ATTR_NORMAL,
baseType: AttributeBaseType.FLOAT32,
type: AttributeType.VEC3,
normalize: false,
},
uvs: {
name: GFXAttributeName.ATTR_TEX_COORD,
baseType: AttributeBaseType.FLOAT32,
type: AttributeType.VEC2,
normalize: false,
},
colors: {
name: GFXAttributeName.ATTR_COLOR,
baseType: AttributeBaseType.FLOAT32,
type: AttributeType.VEC4,
normalize: false,
},
};
class BufferBlob {
private _arrayBufferOrPaddings: Array<ArrayBuffer | number> = [];
private _length = 0;
+6
View File
@@ -1,4 +1,5 @@
import { GFXPrimitiveMode } from '../../gfx/define';
import { IVertexAttribute } from '../assets/mesh';
export interface IGeometryOptions {
/**
@@ -82,4 +83,9 @@ export interface IGeometry {
* whether rays casting from the back face of this geometry could collide with it
*/
doubleSided?: boolean;
/**
* specify vertex attributes, use (positions|normals|uvs|colors) as keys
*/
attributes?: Record<string, IVertexAttribute>;
}
+1
View File
@@ -913,6 +913,7 @@ export function WebGL2CmdFuncUpdateBuffer (device: WebGL2GFXDevice, gpuBuffer: W
if (size === buf.byteLength) {
gl.bufferSubData(gpuBuffer.glTarget, offset, buf);
// if (gl.getBufferParameter(gl.UNIFORM_BUFFER, gl.BUFFER_SIZE) !== buff.length * 4) { debugger; }
} else {
gl.bufferSubData(gpuBuffer.glTarget, offset, new Float32Array(buff, 0, size / 4));
}
+1 -1
View File
@@ -15,7 +15,7 @@ export class Effect {
}
public static parseEffect (effect: EffectAsset, info: IEffectInfo) {
if (!effect.techniques) { return; }
if (!effect.techniques) { return []; }
const { techIdx, defines, states } = info;
const tech = effect.techniques[techIdx || 0];
const passNum = tech.passes.length;