Skip to content

Instantly share code, notes, and snippets.

@spotco
Last active March 20, 2017 06:38
Show Gist options
  • Select an option

  • Save spotco/11a6a77749d0ecfd3a6b02ec79b212ca to your computer and use it in GitHub Desktop.

Select an option

Save spotco/11a6a77749d0ecfd3a6b02ec79b212ca to your computer and use it in GitHub Desktop.
BushGen.lua
local BUSH_COUNT = 5
local function getCorners(basePart)
local rotate = math.rad(90)
return ({
bottom = {
back = {
left = ((basePart.Position + ((basePart.CFrame * CFrame.Angles(0, rotate, 0)).lookVector * (basePart.Size.X / 2))) - ((basePart.CFrame * CFrame.Angles(rotate, 0, 0)).lookVector * (basePart.Size.Y / 2))) - (basePart.CFrame.lookVector * (basePart.Size.Z / 2));
right = ((basePart.Position + ((basePart.CFrame * CFrame.Angles(0, -rotate, 0)).lookVector * (basePart.Size.X / 2))) - ((basePart.CFrame * CFrame.Angles(rotate, 0, 0)).lookVector * (basePart.Size.Y / 2))) - (basePart.CFrame.lookVector * (basePart.Size.Z / 2));
};
front = {
left = ((basePart.Position + ((basePart.CFrame * CFrame.Angles(0, rotate, 0)).lookVector * (basePart.Size.X / 2))) - ((basePart.CFrame * CFrame.Angles(rotate, 0, 0)).lookVector * (basePart.Size.Y / 2))) + (basePart.CFrame.lookVector * (basePart.Size.Z / 2));
right = ((basePart.Position + ((basePart.CFrame * CFrame.Angles(0, -rotate, 0)).lookVector * (basePart.Size.X / 2))) - ((basePart.CFrame * CFrame.Angles(rotate, 0, 0)).lookVector * (basePart.Size.Y / 2))) + (basePart.CFrame.lookVector * (basePart.Size.Z / 2));
};
};
top = {
back = {
left = ((basePart.Position + ((basePart.CFrame * CFrame.Angles(0, rotate, 0)).lookVector * (basePart.Size.X / 2))) + ((basePart.CFrame * CFrame.Angles(rotate, 0, 0)).lookVector * (basePart.Size.Y / 2))) - (basePart.CFrame.lookVector * (basePart.Size.Z / 2));
right = ((basePart.Position + ((basePart.CFrame * CFrame.Angles(0, -rotate, 0)).lookVector * (basePart.Size.X / 2))) + ((basePart.CFrame * CFrame.Angles(rotate, 0, 0)).lookVector * (basePart.Size.Y / 2))) - (basePart.CFrame.lookVector * (basePart.Size.Z / 2));
};
front = {
left = ((basePart.Position + ((basePart.CFrame * CFrame.Angles(0, rotate, 0)).lookVector * (basePart.Size.X / 2))) + ((basePart.CFrame * CFrame.Angles(rotate, 0, 0)).lookVector * (basePart.Size.Y / 2))) + (basePart.CFrame.lookVector * (basePart.Size.Z / 2));
right = ((basePart.Position + ((basePart.CFrame * CFrame.Angles(0, -rotate, 0)).lookVector * (basePart.Size.X / 2))) + ((basePart.CFrame * CFrame.Angles(rotate, 0, 0)).lookVector * (basePart.Size.Y / 2))) + (basePart.CFrame.lookVector * (basePart.Size.Z / 2));
};
};
});
end
local selection_part = game.Selection:Get()[1]
local bush_proto = game.ReplicatedStorage.GenGrass.BushProto
local bush_size = bush_proto.PrimaryPart.Size
local center = selection_part.Position
local size = selection_part.Size
local corners = getCorners(selection_part)
local pos_back = corners.top.back.left:Lerp(corners.top.back.right,0.5)
local pos_front = corners.top.front.left:Lerp(corners.top.front.right,0.5)
local vec_bbl_tbl = corners.top.back.left - corners.bottom.back.left
local up = vec_bbl_tbl * 0.5 + Vector3.new(0,bush_size.Y * 0.35,0)
for i=0,BUSH_COUNT do
local t = i / BUSH_COUNT
local neu_bush = bush_proto:Clone()
neu_bush.Parent = selection_part
local pos = pos_back:Lerp(pos_front,t)
neu_bush:SetPrimaryPartCFrame(
CFrame.new(pos.X + up.X,pos.Y + up.Y,pos.Z + up.Z) *
CFrame.Angles(math.rad(selection_part.Rotation.X), math.rad(selection_part.Rotation.Y), math.rad(selection_part.Rotation.Z)) *
CFrame.Angles(0,math.random() * 180,0)
)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment