private void RecalculateWeights() { weights = new SGT_WeightedRandom(100); for (var i = 0; i < StarVariantCount; i++) { var ssv = GetStarVariant(i); weights.Add(i, ssv != null ? ssv.SpawnProbability : 1.0f); } }
public void RecalculateWeights() { weightedRandom = new SGT_WeightedRandom(10); if (variantList != null) { for (var i = 0; i < variantList.Count; i++) { var variant = variantList[i]; weightedRandom.Add(i, variant.SpawnProbability); } } }
private Mesh GenerateStarMesh(int starCount) { var positions = new Vector3[starCount * 4]; var indices = new int[starCount * 6]; var uv0s = new Vector2[starCount * 4]; var uv1s = new Vector2[starCount * 4]; var normals = new Vector3[starCount * 4]; var colours = new Color[starCount * 4]; var bounds = new Bounds(); var weights = new SGT_WeightedRandom(100); for (var i = 0; i < StarVariantCount; i++) { var ssv = GetStarVariant(i); weights.Add(i, ssv != null ? ssv.SpawnProbability : 1.0f); } for (var i = 0; i < starCount; i++) { var i0 = i * 6; var i1 = i0 + 1; var i2 = i1 + 1; var i3 = i2 + 1; var i4 = i3 + 1; var i5 = i4 + 1; var v0 = i * 4; var v1 = v0 + 1; var v2 = v1 + 1; var v3 = v2 + 1; // Index data indices[i0] = v0; indices[i1] = v1; indices[i2] = v2; indices[i3] = v3; indices[i4] = v2; indices[i5] = v1; // Calculate star values var position = GeneratePosition(); var index = weights.RandomIndex; var po = packer.GetOutput(index); var ssv = GetStarVariant(index); float baseRadius, minRadius, maxRadius; if (ssv != null && ssv.Custom == true) { baseRadius = Random.Range(ssv.CustomRadiusMin, ssv.CustomRadiusMax); minRadius = Mathf.Max(baseRadius - ssv.CustomPulseRadiusMax, ssv.CustomRadiusMin); maxRadius = Mathf.Min(baseRadius + ssv.CustomPulseRadiusMax, ssv.CustomRadiusMax); } else { baseRadius = Random.Range(starRadiusMin, starRadiusMax); minRadius = Mathf.Max(baseRadius - starPulseRadiusMax, starRadiusMin); maxRadius = Mathf.Min(baseRadius + starPulseRadiusMax, starRadiusMax); } var midRadius = (minRadius + maxRadius) * 0.5f; var pulseRadius = (maxRadius - minRadius) * 0.5f; var pulseRate = Random.Range(0.0f, 1.0f); var pulseOffset = Random.Range(0.0f, 1.0f); var colour = new Color(pulseRate, pulseOffset, 0.0f); var uv1 = new Vector2(midRadius, pulseRadius); var rollAngle = Random.Range(-Mathf.PI, Mathf.PI); var right = SGT_Helper.Rotate(Vector2.right * SGT_Helper.InscribedBox, rollAngle); var up = SGT_Helper.Rotate(Vector2.up * SGT_Helper.InscribedBox, rollAngle); bounds.Encapsulate(position); // Write star values into vertex data positions[v0] = position; positions[v1] = position; positions[v2] = position; positions[v3] = position; normals[v0] = SGT_Helper.NewVector3(-right + up, 0.0f); normals[v1] = SGT_Helper.NewVector3(right + up, 0.0f); normals[v2] = SGT_Helper.NewVector3(-right - up, 0.0f); normals[v3] = SGT_Helper.NewVector3(right - up, 0.0f); colours[v0] = colour; colours[v1] = colour; colours[v2] = colour; colours[v3] = colour; if (po != null) { uv0s[v0] = po.UvTopLeft; uv0s[v1] = po.UvTopRight; uv0s[v2] = po.UvBottomLeft; uv0s[v3] = po.UvBottomRight; } uv1s[v0] = uv1; uv1s[v1] = uv1; uv1s[v2] = uv1; uv1s[v3] = uv1; } bounds.Expand(starRadiusMax); var starMesh = new Mesh(); starMesh.name = "Starfield"; starMesh.bounds = bounds; starMesh.vertices = positions; starMesh.normals = normals; starMesh.colors = colours; starMesh.uv = uv0s; starMesh.uv2 = uv1s; starMesh.triangles = indices; return(starMesh); }
private Mesh GenerateStarMesh(int starCount) { var positions = new Vector3[starCount * 4]; var indices = new int[starCount * 6]; var uv0s = new Vector2[starCount * 4]; var uv1s = new Vector2[starCount * 4]; var normals = new Vector3[starCount * 4]; var colours = new Color[starCount * 4]; var bounds = new Bounds(); var weights = new SGT_WeightedRandom(100); for (var i = 0; i < StarVariantCount; i++) { var ssv = GetStarVariant(i); weights.Add(i, ssv != null ? ssv.SpawnProbability : 1.0f); } for (var i = 0; i < starCount; i++) { var i0 = i * 6; var i1 = i0 + 1; var i2 = i1 + 1; var i3 = i2 + 1; var i4 = i3 + 1; var i5 = i4 + 1; var v0 = i * 4; var v1 = v0 + 1; var v2 = v1 + 1; var v3 = v2 + 1; // Index data indices[i0] = v0; indices[i1] = v1; indices[i2] = v2; indices[i3] = v3; indices[i4] = v2; indices[i5] = v1; // Calculate star values var position = GeneratePosition(); var index = weights.RandomIndex; var po = packer.GetOutput(index); var ssv = GetStarVariant(index); float baseRadius, minRadius, maxRadius; if (ssv != null && ssv.Custom == true) { baseRadius = Random.Range(ssv.CustomRadiusMin, ssv.CustomRadiusMax); minRadius = Mathf.Max(baseRadius - ssv.CustomPulseRadiusMax, ssv.CustomRadiusMin); maxRadius = Mathf.Min(baseRadius + ssv.CustomPulseRadiusMax, ssv.CustomRadiusMax); } else { baseRadius = Random.Range(starRadiusMin, starRadiusMax); minRadius = Mathf.Max(baseRadius - starPulseRadiusMax, starRadiusMin); maxRadius = Mathf.Min(baseRadius + starPulseRadiusMax, starRadiusMax); } var midRadius = (minRadius + maxRadius) * 0.5f; var pulseRadius = (maxRadius - minRadius) * 0.5f; var pulseRate = Random.Range(0.0f, 1.0f); var pulseOffset = Random.Range(0.0f, 1.0f); var colour = new Color(pulseRate, pulseOffset, 0.0f); var uv1 = new Vector2(midRadius, pulseRadius); var rollAngle = Random.Range(-Mathf.PI, Mathf.PI); var right = SGT_Helper.Rotate(Vector2.right * SGT_Helper.InscribedBox, rollAngle); var up = SGT_Helper.Rotate(Vector2.up * SGT_Helper.InscribedBox, rollAngle); bounds.Encapsulate(position); // Write star values into vertex data positions[v0] = position; positions[v1] = position; positions[v2] = position; positions[v3] = position; normals[v0] = SGT_Helper.NewVector3(-right + up, 0.0f); normals[v1] = SGT_Helper.NewVector3( right + up, 0.0f); normals[v2] = SGT_Helper.NewVector3(-right - up, 0.0f); normals[v3] = SGT_Helper.NewVector3( right - up, 0.0f); colours[v0] = colour; colours[v1] = colour; colours[v2] = colour; colours[v3] = colour; if (po != null) { uv0s[v0] = po.UvTopLeft; uv0s[v1] = po.UvTopRight; uv0s[v2] = po.UvBottomLeft; uv0s[v3] = po.UvBottomRight; } uv1s[v0] = uv1; uv1s[v1] = uv1; uv1s[v2] = uv1; uv1s[v3] = uv1; } bounds.Expand(starRadiusMax); var starMesh = new Mesh(); starMesh.name = "Starfield"; starMesh.bounds = bounds; starMesh.vertices = positions; starMesh.normals = normals; starMesh.colors = colours; starMesh.uv = uv0s; starMesh.uv2 = uv1s; starMesh.triangles = indices; return starMesh; }