Plane to sphere issue
Hi, like many other people I'm currently trying to build a sphere from 6 planes. I built a scene with a cube made out of 6 plane primitives that looks like this:
And using the following code:
using UnityEngine;
using System.Collections;
public class Spherize : MonoBehaviour
{
void Start ()
{
Mesh mesh = GetComponent<MeshFilter>().mesh;
Vector3[] vertices = mesh.vertices;
for (int i = 0; i < vertices.Length; i++ )
{
vertices[i] = vertices[i].normalized * 5f;
}
mesh.vertices = vertices;
mesh.RecalculateNormals();
mesh.RecalculateBounds();
}
}
The final result, however, isn't what I expected:
Am I missing something? According to most references I found here in Answers my code should work. I'm using Unity 5.2 by the way. Thanks in advance!
Answer by ivodopiviz · Nov 24, 2015 at 04:56 AM
For the ones wondering the same, there's a different approach in this answer:
http://answers.unity3d.com/questions/929567/how-do-you-make-a-sphere-from-6-planes.html
Edit: OK, got it now. It was a local/global coords issue. The final code would be this:
using UnityEngine;
using System.Collections;
public class Spherize : MonoBehaviour
{
void Start ()
{
Mesh mesh = GetComponent<MeshFilter>().mesh;
Vector3[] vertices = mesh.vertices;
for (int i = 0; i < vertices.Length; i++ )
{
vertices[i] = transform.InverseTransformPoint(transform.TransformPoint(vertices[i]).normalized * 5f); // 5f is the radius
}
mesh.vertices = vertices;
mesh.RecalculateNormals();
mesh.RecalculateBounds();
}
void Update ()
{
}
}
Your answer
Follow this Question
Related Questions
Generating Mesh using Points 1 Answer
[SOLVED]Unexpected results 0 Answers
Help with clouds -- Is it possible to procedurally generate them in a 2d game? 1 Answer
Is there any good CSG lib for runtime mesh modification ? 0 Answers
Make camera rotate around an object while keeping entire area visible 0 Answers