轻量封装WebGPU渲染系统示例<30>- 绘制线段(源码)

82 篇文章 3 订阅
73 篇文章 3 订阅

原理说明:

WebGPU提供了绘制基本线条非机制,只要render pipeline primitive对应的 topology属性指定为line-list或者line-strip即可绘制对应的线条。

当前示例源码github地址:

https://github.com/vilyLei/voxwebgpu/blob/feature/rendering/src/voxgpu/sample/LineEntityTest.ts

当前示例运行效果:

WGSL顶点着色器代码:

@group(0) @binding(0) var<uniform> objMat : mat4x4<f32>;
@group(0) @binding(1) var<uniform> viewMat : mat4x4<f32>;
@group(0) @binding(2) var<uniform> projMat : mat4x4<f32>;

struct VertexOutput {
  @builtin(position) Position : vec4<f32>,
  @location(0) vertColor : vec4<f32>
}
@vertex
fn main(
  @location(0) position : vec3<f32>,
  @location(1) color : vec3<f32>,
) -> VertexOutput {

  let wpos = objMat * vec4(position.xyz, 1.0);
  var output : VertexOutput;
  output.Position = projMat * viewMat * wpos;
  output.vertColor = vec4f(color.xyz, 1.0);
  return output;
}

WGSL片段着色器代码:

@group(0) @binding(3) var<uniform> color: vec4f;
@fragment
fn main(
  @location(0) vertColor: vec4<f32>
) -> @location(0) vec4<f32> {
  var color4 = vertColor * color;
  return color4;
}

此示例基于此渲染系统实现,当前示例TypeScript源码如下:

export class LineEntityTest {
	private mRscene = new RendererScene();
	initialize(): void {

		this.initEvent();
		this.initScene();
	}
	private initEvent(): void {
		const rc = this.mRscene;
		new MouseInteraction().initialize(rc, 0, false).setAutoRunning(true);
	}
	private initScene(): void {
		const rsc = this.mRscene;

		let linePositions = [new Vector3(), new Vector3(-100), new Vector3(-150, -180), new Vector3(-150, -180, -100)];
		let line = new Line3DEntity({linePositions});
		line.setColor([1.0,0.0,0.0]);
		rsc.addEntity( line );

		linePositions = [new Vector3(), new Vector3(100), new Vector3(150, 180), new Vector3(150, 180, 100)];
		let lineColors = [new Color4(1.0), new Color4(0.0,1.0), new Color4(0.0,0.0,1.0), new Color4(1.0,0.0,1.0)];
		let colorLine = new Line3DEntity({linePositions, lineColors});
		rsc.addEntity( colorLine );
	}

	run(): void {
		this.mRscene.run();
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值