モデルはコンテナオブジェクトであり、オブジェクトをグループ化します。彼らは BaseParts のコレクションを保持するのに最適で、機能を拡張する複数の機能があります。
モデルは 幾何学的 グループを表すことを目的としています。グループ化に幾何学的な解釈がない場合、たとえば Scripts のコレクション、代わりに Folder を使用します。
構成部品がジョイントで結合されているモデル (物理シミュレーションで移動したり、破壊されたりできる) は通常、PrimaryPart セットを持っています。モデルが移動すると、ピボットとバインディングボックスが "モデルにフォローう" 部分を指定します。1つの場所に留まる静的モデ設定するは、プライマリパーツセットを持つことで恩恵を受けません。
モデルには、Roblox プレイヤーキャラクターを含む幅広い適用範囲があります。また、留意すべき多くのユニークな動作があります:
- Workspace.StreamingEnabled を trueに設定した場所で使用すると、モデルとその子孫がクライアントから複製されたり削除されたりする方法の周りのさまざまな動作が値 ModelStreamingMode によって制御されます。さらに、LevelOfDetail の値はモデルのレンダリングに影響します。
すべての Instance タイプと同様、親 Model がクライアントにレプリケートされることは、すべての子がレプリケートされることを保証しません。これは、特に、これらのインスタンスがクライアント上で実行されるコードによってアクセスされている場合、例えば LocalScript です。親モデルがクライアント上に存在する場合、またはアトミック性が必要でない場合は、 といった値を使用して、モデル全体とすべての子モデルが存在するようにすることができます。
コードサンプル
The following sample includes a basic function that takes a table of objects and parents them into a new Model, returning that Model.
local function groupObjects(objectTable)
local model = Instance.new("Model")
for _, object in pairs(objectTable) do
object.Parent = model
end
return model
end
local objects = {
Instance.new("Part"),
Instance.new("Part"),
}
groupObjects(objects)
概要
プロパティ
インスタンスストリーミングを有効にしたエクスペリエンスのモデルの詳細レベルを設定します。
インスタンスストリーミングが有効になっているときに、モデルストリーミングの動作を制御します Models 。
The primary part of the Model 、または nil が明示的に設定する定されていない場合
モデルの周りにスケールを拡大するために使用される編集者専用のプロパティ。このプロパティを設定すると、スケールが Model/ScaleTo を呼び出したように移動します。
どこに Model が しない を持つピボットが位置するかを決定します。Determines where the pivot of a > which does not have a set Model.PrimaryPart is located.
方法
指定されたプレイヤーのために、このモデルを永続するように設定します。Model.ModelStreamingMode は、追加により変更されるため、 PersistentPerPlayer に設定する必要があります。
モデルのすべての部分を含むボリュームの説明を返します。
返すのは、BaseParts すべてを含む最小のバインディングボックスのサイズ、そして Model に沿って設定されている場合、Model.PrimaryPart と一致します。
このモデルオブジェクトが持続するすべての Player オブジェクトを返します。動作は、このメソッドが Script または LocalScript から呼ばれるかどうかによって異なります。
モデルの正規スケールを返し、新しく作成されたモデルにはデフォルトで 1 が割り当てられ、Model/ScaleTo を介してスケールすると変更されます。
PrimaryPart を指定された位置に移動します。プライマリパーツが指定されていない場合、モデルのルートパーツが使用されます。
指定されたプレイヤーに対して、このモデルを永続しなくする。Model.ModelStreamingMode は、削除により変更されるため、 PersistentPerPlayer に設定する必要があります。
モデルのスケール因子を設定し、すべての子インスタンスのサイズと場所を調整して、スケール因子が 1 のときと比較して、そのスケール因子が初期のサイズと場所に関連しているようにします。
指定された オフセットによってシフトし、モデルの向きを保持します。新しい位置に別の BasePart または Terrain がすでに存在する場合、Model はそのオブジェクトをオーバーライドします。
PVInstance のピボットを取得します。
変換する PVInstance とすべての子孫 PVInstances を通じて、ピボットが現在指定された CFrame に位置します。
プロパティ
LevelOfDetail
インスタンス ストリーミング を有効にしたエクスペリエンスのモデルの詳細レベルを設定します。
StreamingMesh に設定すると、解像度の低い「インポスター」メッシュ(モデルのすべての子部品を包む粗いカラーメッシュ)がストリーミング範囲外にレンダリングされます。
ModelStreamingMode
インスタンスが ストリーミング を有効にしたときに、Models がどのようにストリームされ入出しされるかを制御します。動作は選択された枚数に依存します。ストリーミングが有効になっていない場合、効果はありません。
このプロパティは、ストリーミングが有効になっているときに Studio の プロパティ ウィンドウを通じて変更する必要があり、または Scripts で、決して LocalScripts (そうすると未定義の動作が発生する可能性があります)ではありません。
PrimaryPart
Model の主要な部分にポイントします。主な部分は、モデルのピボットの物理参照として機能する BasePart です。つまり、モデル内のパーツが物理シミュレーションまたは他の手段により移動すると、ピボットは主要なパーツと同期して移動します。
注: Models はデフォルトで PrimaryPart が設定されていません。物理によって実行する必要があるモデルを作成している場合、Studio またはスクリプト内でこのプロパティを手動で設定する必要があります。メインパーツが ではなく 設定されている場合、モデル内のパーツが移動されても、ピボットは世界空間の同じ場所に残ります。
このプロパティを設定するときは、モデルの子孫である BasePart が必須であることに注意してください。モデルの子孫ではない を に設定しようとすると、モデルの子孫ではない が設定されますが、次のシミュレーションステップでリセットされます—これは、モデルの子孫ではない を一時的に設定できると仮定するスクリプトをサポートするためのレガシー動作です。
モデルの一般的なルールは次のとおりです:
- モデルのパーツが WeldConstraints または Motor6Ds のような物理ジョイントで結合されているモデルは、プライマリパーツが割り当てられる必要があります。たとえば、Roblox キャラクターモデルは、デフォルトで Model.PrimaryPart を HumanoidRootPart に設定します。
- スクリプトが明示的に移動しない限り、1か所に留まる静的 (通常 Anchored ) モデルは、Model.PrimaryPart が必要ない傾向があり、1設定するのセットを持つことの恩恵を受けない。
コードサンプル
This code sample creates and throws a dice model, then outputs whether it landed with the blue side facing up. If Model.PrimaryPart was not set, the pivot / bounding box of the dice would rotate, as the parts inside of it are physically simulated during the roll.
-- Create a dice model with two halves and attach them together
local diceModel = Instance.new("Model")
diceModel.Name = "ChanceCube"
local diceTop = Instance.new("Part")
diceTop.Size = Vector3.new(4, 2, 4)
diceTop.Position = Vector3.new(0, 1, 0)
diceTop.Color = Color3.new(0, 0, 1)
diceTop.Parent = diceModel
local diceBottom = diceTop:Clone()
diceBottom.Position = Vector3.new(0, -1, 0)
diceBottom.Color = Color3.new(1, 0, 0)
diceBottom.Parent = diceModel
local weld = Instance.new("WeldConstraint")
weld.Part0 = diceTop
weld.Part1 = diceBottom
weld.Parent = diceModel
-- Put the dice up in the air above the workspace origin (does not require a primary part)
diceModel.Parent = workspace
diceModel:PivotTo(CFrame.new(0, 10, 0))
-- Assign the primary part before physical simulation
-- Without this line, the script will always output the same thing and the bounding box of the model will not change orientation
diceModel.PrimaryPart = diceTop
-- Wait a bit before rolling the dice (let it settle onto the floor)
for i = 5, 1, -1 do
print("Rolling dice in...", i)
task.wait(1)
end
diceTop:ApplyAngularImpulse(Vector3.new(15000, 1000, 5000))
diceTop:ApplyImpulse(Vector3.new(0, 3000, 0))
task.wait(1)
-- Wait for the roll to complete
while diceTop.AssemblyLinearVelocity.Magnitude > 0.1 or diceTop.AssemblyAngularVelocity.Magnitude > 0.1 do
task.wait()
end
-- Get the dice orientation, impacted by the primary part
local orientation = diceModel:GetBoundingBox()
if orientation.YVector.Y > 0.5 then
print("It's the boy!")
else
print("It's his mother!")
end
Scale
プロパティウィンドウでこのプロパティを設定すると、Model/ScaleTo が呼び出されたようにモデルがスケールするため、マテリアル、画像、および部品の 3D ジオメトリなどのすべての後続インスタンスがモデルにスケールされ、モデルが原始サイズに対して指定されたスケール因子を持つようになります。
このプロパティはスタジオでのみ利用可能であり、Script または LocalScript で使用するとエラーが発生します。Model/ScaleTo と Model/GetScale は、スクリプトから使用する必要があります。
WorldPivot
このプロパティは、Model が 持っていない ピボットの場所を決定します。これは、Model.PrimaryPart が設定されている場所です。If the Model does に PrimaryPart がある場合、Model のピボットは、代わりにその主要部分のピボットと同等であり、この WorldPivot プロパティは無視されます。
新しく作成された Model には、 最初の時点 に Model.WorldPivot プロパティが設定されるまで、そのピボットはそのコンテンツのバインディングボックスの中心として扱います。世界軸が初めて設定されたら、この最初の動作を復元することは不可能です。
一般的に、Studio ツールまたはモデル移動機能 (PVInstance:PivotTo() や Model:MoveTo() など) を使用してモデルを移動すると、ワールドの軸が設定され、この新しいモデル動作が終了します。
この振る舞いの目的は、Luau コードが新しいモデルを作成し、それに親を付けることによって単純に意味のあるピボットを取得できるようにすることで、コードでモデルを作成するたびに Model.WorldPivot を明示的に設定する必要を避けることです。
local Workspace = game:GetService("Workspace")local model = Instance.new("Model")Workspace.BluePart.Parent = modelWorkspace.RedPart.Parent = modelmodel.Parent = Workspaceprint(model:GetPivot()) -- Currently equal to the center of the bounding box containing "BluePart" and "RedPart"model:PivotTo(CFrame.new(0, 10, 0)) -- This works without needing to explicitly set "model.WorldPivot"
コードサンプル
This code sample shows a custom function for resetting the pivot of a model back to the center of that model's bounding box.
local function resetPivot(model)
local boundsCFrame = model:GetBoundingBox()
if model.PrimaryPart then
model.PrimaryPart.PivotOffset = model.PrimaryPart.CFrame:ToObjectSpace(boundsCFrame)
else
model.WorldPivot = boundsCFrame
end
end
resetPivot(script.Parent)
方法
GetBoundingBox
この関数は、BasePart 内のすべての子供を含むボリュームの説明を返します。この Model 内。ボリュームの向きは、PrimaryPart の向きに基づいており、モデルが選択されたときにStudioでレンダリングされた選択ボックスと一致します。Terrain:FillBlock() の動作をミラーリングし、そのバインディングボックスの中心を表す CFrame と、サイズを表す Vector3 を返します。
モデルに PrimaryPart がない場合、バインディングボックスは世界軸に沿われます。
local Workspace = game:GetService("Workspace")local model = Workspace.Modellocal part = Workspace.Partlocal orientation, size = model:GetBoundingBox()-- モデルのバインディングボックスと同じ大きさと位置のパーツを再設定するpart.Size = sizepart.CFrame = orientation
戻り値
GetExtentsSize
すべての を含む最小のバインディングボックスのサイズを返します。Model.PrimaryPart が存在する場合、バインディングボックスはその部分に沿われます。主要なパーツが設定されていない場合、機能はモデル内のパーツを選択してバインディングボックスを揃えます。この部分の選択は決定的ではないので、この機能と一貫した結果を得るために Model.PrimaryPart を設定することをお勧めします。
この関数は、最小のバインディングボックスのサイズのみを返し、開発者はバインディングボックスの位置を取得するために独自のメソッドを使用する必要があります。
戻り値
コードサンプル
The code sample below demonstrates how Model.GetExtentsSize can be used to get the size of the bounding box containing the parts.
local model = Instance.new("Model")
model.Parent = workspace
local RNG = Random.new()
for _ = 1, 5 do
local part = Instance.new("Part")
part.Anchored = true
part.Size = Vector3.new(RNG:NextNumber(0.05, 5), RNG:NextNumber(0.05, 5), RNG:NextNumber(0.05, 5))
part.Parent = model
end
print(model:GetExtentsSize())
GetPersistentPlayers
このメソッドが Script から呼び出されると、このモデルが持続するすべての Player オブジェクトが返されます。LocalScript から呼び出されると、このメソッドは、このモデルが LocalPlayer に永続するかどうかのみをチェックします。
戻り値
このモデルオブジェクトが持続するすべての Player オブジェクトを含むテーブル。
GetScale
モデルには、新しく作成されたモデルから 1 で始まる永続的なカノニカルスケールファクターが含まれており、モデルが Model/ScaleTo を呼び出すと変更されます。この関数は、モデルの現在の標準スケール因子を返します。
現在のスケールファクターは、直接 モデルの下のインスタンスのサイズに影響しません。モデルが元のサイズに対してどのようにスケーリングされたかを記憶するために、コンテンツ作成およびスクリプト用途に使用されます。
指定されたセッション内で、モデルは最初の Model/ScaleTo 呼び出しの後、子孫インスタンスの正確なオリジナルサイズ情報をキャッシュします。これは、ScaleTo(x) に続いて ScaleTo(1) を呼び出すと、浮動小数点ドリフトなしでモデルのオリジナル構成を正確に取り戻すことを意味します。浮動小数点ドリフトを避けることは、スケール 機能の代わりにスケール 機能を持つ動機です。
スケールファクターは、1つの方法でエンジンの動作に影響します:モデルのスケールファクターが、そのモデルの下で 演奏された共同オフセットに適用され、アニメーテッドリグがスケーリングされているときでも正しくアニメーションが再生されます。
戻り値
モデルの現在の標準スケール因子。
コードサンプル
This code sample demonstrates substituting in a replacement for all the copies of a tree model using PivotTo and ScaleTo. The pivot and scale of the models are used as a reference ensuring that the relative sizes and locations of the replacement models match those of the originals.
local CollectionService = game:GetService("CollectionService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
-- Find all the models with the tag we want to replace
local items = CollectionService:GetTagged("Tree")
local newModel = ReplicatedStorage.FancyTreeReplacementModel
for _, item in items do
-- Make the new item and scale / position it where the old one was
local newItem = newModel:Clone()
newItem:ScaleTo(item:GetScale())
newItem:PivotTo(item:GetPivot())
-- Add the same tag to the replacement
CollectionService:AddTag(newItem, "Tree")
-- Delete the old item and parent the new one
newItem.Parent = item.Parent
item:Destroy()
end
MoveTo
PrimaryPart を指定された位置に移動します。主要なパーツが指定されていない場合、モデルのルートパーツが使用されますが、ルートパーツは決定的ではなく、MoveTo() を使用するときに常に主要なパーツを設定することをお勧めします。
モデルを移動する障害がある場合、例えば Terrain またはその他の BaseParts がある場合、モデルは通路に何もないまで上向きに移動されます。この動作は望ましくない場合は、PVInstance:PivotTo() 代わりに使用する必要があります。
モデルを移動するときに MoveTo() で回転が保存されないことに注意してください。現在のモデルの回転が保存される必要がある場合は、TranslateBy() または PVInstance:PivotTo() を使用することをお勧めします。
パラメータ
戻り値
コードサンプル
This sample demonstrates how Model:MoveTo avoids collisions.
A simple two part Model is created, and its PrimaryPart is set. An large obstruction part is placed next to it.
After 5 seconds Model:MoveTo is used to direct the model to move inside the obstruction part. However, as MoveTo will not move a model inside of an obstruction the Model is moved up on the Y axis and placed above the obstruction.
local START_POSITION = Vector3.new(-20, 10, 0)
local END_POSITION = Vector3.new(0, 10, 0)
local model = Instance.new("Model")
model.Parent = workspace
local part1 = Instance.new("Part")
part1.Size = Vector3.new(4, 4, 4)
part1.Position = START_POSITION
part1.Anchored = true
part1.BrickColor = BrickColor.new("Bright yellow")
part1.Parent = model
local part2 = Instance.new("Part")
part2.Size = Vector3.new(2, 2, 2)
part2.Position = START_POSITION + Vector3.new(0, 3, 0)
part2.Anchored = true
part2.BrickColor = BrickColor.new("Bright blue")
part2.Parent = model
model.PrimaryPart = part1
model.Parent = workspace
local obstruction = Instance.new("Part")
obstruction.Name = "Obstruction"
obstruction.Size = Vector3.new(10, 10, 10)
obstruction.Position = Vector3.new(0, 10, 0)
obstruction.Anchored = true
obstruction.BrickColor = BrickColor.new("Bright green")
obstruction.Parent = workspace
task.wait(3)
model:MoveTo(END_POSITION)
ScaleTo
モデルには、新しく作成されたモデルで 1 から始まる永続的なカノニカルスケールファクターが含まれています。この関数は、ピボットロケーションの周りでモデルをスケールし、スケール因子 1 で見えるようにします。これを達成するには、2つのことを行います:
- モデルの現在のスケール因子を指定された値に設定する
- すべての子インスタンスを適切に再サイズ化して再配置する
場所のスケーリングは、ピボットロケーションの周りで行われます。
子インスタンスのすべての「幾何学的」プロパティがスケールされます。それは明らかにパーツのサイズを含みますが、次にいくつかのスケールされたプロパティの他の例があります:
- WeldConstraints や Class.Rope|Ropes のような接続の長さ
- 物理速度や力のような Hinge.MaxServoTorque
- パーティクルエミッターのサイズなどの視覚的プロパティ
- Sound.RollOffMinDistance などの他の長さプロパティ
パラメータ
戻り値
TranslateBy
指定された オフセットによってシフトし、モデルの向きを保持します。新しい位置に別の BasePart または Terrain がすでに存在する場合、Model はそのオブジェクトをオーバーライドします。
翻訳はオブジェクトスペースではなくワールドスペースに適用され、モデルのパーツが異なる向きに向いていても、標準の軸に沿って移動し続けます。
パラメータ
戻り値
コードサンプル
This sample demonstrates how Model:TranslateBy ignores collisions and respects the orientation of the model.
A simple two part Model is created, rotated 45 degrees on the Y axis, and its PrimaryPart is set. An large obstruction part is placed next to it.
After 5 seconds Model:TranslateBy is used to direct the model to move inside the obstruction part. The model will move inside of the obstruction and maintain it's current orientation.
local START_POSITION = Vector3.new(-20, 10, 0)
local END_POSITION = Vector3.new(0, 10, 0)
local model = Instance.new("Model")
local part1 = Instance.new("Part")
part1.Size = Vector3.new(4, 4, 4)
part1.CFrame = CFrame.new(START_POSITION) * CFrame.Angles(0, math.rad(45), 0)
part1.Anchored = true
part1.BrickColor = BrickColor.new("Bright yellow")
part1.Parent = model
local part2 = Instance.new("Part")
part2.Size = Vector3.new(2, 2, 2)
part2.CFrame = part1.CFrame * CFrame.new(0, 3, 0)
part2.Anchored = true
part2.BrickColor = BrickColor.new("Bright blue")
part2.Parent = model
model.PrimaryPart = part1
model.Parent = workspace
local obstruction = Instance.new("Part")
obstruction.Name = "Obstruction"
obstruction.Size = Vector3.new(10, 10, 10)
obstruction.Position = Vector3.new(0, 10, 0)
obstruction.Transparency = 0.5
obstruction.Anchored = true
obstruction.BrickColor = BrickColor.new("Bright green")
obstruction.Parent = workspace
task.wait(3)
-- use TranslateBy to shift the model into the obstruction
model:TranslateBy(END_POSITION - START_POSITION)