public void InitializeClusters() { clusters.Clear(); List<TriangleGraph> availableTriangles; if (scene.MainTerrain.GetTrianglesInRegion(RandomHelper.RandomGen, out availableTriangles, region)) { for (int i = 0; i < clusterSize; i++) { Material mat = materials[RandomHelper.RandomGen.Next(materials.Count)]; if (!clusters.ContainsKey(mat)) { clusters.Add(mat, new List<Cluster>()); } int randomIndex = RandomHelper.RandomGen.Next(i % availableTriangles.Count, availableTriangles.Count); TriangleGraph triangle = availableTriangles[randomIndex]; Vector3 position = triangle.GeneratePointInTriangle(RandomHelper.RandomGen); Vector3 normal = triangle.Normal; //position = Vector3.Transform(position, scene.MainTerrain.Transformation.GetTransform()); Cluster cluster = new Cluster(); RandomizeOrientation(cluster, position, normal); clusters[mat].Add(cluster); } } }
void RandomizeOrientation(Cluster cluster, Vector3 position, Vector3 surfaceNormal) { cluster.Transform = new Matrix[randomHelper.Next(minSides, maxSides)]; cluster.Bounds.Min = Vector3.One * float.PositiveInfinity; cluster.Bounds.Max = Vector3.One * float.NegativeInfinity; for (int i = 0; i < cluster.Transform.Length; i++) { Vector3 randScale = Vector3.Lerp(minScale, maxScale, (float)randomHelper.NextDouble()); float randAngle = MathHelper.TwoPi * (float)randomHelper.NextDouble(); cluster.Transform[i] = Matrix.CreateScale(randScale) * Matrix.CreateFromAxisAngle(surfaceNormal, randAngle); cluster.Transform[i].Translation = position + surfaceNormal*0.5f; Vector3 min = Vector3.Transform(new Vector3(-1,-1,0), cluster.Transform[i]); Vector3 max = Vector3.Transform(new Vector3(1, 1, 0), cluster.Transform[i]); cluster.Bounds.Min = Vector3.Min(min, cluster.Bounds.Min); cluster.Bounds.Max = Vector3.Max(max, cluster.Bounds.Max); } }
void RandomizeOrientation(Cluster cluster, Vector3 position, Vector3 surfaceNormal) { cluster.Transform = new Matrix[RandomHelper.RandomGen.Next(minSides, maxSides)]; Vector3 randScale = Vector3.Lerp(minScale, maxScale, (float)RandomHelper.RandomGen.NextDouble()); cluster.Bounds.Min = Vector3.One * float.PositiveInfinity; cluster.Bounds.Max = Vector3.One * float.NegativeInfinity; Vector3 fwd = new Vector3(surfaceNormal.Z, surfaceNormal.X, surfaceNormal.Y); fwd = Vector3.Normalize(fwd - Vector3.Dot(fwd, surfaceNormal) * surfaceNormal); Vector3 right = Vector3.Cross(fwd, surfaceNormal); for (int i = 0; i < cluster.Transform.Length; i++) { float randAngle = MathHelper.TwoPi * (float)RandomHelper.RandomGen.NextDouble(); Matrix orientation = Matrix.Identity; orientation.Up = surfaceNormal; orientation.Right = right;// Vector3.Normalize(new Vector3(normal.Z, normal.X, normal.Y)); orientation.Forward = fwd;// Vector3.Normalize(Vector3.Cross(worldMatrix.Up, worldMatrix.Right)); //orientation *= Matrix.CreateFromAxisAngle(surfaceNormal, randAngle); cluster.Transform[i] = Matrix.CreateScale(randScale) * Matrix.CreateRotationY(randAngle) * orientation; /* cluster.Transform[i] = Matrix.CreateRotationY(randAngle); cluster.Transform[i].Up = surfaceNormal; cluster.Transform[i] *= Matrix.CreateScale(randScale); */ cluster.Transform[i].Translation = position;// +surfaceNormal * randScale; Vector3 min = Vector3.Transform(new Vector3(-1,-1,-0.15f), cluster.Transform[i]); Vector3 max = Vector3.Transform(new Vector3(1, 1, 0.15f), cluster.Transform[i]); cluster.Bounds.Min = Vector3.Min(min, cluster.Bounds.Min); cluster.Bounds.Max = Vector3.Max(max, cluster.Bounds.Max); } }
void InitializeClusters() { for (int i = 0; i < clusterSize; i++) { Cluster cluster = new Cluster(); Material mat = materials[randomHelper.Next(materials.Count)]; Vector3 randPosition = Vector3.Zero; Vector3 randNormal = Vector3.Zero; scene.MainTerrain.GenerateRandomTransform(randomHelper, out randPosition, out randNormal); RandomizeOrientation(cluster, randPosition, randNormal); if (!clusters.ContainsKey(mat)) { clusters.Add(mat, new List<Cluster>()); } clusters[mat].Add(cluster); } }