GenerationService 是一種允許開發人員從文字提示中生成 3D 對象的服務,使用 Roblox 的 Cube 3D 基礎模型 。
網格生成是兩步過程:
- GenerateMeshAsync() 使用文字提示和其他必要參數開始網格生成過程。它返回一個獨特標識符 (生成 ID),可用於恢復未來結果。
目前,生成服務只支持以下使用:
- GenerateMeshAsync() 必須從伺服器指令碼中呼叫。
- LoadGeneratedMeshAsync() 必須從客戶端腳本中呼叫。
因結果,當網格生成請求來自客戶端時,客戶端必須向服務器發送信號以啟動生成。一旦伺服器確定該生成已完成,它應通知適當的客戶呼叫 LoadGeneratedMeshAsync() 並取回網格。請注意,因為生成的網格載入了 EditableMesh 內容,且只載入到客戶端,因此不會複製到其他客戶端。
以下代碼展示此設計模式。查看 這個示範體驗 獲得更詳細的範例。點擊 ⋯ 按鈕,然後 在 Studio 編輯 。
範例程式碼
Adds server-side functionality to handle mesh generation requests from clients.
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local GenerationService = game:GetService("GenerationService")
-- Create a RemoteEvent for client-server communication
local meshGenerationEvent = Instance.new("RemoteEvent")
meshGenerationEvent.Name = "MeshGenerationEvent"
meshGenerationEvent.Parent = ReplicatedStorage
-- Event to send generation results back to clients
local generationResultEvent = Instance.new("RemoteEvent")
generationResultEvent.Name = "GenerationResultEvent"
generationResultEvent.Parent = ReplicatedStorage
-- Handle mesh generation requests from clients
meshGenerationEvent.OnServerEvent:Connect(function(player, prompt)
print("Generating mesh for player", player.Name, "with prompt:", prompt)
-- Generate the mesh on the server
local success, generationId, contextId = pcall(function()
return GenerationService:GenerateMeshAsync(
{["Prompt"] = prompt},
player,
{},
function(intermediateType, intermediateGenerationId, intermediateContextId)
-- Send intermediate result to the requesting client
generationResultEvent:FireClient(
player,
intermediateGenerationId,
true -- isIntermediate
)
end
)
end)
if success then
-- Send the final generation ID to the client
generationResultEvent:FireClient(player, generationId, false)
print("Successfully generated mesh with ID:", generationId)
else
-- Notify the client of failure
generationResultEvent:FireClient(player, nil, false)
warn("Failed to generate mesh:", generationId)
end
end)
Adds client-side functionality to request mesh generation from the server and display the results.
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local GenerationService = game:GetService("GenerationService")
local Players = game:GetService("Players")
-- Connect to the RemoteEvents
local meshGenerationEvent = ReplicatedStorage:WaitForChild("MeshGenerationEvent")
local generationResultEvent = ReplicatedStorage:WaitForChild("GenerationResultEvent")
-- Local player
local player = Players.LocalPlayer
-- Store generated meshes
local generatedMeshes = {}
-- Function to request mesh generation
local function requestMeshGeneration(prompt)
print("Requesting mesh generation for prompt: " .. prompt)
meshGenerationEvent:FireServer(prompt)
end
-- Handle generation results from the server
generationResultEvent.OnClientEvent:Connect(function(generationId, isIntermediate)
if not generationId then
print("Generation failed")
return
end
print(isIntermediate and "Loading intermediate result..." or "Loading final result...")
local mesh = GenerationService:LoadGeneratedMeshAsync(generationId)
if mesh then
-- Clean up previous mesh if it exists
if generatedMeshes[isIntermediate and "intermediate" or "final"] then
generatedMeshes[isIntermediate and "intermediate" or "final"]:Destroy()
end
mesh.Parent = workspace
-- Position the mesh in front of the player
local character = player.Character
if character and character:FindFirstChild("HumanoidRootPart") then
local rootPart = character.HumanoidRootPart
mesh:PivotTo(rootPart.CFrame * CFrame.new(0, 0, -10))
end
-- Store the mesh for cleanup
generatedMeshes[isIntermediate and "intermediate" or "final"] = mesh
print(isIntermediate and "Loaded intermediate result" or "Loaded final result")
else
print("Failed to load mesh")
end
end)
-- Example usage
requestMeshGeneration("toy robot")
概要
方法
- GenerateMeshAsync(inputs : Dictionary,player : Player,options : Dictionary,intermediateResultCallback : function?):Tuple
從文字提示開始生成新的 3D 網格,並返回用於追蹤和回收結果的獨特ID。生成完成後,使用 LoadGeneratedMeshAsync() 載入並顯示生成的網格。
使用提供的 來擷取並載入由 生成的網格。網格以 內容返回。
屬性
方法
GenerateMeshAsync
從文字提示開始生成新的 3D 網格,並返回用於追蹤和回收結果的獨特ID。您可選擇接收中間結果,例如未經處理的網格,通過提供 intermediateResultCallback 函數。生成完成後,使用 LoadGeneratedMeshAsync() 載入並顯示生成的網格。
速率限制 每個經體驗每分鐘的速率限制為 5 代。如果你超過此限制,下一分鐘之後的生成請求將被阻止。
錯誤代碼 | 錯誤 | 說明 | 建議開發者行動 | | ------------------------ | ------------------------------------------------------------------------------------ | 已超過網格生成上限 | 每分鐘最大網格生成數已超過。| 等待速度限制重置。 | | 審核失敗 | 網格生成被標記為審核。| 檢查並修改提示以確保遵守 Roblox 的管理準則。| | 內部伺服器錯誤 | 伺服器發生了未預期的問題。| 稍後重試請求或檢查伺服器狀態以確認問題。| | 角色限制超出 | 這個生成請求的輸入提示長度超出限制。| 減少字典 字串中的字符數量。 |
參數
包含網格生成提示的辭典。目前,唯一支持的鑰匙是描述要生成的網格的 Prompt (字串)。
額外生成選項。目前沒有選項被支持。
一個回調功能被觸發與中間生成結果。對於提取早期網格版本(例如,在應用紋理之前)有用。
返回
一個生成ID和上下文ID的tuple。
- 生成 ID:每次呼叫 GenerateMeshAsync() 的唯一 ID 返回。
- 上下文 ID:目前未使用。
範例程式碼
Simple example showing how to use GenerateMeshAsync() to generate a mesh from a prompt.
local generationId, contextId = GenerationService:GenerateMeshAsync({["Prompt"] = "toaster"}, player, {})
Example showing how to use GenerateMeshAsync() with a callback for intermediate results.
local generationId, contextId = GenerationService:GenerateMeshAsync({["Prompt"] = prompt}, player, {}, function(intermediateType, generationId, contextId)
-- Add your logic here
end)
Example showing how to handle errors when generating meshes with GenerateMeshAsync().
local success, generationIdOrError, contextId = pcall(function()
return GenerationService:GenerateMeshAsync(
{["Prompt"] = "robot toy"},
player,
{},
function(intermediateType, intermediateId, intermediateContextId)
-- Notify client about intermediate result
generationResultEvent:FireClient(player, intermediateId, true)
end
)
end)
-- Handle a specific error code
if generationIdOrError == "Rate limit exceeded" then
-- Wait for the rate limit to reset
elseif success then
-- Send the final generation ID to the client
generationResultEvent:FireClient(player, generationIdOrError, false)
end
LoadGeneratedMeshAsync
使用提供的生成ID恢復並載入由 GenerationService:GenerateMeshAsync() 生成的網格。網格返回為 MeshPart 內容的 EditableMesh 。因為可編輯的網格不會被複製,所載入的網格不會被複製到任何其他客戶端,只能每 generationId 載入一次。
參數
由 GenerateMeshAsync() 返回的獨特ID。識別要加載入的網格。
返回
生成的網格,以 MeshPart 形式返回,包含 EditableMesh 。
範例程式碼
Example showing how to load a generated mesh using LoadGeneratedMeshAsync().
-- Take your generation ID generated on the server and load your generation
local mesh = GenerationService:LoadGeneratedMeshAsync(generationId)
mesh.Parent = workspace