GenerationService
*เนื้อหานี้แปลโดยใช้ AI (เวอร์ชัน Beta) และอาจมีข้อผิดพลาด หากต้องการดูหน้านี้เป็นภาษาอังกฤษ ให้คลิกที่นี่
GenerationService เป็นบริการที่ช่วยให้นักพัฒนาสามารถสร้างวัตถุ 3D จากคำแนะนำข้อความโดยใช้รูปแบบฐานข้อมูล 3D ของ Roblox Cube 3D
การสร้างเมชเป็นกระบวนการสองขั้นตอน:
- GenerateMeshAsync() เริ่มกระบวนการสร้างเมชโดยใช้ข้อความแจ้งและพารามิเตอร์ที่จำเป็นอื่นๆมันส่งคืนตัวระบุที่ไม่ซ้ำกัน (ID รุ่น) ที่สามารถใช้เพื่อดึงผลลัพธ์ในอนาคตได้
- LoadGeneratedMeshAsync() โหลดเมทริกซ์ที่สร้างขึ้นในประสบการณ์ เมทริกซ์ถูกส่งคืนเป็น MeshPart ที่มี EditableMesh
ขณะนี้ GenerationService สนับสนุนการใช้งานต่อไปนี้เท่านั้น:
- 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() เพื่อโหลดและแสดงเมชที่สร้างขึ้น
ดึงและโหลดเมชที่สร้างโดย GenerationService:GenerateMeshAsync() โดยใช้ generationId ที่ให้ไว้ เมชจะถูกส่งคืนเป็น MeshPart ที่มีเนื้อหา EditableMesh
คุณสมบัติ
วิธีการ
GenerateMeshAsync
เริ่มการสร้างเมทริกซ์ 3D ผลการค้นหาID ที่ไม่ซ้ำกันที่ใช้ในการติดตามและดึงผลลัพธ์คุณสามารถรับผลลัพธ์ชั่วคราวได้ เช่น เมชที่ไม่มีเทกเจอร์ โดยการให้ฟังก์ชัน intermediateResultCallbackหลังจากการสร้างเสร็จสมบูรณ์ให้ใช้ LoadGeneratedMeshAsync() เพื่อโหลดและแสดงเมชที่สร้างขึ้น
ขีดจํากัดอัตรา มีขีดจํากัดอัตรา 5 รุ่นต่อนาทีต่อประสบการณ์หากคุณเกินขีดจํากัดนี้คําขอการสร้างรุ่นต่อไปจะถูกบล็อกจนกว่าจะถึงนาทีถัดไป
รหัสข้อผิดพลาด | ข้อผิดพลาด | คําอธิบาย | การกระทํานักพัฒนาที่แนะนํา | | ------------------------ | ------------------------------------------------------------------------------------ | อัตราการจํากัดถูกเกิน | จํานวนการสร้างเมชที่สูงสุดถูกเกินสําหรับนาที| รอจนกว่าขีดจํากัดอัตราจะรีเซ็ต | | ความล้มเหลวในการควบคุม | การสร้างเมชถูกธงเพื่อการควบคุม| ตรวจสอบและปรับแต่งคําแนะนําเพื่อให้แน่ใจว่าสอดคล้องกับนโยบายการควบคุมของ Roblox| | ข้อผิดพลาดภายในเซิร์ฟเวอร์ | เกิดปัญหาไม่คาดคิดบนเซิร์ฟเวอร์ | ลองใหม่อีกครั้งในภายหลังหรือตรวจสอบสถานะเซิร์ฟเวอร์สำหรับปัญหา | | เกินขีดจํากัดตัวละครแล้ว | ระยะเวลาของการแจ้งเตือนสําหรับคําขอรุ่นนี้เกินขีดจํากัด| ลดจํานวนตัวอักษรในสตริง Prompt ของสารานุกรม input |
พารามิเตอร์
สารานุกรมที่มีคําแนะนําการสร้างเมชขณะนี้คีย์ที่สนับสนุนเพียงอย่างเดียวคือ Prompt (สตริง) ที่อธิบายเกี่ยวกับเมชที่จะสร้าง
ตัวเลือกการสร้างเพิ่มเติม ขณะนี้ไม่มีตัวเลือกที่สนับสนุน
ฟังก์ชัน callback ถูกกระตุ้นด้วยผลลัพธ์การสร้างชั่วคราว มีประโยชน์สำหรับการดึงเวอร์ชันเมชที่เร็วกว่า (เช่น ก่อนที่จะใช้เทกเจอร์)
ส่งค่ากลับ
คู่ของรหัสการสร้างและรหัสบริบท
- รหัสการสร้าง: รหัสที่ไม่ซ้ำกันที่ส่งคืนสำหรับแต่ละคำขอของ GenerateMeshAsync()
- 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
ดึงและโหลดเมชที่สร้างโดย GenerationService:GenerateMeshAsync() โดยใช้ generationId ที่ให้มาเมชจะถูกส่งคืนเป็น MeshPart ที่มีเนื้อหา EditableMeshเนื่องจากเมชที่สามารถแก้ไขได้ไม่ถูกสําเนา เมชที่โหลดจึงไม่ถูกสําเนาไปยังไคลเอนต์อื่น ๆ และสามารถโหลดได้เพียงครั้งเดียวต่อ generationId
พารามิเตอร์
ID ที่ไม่ซ้ำกันที่ส่งคืนโดย GenerateMeshAsync() ระบุเมชที่จะโหลด
ส่งค่ากลับ
เมชที่สร้างขึ้นที่ส่งคืนเป็น 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