Compare commits

..

1 Commits

Author SHA1 Message Date
SpecialX
bd6e0f4771 优化顶点数据布局:从多数组改为单数组 2025-11-19 11:49:41 +08:00
3 changed files with 26 additions and 66 deletions

View File

@@ -1,30 +1,13 @@
export class QuadGeometry { export class QuadGeometry {
public positions: number[]; public vertices: number[];
public colors: number[];
public texCoords: number[];
public indices: number[]; public indices: number[];
constructor() { constructor() {
this.positions = [ this.vertices = [
-0.5, -0.5, -0.5, -0.5, 0.0, 1.0, 1.0, 0.0, 0.0,
0.5, -0.5, 0.5, -0.5, 1.0, 1.0, 0.0, 1.0, 0.0,
-0.5, 0.5, -0.5, 0.5, 0.0, 0.0, 0.0, 0.0, 1.0,
0.5, 0.5, 0.5, 0.5, 1.0, 0.0, 1.0, 1.0, 1.0
]
this.colors = [
1.0, 0.0, 0.0,
0.0, 1.0, 0.0,
0.0, 0.0, 1.0,
1.0, 1.0, 1.0,
]
this.texCoords = [
0.0, 1.0,
1.0, 1.0,
0.0, 0.0,
1.0, 0.0,
] ]
this.indices = [ this.indices = [

View File

@@ -6,10 +6,8 @@ class Renderer{
private device! : GPUDevice; private device! : GPUDevice;
private context! : GPUCanvasContext; private context! : GPUCanvasContext;
private pipeline! : GPURenderPipeline; private pipeline! : GPURenderPipeline;
private postitionBuffer! : GPUBuffer; private verticesBuffer! : GPUBuffer;
private indexBuffer! : GPUBuffer; private indexBuffer! : GPUBuffer;
private colorBuffer! : GPUBuffer;
private texCoordBuffer! : GPUBuffer;
private textureBindGroup! : GPUBindGroup; private textureBindGroup! : GPUBindGroup;
private testTexture! : Texture; private testTexture! : Texture;
@@ -53,60 +51,42 @@ class Renderer{
const geometry = new QuadGeometry(); const geometry = new QuadGeometry();
this.postitionBuffer = BufferUtil.createVertexBuffer(this.device,new Float32Array(geometry.positions)); this.verticesBuffer = BufferUtil.createVertexBuffer(this.device,new Float32Array(geometry.vertices));
this.colorBuffer = BufferUtil.createVertexBuffer(this.device, new Float32Array(geometry.colors));
this.texCoordBuffer = BufferUtil.createVertexBuffer(this.device,new Float32Array(geometry.texCoords));
this.indexBuffer = BufferUtil.createIndexBuffer(this.device, new Uint16Array(geometry.indices)); this.indexBuffer = BufferUtil.createIndexBuffer(this.device, new Uint16Array(geometry.indices));
} }
private prepareModel() { private prepareModel() {
const shaderModule = this.device.createShaderModule({code: shaderSource}); const shaderModule = this.device.createShaderModule({code: shaderSource});
const postitionBufferLayout : GPUVertexBufferLayout = const BufferLayout : GPUVertexBufferLayout =
{ {
arrayStride: 2 * Float32Array.BYTES_PER_ELEMENT, arrayStride: 7 * Float32Array.BYTES_PER_ELEMENT,
attributes: [ attributes: [
{ {
shaderLocation: 0, shaderLocation: 0,
offset: 0, offset: 0,
format: 'float32x2' format: 'float32x2'
} ,
{
shaderLocation: 1,
offset: 2 * Float32Array.BYTES_PER_ELEMENT,
format: 'float32x2'
},
{
shaderLocation: 2,
offset: 4 * Float32Array.BYTES_PER_ELEMENT,
format: 'float32x3'
} }
], ],
stepMode: 'vertex' stepMode: 'vertex'
} }
const colorBufferLayout : GPUVertexBufferLayout =
{
arrayStride: 3 * Float32Array.BYTES_PER_ELEMENT,
attributes: [
{
shaderLocation: 1,
offset: 0,
format: 'float32x3'
}]
}
const textureCoordBufferLayout : GPUVertexBufferLayout =
{
arrayStride: 2 * Float32Array.BYTES_PER_ELEMENT,
attributes: [
{
shaderLocation: 2,
offset: 0,
format: 'float32x2'
}],
stepMode: 'vertex'
}
const vertexState: GPUVertexState = { const vertexState: GPUVertexState = {
module : shaderModule, module : shaderModule,
entryPoint: "VertexMain", entryPoint: "VertexMain",
buffers: buffers:
[ [
postitionBufferLayout, BufferLayout
colorBufferLayout,
textureCoordBufferLayout
] ]
} }
@@ -200,9 +180,7 @@ class Renderer{
//Draw here //Draw here
passEncoder.setPipeline(this.pipeline ); passEncoder.setPipeline(this.pipeline );
passEncoder.setIndexBuffer(this.indexBuffer, 'uint16'); passEncoder.setIndexBuffer(this.indexBuffer, 'uint16');
passEncoder.setVertexBuffer(0, this.postitionBuffer); passEncoder.setVertexBuffer(0, this.verticesBuffer);
passEncoder.setVertexBuffer(1, this.colorBuffer);
passEncoder.setVertexBuffer(2, this.texCoordBuffer);
passEncoder.setBindGroup(0, this.textureBindGroup) passEncoder.setBindGroup(0, this.textureBindGroup)
passEncoder.drawIndexed(6, 1, 0, 0, 0); passEncoder.drawIndexed(6, 1, 0, 0, 0);

View File

@@ -1,15 +1,14 @@
struct VertexOut { struct VertexOut {
@builtin(position) pos: vec4<f32>, @builtin(position) pos: vec4<f32>,
@location(0) color: vec4<f32>, @location(0) texcoord: vec2<f32>,
@location(1) texcoord: vec2<f32>, @location(1) color: vec4<f32>,
} }
@vertex @vertex
fn VertexMain( fn VertexMain(
@location(0) pos: vec2<f32>, @location(0) pos: vec2<f32>,
@location(1) color: vec3<f32>, @location(1) texcoord: vec2<f32>,
@location(2) texcoord: vec2<f32>, @location(2) color: vec3<f32>,
@builtin(vertex_index) vertexIndex: u32,
) -> VertexOut ) -> VertexOut
{ {
var output : VertexOut; var output : VertexOut;