Improved light baking, updated splashpack structure and documentation
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
namespace SplashEdit.RuntimeCode
|
||||
@@ -5,36 +6,111 @@ namespace SplashEdit.RuntimeCode
|
||||
public class PSXObjectExporter : MonoBehaviour
|
||||
{
|
||||
public PSXBPP BitDepth = PSXBPP.TEX_8BIT; // Defines the bit depth of the texture (e.g., 4BPP, 8BPP)
|
||||
public bool MeshIsStatic = true; // Determines if the mesh is static, affecting how it's processed. Non-static meshes don't export correctly as of now.
|
||||
|
||||
[HideInInspector]
|
||||
public PSXTexture2D Texture; // Stores the converted PlayStation-style texture
|
||||
public List<PSXTexture2D> Textures = new List<PSXTexture2D>(); // Stores the converted PlayStation-style texture
|
||||
|
||||
[HideInInspector]
|
||||
public PSXMesh Mesh; // Stores the converted PlayStation-style mesh
|
||||
|
||||
/// <summary>
|
||||
/// Converts the object's material texture into a PlayStation-compatible texture.
|
||||
/// </summary>
|
||||
public void CreatePSXTexture2D()
|
||||
public bool PreviewNormals = false;
|
||||
public float normalPreviewLength = 0.5f; // Length of the normal lines
|
||||
|
||||
private void OnDrawGizmos()
|
||||
{
|
||||
Renderer renderer = GetComponent<Renderer>();
|
||||
if (renderer != null && renderer.sharedMaterial != null && renderer.sharedMaterial.mainTexture is Texture2D texture)
|
||||
|
||||
if (PreviewNormals)
|
||||
{
|
||||
Texture = PSXTexture2D.CreateFromTexture2D(texture, BitDepth);
|
||||
Texture.OriginalTexture = texture; // Stores reference to the original texture
|
||||
MeshFilter filter = GetComponent<MeshFilter>();
|
||||
|
||||
if (filter != null)
|
||||
{
|
||||
|
||||
Mesh mesh = filter.sharedMesh;
|
||||
Vector3[] vertices = mesh.vertices;
|
||||
Vector3[] normals = mesh.normals;
|
||||
|
||||
Gizmos.color = Color.green; // Normal color
|
||||
|
||||
for (int i = 0; i < vertices.Length; i++)
|
||||
{
|
||||
Vector3 worldVertex = transform.TransformPoint(vertices[i]); // Convert to world space
|
||||
Vector3 worldNormal = transform.TransformDirection(normals[i]); // Transform normal to world space
|
||||
|
||||
Gizmos.DrawLine(worldVertex, worldVertex + worldNormal * normalPreviewLength);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Converts the object's material texture into a PlayStation-compatible texture.
|
||||
/// </summary>
|
||||
///
|
||||
public void CreatePSXTextures2D()
|
||||
{
|
||||
Renderer renderer = GetComponent<Renderer>();
|
||||
if (renderer != null)
|
||||
{
|
||||
Material[] materials = renderer.sharedMaterials;
|
||||
Textures = new List<PSXTexture2D>(); // Ensure the list is initialized
|
||||
|
||||
foreach (Material mat in materials)
|
||||
{
|
||||
if (mat != null && mat.mainTexture != null)
|
||||
{
|
||||
Texture mainTexture = mat.mainTexture;
|
||||
Texture2D tex2D = null;
|
||||
|
||||
// Check if it's already a Texture2D
|
||||
if (mainTexture is Texture2D existingTex2D)
|
||||
{
|
||||
tex2D = existingTex2D;
|
||||
}
|
||||
else
|
||||
{
|
||||
// If not a Texture2D, try to convert
|
||||
tex2D = ConvertToTexture2D(mainTexture);
|
||||
}
|
||||
|
||||
if (tex2D != null)
|
||||
{
|
||||
PSXTexture2D tex = PSXTexture2D.CreateFromTexture2D(tex2D, BitDepth);
|
||||
tex.OriginalTexture = tex2D; // Store reference to the original texture
|
||||
Textures.Add(tex);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private Texture2D ConvertToTexture2D(Texture texture)
|
||||
{
|
||||
// Create a new Texture2D with the same dimensions and format
|
||||
Texture2D texture2D = new Texture2D(texture.width, texture.height, TextureFormat.RGBA32, false);
|
||||
|
||||
// Read the texture pixels
|
||||
RenderTexture currentActiveRT = RenderTexture.active;
|
||||
RenderTexture.active = texture as RenderTexture;
|
||||
|
||||
texture2D.ReadPixels(new Rect(0, 0, texture.width, texture.height), 0, 0);
|
||||
texture2D.Apply();
|
||||
|
||||
RenderTexture.active = currentActiveRT;
|
||||
|
||||
return texture2D;
|
||||
}
|
||||
/// <summary>
|
||||
/// Converts the object's mesh into a PlayStation-compatible mesh.
|
||||
/// </summary>
|
||||
public void CreatePSXMesh(float GTEScaling)
|
||||
{
|
||||
MeshFilter meshFilter = gameObject.GetComponent<MeshFilter>();
|
||||
if (meshFilter != null)
|
||||
Renderer renderer = GetComponent<Renderer>();
|
||||
if (renderer != null)
|
||||
{
|
||||
Mesh = PSXMesh.CreateFromUnityMesh(meshFilter.sharedMesh, GTEScaling, transform, MeshIsStatic, Texture.Width, Texture.Height);
|
||||
Mesh = PSXMesh.CreateFromUnityRenderer(renderer, GTEScaling, transform, Textures);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user