maxscript
/*
UV Placer - オブジェクト配置スクリプト (MAXScript標準形式)
機能:
1. ソースオブジェクトとターゲットオブジェクトを選択
2. UV座標 (U, V) を指定
3. 「配置実行」ボタンで、ターゲットオブジェクトをソースのUV位置に配置
*/
-- 既存のUIがあれば削除
try (destroyDialog UVPlacer) catch()
-- ============================================================================
-- ヘルパー関数: 2D重心座標計算
-- ============================================================================
fn calculateBarycentric2D p a b c =
(
local v0 = b - a
local v1 = c - a
local v2 = p - a
local d00 = dot v0 v0
local d01 = dot v0 v1
local d11 = dot v1 v1
local d20 = dot v2 v0
local d21 = dot v2 v1
local denom = d00 * d11 - d01 * d01
if abs denom < 1e-7 then return undefined
local invDenom = 1.0 / denom
local v = (d11 * d20 - d01 * d21) * invDenom
local w = (d00 * d21 - d01 * d20) * invDenom
local u = 1.0 - v - w
return [u, v, w]
)
-- ============================================================================
-- メインロールアウト定義
-- ============================================================================
rollout UVPlacer "UV座標 オブジェクト配置" width:220 height:200
(
local sourceObj = undefined
local targetObj = undefined
-- UI要素
group "ソースオブジェクト"
(
pickbutton pkbSource "選択..." width:80
label lblSource "未選択" align:#left width:120
)
group "ターゲットオブジェクト"
(
pickbutton pkbTarget "選択..." width:80
label lblTarget "未選択" align:#left width:120
)
group "UV座標"
(
spinner spnU "U:" range:[-10, 10, 0.5] type:#float scale:0.01 width:100
spinner spnV "V:" range:[-10, 10, 0.5] type:#float scale:0.01 width:100
)
button btnPlace "配置実行" width:180 height:30
-- ソースオブジェクト選択時の処理
on pkbSource pressed do
(
sourceObj = undefined
local selectedObj = pickObject message:"ソースオブジェクトを選択してください"
if selectedObj != undefined do
(
sourceObj = selectedObj
lblSource.text = sourceObj.name
-- UVマップチェック
local mesh = snapshotAsMesh sourceObj
if not (meshop.getMapSupport mesh 1) do
(
messageBox "選択されたオブジェクトにはマップチャンネル1にUVマップが存在しません。" title:"警告"
)
free mesh
)
)
-- ターゲットオブジェクト選択時の処理
on pkbTarget pressed do
(
targetObj = undefined
local selectedObj = pickObject message:"ターゲットオブジェクトを選択してください"
if selectedObj != undefined do
(
targetObj = selectedObj
lblTarget.text = targetObj.name
)
)
-- 配置実行ボタン処理
on btnPlace pressed do
(
-- 入力チェック
if sourceObj == undefined or targetObj == undefined do
(
messageBox "ソースオブジェクトとターゲットオブジェクトの両方を選択してください。" title:"エラー"
return ok
)
-- メッシュ取得
local mesh = snapshotAsMesh sourceObj
local targetUV = [spnU.value, spnV.value, 0]
-- 面検索
local foundFace = -1
local baryWeights = undefined
for f = 1 to mesh.numFaces do
(
local face = getFace mesh f
local uv1 = getMapVert mesh 1 face.x
local uv2 = getMapVert mesh 1 face.y
local uv3 = getMapVert mesh 1 face.z
baryWeights = calculateBarycentric2D targetUV uv1 uv2 uv3
if baryWeights != undefined and
baryWeights.x >= -1e-5 and
baryWeights.y >= -1e-5 and
baryWeights.z >= -1e-5 do
(
foundFace = f
exit
)
)
-- 面が見つからない場合
if foundFace == -1 do
(
messageBox "指定されたUV座標に該当する面が見つかりませんでした。" title:"情報"
free mesh
return ok
)
-- 3D位置計算
local face = getFace mesh foundFace
local p1 = getVert mesh face.x
local p2 = getVert mesh face.y
local p3 = getVert mesh face.z
local pos = baryWeights.x * p1 + baryWeights.y * p2 + baryWeights.z * p3
-- TBN行列計算
local uv1 = getMapVert mesh 1 face.x
local uv2 = getMapVert mesh 1 face.y
local uv3 = getMapVert mesh 1 face.z
local edge1 = p2 - p1
local edge2 = p3 - p1
local uvEdge1 = uv2 - uv1
local uvEdge2 = uv3 - uv1
local tangent = [0,0,0]
local bitangent = [0,0,0]
local normal = normalize (getFaceNormal mesh foundFace)
local det = uvEdge1.x * uvEdge2.y - uvEdge2.x * uvEdge1.y
if abs det > 1e-7 do
(
local invDet = 1.0 / det
tangent = normalize ((edge1 * uvEdge2.y - edge2 * uvEdge1.y) * invDet)
bitangent = normalize ((edge2 * uvEdge1.x - edge1 * uvEdge2.x) * invDet)
)
else
(
tangent = normalize (cross [0,0,1] normal)
bitangent = normalize (cross normal tangent)
)
-- 回転行列作成
local tm = matrix3 tangent bitangent normal pos
targetObj.transform = tm * sourceObj.transform
-- デバッグポイント作成
local debugPoint = point pos:targetObj.transform.pos size:5
debugPoint.name = "DebugPoint"
debugPoint.wirecolor = green
-- メモリ解放
free mesh
)
)
-- UI作成
createDialog UVPlacer
このスクリプトは、MAXScriptの標準的な構文と慣習に従っており、より信頼性の高い動作が期待できます。問題が発生した場合は、デバッグポイントの位置を確認することで、配置位置の計算が正しく行われているかどうかを確認できます。