- Home /
Generating a Displacement Map in Unity3d?
So far I have been able to spawn randomly sized spheres on the go. but I want these to look like asteroids, and not perfectly smooth spheres. I would like to distort the geometry of an object as it is generating in order to make it feel more like you are actually flying among asteroids.
Answer by tanoshimi · Nov 08, 2013 at 11:48 PM
I suggest you look at the procedural examples project in the asset store - particularly the heightmap generator and crumple modifier to show how to deform a mesh in code at runtime.
Here is my script:
var copper : boolean = false; var gold : boolean = false;
//var astoids : GameObject[];
var metal : int[];
function Start ()
{
randnumb = Random.Range (1,100);
for (i=0;i < randnumb; i += 1){
var astroid : GameObject = GameObject.CreatePrimitive(PrimitiveType.Sphere);
astroid.transform.position = Vector3(Random.Range(0, 10000), Random.Range(0, 10000), Random.Range(0, 10000));
var randomx = Random.Range(50, 300.0);
var randomy = Random.Range(50, 300.0);
var randomz = Random.Range(50, 300.0);
astroid.transform.localScale = Vector3(randomx, randomy, randomz);
var volume = 4/3*$$anonymous$$athf.PI*randomx*randomy*randomz;
var metals = Random.Range(0, metal.length);
if(metals == 0)
copper = true;
if(metals == 1)
gold = true;
if(copper == true)
{
var copper1 = 8.96 * Random.Range(1, 100);
astroid.tag = 'Copper';
}
if(gold == true)
{
var gold1 = 19.32 * Random.Range(1, 100);
astroid.tag = 'Gold';
}
if(copper || gold == true)
{
var resources = copper1 || gold1;
}
astroid.AddComponent(Rigidbody);
astroid.rigidbody.mass = 2.9 * volume + resources;
astroid.AddComponent("Gravity");
}
}
Here is the cumplemesh script:
var scale = 1.0;
var speed = 1.0;
var recalculateNormals = false;
private var baseVertices : Vector3[];
private var noise : Perlin;
function Start ()
{
noise = new Perlin ();
}
function Update () {
var mesh : $$anonymous$$esh = GetComponent($$anonymous$$eshFilter).mesh;
if (baseVertices == null)
baseVertices = mesh.vertices;
var vertices = new Vector3[baseVertices.Length];
var timex = Time.time * speed + 0.1365143;
var timey = Time.time * speed + 1.21688;
var timez = Time.time * speed + 2.5564;
for (var i=0;i<vertices.Length;i++)
{
var vertex = baseVertices[i];
vertex.x += noise.Noise(timex + vertex.x, timex + vertex.y, timex + vertex.z) * scale;
vertex.y += noise.Noise(timey + vertex.x, timey + vertex.y, timey + vertex.z) * scale;
vertex.z += noise.Noise(timez + vertex.x, timez + vertex.y, timez + vertex.z) * scale;
vertices[i] = vertex;
}
mesh.vertices = vertices;
if (recalculateNormals)
mesh.RecalculateNormals();
mesh.RecalculateBounds();
}
I am not 100% on how to combine these two scripts.
Your answer
Follow this Question
Related Questions
Teleport to a random height 1 Answer
Transform to a random height 0 Answers
Pick a Transform[] or GameObject[] from script? 2 Answers
Semi-random prefabs generating 0 Answers