轻量封装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();
	}
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/179247.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

【追求卓越04】数据结构--栈与队列

引导 今天我们开始学习栈与队列的内容&#xff0c;我觉得栈并不难&#xff0c;所以篇幅也就不会那么多了。在虚拟空间中&#xff0c;栈是用户空间中的一种数据结构&#xff0c;它主要用于保存局部变量。那么问题来了&#xff0c;为什么用栈来保存局部变量&#xff0c;不用别的数…

探秘开发app与小程序:一场技术与创新的博弈

app与小程序&#xff1a;一场技术与创新的博弈随着科技的飞速发展&#xff0c;移动应用程序已经成为我们日常生活中不可或缺的一部分。在这个充满竞争的时代&#xff0c;企业纷纷投身于开发各类移动应用&#xff0c;以期在市场中占据一席之地。然而&#xff0c;面对多样化的应用…

从0开始学习JavaScript--JavaScript迭代器

JavaScript迭代器&#xff08;Iterator&#xff09;是一种强大的编程工具&#xff0c;它提供了一种统一的方式来遍历不同数据结构中的元素。本文将深入探讨JavaScript迭代器的基本概念、用法&#xff0c;并通过丰富的示例代码展示其在实际应用中的灵活性和强大功能。 迭代器的…

Redis-Redis缓存高可用集群

1、Redis集群方案比较 哨兵模式 在redis3.0以前的版本要实现集群一般是借助哨兵sentinel工具来监控master节点的状态&#xff0c;如果master节点异常&#xff0c;则会做主从切换&#xff0c;将某一台slave作为master&#xff0c;哨兵的配置略微复杂&#xff0c;并且性能和高可…

ubuntu cutecom串口调试工具使用方法(图形界面)

文章目录 Ubuntu下使用CuteCom进行串口调试使用指南什么是CuteCom&#xff1f;主要特点 安装CuteCom使用APT包管理器从源码编译安装 配置串口CuteCom界面解析&#xff08;启动cutecom&#xff09;使用CuteCom进行数据发送和接收配置串口参数数据接收数据发送 高级功能和技巧流控…

Pycharm的程序调试

有如下代码需要进行调试&#xff1a; i 1 while i < 10:print(i)步骤一&#xff1a;设置断点 步骤二&#xff1a;进入调试视图 方式1&#xff1a;右键单击编辑区&#xff1a;点击’Debug模块名’ ​ 方式2&#xff1a;ShiftF9 ​ 方式3&#xff1a;单机工具栏上的调试按钮…

Bean依赖注入注解开发

value Value("xfy")private String userName;private String userName;Value("xiao")public void setUserName(String userName) {this.userName userName;} Autowired // 根据类型进行注入 如果同一类型的Bean有多个&#xff0c;尝试根基名字进行二次…

地磁传感器在城市交通智能监控系统的作用

地磁传感器的功能作用 地磁传感器的功能是相当强大的&#xff1a;当驾驶员把车辆停在车位上&#xff0c;地磁传感器能自动感应车辆的到来并开始计时&#xff1b;待车辆要离开时&#xff0c;传感器会自动把停车时间传送到中继站进行计费。因此&#xff0c;解决停车收费效率低下…

Antd Design的inputNumber实现千位分隔符和小数点并存

代码来自文章: react中使用antDesign的Input/InputNumber最多保留两位小数&#xff0c;多的小数位禁止输入&#xff0c;且实现输入实时校验并添加千位分隔符, 正则忘了很多, 我主要做个笔记. //定义InputNumber的参数 const NumberProps {min: 0,//最小值max: …

druid keepAlive 导致数据库连接数飙升

一.背景 应用在执行完某个复杂业务&#xff0c;主要包含20几个查询SQL的操作后&#xff0c;会导致数据库连接池一直升高 druid版本&#xff1a;1.2.11 druid配置文件&#xff1a; spring.datasource.druid.maxActive100 spring.datasource.druid.initialSize20 spring.datas…

读像火箭科学家一样思考笔记06_初学者之心

1. 专业化是目前流行的趋势 1.1. 通才&#xff08;generalist&#xff09;是指博而不精之人 1.2. 懂得的手艺越多&#xff0c;反而会家徒四壁 1.2.1. 希腊谚语 1.3. 这种态度代价很大&#xff0c;它阻断了不同学科思想的交融 2. 组合游戏 2.1. 某个行业的变革可能始于另一…

ubuntu上编译proj-7.1.0出现tiffio.h找不到的错误

作者&#xff1a;朱金灿 来源&#xff1a;clever101的专栏 为什么大多数人学不会人工智能编程&#xff1f;>>> 在编译ubuntu上编译proj-7.1.0出现下面错误&#xff1a; grids.cpp:41:10: fatal error: tiffio.h: No such file or directory41 | #include "tif…

2023亚太杯数学建模B题思路 - 玻璃温室中的微气候法规

# 1 赛题 问题B 玻璃温室中的微气候法规 温室作物的产量受到各种气候因素的影响&#xff0c;包括温度、湿度和风速[1]。其中&#xff0c;适 宜的温度和风速是植物生长[2]的关键。为了调节玻璃温室内的温度、风速等气候因素 , 温室的设计通常采用带有温室风扇的通风系统&#x…

SAP_ABAP_面试篇_关于Function Module函数的三种处理类型

关于 Function Module 这个技术点&#xff0c;在面试过程中一般会考察以下几个问题&#xff1a; 1 函数处理类型的更新模式 一般会问到异步和事务&#xff08;逻辑单元 LUW&#xff09;&#xff0c;异步函数的调试方式、SM13监控更新函数的执行过程&#xff08;V1 与 V2 模式…

STM32_3(GPIO)

GPIO简介 GPIO&#xff08;General Purpose Input Output&#xff09;通用输入输出口8种输入输出模式输出模式可控制端口输出高电平&#xff0c;驱动LED、蜂鸣器、模拟通信协议输出时许等输入模式可读取端口的高低电平或电压&#xff0c;用于读取按键输入、外接模块电平信号输…

《数学之美》第三版的读书笔记一、主要是马尔可夫假设、隐马尔可夫模型、图论深度/广度、PageRank相关算法、TF-IDF词频算法

1、马尔可夫假设 从19世纪到20世纪初,俄国有个数学家叫马尔可夫他提出了一种方法,假设任意一个词出现的概率只同它前面的词有关。这种假设在数学上称为马尔可夫假设。 2、二元组的相对频度 利用条件概率的公式,某个句子出现的概率等于每一个词出现的条件概率相乘,于是可展…

STM32_5(中断)

中断系统 中断&#xff1a;在主程序运行过程中&#xff0c;出现了特定的中断触发条件&#xff08;中断源&#xff09;&#xff0c;使得CPU暂停当前正在运行的程序&#xff0c;转而去处理中断程序&#xff0c;处理完成后又返回原来被暂停的位置继续运行中断优先级&#xff1a;当…

计算公式-dB转换,噪声,IP3,OP1dB,耗散

1. dB和log转换公式 dB在缺省情况下总是定义功率单位&#xff0c;以 10lg 为计。 d B 10 l g ( B ) dB 10lg(B) dB10lg(B) P o w e r G a i n ( d B ) 10 l g ( P o u t P i n ) Power Gain(dB) 10lg(\frac{P_{out}}{P_{in}}) PowerGain(dB)10lg(Pin​Pout​​) 2. 级联情…

Django 集成 Celery 实现高效的异步任务处理

概要 在复杂的 Web 应用中&#xff0c;处理长时间运行的任务或定期任务是一项挑战。Django 作为一个强大的 Python Web 框架&#xff0c;可以通过集成 Celery 这一异步任务队列来优化这些任务的处理。Celery 不仅能提高应用性能&#xff0c;还能改善用户体验。本文将深入探讨如…

redis的高可用(主从复制和哨兵模式)

redis的高可用&#xff08;主从复制和哨兵模式&#xff09; redis的性能管理&#xff1a;redis的数据缓存在内存当中 INFO memory&#xff1a;查看redis内存使用情况 used_memory:1800800&#xff1a;redis中数据占用的内存 used_memory_rss:5783552&#xff1a;redis向操作…
最新文章