Skip to content

Commit 847d4bf

Browse files
committed
fix: Fix radius check, and format some files
1 parent 0067cce commit 847d4bf

File tree

3 files changed

+32
-39
lines changed

3 files changed

+32
-39
lines changed

places/test_plugin_place.rbxl

3.26 KB
Binary file not shown.

src/init.client.lua

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ local ServiceBag = require("ServiceBag")
2020
local plugin = plugin
2121
-- luacheck: pop ignore
2222

23-
local IS_DEBUG_MODE = script:IsDescendantOf(game)
23+
local IS_DEBUG_MODE = script:IsDescendantOf(game) and not script:FindFirstAncestorWhichIsA("PluginDebugService")
2424

2525
local selectionService
2626
if IS_DEBUG_MODE then
@@ -58,7 +58,6 @@ local function activate(button)
5858
serviceBag:Init()
5959
serviceBag:Start()
6060

61-
6261
local converter = maid:Add(TerrainConverter.new(serviceBag))
6362
local newScreenGui = maid:Add(screenGui:Clone())
6463

@@ -101,11 +100,8 @@ if IS_DEBUG_MODE then
101100
activate()
102101
else
103102
local toolbar = plugin:CreateToolbar("Object")
104-
local button = toolbar:CreateButton(
105-
"Part to Terrain",
106-
"Converts roblox parts to terrain",
107-
"rbxassetid://1618168422"
108-
)
103+
local button =
104+
toolbar:CreateButton("Part to Terrain", "Converts roblox parts to terrain", "rbxassetid://1618168422")
109105

110106
button.Click:Connect(function()
111107
if not isActive then
@@ -118,4 +114,4 @@ else
118114
plugin.Unloading:Connect(function()
119115
deactivate(button)
120116
end)
121-
end
117+
end

src/modules/Shared/TerrainConverter.lua

Lines changed: 28 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,6 @@ function TerrainConverter:CanConvert(items)
7272
return false
7373
end
7474

75-
7675
function TerrainConverter:_canConvertPart(item)
7776
if not (item:IsA("Part") or item:IsA("WedgePart")) then
7877
return false
@@ -138,12 +137,12 @@ function TerrainConverter:_convertPart(part, material)
138137
elseif part.Shape == Enum.PartType.Block then
139138
self:_fillBlock(part.CFrame, part.Size, material)
140139
elseif part.Shape == Enum.PartType.Ball then
141-
self:_fillBall(part.Position, part.Size.x/2, material)
140+
self:_fillBall(part.Position, part.Size.x / 2, material)
142141
elseif part.Shape == Enum.PartType.Cylinder then
143142
local size = part.Size
144143
local height = size.x
145-
local radius = math.min(size.y, size.z)
146-
self:_fillCylinder(part.CFrame * CFrame.Angles(0, 0, math.pi/2), height, radius, material)
144+
local radius = math.min(size.y, size.z) / 2
145+
self:_fillCylinder(part.CFrame * CFrame.Angles(0, 0, math.pi / 2), height, radius, material)
147146
else
148147
warn(("[PartToTerrain] - Bad part.Shape, '%s' is not supported"):format(tostring(part.Shape.Name)))
149148
return false
@@ -157,7 +156,7 @@ function TerrainConverter:_convertPart(part, material)
157156
end
158157

159158
function TerrainConverter:_fillWedge(wedgeCFrame, wedgeSize, desiredMaterial)
160-
if (self.OverwriteTerrain.Value and self.OverwriteWater.Value) then
159+
if self.OverwriteTerrain.Value and self.OverwriteWater.Value then
161160
Workspace.Terrain:FillWedge(wedgeCFrame, wedgeSize, desiredMaterial)
162161
return
163162
end
@@ -166,7 +165,7 @@ function TerrainConverter:_fillWedge(wedgeCFrame, wedgeSize, desiredMaterial)
166165
end
167166

168167
function TerrainConverter:_fillCylinder(cylinderCFrame, height, radius, desiredMaterial)
169-
if (self.OverwriteTerrain.Value and self.OverwriteWater.Value) then
168+
if self.OverwriteTerrain.Value and self.OverwriteWater.Value then
170169
Workspace.Terrain:FillCylinder(cylinderCFrame, height, radius, desiredMaterial)
171170
return
172171
end
@@ -175,7 +174,7 @@ function TerrainConverter:_fillCylinder(cylinderCFrame, height, radius, desiredM
175174
end
176175

177176
function TerrainConverter:_fillBlock(blockCFrame, blockSize, desiredMaterial)
178-
if (self.OverwriteTerrain.Value and self.OverwriteWater.Value) then
177+
if self.OverwriteTerrain.Value and self.OverwriteWater.Value then
179178
Workspace.Terrain:FillBlock(blockCFrame, blockSize, desiredMaterial)
180179
return
181180
end
@@ -185,22 +184,22 @@ function TerrainConverter:_fillBlock(blockCFrame, blockSize, desiredMaterial)
185184

186185
-- https://pastebin.com/S03Q8ftH
187186

188-
local aa_size, aa_position = BoundingBoxUtils.getBoundingBox({{
189-
Size = blockSize;
190-
CFrame = blockCFrame;
191-
}})
187+
local aa_size, aa_position = BoundingBoxUtils.getBoundingBox({ {
188+
Size = blockSize,
189+
CFrame = blockCFrame,
190+
} })
192191

193192
local smallestSize = math.min(blockSize.x, blockSize.y, blockSize.z)
194193
local resolution = self.RESOLUTION
195194

196195
-- make sure to get all grids
197-
local min = aa_position - aa_size/2
198-
local max = aa_position + aa_size/2
196+
local min = aa_position - aa_size / 2
197+
local max = aa_position + aa_size / 2
199198
local region = Region3.new(min, max):ExpandToGrid(resolution)
200-
min = region.CFrame.p - region.Size/2
199+
min = region.CFrame.p - region.Size / 2
201200

202201
-- luacheck: push ignore
203-
max = region.CFrame.p + region.Size/2
202+
max = region.CFrame.p + region.Size / 2
204203
-- luacheck: pop ignore
205204

206205
local materialVoxels, occupancyVoxels = Workspace.Terrain:ReadVoxels(region, resolution)
@@ -212,22 +211,20 @@ function TerrainConverter:_fillBlock(blockCFrame, blockSize, desiredMaterial)
212211

213212
-- Since we only care about the size if it's less than one cell, we clamp this to make the calculations below faster.
214213
local sizeCellClamped = (blockSize / resolution)
215-
sizeCellClamped = Vector3.new(
216-
math.min(1, sizeCellClamped.x),
217-
math.min(1, sizeCellClamped.y),
218-
math.min(1, sizeCellClamped.z))
214+
sizeCellClamped =
215+
Vector3.new(math.min(1, sizeCellClamped.x), math.min(1, sizeCellClamped.y), math.min(1, sizeCellClamped.z))
219216
local sizeCellsHalfOffset = blockSize * (0.5 / resolution) + Vector3.new(0.5, 0.5, 0.5)
220217

221-
for x=1, size.X do
218+
for x = 1, size.X do
222219
local cellPosX = min.x + (x - 0.5) * resolution
223-
for y=1, size.Y do
220+
for y = 1, size.Y do
224221
local cellPosY = min.y + (y - 0.5) * resolution
225-
for z=1, size.Z do
222+
for z = 1, size.Z do
226223
local cellPosZ = min.z + (z - 0.5) * resolution
227224
local position = Vector3.new(cellPosX, cellPosY, cellPosZ)
228225

229226
-- -0.5 to 0.5
230-
local offset = blockCFrame:pointToObjectSpace(position)/resolution
227+
local offset = blockCFrame:pointToObjectSpace(position) / resolution
231228

232229
-- Draw.Point(position)
233230

@@ -269,7 +266,7 @@ function TerrainConverter:_fillBlock(blockCFrame, blockSize, desiredMaterial)
269266
end
270267

271268
function TerrainConverter:_fillBall(center, radius, desiredMaterial)
272-
if (self.OverwriteTerrain.Value and self.OverwriteWater.Value) then
269+
if self.OverwriteTerrain.Value and self.OverwriteWater.Value then
273270
Workspace.Terrain:FillBall(center, radius, desiredMaterial)
274271
return
275272
end
@@ -283,23 +280,23 @@ function TerrainConverter:_fillBall(center, radius, desiredMaterial)
283280
local max = center + radius3
284281
local region = Region3.new(min, max):ExpandToGrid(resolution)
285282

286-
min = region.CFrame.p - region.Size/2
283+
min = region.CFrame.p - region.Size / 2
287284
-- luacheck: push ignore
288-
max = region.CFrame.p + region.Size/2
285+
max = region.CFrame.p + region.Size / 2
289286
-- luacheck: pop ignore
290287

291288
local materialVoxels, occupancyVoxels = Workspace.Terrain:ReadVoxels(region, resolution)
292289
local size = materialVoxels.Size
293-
for x=1, size.X do
290+
for x = 1, size.X do
294291
local cellX = min.x + (x - 0.5) * resolution - center.x
295-
for y=1, size.Y do
292+
for y = 1, size.Y do
296293
local cellY = min.y + (y - 0.5) * resolution - center.y
297-
for z=1, size.Z do
294+
for z = 1, size.Z do
298295
local cellZ = min.z + (z - 0.5) * resolution - center.z
299296

300297
local cellMaterial = materialVoxels[x][y][z]
301298
local cellOccupancy = occupancyVoxels[x][y][z]
302-
local distance = math.sqrt(cellX*cellX + cellY*cellY + cellZ*cellZ)
299+
local distance = math.sqrt(cellX * cellX + cellY * cellY + cellZ * cellZ)
303300
local brushOccupancy = math.max(0, math.min(1, (radius + 0.5 * resolution - distance) / resolution))
304301

305302
-- Use terrain tools filling behavior here
@@ -326,4 +323,4 @@ function TerrainConverter:_fillBall(center, radius, desiredMaterial)
326323
Workspace.Terrain:WriteVoxels(region, resolution, materialVoxels, occupancyVoxels)
327324
end
328325

329-
return TerrainConverter
326+
return TerrainConverter

0 commit comments

Comments
 (0)