• 轻量封装WebGPU渲染系统示例<36>- 广告板(Billboard)(WGSL源码)


    原理不再赘述,请见wgsl shader实现。

    当前示例源码github地址:

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

    当前示例运行效果:

    WGSL顶点shader:

    1. @group(0) @binding(0) var objMat : mat4x4<f32>;
    2. @group(0) @binding(1) var viewMat : mat4x4<f32>;
    3. @group(0) @binding(2) var projMat : mat4x4<f32>;
    4. @group(0) @binding(3) var billParam: vec4<f32>;
    5. struct VertexOutput {
    6. @builtin(position) Position : vec4<f32>,
    7. @location(0) uv : vec2<f32>
    8. }
    9. @vertex
    10. fn main(
    11. @location(0) position : vec3<f32>,
    12. @location(1) uv : vec2<f32>
    13. ) -> VertexOutput {
    14. let cosv = cos(billParam.z);
    15. let sinv = sin(billParam.z);
    16. let vtx = position.xy * billParam.xy;
    17. let vtx_pos = vec2<f32>(vtx.x * cosv - vtx.y * sinv, vtx.x * sinv + vtx.y * cosv);
    18. var viewV = viewMat * objMat * vec4f(0.0,0.0,0.0,1.0);
    19. viewV = vec4<f32>(viewV.xy + vtx_pos.xy, viewV.zw);
    20. var projV = projMat * viewV;
    21. projV.z = ((projV.z / projV.w) + billParam.w) * projV.w;
    22. var output : VertexOutput;
    23. output.Position = projV;
    24. output.uv = uv;
    25. return output;
    26. }

    WGSL片段shader:

    1. @group(0) @binding(4) var color: vec4f;
    2. @group(0) @binding(5) var uvScale: vec4f;
    3. @group(0) @binding(6) var billSampler: sampler;
    4. @group(0) @binding(7) var billTexture: texture_2d<f32>;
    5. @fragment
    6. fn main(
    7. @location(0) uv: vec2f
    8. ) -> @location(0) vec4f {
    9. var c4 = textureSample(billTexture, billSampler, uv * uvScale.xy + uvScale.zw) * color;
    10. return c4;
    11. }

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

    1. export class BillboardEntityTest {
    2. private mRscene = new RendererScene();
    3. initialize(): void {
    4. this.mRscene.initialize();
    5. this.initScene();
    6. this.initEvent();
    7. }
    8. private initScene(): void {
    9. this.initEntities();
    10. }
    11. private initEntities(): void {
    12. let rc = this.mRscene;
    13. let diffuseTex0 = { diffuse: { url: "static/assets/flare_core_02.jpg" } };
    14. let entity = new FixScreenPlaneEntity({ extent: [-0.8, -0.8, 1.6, 1.6], textures: [diffuseTex0] });
    15. entity.color = [0.1, 0.3, 0.5];
    16. rc.addEntity(entity);
    17. rc.addEntity(new AxisEntity());
    18. for (let i = 0; i < 10; ++i) {
    19. let billboard = new BillboardEntity({ textures: [diffuseTex0] });
    20. billboard.color = [0.5, 0.5, 2];
    21. billboard.scale = Math.random() * 2 + 1;
    22. billboard.transform.setPosition([Math.random() * 1000 - 500, 0, 0]);
    23. rc.addEntity(billboard);
    24. let diffuseTex1 = { diffuse: { url: "static/assets/testEFT4_01.jpg", flipY: true } };
    25. billboard = new BillboardEntity({ textures: [diffuseTex1] });
    26. billboard.color = [1.8, 1.5, 0.5];
    27. // billboard.color = [0.8, 0.5, 0.5];
    28. billboard.scale = Math.random() * 2 + 1;
    29. billboard.uvScale = [0.5, 0.5];
    30. billboard.uvOffset = [1, 1];
    31. // billboard.uvOffset = [0.5, 1];
    32. billboard.transform.setPosition([0, Math.random() * 1000 - 500, 0]);
    33. rc.addEntity(billboard);
    34. }
    35. }
    36. private initEvent(): void {
    37. const rc = this.mRscene;
    38. rc.addEventListener(MouseEvent.MOUSE_DOWN, this.mouseDown);
    39. new MouseInteraction().initialize(rc, 0, false).setAutoRunning(true);
    40. }
    41. private mouseDown = (evt: MouseEvent): void => {};
    42. run(): void {
    43. this.mRscene.run();
    44. }
    45. }

  • 相关阅读:
    BFS专题6 字符迷宫
    2020最新Java面试题
    微信小程序开发校园第二课堂+后台管理系统|前后分离VUE.js在线学习网
    【前端面试】(四)JavaScript var let const的区别
    python+nodejs+php+springboot+vue 学生选课程作业提交教学辅助管理系统
    良品铺子:已成“良品”,但能成“优品”吗?
    results=DBHelper.exeUpdate(conn,cur,exesql)是什么意思
    AI-数学-高中-40法向量求法
    Lvs +keepalivede : 高可用集群
    网络爬虫——urllib(1)
  • 原文地址:https://blog.csdn.net/vily_lei/article/details/134529358