blobt
4 years ago
3 changed files with 519 additions and 0 deletions
@ -0,0 +1,128 @@ |
|||
import { vec2, vec3, vec4, mat4 } from "../math"; |
|||
import { GLAttribBits, GLAttribState } from "../webgl/WebGLAttribState"; |
|||
import { GLStaticMesh } from "../webgl/WebGLMesh"; |
|||
|
|||
export class GeometryData { |
|||
|
|||
/** |
|||
* @var 点的位置 |
|||
*/ |
|||
public positions: vec3[] = []; |
|||
|
|||
/** |
|||
* @var uv坐标 |
|||
*/ |
|||
public uvs: vec2[] = []; |
|||
|
|||
/** |
|||
* @var 法线坐标 |
|||
*/ |
|||
public normals: vec3[] = []; |
|||
|
|||
/** |
|||
* @var 颜色坐标 |
|||
*/ |
|||
public colors: vec4[] = []; |
|||
|
|||
/** |
|||
* @var 切线坐标 |
|||
*/ |
|||
public tangents: vec4[] = []; |
|||
|
|||
/** |
|||
* @var 索引 |
|||
*/ |
|||
public indices: number[] = []; |
|||
|
|||
/** |
|||
* 生成一个attrib bit |
|||
* @returns |
|||
*/ |
|||
public getAttribBits(): GLAttribBits { |
|||
|
|||
if (this.positions.length === 0) { |
|||
throw new Error("position data is required"); |
|||
} |
|||
|
|||
let ret: GLAttribBits = GLAttribState.POSITION_BIT; |
|||
|
|||
if (this.uvs.length > 0) { |
|||
ret |= GLAttribState.TEXCOORD_BIT; |
|||
} |
|||
|
|||
if (this.normals.length > 0) { |
|||
ret |= GLAttribState.NORMAL_BIT; |
|||
} |
|||
|
|||
if (this.colors.length > 0) { |
|||
ret |= GLAttribState.COLOR_BIT; |
|||
} |
|||
|
|||
if (this.tangents.length > 0) { |
|||
ret |= GLAttribState.TANGENT_BIT; |
|||
} |
|||
|
|||
return ret; |
|||
} |
|||
|
|||
public makeStaticVAO(gl: WebGLRenderingContext, needNormal: boolean = false, needUV: boolean = true): GLStaticMesh { |
|||
|
|||
let bits: GLAttribBits = this.getAttribBits(); |
|||
|
|||
if (needNormal === false) { |
|||
bits &= ~GLAttribState.NORMAL_BIT; |
|||
} |
|||
|
|||
if (needUV === false) { |
|||
bits &= ~GLAttribState.TEXCOORD_BIT; |
|||
} |
|||
|
|||
let stride: number = GLAttribState.getVertexBytesStride(bits); |
|||
let step: number = stride / Float32Array.BYTES_PER_ELEMENT; |
|||
let arrayBuffer: ArrayBuffer = new ArrayBuffer(stride * this.positions.length); |
|||
let buffer: Float32Array = new Float32Array(arrayBuffer); |
|||
|
|||
for (let i: number = 0; i < this.positions.length; i++) { |
|||
let j: number = i * step; |
|||
let idx: number = 0; |
|||
|
|||
//位置
|
|||
buffer[j + (idx++)] = this.positions[i].x; |
|||
buffer[j + (idx++)] = this.positions[i].y; |
|||
buffer[j + (idx++)] = this.positions[i].z; |
|||
|
|||
//法线
|
|||
if (bits & GLAttribState.NORMAL_BIT) { |
|||
buffer[j + (idx++)] = this.normals[i].x; |
|||
buffer[j + (idx++)] = this.normals[i].y; |
|||
buffer[j + (idx++)] = this.normals[i].z; |
|||
} |
|||
|
|||
//纹理
|
|||
if (bits & GLAttribState.TEXCOORD_BIT) { |
|||
buffer[j + (idx++)] = this.uvs[i].x; |
|||
buffer[j + (idx++)] = this.uvs[i].y; |
|||
} |
|||
|
|||
//颜色
|
|||
if (bits & GLAttribState.COLOR_BIT) { |
|||
buffer[j + (idx++)] = this.colors[i].x; |
|||
buffer[j + (idx++)] = this.colors[i].y; |
|||
buffer[j + (idx++)] = this.colors[i].z; |
|||
buffer[j + (idx++)] = this.colors[i].w; |
|||
} |
|||
|
|||
//切线
|
|||
if (bits & GLAttribState.TANGENT_BIT) { |
|||
buffer[j + (idx++)] = this.tangents[i].x; |
|||
buffer[j + (idx++)] = this.tangents[i].y; |
|||
buffer[j + (idx++)] = this.tangents[i].z; |
|||
buffer[j + (idx++)] = this.tangents[i].w; |
|||
} |
|||
} |
|||
|
|||
let mesh: GLStaticMesh = new GLStaticMesh(gl, bits, buffer, this.indices.length > 0 ? new Uint16Array(this.indices) : null); |
|||
|
|||
return mesh; |
|||
} |
|||
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue