元宇宙技術(shù)在傳統(tǒng)互聯(lián)網(wǎng)的基礎上,元宇宙在沉浸感、參與度、永續(xù)性等多方面提出了更高的要求,因此將會由許多獨立工具、平臺、基礎設施、協(xié)議等來支持其運行。隨著AR、VR、5G、云計算等技術(shù)成熟度提升,元宇宙有望逐步從概念走向現(xiàn)實。
元宇宙的發(fā)展模式
(1)循序漸進是元宇宙的主要過程,其中在技術(shù)端、內(nèi)容端、載體端都隨著時代的發(fā)展而在不斷演變。關(guān)于將區(qū)塊鏈應用變得如何豐富化這個問題以太坊正在努力改進中,同樣的Coinbase、Uniswap以及Opesea為代表的交易所也在為區(qū)塊鏈經(jīng)濟提供更好的交易能力,他們的發(fā)展始終在遵循自然規(guī)律,循序漸進的發(fā)展。
(2)內(nèi)容端,元宇宙這一概念在游戲中不斷增加,其生態(tài)形式也不斷加強,當然用戶數(shù)也隨之增長。尤其是以Roblox、Sandbox為代表的UGC元宇宙概念,確保游戲得益于玩家的參與而不斷豐富自己游戲的內(nèi)容。
(3)載體端,比較明顯的表現(xiàn)是在通信技術(shù)、虛擬現(xiàn)實以及芯片等底層技術(shù)的改進和升級中。
Metauniverse emphasizes digital space and virtual world,but it does not mean that it is just digital space and virtual world.It can realize the interaction between virtual world,real world and real society,and can strengthen reality with emptiness.Therefore,metauniverse is an important new track to drive digital technology innovation and enable the real economy.Our government,scientific research institutions,enterprises and individuals need to actively participate in promoting the rapid development of metauniverse.
Do a good job in the top-level design at the macro level,and establish the basic concept and main objectives of the meta universe;Supporting technologies for building the meta universe,especially promoting the research and development of some key technologies;Establish relevant standards of metauniverse,from national standards to international standards,so as to gain a voice in the international science and technology field,avoid wasting financial and material resources and energy,improve R&D efficiency,I35模式7O98開發(fā)O7I8.and realize automation and intelligent interoperability;Formulate relevant laws and regulations,standardize in development,develop in standardization,and promote the healthy development of meta universe industry and industrial meta universe.
c#執(zhí)行l(wèi)ua代碼
這里將分三個步驟:
加載lua代碼到vm中,對應api-luaL_loadbuffer
luaL_loadbuffer會同時在棧上壓入代碼塊的指針
執(zhí)行l(wèi)ua代碼,對應api-lua_pcall
lua_pcall會從棧上依次彈出{nargs}個數(shù)據(jù)作為函數(shù)參數(shù),再彈出函數(shù)進行執(zhí)行,并將結(jié)果壓入棧
如果lua代碼有返回值,那么通過lua_toXXX相關(guān)api從棧上獲取結(jié)果
完整的代碼如下:
private bool DoLuaCode(System.IntPtr L,string luaCode){
//加載lua代碼
if(Lua.luaL_loadbuffer(L,luaCode,"")==0){
//執(zhí)行棧頂?shù)暮瘮?shù)
if(Lua.lua_pcall(L,0,1,0)==0){
//函數(shù)執(zhí)行完成后,返回值會依次依次押入棧
return true;
}else{
Debug.LogError("pcall failed!");
return false;
}
}else{
Debug.LogError("load buffer failed");
return false;
}
}
假如我們有一段lua代碼:
return'hello,i am from lua'
這段lua僅僅返回一段字符串,那么利用DoLuaCode去執(zhí)行就是:
//lua代碼
string luaCode="return'hello,i am from lua'";
if(DoLuaCode(L,luaCode)){
Debug.Log(Lua.lua_tostring(L,-1));
//lua_toXXX不會出棧,需要lua_pop才能出棧
Lua.lua_pop(L,1);
}
由于此處lua代碼返回的是字符串,因此使用lua_tostring(L,-1)來將棧頂?shù)脑剞D(zhuǎn)為字符串并返回,相應的我們還能看到有l(wèi)ua_tonumber,lua_toboolean等等.
4.c#調(diào)用lua全局函數(shù)
接下來的例子將說明一下c#端如何執(zhí)行l(wèi)ua中的全局函數(shù)。
假設現(xiàn)在我們有一段lua代碼如下:
function addSub(a,b)
return a+b,a-b;
end
通過DoLuaCode來運行以上的lua代碼,就得到了一個全局的addSub函數(shù),這個函數(shù)會返回a,b相加和相減的結(jié)果。
為了在c#端執(zhí)行以上的lua函數(shù),需要按以下步驟進行:
將全局函數(shù)壓入棧中,對應api-lua_getglobal
將函數(shù)所需的參數(shù)依次壓入棧中,對應api-lua_pushnumber
執(zhí)行棧中函數(shù),對應api-lua_pcall
獲取函數(shù)返回結(jié)果,對應api-lua_tonumber
完整c#代碼如下:
//從全局表里讀取addSub函數(shù),并壓入棧
Lua.lua_getglobal(L,"addSub");
//壓入?yún)?shù)a
Lua.lua_pushnumber(L,101);
//壓入?yún)?shù)b
Lua.lua_pushnumber(L,202);
//2個參數(shù),2個返回值
Lua.lua_pcall(L,2,2,0);
//pcall會讓參數(shù)和函數(shù)指針都出棧
//pcall執(zhí)行完畢后,會將結(jié)果壓入棧
Debug.Log(Lua.lua_tonumber(L,-2));
Debug.Log(Lua.lua_tonumber(L,-1));
Lua.lua_pop(L,2);
5.lua注冊并調(diào)用c#靜態(tài)函數(shù)
首先,想要被Lua調(diào)用的c#函數(shù),都必須滿足以下的格式:
public delegate int LuaCSFunction(System.IntPtr luaState);
同時需要加上特性:
MonoPInvokeCallback(typeof(LuaCSFunction))
我們可以通過以下方式,將一個LuaCSFunction注冊到lua中:
static void RegisterCSFunctionGlobal(System.IntPtr L,string funcName,LuaCSFunction func){
//將LuaCSFunction壓入棧中
Lua.lua_pushcfunction(L,func);
//lua_setglobal會彈出棧頂元素,并按給定的名字作為key將其加入到全局表
Lua.lua_setglobal(L,funcName);
}
那么,當我們在lua中執(zhí)行c#注冊的函數(shù)時,其交互過程如下:
LuaVM會臨時分配一個局部棧結(jié)構(gòu)(這里要區(qū)分開始通過luaL_newstate創(chuàng)建的全局棧,兩者是獨立的)
LuaVM會將lua側(cè)的函數(shù)參數(shù)壓入這個臨時棧,然后將棧指針傳給LuaCSFunction
LuaCSFunction在實現(xiàn)上需要從這個棧中讀取lua側(cè)壓入的參數(shù),然后執(zhí)行真正的相關(guān)邏輯,并將最終結(jié)果壓入棧中
LuaCSFunction需要返回一個int值,表示往棧中壓入了多少個返回值
Lua從棧中獲取C#側(cè)壓入的0/1/多個返回值
官方說明文檔可以參考-Calling C from Lua
接下來要將演示如何將一個c#靜態(tài)函數(shù)Print注入到lua中,實現(xiàn)lua中調(diào)用c#端的日志輸出功能。
我們定義一個c#靜態(tài)函數(shù)如下:
[MonoPInvokeCallback(typeof(LuaCSFunction))]
private static int Print(System.IntPtr localL){
//獲取棧中元素個數(shù)
var count=Lua.lua_gettop(localL);
System.Text.StringBuilder s=new System.Text.StringBuilder();
for(var i=1;i<=count;i++){
//依次讀取print的每個參數(shù),合并成一個string
s.Append(Lua.lua_tostring(localL,i));
s.Append('');
}
Debug.Log(s);
//print函數(shù)沒有返回值
return 0;
}
lua_gettop可以獲取棧中的元素個數(shù),此處代表了lua端壓入棧中的函數(shù)參數(shù)個數(shù)
然后我們通過以下方式將這個c#側(cè)的Print注冊到lua中,命名為print。
//將LuaCSFunction壓入棧中
Lua.lua_pushcfunction(L,Print);
//lua_setglobal會彈出棧頂元素,并按給定的名字作為key將其加入到全局表
Lua.lua_setglobal(L,"print");
接下來我們執(zhí)行以下的lua代碼:
print('hello','csharp')
就能看到編輯器中輸出
hello csharp
6.lua注冊c#類型
通常我們使用lua中的table來模擬c#中的類。一般類的注冊思路如下:
在lua中創(chuàng)建一個與c#類同名的表
將c#類的靜態(tài)函數(shù)都注冊到lua的這個同名表里
下面演示一下如何將Unity中的Debug類注冊到lua中:
Lua.lua_createtable(L,0,1);
Lua.lua_setglobal(L,"Debug");
其實很簡單:
lua_createtable會創(chuàng)建一個table,壓入棧頂
lua_setglobal會彈出棧頂元素,并將其加到全局表里
這樣我們在lua里就有了一個名為Debug的表可供全局訪問。但目前這個表是空空如也的,我們還需要為其添加靜態(tài)函數(shù)。(tips:實際上完整的設計中,還需要為class table設置metatable,增加一些限制性,但這里先不表)
6.1注入類的靜態(tài)函數(shù)
首先我們定義一個符合LuaCSFunction形式的c#函數(shù)如下:
[MonoPInvokeCallback(typeof(LuaCSFunction))]
private static int Debug_Log(System.IntPtr L){
string msg=Lua.lua_tostring(L,1);
Debug.Log(msg);
return 0;
}
這個c#函數(shù)是對Debug.Log的一個封裝。
然后可以通過以下方式將這個c#函數(shù)注冊到lua中的Debug表中:
Lua.lua_createtable(L,0,1);
//往棧中壓入字符串'Log'
Lua.lua_pushstring(L,"Log");
//往棧中壓入函數(shù)Debug_Log
Lua.lua_pushcfunction(L,Debug_Log);
//從棧中彈出一個元素作為key,再彈出一個元素作為value,作為pair賦值到index指定的table
Lua.lua_settable(L,1);
Lua.lua_setglobal(L,"Debug");
審核編輯:符乾江
-
互聯(lián)網(wǎng)
+關(guān)注
關(guān)注
55文章
11251瀏覽量
106516 -
元宇宙
+關(guān)注
關(guān)注
13文章
1404瀏覽量
12074
發(fā)布評論請先 登錄
TechWiz LCD 3D應用:FFS仿真

【Milk-V Duo S 開發(fā)板免費體驗】3- 嘗試免費3D打印了開發(fā)板外殼
TechWiz LCD 3D應用:局部液晶配向
答疑|3D打印能打印立體字母嗎?
TechWiz LCD 3D應用:撓曲電效用仿真
使用海爾曼太通/HellermannTyton 3D CAD 模型進行快速高效的設計
3D打印可以打印那種柔韌性好,能隨意變形的模型嗎?
TechWiz LCD 3D應用:局部液晶配向
騰訊混元3D AI創(chuàng)作引擎正式發(fā)布
TechWiz LCD 3D應用:局部液晶配向
TechWiz LCD 3D應用:撓曲電效用仿真

3D 建模:塑造未來的無限可能
安寶特產(chǎn)品 安寶特3D Analyzer:智能的3D CAD高級分析工具

安寶特產(chǎn)品 3D Evolution : 基于特征實現(xiàn)無損CAD格式轉(zhuǎn)換

評論