• 轻量封装WebGPU渲染系统示例<37>- 多个局部点光源应用于非金属材质形成的效果(源码)


    当前示例源码github地址:

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

    当前示例运行效果:

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

    1. export class BasePbrMaterialMultiLights {
    2. private mRscene = new RendererScene();
    3. initialize(): void {
    4. this.mRscene.initialize({rpassparam: { multisampleEnabled: true } });
    5. this.initScene();
    6. this.initEvent();
    7. }
    8. private hdrEnvtex = new SpecularEnvBrnTexture();
    9. private createTextures(ns: string): WGTextureDataDescriptor[] {
    10. const albedoTex = { albedo: { url: `static/assets/pbr/${ns}/albedo.jpg` } };
    11. const normalTex = { normal: { url: `static/assets/pbr/${ns}/normal.jpg` } };
    12. const aoTex = { ao: { url: `static/assets/pbr/${ns}/ao.jpg` } };
    13. const roughnessTex = { roughness: { url: `static/assets/pbr/${ns}/roughness.jpg` } };
    14. const metallicTex = { metallic: { url: `static/assets/pbr/${ns}/metallic.jpg` } };
    15. let textures = [this.hdrEnvtex, albedoTex, normalTex, aoTex, roughnessTex, metallicTex];
    16. return textures;
    17. }
    18. private initScene(): void {
    19. this.initEntities();
    20. }
    21. private initEntities(): void {
    22. let rc = this.mRscene;
    23. rc.addEntity(new AxisEntity());
    24. let callback = (): void => {
    25. let pos = new Vector3(0, 0, 0);
    26. // let material = this.createModelEntity(monkeySrc, "grass", pos);
    27. // let material = this.createModelEntity(monkeySrc, "rusted_iron", pos);
    28. // let material = this.createModelEntity(monkeySrc, "gold", pos);
    29. let material = this.createModelEntity(monkeySrc, "plastic", pos);
    30. let property = material.property;
    31. property.ambient.value = [0.0, 0.2, 0.2];
    32. property.albedo.value = [0.7, 0.7, 0.3];
    33. property.arms.roughness = 0.8;
    34. property.armsBase.value = [0, 0, 0];
    35. property.uvParam.value = [2, 2];
    36. // property.specularFactor.value = [0.0,0.0,1.1];
    37. property.param.scatterIntensity = 32;
    38. this.createBoxEntity("plastic", new Vector3(0, -110.0, 0), this.mLightParams[0]);
    39. this.createBillboards(this.mLightParams[0]);
    40. };
    41. let monkeySrc = new ModelEntity({
    42. callback,
    43. modelUrl: "static/assets/draco/monkey.drc"
    44. });
    45. }
    46. private mLightParams: LightShaderDataParam[] = [];
    47. private createModelEntity(srcEntity: ModelEntity, texName: string, position: Vector3DataType): BasePBRMaterial {
    48. let rc = this.mRscene;
    49. let lightData = this.createLightData(position);
    50. let material = new BasePBRMaterial();
    51. material.setLightParam(lightData);
    52. material.addTextures(this.createTextures(texName));
    53. let monkey = new ModelEntity({
    54. materials: [material],
    55. geometry: srcEntity.geometry,
    56. transform: { position, scale: [100, 100, 100], rotation: [0, 90, 0] }
    57. });
    58. rc.addEntity(monkey);
    59. return material;
    60. }
    61. private createBoxEntity(texName: string, position: Vector3DataType, lightData: LightShaderDataParam): BasePBRMaterial {
    62. let rc = this.mRscene;
    63. let material = new BasePBRMaterial();
    64. material.setLightParam(lightData);
    65. material.addTextures(this.createTextures(texName));
    66. let box = new BoxEntity({
    67. materials: [material],
    68. transform: { position, scale: [7, 0.1, 7] }
    69. });
    70. rc.addEntity(box);
    71. return material;
    72. }
    73. private createLightData(position: Vector3DataType): LightShaderDataParam {
    74. let pos = new Vector3().setVector4(position);
    75. let pv0 = pos.clone().addBy(new Vector3(0, 200, 0));
    76. let pv1 = pos.clone().addBy(new Vector3(200, 0, 0));
    77. let pv2 = pos.clone().addBy(new Vector3(0, 0, 200));
    78. let pv3 = pos.clone().addBy(new Vector3(-200, 0, 0));
    79. let pv4 = pos.clone().addBy(new Vector3(0, 0, -200));
    80. let posList = [pv0, pv1, pv2, pv3, pv4];
    81. let c0 = new Color4(0.1 + Math.random() * 13, 0.1 + Math.random() * 13, 0.0, 0.00002);
    82. let c1 = new Color4(0.0, 0.1 + Math.random() * 13, 1.0, 0.00002);
    83. let c2 = new Color4(0.0, 0.1 + Math.random() * 13, 0.1 + Math.random() * 13, 0.00002);
    84. let c3 = new Color4(0.1 + Math.random() * 13, 1.0, 0.1 + Math.random() * 13, 0.00002);
    85. let c4 = new Color4(0.5, 1.0, 0.1 + Math.random() * 13, 0.00002);
    86. let colorList = [c0, c1, c2, c3, c4];
    87. let pointLightsTotal = posList.length;
    88. let j = 0;
    89. let lightsData = new Float32Array(4 * pointLightsTotal);
    90. let lightColorsData = new Float32Array(4 * pointLightsTotal);
    91. for (let i = 0; i < lightsData.length; ) {
    92. const pv = posList[j];
    93. pv.w = 0.00002;
    94. pv.toArray4(lightsData, i);
    95. const c = colorList[j];
    96. c.toArray4(lightColorsData, i);
    97. j++;
    98. i += 4;
    99. }
    100. let param = { lights: lightsData, colors: lightColorsData, pointLightsTotal };
    101. this.mLightParams.push(param);
    102. return param;
    103. }
    104. private createBillboards(param: LightShaderDataParam): void {
    105. let rc = this.mRscene;
    106. let pointLightsTotal = param.pointLightsTotal;
    107. let lights = param.lights;
    108. let colors = param.colors;
    109. let diffuseTex0 = { diffuse: { url: "static/assets/flare_core_03.jpg" } };
    110. for (let i = 0; i < pointLightsTotal; ++i) {
    111. let billboard = new BillboardEntity({ size: 50, textures: [diffuseTex0] });
    112. let pv = new Vector3().fromArray3(lights, i * 4);
    113. let c = new Color4().fromArray3(colors, i * 4);
    114. c.a = 1.0;
    115. billboard.color = c.scaleBy(0.1);
    116. billboard.scale = 1.0;
    117. billboard.transform.setPosition(pv);
    118. rc.addEntity(billboard);
    119. }
    120. }
    121. private initEvent(): void {
    122. const rc = this.mRscene;
    123. rc.addEventListener(MouseEvent.MOUSE_DOWN, this.mouseDown);
    124. new MouseInteraction().initialize(rc, 0, false).setAutoRunning(true);
    125. }
    126. private mouseDown = (evt: MouseEvent): void => {};
    127. run(): void {
    128. this.mRscene.run();
    129. }
    130. }

  • 相关阅读:
    1985-2020年全国各省一二三产业就业人数/各省分产业就业人数数据(无缺失)
    数据结构之时空复杂度
    CNN多输入时间序列+一维向量
    口袋参谋:如何写出高权重标题?用对这招很重要!
    《Effective C++》阅读总结(三):资源管理
    Ubuntu18.04LTS安装配置VScode及下载C++相应第三方库
    git 给分支添加描述
    将输入对象转换为数组数组的维度大于等于1numpy.atleast_1d()
    Mac 中修改默认 的Python 版本
    在神经网络引入了非线性,非线性回归 神经网络
  • 原文地址:https://blog.csdn.net/vily_lei/article/details/134539370