mcpField
R
roblox-world-building

roblox-world-building

github.com/el4cteo创意与媒体54532 次安装2 周前更新
代码仓库

关于 roblox-world-building

面向 Roblox 开发者的代码化世界构建指南,涵盖场景、光照、地形、程序化模型与运行时效果,并给出性能和安全注意事项。

主要功能

  1. 01创建和组织 Part 与 Model
  2. 02配置光照及昼夜循环
  3. 03生成与编辑地形
  4. 04使用 GeometryService 进行几何操作
  5. 05构建程序化模型与运行时网格
  6. 06添加粒子、光束和其他效果

适用场景

  1. 01用 Luau 搭建静态场景
  2. 02生成带种子的程序化地形
  3. 03实现可破坏墙体或碎裂效果
  4. 04在运行时编辑网格与图像

MCP 相关

提及可通过 Studio MCP 的 `generate_procedural_model` 生成 ProceduralModel。

安装

复制下面的命令,在终端中运行。

$npx skills add https://github.com/el4cteo/roblox-skills --skill roblox-world-building

安装后在 Claude Code 中输入/roblox-world-building确认已加载。

详细介绍

使用代码构建世界

零件和模型

--!strict
local function makePart(size: Vector3, cframe: CFrame, props: { [string]: any }?): Part
	local part = Instance.new("Part")
	part.Anchored = true
	part.Size = size
	part.CFrame = cframe
	part.TopSurface = Enum.SurfaceType.Smooth
	part.BottomSurface = Enum.SurfaceType.Smooth
	for key, value in props or {} do
		(part :: any)[key] = value
	end
	return part -- caller sets Parent last
end

local floor = makePart(Vector3.new(64, 1, 64), CFrame.new(0, 0, 0), {
	Material = Enum.Material.Slate,
	Color = Color3.fromRGB(90, 90, 100),
})
local model = Instance.new("Model")
model.Name = "Arena"
floor.Parent = model
model.WorldPivot = floor.CFrame
model.Parent = workspace
model:PivotTo(CFrame.new(0, 10, 0) * CFrame.Angles(0, math.rad(45), 0))
  • 先设置属性,最后设置父级(一次复制/物理插入)。锚定静态几何体。
  • 使用 PivotTo/GetPivot 移动/旋转模型;Model.WorldPivot / PrimaryPart 定义枢轴。
  • 将逻辑上相关的零件归入模型(流式加载、LOD、ModelStreamingMode),不要使用庞大的容器。
  • 装饰性零件:设置 CanCollide/CanTouch/CanQuery = false;对于微小细节,设置 CastShadow = false。
  • 材质:Enum.Material.*;通过 MaterialService 中的 MaterialVariant(设置 part.MaterialVariant = "Name")或 MeshPart 上的 SurfaceAppearance(PBR)实现自定义外观。Decal.Texture 已被 ColorMapContent 取代。
  • 大量相同网格的开销很低(实例化);大量不同网格/联合体则开销较高。

光照

属性指引
Lighting.LightingStyleRealistic(写实)或 Soft(风格化)。取代已弃用的 Lighting.Technology。
Lighting.PrioritizeLightingQuality优先保证画质(例如搭配 Soft 使用阴影贴图),而非低端设备上的性能。
ClockTime / GeographicLatitude太阳位置;为实现昼夜变化,可对 ClockTime 进行动画处理。
Brightness、ExposureCompensation、Ambient/OutdoorAmbient整体曝光和补光。
EnvironmentDiffuseScale/EnvironmentSpecularScale天空盒对光照和反射的贡献。
子对象Atmosphere(雾霭/密度,取代雾效)、Sky、Clouds(位于 Terrain 中)、BloomEffect、ColorCorrectionEffect、ColorGradingEffect、DepthOfFieldEffect、SunRaysEffect、BlurEffect。
--!strict
-- Server: smooth day/night cycle (one full day every 20 minutes).
local Lighting = game:GetService("Lighting")
local RunService = game:GetService("RunService")

local DAY_LENGTH_SECONDS = 20 * 60

RunService.Heartbeat:Connect(function(deltaTime)
	Lighting.ClockTime = (Lighting.ClockTime + deltaTime * 24 / DAY_LENGTH_SECONDS) % 24
end)

对于逐帧视觉变化,建议根据每个客户端上的 workspace:GetServerTimeNow() 计算 ClockTime(没有复制流量,且非常平滑)。本地光源:PointLight、 SpotLight、SurfaceLight——数量不宜多,并设置 Shadows = true。

地形

--!strict
local terrain = workspace.Terrain

terrain:FillBlock(CFrame.new(0, -10, 0), Vector3.new(512, 20, 512), Enum.Material.Grass)
terrain:FillBall(Vector3.new(0, 0, 0), 24, Enum.Material.Air) -- carve a crater
terrain:FillBlock(CFrame.new(40, -2, 0), Vector3.new(30, 4, 30), Enum.Material.Water)
terrain:FillCylinder(CFrame.new(-40, 5, 0), 20, 8, Enum.Material.Rock)
  • 批量生成:构建一个 Region3(使用 :ExpandToGrid(4) 对齐),并通过材料/占用率数组使用 ReadVoxels/WriteVoxels (每个体素为 4 个 stud),分块处理,并在每个块之间让出执行权。
  • terrain:Clear();通过 Terrain.WaterColor、WaterWaveSize 等设置水体外观。
  • 程序化高度图:使用八度噪声采样 math.noise(x * scale, z * scale, seed);使用 Random.new(seed) 生成确定性世界;在服务端生成,或使用 Parallel Luau 进行高强度计算。

游戏内实体建模和破坏(GeometryService)

--!strict
local GeometryService = game:GetService("GeometryService")

-- Punch a hole through a wall (e.g. explosions, building games).
local function carve(wall: BasePart, cutter: BasePart): { Instance }?
	local ok, results = pcall(function()
		return GeometryService:SubtractAsync(wall, { cutter }, {
			CollisionFidelity = Enum.CollisionFidelity.Default,
			RenderFidelity = Enum.RenderFidelity.Automatic,
			SplitApart = true, -- disconnected pieces become separate parts
		})
	end)
	if not ok or not results then
		return nil
	end
	for _, piece in results do
		if piece:IsA("BasePart") then
			piece.Anchored = wall.Anchored
			piece.Parent = wall.Parent
		end
	end
	wall:Destroy()
	return results
end

return carve
  • UnionAsync、IntersectAsync、SubtractAsync 会返回新的未设置父级的零件;FragmentAsync(part, sites) 使用来自 GenerateFragmentSites 的点,通过 Voronoi 算法将零件击碎;SweepPartAsync 沿 CFrame 挤出零件; CalculateConstraintsToPreserve 在替换零件时保留焊接和约束。
  • 这些操作开销较高:在服务端运行、限制调用频率,并对碎片进行池化处理/清理。

ProceduralModel(参数驱动的模型)

ProceduralModel 实例会根据公开 Attributes(参数)和 OnGenerate(params, targetContainer) 的生成器 ModuleScript 重新生成其内容;可在编辑时(撤销/重做、Team Create)和运行时使用。可通过 Studio、Assistant 或 Studio MCP(generate_procedural_model)生成。Creator Store 中的程序化模型会自动进行沙盒隔离。

运行时网格和图像

AssetService:CreateEditableMesh() / CreateEditableMeshAsync(content) 和 CreateEditableImage 支持在运行时编辑几何体和像素(可变形地形区块、绘画、自定义头像)。已发布的游戏要求所有者拥有 ID 且年满 13 岁并通过验证,并在 Creator Dashboard 中启用 Enable Mesh / Image APIs;客户端内存预算严格;只能加载由游戏所有者拥有或与其共享的资源。使用 AssetService:CreateMeshPartAsync(Content.fromObject(editableMesh)) 显示。对于大量顶点/面,请使用 EditableMesh 批处理 API 和并行查询(已向正式游戏开放),而非逐个元素调用。

效果

  • ParticleEmitter(用 :Emit(n) 实现爆发)、Beam(连接两个 attachment;用于激光、绳索)、Trail (用于剑、投射物)、Highlight(描边/填充;数量有限)、Explosion(触发物理效果 —— 设 DestroyJointRadiusPercent = 0 且 BlastPressure = 0 可仅作视觉效果)。
  • 在客户端生成装饰性效果;将其销毁或放入对象池(Debris:AddItem(obj, seconds) 即可)。

相关技能

roblox-performance(渲染开销、流式加载)、roblox-architecture(流式加载设置)、 roblox-studio-mcp(通过 MCP 在 Studio 中构建)、roblox-physics(约束、破坏物理)。

评论

支持 Markdown