当前位置: 首页 > news >正文

轻量封装WebGPU渲染系统示例<38>- 动态构建WGSL材质Shader(源码)

实现原理: 基于宏定义和WGSL文件系统实现(还在完善中...)

当前示例源码github地址:

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

当前示例运行效果:

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

export class DynamicShaderBuilding {private mRscene = new RendererScene();initialize(): void {this.mRscene.initialize({ canvasWith: 1024, canvasHeight: 1024, rpassparam: { multisampleEnabled: true } });this.initScene();this.initEvent();}private hdrEnvtex = new SpecularEnvBrnTexture();private createTextures(ns: string): WGTextureDataDescriptor[] {const albedoTex = { albedo: { url: `static/assets/pbr/${ns}/albedo.jpg` } };const normalTex = { normal: { url: `static/assets/pbr/${ns}/normal.jpg` } };const aoTex = { ao: { url: `static/assets/pbr/${ns}/ao.jpg` } };const roughnessTex = { roughness: { url: `static/assets/pbr/${ns}/roughness.jpg` } };const metallicTex = { metallic: { url: `static/assets/pbr/${ns}/metallic.jpg` } };let textures = [this.hdrEnvtex,albedoTex,normalTex,aoTex,roughnessTex,metallicTex] as WGTextureDataDescriptor[];return textures;}private initScene(): void {this.initEntities();}private initEntities(): void {let callback = (): void => {let pos = new Vector3(0, 0, 0);let basePos = new Vector3(-300, 0, -400);let dis = 250;let textures = this.createTextures("plastic");let material = this.createModelEntity(monkeySrc, pos.clone().add(new Vector3(0, 0, 0)).addBy(basePos), textures.slice(0, 0));this.applyMaterialPPt(material);material = this.createModelEntity(monkeySrc, pos.clone().add(new Vector3(dis, 0, 0)).addBy(basePos), textures.slice(0, 1));this.applyMaterialPPt(material);material = this.createModelEntity(monkeySrc, pos.clone().add(new Vector3(dis * 2, 0, 0)).addBy(basePos), textures.slice(0, 2));this.applyMaterialPPt(material);material = this.createModelEntity(monkeySrc, pos.clone().add(new Vector3(0, 0, dis)).addBy(basePos), textures.slice(0, 3));this.applyMaterialPPt(material);material = this.createModelEntity(monkeySrc, pos.clone().add(new Vector3(dis, 0, dis)).addBy(basePos), textures.slice(0, 4));this.applyMaterialPPt(material);material = this.createModelEntity(monkeySrc, pos.clone().add(new Vector3(dis * 2, 0, dis)).addBy(basePos), textures.slice(0, 5));this.applyMaterialPPt(material);material = this.createModelEntity(monkeySrc, pos.clone().add(new Vector3(0, 0, dis * 2)).addBy(basePos), textures.slice(0, 6));this.applyMaterialPPt(material);material = this.createModelEntity(monkeySrc, pos.clone().add(new Vector3(dis, 0, dis * 2)).addBy(basePos), textures);material.property.glossiness = false;this.applyMaterialPPt(material);material = this.createModelEntity(monkeySrc, pos.clone().add(new Vector3(dis * 2, 0, dis * 2)).addBy(basePos), textures);material.property.toneMapping = false;this.applyMaterialPPt(material);material = this.createModelEntity(monkeySrc, pos.clone().add(new Vector3(0, 0, dis * 3)).addBy(basePos), textures.slice(0, 6));material.property.metallicCorrection = false;material.property.glossiness = false;this.applyMaterialPPt(material);material = this.createModelEntity(monkeySrc, pos.clone().add(new Vector3(dis, 0, dis * 3)).addBy(basePos), textures.slice(0, 6));material.property.glossiness = false;material.property.toneMapping = false;this.applyMaterialPPt(material);material = this.createModelEntity(monkeySrc, pos.clone().add(new Vector3(dis * 2, 0, dis * 3)).addBy(basePos), textures.slice(0, 6));material.property.glossiness = false;material.property.toneMapping = false;material.property.metallicCorrection = false;this.applyMaterialPPt(material);};let monkeySrc = new ModelEntity({callback,modelUrl: "static/assets/draco/monkey.drc"});}private applyMaterialPPt(material: BasePBRMaterial): void {let property = material.property;property.ambient.value = [0.0, 0.2, 0.2];property.albedo.value = [0.7, 0.7, 0.3];property.arms.roughness = 0.8;property.armsBase.value = [0, 0, 0];property.uvParam.value = [2, 2];property.param.scatterIntensity = 32;}private mLightParams: LightShaderDataParam[] = [];private createModelEntity(srcEntity: ModelEntity, position: Vector3DataType, textures: WGTextureDataDescriptor[]): BasePBRMaterial {let rc = this.mRscene;let lightParam = this.createLightData(position);let material = new BasePBRMaterial();material.setLightParam(lightParam);material.addTextures(textures);let monkey = new ModelEntity({materials: [material],geometry: srcEntity.geometry,transform: { position, scale: [100, 100, 100], rotation: [0, 90, 0] }});rc.addEntity(monkey);return material;}private createLightData(position: Vector3DataType): LightShaderDataParam {let pos = new Vector3().setVector4(position);let pv0 = pos.clone().addBy(new Vector3(0, 200, 0));let pv1 = pos.clone().addBy(new Vector3(200, 0, 0));let pv2 = pos.clone().addBy(new Vector3(0, 0, 200));let pv3 = pos.clone().addBy(new Vector3(-200, 0, 0));let pv4 = pos.clone().addBy(new Vector3(0, 0, -200));let posList = [pv0, pv1, pv2, pv3, pv4];let c0 = new Color4(0.1 + Math.random() * 13, 0.1 + Math.random() * 13, 0.0, 0.00002);let c1 = new Color4(0.0, 0.1 + Math.random() * 13, 1.0, 0.00002);let c2 = new Color4(0.0, 0.1 + Math.random() * 13, 0.1 + Math.random() * 13, 0.00002);let c3 = new Color4(0.1 + Math.random() * 13, 1.0, 0.1 + Math.random() * 13, 0.00002);let c4 = new Color4(0.5, 1.0, 0.1 + Math.random() * 13, 0.00002);let colorList = [c0, c1, c2, c3, c4];let pointLightsTotal = posList.length;let j = 0;let lightsData = new Float32Array(4 * pointLightsTotal);let lightColorsData = new Float32Array(4 * pointLightsTotal);for (let i = 0; i < lightsData.length;) {const pv = posList[j];pv.w = 0.00002;pv.toArray4(lightsData, i);const c = colorList[j];c.toArray4(lightColorsData, i);j++;i += 4;}let param = { lights: lightsData, colors: lightColorsData, pointLightsTotal };this.mLightParams.push(param);return param;}private initEvent(): void {const rc = this.mRscene;rc.addEventListener(MouseEvent.MOUSE_DOWN, this.mouseDown);new MouseInteraction().initialize(rc, 0, false).setAutoRunning(true);}private mouseDown = (evt: MouseEvent): void => { };run(): void {this.mRscene.run();}
}

相关文章:

  • 批量添加PPT备注
  • HTTP四大参数类型及请求参数的方式和如何接收
  • Kubernetes 离线部署 Spinnaker
  • webSocket基于面向对象二次封装
  • C语言-字符串逆序
  • 芯片设计—低功耗isolation cell
  • householder进行矩阵QR分解
  • Anaconda深度学习环境配置命令参考
  • 人工智能对我们的生活影响有多大
  • 【Spring】SpringBoot的扩展点之ApplicationContextInitializer
  • python-冒泡排序
  • Golang基础-面向对象篇
  • 1.2.1 C语言结构体初始化方法总结
  • Nginx 开源版安装
  • 下一代ETL工具:微服务架构的全新数据集成平台
  • @jsonView过滤属性
  • 0x05 Python数据分析,Anaconda八斩刀
  • css布局,左右固定中间自适应实现
  • C学习-枚举(九)
  • docker-consul
  • Docker入门(二) - Dockerfile
  • JS笔记四:作用域、变量(函数)提升
  • Map集合、散列表、红黑树介绍
  • orm2 中文文档 3.1 模型属性
  • Vue全家桶实现一个Web App
  • Vue实战(四)登录/注册页的实现
  • 读懂package.json -- 依赖管理
  • 类orAPI - 收藏集 - 掘金
  • 设计模式(12)迭代器模式(讲解+应用)
  • 使用Tinker来调试Laravel应用程序的数据以及使用Tinker一些总结
  • 消息队列系列二(IOT中消息队列的应用)
  • LevelDB 入门 —— 全面了解 LevelDB 的功能特性
  • #14vue3生成表单并跳转到外部地址的方式
  • #gStore-weekly | gStore最新版本1.0之三角形计数函数的使用
  • #QT(一种朴素的计算器实现方法)
  • #我与Java虚拟机的故事#连载05:Java虚拟机的修炼之道
  • #中的引用型是什么意识_Java中四种引用有什么区别以及应用场景
  • (09)Hive——CTE 公共表达式
  • (博弈 sg入门)kiki's game -- hdu -- 2147
  • (九)信息融合方式简介
  • (牛客腾讯思维编程题)编码编码分组打印下标(java 版本+ C版本)
  • (转)ObjectiveC 深浅拷贝学习
  • .NET delegate 委托 、 Event 事件
  • .NET6 命令行启动及发布单个Exe文件
  • .net打印*三角形
  • .sh 的运行
  • @Bean, @Component, @Configuration简析
  • @DateTimeFormat 和 @JsonFormat 注解详解
  • [ IO.File ] FileSystemWatcher
  • [ vulhub漏洞复现篇 ] ECShop 2.x / 3.x SQL注入/远程执行代码漏洞 xianzhi-2017-02-82239600
  • [20181219]script使用小技巧.txt
  • [BZOJ1178][Apio2009]CONVENTION会议中心
  • [C++][基础]1_变量、常量和基本类型
  • [CF543A]/[CF544C]Writing Code
  • [ComfyUI进阶教程] animatediff视频提示词书写要点