x


Mesh collider on procedurally generated mesh

Hi,

I'm having a problem assigning my mesh a collider.

I've tweaked the CrumpleMesh scene (from the procedural examples package) and now have a flat plane that, on run time, is deformed by Perlin noise. I can assign the mesh itself a collider but this does not follow the contours of the deformed mesh (e.g. the hills and troughs).

So- in light of the above, what would be the best way of assigning a collider to my mesh after it has been deformed (I imagine I need to add something to the script, but can't quite work out what).

FYI, I'm using the C# version of the script:

using UnityEngine;
using System.Collections;
using Graphics.Tools.Noise;
using Graphics.Tools.Noise.Primitive;
using Graphics.Tools.Noise.Filter;

public class CrumpleMesh : MonoBehaviour {

    public float scale = 1f;
    public float speed = 0f;
    public bool recalculateNormals = false;

    private Vector3[] _baseVertices;
    private IModule3D _noise;

    //seed
    private Mesh _mesh;
    static private System.Random _rnd = new System.Random(999);

    /// <summary>
    /// 
    /// </summary>
    void Start () {
        _mesh = GetComponent<MeshFilter>().mesh;
    }//end Start

    /// <summary>
    /// 
    /// </summary>
    void Update () {

        if (_noise == null){
            CreatePrimitive();
        }//end if

        if (_baseVertices == null){
            _baseVertices = _mesh.vertices;
        }//end if

        Vector3[] vertices = new Vector3[_baseVertices.Length];

        float timex = Time.time * speed + 1.2584f;
        float timey = Time.time * speed + 40.6584f;
        float timez = Time.time * speed + 1.6518f;

        for (int i = 0; i < vertices.Length; i++){

            Vector3 vertex = _baseVertices[i];

            vertex.x += _noise.GetValue(timex + vertex.x, timex + vertex.y, timex + vertex.z) * scale;
            vertex.y += _noise.GetValue(timey + vertex.x, timey + vertex.y, timey + vertex.z) * scale;
            vertex.z += _noise.GetValue(timez + vertex.x, timez + vertex.y, timez + vertex.z) * scale;

            vertices[i] = vertex;

        }//end for

        _mesh.vertices = vertices;

        if(recalculateNormals){
            _mesh.RecalculateNormals();
        }//end if

        _mesh.RecalculateBounds();

    }//end Update

    /// <summary>
    /// 
    /// </summary>
    private void CreatePrimitive(){

        SimplexPerlin primitive = new SimplexPerlin();
        primitive.Seed = (int)(_rnd.Next() * _rnd.Next());

        SinFractal filter = new SinFractal();
        filter.OctaveCount = 10f;
        filter.Primitive3D = primitive;

        _noise = primitive;

    }//end CreatePrimitive

}//end class

Thank you for any responses.

more ▼

asked Dec 14, 2011 at 11:41 PM

sam.ibbitson gravatar image

sam.ibbitson
5 16 12 14

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

Copy the generated mesh to the meshcollider's mesh. i.e., GetComponent<MeshCollider>().mesh = _mesh;. Keep in mind that updating a mesh collider is very slow, and not actually something you want to do in Update.

more ▼

answered Dec 15, 2011 at 12:13 AM

Eric5h5 gravatar image

Eric5h5
108k 55 181 655

This is what I was looking for- thanks!

Dec 18, 2011 at 10:31 AM sam.ibbitson
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x9668
x2121
x443
x200

asked: Dec 14, 2011 at 11:41 PM

Seen: 3388 times

Last Updated: Dec 18, 2011 at 10:31 AM