輕量封裝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.pswp.cn/news/161849.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/161849.shtml
英文地址,請注明出處:http://en.pswp.cn/news/161849.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

編寫bat程序 快速開啟 redis 服務

一鍵開啟redis服務 編寫txt文件&#xff0c;代碼如下&#xff1a;cd /d E:\Redis\Redis-x64-5.0.14.1 redis-server.exe redis.windows.conf這里的redis的安裝目錄記得改成自己的 將文件后綴的.txt改成.bat&#xff0c;然后雙擊運行就可以啦

前綴和及差分數組

前綴和 原數組x0x1x2x3x4x5前綴和數組x0x0x1x0x1x2x0x1x2x3x0x1x2x3x4x0x1x2x3x4x5前綴和數組代數形式x0’x1’x2’x3’x4’x5’ 計算原數組某區間的和 sum[x1,x2,x3] 利用前綴和計算 x3-x0 x0x1x2x3-x0 x1x2x3 差分數組 x0x1x2x3x4x5原數組x0x1x2x3x4x5差分數組x0x1-x0x…

模擬電路定理

模擬電路是指由電子元件、電路拓撲和信號處理單元等構成的電路&#xff0c;用于模擬現實世界中的信號和系統。在模擬電路中&#xff0c;有許多重要的定理和規律&#xff0c;下面列舉了一些常見的定理。 1. 基爾霍夫電流定律&#xff08;Kirchhoffs Current Law&#xff09; 基…

HTTP四大參數類型及請求參數的方式和如何接收

HTTP 請求中4大參數類型和接收方法。 1、請求頭參數head 請求頭參數顧名思義&#xff0c;是存放在請求頭中發送給服務器的參數&#xff0c;服務器通過解析請求頭獲取參數內容。通常會存放本次請求的基本設置&#xff0c;以幫助服務器理解并解析本次請求的body體。 參數形式如…

C++學習 --string

目錄 1&#xff0c; 什么是string 2&#xff0c; 創建string 3&#xff0c; 操作string 3-1&#xff0c; 賦值 3-1-1&#xff0c; 賦值() 3-1-1&#xff0c; 賦值(assign) 3-2&#xff0c; 修改 3-2-1&#xff0c; 拼接 3-2-1-1&#xff0c; 拼接() 3-2-1-2&#xff…

srs的webrtc信令分析

關于webrtc的流信令只有四個 /rtc/v1/publish/&#xff0c;這是推流接口&#xff0c;是推流客戶端跟SRS交換SDP的接口 /rtc/v1/play/&#xff0c;這是拉流接口&#xff0c;是拉流客戶端跟SRS交換SDP的接口 /rtc/v1/whip/&#xff0c;這也是推流接口&#xff0c;作用是也是交換…

C#開發的OpenRA游戲之屬性RenderSprites(8)

C#開發的OpenRA游戲之屬性RenderSprites(8) 本文開始學習RenderSprites屬性,這個屬性是跟渲染有關的,因此它就攝及顏色相關的內容,所以我們先來學習一下調色板,這是舊游戲的圖片文件保存的格式,如果放在現代來看,不會再采用這種方法,畢竟現在存儲空間變大,便宜了,并…

JDBC 操作 SQL Server 時如何傳入列表參數

本文是作為將要對 PostgreSQL 的 in, any() 操作的一個鋪墊&#xff0c;也是對先前用 JDBC 操作 SQL Server 的溫習。以此記錄一下用 JDBC 查詢 SQL Server 時如何傳遞一個列表參數。比如想像一下查詢語句 select * from users where id in (?) 我們是否能給這里的問題參數傳遞…

idea編譯問題導致接口調用不通

問題背景&#xff1a; 1.idea版本2021&#xff0c;springboot&#xff0c;父子maven項目&#xff0c;創建了一個新的model。啟動之后&#xff0c;調試controller接口&#xff0c;接口一直報404。 問題分析&#xff1a; 1.查看編譯后的文件&#xff0c;發現java代碼一直沒編譯…

Vue3使用dataV報錯問題解決

DataV官網&#xff1a;https://datav-vue3.jiaminghi.com/guide/ vue2中是沒有問題的&#xff0c;這是第一次在vue3中使用發現的報錯問題 報錯問題 首先安裝&#xff1a; pnpm add dataview/datav-vue3 1. 全局注冊報錯 然后main.ts全局注冊 import { createApp } f…

html網站-關于發展歷程的案例

一、案例一 1.效果圖&#xff1a; 2.代碼&#xff1a; 所用到的文件自行在官網下載&#xff0c;也可在git上拉取。 <!DOCTYPE html> <html><head><meta http-equiv"Content-Type" content"text/html; charsetutf-8" /><meta…

USB驅動開發基礎

USB標準 USB1.0&#xff0c; 1996&#xff0c;低速1.5Mbps和高速12Mbps&#xff0c;USB1.1 iMac G3&#xff0c;Type A和Type B接口USB 2.0 2000&#xff0c; 480Mpbs&#xff0c;Type A/B/C接口、Micro A/BUSB 3.0 5Gbps, 隨著USB 3.2命名規定&#xff0c;現在也叫USB 3.2 Ge…

Nginx模塊開發之http過濾器filter

文章目錄 什么是過濾模塊Nginx相關數據結構介紹ngx_module_t的數據結構ngx_http_module_t數據結構ngx_command_s數據結構 相關宏定義filter&#xff08;過濾器&#xff09;實現Nginx模塊開發流程Nginx 模塊執行具體實現流程create_loc_confmerge_loc_confpostconfiguration修改…

使用OkHttp庫爬取百度云視頻詳細步驟

目錄 摘要 一、OkHttp庫簡介 二、爬蟲基本概念 三、使用OkHttp庫爬取百度云視頻 1、發送HTTP請求 2、處理響應 3、下載文件 四、可能遇到的問題及解決方案 五、注意事項 總結與建議 摘要 本文將詳細介紹如何使用OkHttp庫爬取百度云視頻。文章首先簡要介紹OkHttp庫和…

【collections】Python中的OrderDict

【collections】Python中的OrderDict 文章目錄 【collections】Python中的OrderDict1. 什么是OrderedDict2. Toy Code 1. 什么是OrderedDict 其實很簡單OrderedDict是Python中一個字典dict的變體&#xff0c;它可以按照元素添加的順序來保持鍵值對&#xff08;key-value pair&…

GPIO模式詳解:推挽/開漏/浮空/上拉/下拉/施密特(遲滯)輸入

GPIO(General Purpose Input Output)可用于執行數字輸入或輸出功能。典型的應用包括從/向模擬或數字傳感器/設備讀寫數值、驅動LED、為I2C通信驅動時鐘、生成外部組件的觸發、發出中斷等。 文章目錄 1 GPIO簡介2 輸出模式2.1 推挽輸出2.2 開漏輸出 3 輸入模式3.1 高阻態(浮空)、…

推薦一款適合做智慧旅游的前端模板

目錄 前言 一、功能介紹 二、前端技術介紹 三、功能及界面設計介紹 1、數據概覽 2、車輛監控 3、地圖界面 4、其它功能 四、擴展說明 總結 前言 智慧旅游是一種全新的旅游業務模式&#xff0c;它充分利用先進的信息技術&#xff0c;提升旅游體驗&#xff0c;優化旅游管…

【Axure高保真原型】樹形表格

今天和大家分享樹形表格的原型模板&#xff0c;點擊樹的箭頭可以打開或者收起子節點&#xff0c;點擊表格內容&#xff0c;可以選中該行內容實現高亮變色效果&#xff0c;樹形表格是通過中繼器制作的&#xff0c;使用簡單&#xff0c;只需要按要求填寫中繼器表格即可&#xff0…

2023亞太杯數學建模思路 - 案例:粒子群算法

文章目錄 1 什么是粒子群算法&#xff1f;2 舉個例子3 還是一個例子算法流程算法實現建模資料 # 0 賽題思路 &#xff08;賽題出來以后第一時間在CSDN分享&#xff09; https://blog.csdn.net/dc_sinor?typeblog 1 什么是粒子群算法&#xff1f; 粒子群算法&#xff08;Pa…

安防監控視頻融合平臺EasyCVR定制化頁面開發

安防監控EasyCVR視頻匯聚平臺基于云邊端智能協同&#xff0c;支持海量視頻的輕量化接入與匯聚、轉碼與處理、全網智能分發、視頻集中存儲等。安防視頻平臺EasyCVR拓展性強&#xff0c;視頻能力豐富&#xff0c;具體可實現視頻監控直播、視頻輪播、視頻錄像、云存儲、回放與檢索…