SDFBakeTool.cs 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.Rendering;
  5. using Object = UnityEngine.Object;
  6. using UnityEngine.VFX.SDF;
  7. namespace UnityEditor.VFX.SDF
  8. {
  9. class SDFBakeTool : EditorWindow
  10. {
  11. [MenuItem("Window/Visual Effects/Utilities/SDF Bake Tool", false, 3013)]
  12. static void OpenWindow()
  13. {
  14. GetWindow<SDFBakeTool>();
  15. }
  16. [SerializeField]
  17. private SdfBakerSettings m_Settings;
  18. private SerializedObject m_SettingsSO;
  19. private RenderTexture m_BakedSDF;
  20. private SdfBakerPreview m_MeshPreview;
  21. private Texture3DPreview m_TexturePreview;
  22. private bool m_RefreshMeshPreview = false;
  23. private bool m_ShowAdvanced;
  24. private MeshToSDFBaker m_Baker;
  25. private bool m_FoldOutParameters = true;
  26. private bool m_PrefabChanged = false;
  27. private bool m_LiveUpdate = false;
  28. private int maxResolution
  29. {
  30. get { return m_Settings.m_MaxResolution; }
  31. set { m_Settings.m_MaxResolution = value; }
  32. }
  33. private Vector3 boxSizeReference
  34. {
  35. get { return m_Settings.m_BoxSizeReference; }
  36. set { m_Settings.m_BoxSizeReference = value; }
  37. }
  38. private Vector3 boxCenter
  39. {
  40. get { return m_Settings.m_BoxCenter; }
  41. set { m_Settings.m_BoxCenter = value; }
  42. }
  43. private int signPassesCount
  44. {
  45. get { return m_Settings.m_SignPassesCount; }
  46. set { m_Settings.m_SignPassesCount = value; }
  47. }
  48. private float inOutThreshold
  49. {
  50. get { return m_Settings.m_InOutThreshold; }
  51. set { m_Settings.m_InOutThreshold = value; }
  52. }
  53. private float surfaceOffset
  54. {
  55. get { return m_Settings.m_SurfaceOffset; }
  56. set { m_Settings.m_SurfaceOffset = value; }
  57. }
  58. private ModelSource modelSource
  59. {
  60. get { return m_Settings.m_ModelSource; }
  61. set { m_Settings.m_ModelSource = value; }
  62. }
  63. private PreviewChoice previewObject
  64. {
  65. get { return m_Settings.m_PreviewObject; }
  66. set { m_Settings.m_PreviewObject = value; }
  67. }
  68. private Mesh selectedMesh
  69. {
  70. get { return m_Settings.m_SelectedMesh; }
  71. set { m_Settings.m_SelectedMesh = value; }
  72. }
  73. private GameObject meshPrefab
  74. {
  75. get { return m_Settings.m_MeshPrefab; }
  76. set { m_Settings.m_MeshPrefab = value; }
  77. }
  78. private Mesh mesh
  79. {
  80. get { return m_Settings.m_Mesh; }
  81. set { m_Settings.m_Mesh = value; }
  82. }
  83. [SerializeField]
  84. private Vector3 m_ActualBoxSize;
  85. protected void OnGUI()
  86. {
  87. if (m_Settings == null)
  88. {
  89. m_Settings = CreateInstance<SdfBakerSettings>();
  90. m_Settings.name = "None";
  91. }
  92. bool needsUpdate = false;
  93. Undo.RecordObject(this, "Settings Asset change");
  94. Undo.RecordObject(m_Settings, "SDF Baker Parameter change");
  95. GUILayout.BeginHorizontal();
  96. EditorGUI.BeginChangeCheck();
  97. var newSettings = (SdfBakerSettings)EditorGUILayout.ObjectField(Contents.settingsAsset, m_Settings, typeof(SdfBakerSettings), true, GUILayout.MinWidth(20),
  98. GUILayout.MaxWidth(400), GUILayout.ExpandWidth(true));
  99. if (EditorGUI.EndChangeCheck())
  100. {
  101. if (newSettings != null)
  102. {
  103. LoadSettings(newSettings);
  104. }
  105. else
  106. {
  107. CreateNewSession();
  108. }
  109. }
  110. GUILayout.Space(5);
  111. GUI.enabled = EditorUtility.IsDirty(m_Settings);
  112. if (GUILayout.Button(Contents.saveSettings, GUILayout.MinWidth(20), GUILayout.ExpandWidth(true)))
  113. {
  114. SaveSettings();
  115. }
  116. GUI.enabled = true;
  117. DrawContextIcon();
  118. GUILayout.EndHorizontal();
  119. GUILayout.Space(5);
  120. EditorGUI.BeginChangeCheck();
  121. maxResolution = Mathf.Clamp(EditorGUILayout.IntField(Contents.maxResolution, maxResolution), 8, 1024);
  122. bool maxResChanged = EditorGUI.EndChangeCheck();
  123. needsUpdate |= maxResChanged;
  124. if (maxResolution > 255)
  125. EditorGUILayout.HelpBox(
  126. "Higher resolutions are more expensive to calculate and can make Unity unstable.",
  127. MessageType.Warning);
  128. EditorGUI.BeginChangeCheck();
  129. var prevWideMode = EditorGUIUtility.wideMode;
  130. EditorGUIUtility.wideMode = true;
  131. boxCenter = EditorGUILayout.Vector3Field("Box Center", boxCenter);
  132. needsUpdate |= EditorGUI.EndChangeCheck();
  133. EditorGUI.BeginChangeCheck();
  134. boxSizeReference = EditorGUILayout.Vector3Field(Contents.boxSizeReference, boxSizeReference);
  135. bool boxSizeChanged = EditorGUI.EndChangeCheck();
  136. needsUpdate |= boxSizeChanged;
  137. if (boxSizeChanged || maxResChanged)
  138. {
  139. m_ActualBoxSize = SnapBoxToVoxels();
  140. }
  141. using (new EditorGUI.DisabledScope(true))
  142. {
  143. SerializedObject selfSO = new SerializedObject(this);
  144. EditorGUILayout.PropertyField(selfSO.FindProperty("m_ActualBoxSize"), Contents.actualBoxSize);
  145. }
  146. if (boxSizeReference.x * boxSizeReference.y * boxSizeReference.z <= Single.Epsilon)
  147. EditorGUILayout.HelpBox("The volume of your bounding box is zero.", MessageType.Warning);
  148. int estimatedGridSize = EstimateGridSize();
  149. if (estimatedGridSize > 1 << 21 || selectedMesh == null)
  150. {
  151. GUI.enabled = false;
  152. m_LiveUpdate = false;
  153. }
  154. EditorGUI.BeginChangeCheck();
  155. m_LiveUpdate = EditorGUILayout.Toggle(Contents.liveUpdate, m_LiveUpdate);
  156. needsUpdate |= (EditorGUI.EndChangeCheck() & m_LiveUpdate);
  157. GUI.enabled = true;
  158. if (m_LiveUpdate)
  159. EditorGUILayout.HelpBox(
  160. "Baking the mesh in real-time might cause slowdowns or instabilities when the resolution and/or the sign passes count is high ",
  161. MessageType.Warning);
  162. bool fitPaddingChanged = false;
  163. if (m_ShowAdvanced)
  164. {
  165. m_FoldOutParameters = EditorGUILayout.BeginFoldoutHeaderGroup(m_FoldOutParameters, Contents.bakingParameters);
  166. EditorGUI.BeginChangeCheck();
  167. if (m_FoldOutParameters)
  168. {
  169. EditorGUI.indentLevel++;
  170. signPassesCount = Mathf.Clamp(EditorGUILayout.IntField(Contents.signPass, signPassesCount), 0,
  171. 20);
  172. inOutThreshold = EditorGUILayout.Slider(Contents.inOutParam, inOutThreshold, 0.0f, 1.0f);
  173. surfaceOffset = EditorGUILayout.Slider(Contents.sdfOffset, surfaceOffset, -0.5f, 0.5f);
  174. EditorGUI.indentLevel--;
  175. }
  176. EditorGUILayout.EndFoldoutHeaderGroup();
  177. needsUpdate |= EditorGUI.EndChangeCheck();
  178. EditorGUI.BeginChangeCheck();
  179. m_Settings.m_FitPaddingVoxel = EditorGUILayout.Vector3IntField(Contents.fitPadding, m_Settings.m_FitPaddingVoxel);
  180. fitPaddingChanged = EditorGUI.EndChangeCheck();
  181. }
  182. EditorGUIUtility.wideMode = prevWideMode;
  183. if (mesh != null)
  184. {
  185. EditorGUILayout.BeginHorizontal();
  186. EditorGUI.BeginChangeCheck();
  187. if (GUILayout.Button(Contents.fitBoxToMesh))
  188. {
  189. FitBoxToMesh();
  190. }
  191. needsUpdate |= EditorGUI.EndChangeCheck();
  192. EditorGUI.BeginChangeCheck();
  193. if (GUILayout.Button(Contents.fitCubeToMesh))
  194. {
  195. FitCubeToMesh();
  196. }
  197. needsUpdate |= EditorGUI.EndChangeCheck();
  198. EditorGUILayout.EndHorizontal();
  199. }
  200. EditorGUI.BeginChangeCheck();
  201. modelSource = (ModelSource)EditorGUILayout.EnumPopup(Contents.bakeSource, modelSource);
  202. bool changedSource = EditorGUI.EndChangeCheck();
  203. needsUpdate |= changedSource;
  204. switch (modelSource)
  205. {
  206. case ModelSource.Mesh:
  207. EditorGUI.BeginChangeCheck();
  208. selectedMesh =
  209. (Mesh)EditorGUILayout.ObjectField(Contents.mesh, selectedMesh, typeof(Mesh), false);
  210. bool meshFieldHasChanged = EditorGUI.EndChangeCheck();
  211. needsUpdate |= meshFieldHasChanged;
  212. m_RefreshMeshPreview |= meshFieldHasChanged | changedSource;
  213. if (m_RefreshMeshPreview)
  214. {
  215. m_Settings.ApplySelectedMesh();
  216. }
  217. break;
  218. case ModelSource.MeshPrefab:
  219. EditorGUI.BeginChangeCheck();
  220. meshPrefab =
  221. (GameObject)EditorGUILayout.ObjectField(Contents.meshPrefab, meshPrefab, typeof(GameObject),
  222. false);
  223. meshFieldHasChanged = EditorGUI.EndChangeCheck() || m_PrefabChanged;
  224. m_PrefabChanged = false;
  225. needsUpdate |= meshFieldHasChanged;
  226. m_RefreshMeshPreview |= meshFieldHasChanged | changedSource || (mesh == null);
  227. bool rebuildMesh = m_RefreshMeshPreview;
  228. if (rebuildMesh)
  229. {
  230. m_Settings.BuildMeshFromPrefab();
  231. rebuildMesh = false;
  232. }
  233. break;
  234. }
  235. if (mesh == null) m_LiveUpdate = false;
  236. if (m_RefreshMeshPreview && mesh != null)
  237. {
  238. FitBoxToMesh();
  239. m_MeshPreview?.Dispose();
  240. m_MeshPreview = new SdfBakerPreview(mesh);
  241. m_RefreshMeshPreview = false;
  242. }
  243. if (mesh == null || (estimatedGridSize > UnityEngine.VFX.SDF.MeshToSDFBaker.kMaxGridSize) || InternalMeshUtil.GetPrimitiveCount(mesh) == 0)
  244. {
  245. GUI.enabled = false;
  246. }
  247. if (GUILayout.Button("Bake mesh") || m_LiveUpdate && needsUpdate)
  248. {
  249. if (m_Baker == null)
  250. {
  251. m_Baker = new MeshToSDFBaker(boxSizeReference, boxCenter, maxResolution, mesh, signPassesCount,
  252. inOutThreshold, surfaceOffset);
  253. }
  254. else
  255. {
  256. m_Baker.Reinit(boxSizeReference, boxCenter, maxResolution, mesh, signPassesCount,
  257. inOutThreshold, surfaceOffset);
  258. }
  259. m_Baker.BakeSDF();
  260. m_BakedSDF = m_Baker.SdfTexture;
  261. }
  262. GUI.enabled = true;
  263. bool canSave = true;
  264. if ((m_BakedSDF == null) || (m_Baker == null))
  265. {
  266. canSave = false;
  267. GUI.enabled = false;
  268. }
  269. if (GUILayout.Button(canSave ? Contents.saveSDF : Contents.saveSDFBlocked))
  270. {
  271. m_Baker.SaveToAsset();
  272. }
  273. GUI.enabled = true;
  274. previewObject = (PreviewChoice)EditorGUILayout.EnumPopup(Contents.previewChoice, previewObject);
  275. if ((previewObject & PreviewChoice.Mesh) != 0)
  276. {
  277. UpdateMeshPreview();
  278. }
  279. if ((previewObject & PreviewChoice.Texture) != 0)
  280. {
  281. UpdateTexture3dPreview();
  282. }
  283. if (needsUpdate)
  284. EditorUtility.SetDirty(m_Settings);
  285. }
  286. private void UpdateTexture3dPreview()
  287. {
  288. if (m_BakedSDF)
  289. {
  290. if (m_TexturePreview == null) m_TexturePreview = CreateInstance<Texture3DPreview>();
  291. m_TexturePreview.Texture = m_BakedSDF;
  292. GUILayout.BeginHorizontal();
  293. m_TexturePreview.OnPreviewSettings(new Object[] { m_BakedSDF });
  294. GUILayout.EndHorizontal();
  295. Rect rect = GUILayoutUtility.GetRect(100, 2000, 100, 2000, GUIStyle.none);
  296. m_TexturePreview.OnPreviewGUI(rect, GUIStyle.none);
  297. EditorGUI.DropShadowLabel(rect, m_TexturePreview.GetInfoString());
  298. }
  299. }
  300. private void UpdateMeshPreview()
  301. {
  302. if (mesh)
  303. {
  304. GUILayout.BeginHorizontal(GUIContent.none, EditorStyles.toolbar);
  305. GUILayout.Label(mesh.name, new GUIStyle("ToolbarBoldLabel"));
  306. GUILayout.FlexibleSpace();
  307. m_MeshPreview?.OnPreviewSettings();
  308. GUILayout.EndHorizontal();
  309. m_MeshPreview.sizeBoxReference = boxSizeReference;
  310. m_MeshPreview.actualSizeBox = m_ActualBoxSize;
  311. m_MeshPreview.centerBox = boxCenter;
  312. m_MeshPreview?.OnPreviewGUI(GUILayoutUtility.GetRect(100, 2000, 100, 2000, GUIStyle.none),
  313. GUIStyle.none);
  314. }
  315. }
  316. private void DrawContextIcon()
  317. {
  318. GUILayout.FlexibleSpace();
  319. EditorGUI.BeginChangeCheck();
  320. var rect = GUILayoutUtility.GetRect(Contents.contextMenuIcon, GUIStyle.none, GUILayout.Height(20), GUILayout.Width(20));
  321. GUI.Button(rect, Contents.contextMenuIcon, GUIStyle.none);
  322. if (EditorGUI.EndChangeCheck())
  323. {
  324. OnContextClick(new Vector2(rect.x, rect.yMax));
  325. }
  326. }
  327. private void OnContextClick(Vector2 pos)
  328. {
  329. var menu = new GenericMenu();
  330. menu.AddItem(EditorGUIUtility.TrTextContent("Show Additional Properties"), m_ShowAdvanced, () => m_ShowAdvanced = !m_ShowAdvanced);
  331. menu.AddItem(EditorGUIUtility.TrTextContent("Create new session"), false, CreateNewSession);
  332. menu.DropDown(new Rect(pos, Vector2.zero));
  333. }
  334. private void OnEnable()
  335. {
  336. titleContent = Contents.title;
  337. minSize = new Vector2(300.0f, 400.0f);
  338. if (m_Settings == null)
  339. {
  340. m_Settings = CreateInstance<SdfBakerSettings>();
  341. m_Settings.name = "None";
  342. }
  343. if (m_TexturePreview == null) m_TexturePreview = CreateInstance<Texture3DPreview>();
  344. m_TexturePreview.OnEnable();
  345. if (m_BakedSDF != null)
  346. m_TexturePreview.Texture = m_BakedSDF;
  347. if (m_MeshPreview == null) m_MeshPreview = new SdfBakerPreview(mesh); // Not sure if necessary
  348. Undo.undoRedoPerformed += OnUndoRedoPerformed;
  349. PrefabUtility.prefabInstanceUpdated += OnPrefabInstanceUpdated;
  350. Selection.selectionChanged += OnSelectionChanged;
  351. }
  352. public void OnDisable()
  353. {
  354. m_MeshPreview?.Dispose();
  355. m_Baker?.Dispose();
  356. if (m_BakedSDF)
  357. {
  358. m_BakedSDF.Release();
  359. }
  360. if (m_TexturePreview)
  361. {
  362. m_TexturePreview.OnDisable();
  363. m_TexturePreview = null;
  364. }
  365. Undo.undoRedoPerformed -= OnUndoRedoPerformed;
  366. PrefabUtility.prefabInstanceUpdated -= OnPrefabInstanceUpdated;
  367. Selection.selectionChanged -= OnSelectionChanged;
  368. }
  369. void OnPrefabInstanceUpdated(GameObject prefab)
  370. {
  371. if (prefab != null && meshPrefab != null && prefab.name == meshPrefab.name)
  372. {
  373. m_PrefabChanged = true;
  374. }
  375. }
  376. void OnUndoRedoPerformed()
  377. {
  378. m_MeshPreview?.Dispose();
  379. m_MeshPreview = new SdfBakerPreview(mesh);
  380. }
  381. void OnSelectionChanged()
  382. {
  383. if (Selection.activeObject is SdfBakerSettings)
  384. {
  385. SdfBakerSettings settings = Selection.activeObject as SdfBakerSettings;
  386. LoadSettings(settings);
  387. Repaint();
  388. }
  389. }
  390. void FitBoxToMesh()
  391. {
  392. boxCenter = mesh.bounds.center;
  393. boxSizeReference = mesh.bounds.extents * 2.0f;
  394. var absolutePadding = GetAbsolutePadding();
  395. boxSizeReference += absolutePadding;
  396. m_ActualBoxSize = SnapBoxToVoxels();
  397. boxSizeReference = m_ActualBoxSize;
  398. }
  399. void FitCubeToMesh()
  400. {
  401. float maxSize = Mathf.Max(mesh.bounds.extents.x, mesh.bounds.extents.y, mesh.bounds.extents.z) * 2.0f;
  402. boxSizeReference = new Vector3(maxSize, maxSize, maxSize);
  403. boxCenter = mesh.bounds.center;
  404. var absolutePadding = GetAbsolutePadding();
  405. boxSizeReference += absolutePadding;
  406. m_ActualBoxSize = SnapBoxToVoxels();
  407. }
  408. private Vector3 GetAbsolutePadding()
  409. {
  410. float maxExtent = Mathf.Max(boxSizeReference.x, Mathf.Max(boxSizeReference.y, boxSizeReference.z));
  411. float voxelSize = maxExtent / maxResolution;
  412. Vector3 absolutePadding = 2 * voxelSize * new Vector3(m_Settings.m_FitPaddingVoxel.x, m_Settings.m_FitPaddingVoxel.y,
  413. m_Settings.m_FitPaddingVoxel.z);
  414. return absolutePadding;
  415. }
  416. int EstimateGridSize()
  417. {
  418. float maxExtent = Mathf.Max(boxSizeReference.x, Mathf.Max(boxSizeReference.y, boxSizeReference.z));
  419. int dimX, dimY, dimZ;
  420. if (maxExtent == boxSizeReference.x)
  421. {
  422. dimX = Mathf.Max(Mathf.RoundToInt(maxResolution * boxSizeReference.x / maxExtent), 1);
  423. dimY = Mathf.Max(Mathf.CeilToInt(maxResolution * boxSizeReference.y / maxExtent), 1);
  424. dimZ = Mathf.Max(Mathf.CeilToInt(maxResolution * boxSizeReference.z / maxExtent), 1);
  425. }
  426. else if (maxExtent == boxSizeReference.y)
  427. {
  428. dimY = Mathf.Max(Mathf.RoundToInt(maxResolution * boxSizeReference.y / maxExtent), 1);
  429. dimX = Mathf.Max(Mathf.CeilToInt(maxResolution * boxSizeReference.x / maxExtent), 1);
  430. dimZ = Mathf.Max(Mathf.CeilToInt(maxResolution * boxSizeReference.z / maxExtent), 1);
  431. }
  432. else
  433. {
  434. dimZ = Mathf.Max(Mathf.RoundToInt(maxResolution * boxSizeReference.z / maxExtent), 1);
  435. dimY = Mathf.Max(Mathf.CeilToInt(maxResolution * boxSizeReference.y / maxExtent), 1);
  436. dimX = Mathf.Max(Mathf.CeilToInt(maxResolution * boxSizeReference.x / maxExtent), 1);
  437. }
  438. return dimX * dimY * dimZ;
  439. }
  440. Vector3 SnapBoxToVoxels(int refAxis = 0)
  441. {
  442. float maxExtent = Mathf.Max(boxSizeReference.x, Mathf.Max(boxSizeReference.y, boxSizeReference.z));
  443. int dimX, dimY, dimZ;
  444. if (refAxis == 0 || refAxis > 3) // Default behavior, choose largest dimension
  445. {
  446. if (maxExtent == boxSizeReference.x)
  447. {
  448. refAxis = 1;
  449. }
  450. if (maxExtent == boxSizeReference.y)
  451. {
  452. refAxis = 2;
  453. }
  454. if (maxExtent == boxSizeReference.z)
  455. {
  456. refAxis = 3;
  457. }
  458. }
  459. if (refAxis == 1)
  460. {
  461. dimX = Mathf.Max(Mathf.RoundToInt(maxResolution * boxSizeReference.x / maxExtent), 1);
  462. dimY = Mathf.Max(Mathf.CeilToInt(maxResolution * boxSizeReference.y / maxExtent), 1);
  463. dimZ = Mathf.Max(Mathf.CeilToInt(maxResolution * boxSizeReference.z / maxExtent), 1);
  464. float voxelSize = boxSizeReference.x / dimX;
  465. var tmpBoxSize = boxSizeReference;
  466. tmpBoxSize.x = dimX * voxelSize;
  467. tmpBoxSize.y = dimY * voxelSize;
  468. tmpBoxSize.z = dimZ * voxelSize;
  469. return tmpBoxSize;
  470. }
  471. else if (refAxis == 2)
  472. {
  473. dimY = Mathf.Max(Mathf.RoundToInt(maxResolution * boxSizeReference.y / maxExtent), 1);
  474. dimX = Mathf.Max(Mathf.CeilToInt(maxResolution * boxSizeReference.x / maxExtent), 1);
  475. dimZ = Mathf.Max(Mathf.CeilToInt(maxResolution * boxSizeReference.z / maxExtent), 1);
  476. float voxelSize = boxSizeReference.y / dimY;
  477. var tmpBoxSize = boxSizeReference;
  478. tmpBoxSize.x = dimX * voxelSize;
  479. tmpBoxSize.y = dimY * voxelSize;
  480. tmpBoxSize.z = dimZ * voxelSize;
  481. return tmpBoxSize;
  482. }
  483. else
  484. {
  485. dimZ = Mathf.Max(Mathf.RoundToInt(maxResolution * boxSizeReference.z / maxExtent), 1);
  486. dimY = Mathf.Max(Mathf.CeilToInt(maxResolution * boxSizeReference.y / maxExtent), 1);
  487. dimX = Mathf.Max(Mathf.CeilToInt(maxResolution * boxSizeReference.x / maxExtent), 1);
  488. float voxelSize = boxSizeReference.z / dimZ;
  489. var tmpBoxSize = boxSizeReference;
  490. tmpBoxSize.x = dimX * voxelSize;
  491. tmpBoxSize.y = dimY * voxelSize;
  492. tmpBoxSize.z = dimZ * voxelSize;
  493. return tmpBoxSize;
  494. }
  495. }
  496. void CreateNewSession()
  497. {
  498. if (AssetDatabase.Contains(m_Settings))
  499. {
  500. m_Settings = CreateInstance<SdfBakerSettings>();
  501. m_Settings.name = "None";
  502. }
  503. else
  504. {
  505. m_Settings.ResetToDefault();
  506. }
  507. m_BakedSDF = null;
  508. if (m_MeshPreview != null)
  509. {
  510. m_MeshPreview.Dispose();
  511. m_MeshPreview = null;
  512. }
  513. if (m_TexturePreview != null)
  514. {
  515. m_TexturePreview.OnDisable();
  516. m_TexturePreview = null;
  517. }
  518. m_RefreshMeshPreview = false;
  519. if (m_Baker != null)
  520. {
  521. m_Baker.Dispose();
  522. m_Baker = null;
  523. }
  524. }
  525. void SaveSettings()
  526. {
  527. string path;
  528. if (AssetDatabase.Contains(m_Settings))
  529. {
  530. bool restoreInspectorSelection = Selection.activeObject == m_Settings;
  531. path = AssetDatabase.GetAssetPath(m_Settings);
  532. var newSettings = Instantiate(m_Settings);
  533. AssetDatabase.DeleteAsset(path);
  534. newSettings.hideFlags = HideFlags.None;
  535. AssetDatabase.CreateAsset(newSettings, path);
  536. m_Settings = newSettings;
  537. if (restoreInspectorSelection)
  538. {
  539. Selection.activeObject = m_Settings;
  540. }
  541. return;
  542. }
  543. path = EditorUtility.SaveFilePanelInProject("Save the SDF Baker Settings as", "SDF Settings", "asset", "");
  544. if (path != "")
  545. {
  546. AssetDatabase.DeleteAsset(path);
  547. m_Settings.hideFlags = HideFlags.None;
  548. AssetDatabase.CreateAsset(m_Settings, path);
  549. }
  550. }
  551. internal void LoadSettings(SdfBakerSettings newSettings)
  552. {
  553. m_Settings = newSettings;
  554. m_ActualBoxSize = SnapBoxToVoxels();
  555. if (m_TexturePreview != null)
  556. {
  557. m_TexturePreview.OnDisable();
  558. }
  559. m_TexturePreview = CreateInstance<Texture3DPreview>();
  560. if (m_MeshPreview != null)
  561. {
  562. m_MeshPreview.Dispose();
  563. }
  564. m_MeshPreview = new SdfBakerPreview(mesh);
  565. m_BakedSDF = null;
  566. }
  567. internal void ForceRefreshMeshPreview()
  568. {
  569. m_RefreshMeshPreview = true;
  570. }
  571. static class Contents
  572. {
  573. internal static GUIContent title = new GUIContent("SDF Bake Tool");
  574. internal static GUIContent maxResolution = new GUIContent("Max resolution", "Sets the number of voxels of the largest dimension.");
  575. internal static GUIContent mesh = new GUIContent("Mesh");
  576. internal static GUIContent boxSizeReference = new GUIContent("Desired Box Size", "Size of the desired Bounding Box. The Actual Bounding Box will be slightly modified to make sure that each voxel is a cube. Displayed in white in the mesh preview.");
  577. internal static GUIContent actualBoxSize = new GUIContent("Actual Box Size", "Size of the Bounding Box where the mesh will actually be baked. It may differ from the desired Bounding Box to make sure that each voxel is a cube. Displayed in green in the mesh preview.");
  578. internal static GUIContent boxCenter = new GUIContent("Box Center");
  579. internal static GUIContent meshPrefab = new GUIContent("Mesh Prefab");
  580. internal static GUIContent previewChoice = new GUIContent("Preview Object");
  581. internal static GUIContent bakeSource = new GUIContent("Model Source");
  582. internal static GUIContent liveUpdate = new GUIContent("Live Update", "When enabled, every modification to the settings will trigger a new bake of the SDF, and the preview will be updated accordingly.");
  583. internal static GUIContent signPass = new GUIContent("Sign passes count",
  584. "Increasing the number of sign passes can help refine the distinction between the inside and the outside of the mesh." +
  585. " This can be useful for models containing holes or self-intersections");
  586. internal static GUIContent inOutParam = new GUIContent("In/Out Threshold",
  587. "This helps arbitrate what is inside from what is outside of the shape. Low values will include more points in the inside (negative sign), and vice versa.");
  588. internal static GUIContent sdfOffset = new GUIContent("Surface Offset",
  589. "Selects the amount of offset applied to the surface. This amount is in the normalized voxel space.");
  590. internal static GUIContent saveSDF = new GUIContent("Save SDF");
  591. internal static GUIContent saveSDFBlocked = new GUIContent("Save SDF",
  592. "There is nothing to save yet. Please use the Bake Mesh button before saving.");
  593. static Texture2D paneOptionsIconDark = (Texture2D)EditorGUIUtility.Load("Builtin Skins/DarkSkin/Images/pane options.png");
  594. static Texture2D paneOptionsIconLight = (Texture2D)EditorGUIUtility.Load("Builtin Skins/LightSkin/Images/pane options.png");
  595. static Texture2D paneOptionsIcon { get { return EditorGUIUtility.isProSkin ? paneOptionsIconDark : paneOptionsIconLight; } }
  596. internal static GUIContent contextMenuIcon = new GUIContent(paneOptionsIcon, "Additional Properties");
  597. internal static GUIContent fitPadding = new GUIContent("Fit Padding", "Controls the padding, in voxel, to apply when using \"Fit Box/Cube to Mesh\".");
  598. internal static GUIContent fitBoxToMesh = new GUIContent("Fit box to Mesh", "Fits the bounding box of the bake to the bounding box of the mesh. Padding specified in \"Fit Padding\" (in Additional Properties) will be applied.");
  599. internal static GUIContent fitCubeToMesh = new GUIContent("Fit cube to Mesh", "Fits the bounding box of the bake to the bounding cube of the mesh. Padding specified in \"Fit Padding\" (in Additional Properties) will be applied.");
  600. internal static GUIContent bakingParameters = new GUIContent("Baking parameters");
  601. internal static GUIContent createNewSession = new GUIContent("New Session", "Resets the tool to its default parameters, creating a new unsaved settings assets. This will also erase the current baked SDF texture if there is any.");
  602. internal static GUIContent saveSettings = new GUIContent("Save Settings", "Saves the settings of the tool into an asset.");
  603. internal static GUIContent settingsAsset = new GUIContent("Settings Asset");
  604. }
  605. }
  606. }