diff options
| -rw-r--r-- | Scripts/Impostors.cs | 64 |
1 files changed, 45 insertions, 19 deletions
diff --git a/Scripts/Impostors.cs b/Scripts/Impostors.cs index e10daf1..4ae47a3 100644 --- a/Scripts/Impostors.cs +++ b/Scripts/Impostors.cs @@ -18,15 +18,15 @@ public class Impostors : MonoBehaviour [Range(1, 4096)] public int cameraResolution = 256; - [Tooltip("Distance multiplier from sphere center for camera placement")] - public float cameraDistance = 2f; + [Tooltip("Near clipping distance - cameras are placed this distance outside the sphere")] + public float nearClippingDistance = 0.01f; [HideInInspector] public GameObject[] cameraObjects; [HideInInspector] public Camera[] cameras; - private float radius() { return sphere_radius_ * transform.localScale.x; } + private float radius() { return sphere_radius_ * transform.lossyScale.x; } void OnDrawGizmos() { @@ -47,7 +47,7 @@ public class Impostors : MonoBehaviour for (int x = 0; x < gridResolution; x++) { Vector3 hemispherePos = PlaneToHemiOctahedron(x, y); - Vector3 worldPos = transform.position + hemispherePos * currentRadius * cameraDistance; + Vector3 worldPos = transform.position + hemispherePos * (currentRadius + nearClippingDistance); // Draw camera position Gizmos.DrawSphere(worldPos, currentRadius * 0.05f); @@ -94,6 +94,13 @@ public class Impostors : MonoBehaviour // Clean up existing cameras DestroyExistingCameras(); + // Create parent GameObject for all cameras + GameObject camerasParent = new GameObject("Cameras"); + camerasParent.transform.parent = transform; + camerasParent.transform.localPosition = Vector3.zero; + camerasParent.transform.localRotation = Quaternion.identity; + camerasParent.transform.localScale = Vector3.one; + int totalCameras = gridResolution * gridResolution; cameraObjects = new GameObject[totalCameras]; cameras = new Camera[totalCameras]; @@ -106,20 +113,22 @@ public class Impostors : MonoBehaviour // Get position on hemisphere Vector3 hemisphereDir = PlaneToHemiOctahedron(x, y); float currentRadius = radius(); - Vector3 worldPos = transform.position + hemisphereDir * currentRadius * cameraDistance; + + // Place camera offset distance outside bounding sphere surface + Vector3 worldPos = transform.position + hemisphereDir * (currentRadius + nearClippingDistance); // Create camera GameObject GameObject camObj = new GameObject($"ImpostorCamera_{x}_{y}"); - camObj.transform.parent = transform; + camObj.transform.parent = camerasParent.transform; camObj.transform.position = worldPos; camObj.transform.LookAt(transform.position); // Add and configure camera Camera cam = camObj.AddComponent<Camera>(); cam.orthographic = true; - cam.orthographicSize = currentRadius; - cam.nearClipPlane = 0.01f; - cam.farClipPlane = currentRadius * cameraDistance * 2f; + cam.orthographicSize = sphere_radius_; + cam.nearClipPlane = nearClippingDistance; + cam.farClipPlane = sphere_radius_ * 2f + nearClippingDistance; cam.enabled = false; // Only enable during baking cameraObjects[index] = camObj; @@ -133,14 +142,11 @@ public class Impostors : MonoBehaviour public void DestroyExistingCameras() { - // Find and destroy all children with "ImpostorCamera" in name - for (int i = transform.childCount - 1; i >= 0; i--) + // Find and destroy the "Cameras" parent GameObject + Transform camerasTransform = transform.Find("Cameras"); + if (camerasTransform != null) { - Transform child = transform.GetChild(i); - if (child.name.StartsWith("ImpostorCamera")) - { - DestroyImmediate(child.gameObject); - } + DestroyImmediate(camerasTransform.gameObject); } cameraObjects = null; @@ -190,10 +196,30 @@ public class Impostors : MonoBehaviour // Save texture to file byte[] bytes = atlasTexture.EncodeToPNG(); - string path = Path.Combine(Application.dataPath, "yum_food/3ner/ho_bake.png"); - File.WriteAllBytes(path, bytes); - Debug.Log($"Baked texture saved to: {path}"); + // Get currently selected folder in Project window + string folder = "Assets"; + if (Selection.activeObject != null) + { + string selectedPath = AssetDatabase.GetAssetPath(Selection.activeObject); + if (!string.IsNullOrEmpty(selectedPath)) + { + if (AssetDatabase.IsValidFolder(selectedPath)) + { + folder = selectedPath; + } + else + { + folder = Path.GetDirectoryName(selectedPath); + } + } + } + + string assetPath = Path.Combine(folder, "ho_bake.png"); + string fullPath = Path.Combine(Application.dataPath, "..", assetPath); + File.WriteAllBytes(fullPath, bytes); + + Debug.Log($"Baked texture saved to: {assetPath}"); Debug.Log($"Atlas size: {texWidth}x{texHeight} ({gridResolution}x{gridResolution} grid of {cameraResolution}x{cameraResolution} images)"); // Refresh asset database |
