#Trying to outline gameobjects
1 messages · Page 1 of 1 (latest)
The code for the custom Cube
public static GameObject Cube2()
{
// Define the 8 vertices of a unit cube centered at the origin
Vector3[] verts = new Vector3[]
{
new Vector3(-0.5f, -0.5f, -0.5f), // 0
new Vector3( 0.5f, -0.5f, -0.5f), // 1
new Vector3( 0.5f, -0.5f, 0.5f), // 2
new Vector3(-0.5f, -0.5f, 0.5f), // 3
new Vector3(-0.5f, 0.5f, -0.5f), // 4
new Vector3( 0.5f, 0.5f, -0.5f), // 5
new Vector3( 0.5f, 0.5f, 0.5f), // 6
new Vector3(-0.5f, 0.5f, 0.5f), // 7
};
// Define the 12 triangles (two per face)
int[] tris = new int[]
{
// Bottom
0, 2, 1, 0, 3, 2,
// Top
4, 5, 6, 4, 6, 7,
// Front
3, 6, 2, 3, 7, 6,
// Back
0, 1, 5, 0, 5, 4,
// Left
0, 4, 7, 0, 7, 3,
// Right
1, 2, 6, 1, 6, 5
};
// Create the cube GameObject
GameObject cube = new GameObject("Cube2", typeof(MeshFilter), typeof(MeshRenderer));
// Build mesh
Mesh mesh = new Mesh();
mesh.vertices = verts;
mesh.triangles = tris;
mesh.RecalculateNormals();
cube.GetComponent<MeshFilter>().mesh = mesh;
// Assign a white unlit material
var whiteMat = new Material(Shader.Find("Unlit/Color"));
whiteMat.color = Color.white;
cube.GetComponent<MeshRenderer>().material = whiteMat;
// Add a MeshCollider and assign the mesh
MeshCollider collider = cube.AddComponent<MeshCollider>();
collider.sharedMesh = mesh;
collider.convex = true;
// Position cube at the origin
cube.transform.position = Vector3.zero;
return cube;
}
The outline code
public static void AddOutline(GameObject obj, Color outlineColor, float outlineWidthFraction = 0.03f)
{
if (obj == null) return;
// Support both MeshRenderer and SkinnedMeshRenderer
Renderer renderer = obj.GetComponent<MeshRenderer>() as Renderer
?? obj.GetComponent<SkinnedMeshRenderer>() as Renderer;
if (renderer == null) return;
// Calculate the largest dimension of the bounds
Bounds bounds = renderer.bounds;
float maxDimension = Mathf.Max(bounds.size.x, bounds.size.y, bounds.size.z);
float outlineWidth = maxDimension > 0f ? maxDimension * outlineWidthFraction : outlineWidthFraction;
Shader outlineShader = Shader.Find("Custom/OutlineOnly");
if (outlineShader == null)
{
Debug.LogError("OutlineOnly shader not found.");
return;
}
var outlineMat = new Material(outlineShader);
outlineMat.SetColor("_OutlineColor", outlineColor);
outlineMat.SetFloat("_OutlineWidth", outlineWidth);
// Set render queue if needed (e.g., for transparency)
// outlineMat.renderQueue = 3000; // Uncomment for transparent objects
var materials = renderer.sharedMaterials;
foreach (var mat in materials)
{
if (mat != null && mat.shader == outlineShader)
{
// Already has outline, update color/width
mat.SetColor("_OutlineColor", outlineColor);
mat.SetFloat("_OutlineWidth", outlineWidth);
return;
}
}
// Add outline as second material
var newMats = new Material[materials.Length + 1];
materials.CopyTo(newMats, 0);
newMats[materials.Length] = outlineMat;
renderer.materials = newMats;
}
I think it's because the custom coded cube is partly transparent that the outline is showing through to the outside of the Gameobject. No idea how to correct this though.
I do have another custom coded Prism that works fine though.
Don't make the custom coded cube transparent?
It's not transparent by default, and I tried disabling everything that could make it transparent.
Well, I'm not seeing any transparent material being set in the code here as you're just using Unity's unlit
so unless you're changing it anywhere else. I'm feeling like the outline material might just be overriding the current unlit material
You may need to create a submesh to your cube if you want to add another outline material perhaps
But then again I expect you to get errors when accessing the material array there hmm
actually it would be correct, it's more setting the material with the array where it may not have that many material slots so it just truncates* it.