67 lines
2.7 KiB
C#
67 lines
2.7 KiB
C#
#if UNITY_EDITOR
|
|
using UnityEditor;
|
|
using UnityEngine;
|
|
using UnityEngine.Rendering;
|
|
using SomniumSpace.Worlds.TheCloud;
|
|
|
|
namespace UnifiedMinds.LightingEditor
|
|
{
|
|
public static class SpotlightBeamMenu
|
|
{
|
|
const string MeshPath = "Assets/Spotlight/SpotlightBeamCone.asset";
|
|
const string MatPath = "Assets/Spotlight/SpotlightBeam.mat";
|
|
|
|
[MenuItem("GameObject/Light/Spotlight Beam", false, 20)]
|
|
public static void Create(MenuCommand cmd)
|
|
{
|
|
var go = new GameObject("Spotlight Beam");
|
|
GameObjectUtility.SetParentAndAlign(go, cmd.context as GameObject);
|
|
|
|
go.AddComponent<MeshFilter>().sharedMesh = GetOrCreateConeMesh();
|
|
var mr = go.AddComponent<MeshRenderer>();
|
|
mr.sharedMaterial = AssetDatabase.LoadAssetAtPath<Material>(MatPath);
|
|
mr.shadowCastingMode = ShadowCastingMode.Off;
|
|
mr.receiveShadows = false;
|
|
mr.lightProbeUsage = LightProbeUsage.Off;
|
|
mr.reflectionProbeUsage = ReflectionProbeUsage.Off;
|
|
|
|
go.AddComponent<SpotlightBeam>();
|
|
|
|
Undo.RegisterCreatedObjectUndo(go, "Create Spotlight Beam");
|
|
Selection.activeGameObject = go;
|
|
}
|
|
|
|
// Cone: apex at origin, base circle (radius 1) at z = 1, smooth side normals.
|
|
static Mesh GetOrCreateConeMesh()
|
|
{
|
|
var existing = AssetDatabase.LoadAssetAtPath<Mesh>(MeshPath);
|
|
if (existing != null) return existing;
|
|
|
|
const int seg = 48;
|
|
var verts = new Vector3[seg * 2];
|
|
var norms = new Vector3[seg * 2];
|
|
var tris = new int[seg * 3];
|
|
for (int i = 0; i < seg; i++)
|
|
{
|
|
float a = (i / (float)seg) * Mathf.PI * 2f;
|
|
var rim = new Vector3(Mathf.Cos(a), Mathf.Sin(a), 1f);
|
|
// Side normal of a cone with radius 1 / height 1 points outward and slightly back toward the apex
|
|
var n = new Vector3(Mathf.Cos(a), Mathf.Sin(a), -1f).normalized;
|
|
verts[i] = Vector3.zero; // one apex vertex per segment => smooth per-segment normals
|
|
norms[i] = n;
|
|
verts[seg + i] = rim;
|
|
norms[seg + i] = n;
|
|
tris[i * 3 + 0] = i;
|
|
tris[i * 3 + 1] = seg + i;
|
|
tris[i * 3 + 2] = seg + (i + 1) % seg;
|
|
}
|
|
var mesh = new Mesh { name = "SpotlightBeamCone", vertices = verts, normals = norms, triangles = tris };
|
|
mesh.bounds = new Bounds(new Vector3(0, 0, 0.5f), new Vector3(2f, 2f, 1f));
|
|
AssetDatabase.CreateAsset(mesh, MeshPath);
|
|
AssetDatabase.SaveAssets();
|
|
return mesh;
|
|
}
|
|
}
|
|
}
|
|
#endif
|