protected override void OnInspector() { var updateMesh = false; var updateModel = false; var updateMaterial = false; BeginError(Any(t => t.Mesh == null)); DrawDefault("Mesh", ref updateMesh); EndError(); BeginError(Any(t => t.Material == null)); DrawDefault("Material", ref updateMaterial); EndError(); DrawDefault("CameraOffset"); // Updated automatically DrawDefault("FollowCameras"); // Automatically updated if (Any(t => t.FollowCameras == true)) { BeginIndent(); BeginError(Any(t => t.FollowDistance <= 0.0f)); DrawDefault("FollowDistance"); // Automatically updated EndError(); EndIndent(); } Separator(); DrawDefault("Depth"); // Automatically updated if (Any(t => t.Mesh == null && t.GetComponent <SgtFlareMesh>() == null)) { Separator(); if (Button("Add Mesh") == true) { Each(t => SgtHelper.GetOrAddComponent <SgtFlareMesh>(t.gameObject)); } } if (Any(t => t.Material == null && t.GetComponent <SgtFlareMaterial>() == null)) { Separator(); if (Button("Add Material") == true) { Each(t => SgtHelper.GetOrAddComponent <SgtFlareMaterial>(t.gameObject)); } } if (updateMesh == true) { DirtyEach(t => t.UpdateMesh()); } if (updateModel == true) { DirtyEach(t => t.UpdateModel()); } if (updateMaterial == true) { DirtyEach(t => t.UpdateMaterial()); } }
protected override void OnDestroy() { base.OnDestroy(); SgtHelper.Destroy(lightingLut); }
private void RegenerateMeshes() { var stars = default(List <SgtStarfieldStar>); var pool = default(bool); CalculateStars(out stars, out pool); if (stars != null) { // Sort stars into groups for (var i = stars.Count - 1; i >= 0; i--) { var star = stars[i]; if (star != null) { var group = GetGroup(star.Sprite); group.Stars.Add(star); } } // Pool stars? if (pool == true) { SgtClassPool <SgtStarfieldStar> .Add(stars); } // Build groups for (var i = groups.Count - 1; i >= 0; i--) { var group = groups[i]; if (group.Stars.Count > 0) { var groupStars = group.Stars; var minMaxSet = false; var min = default(Vector3); var max = default(Vector3); SgtProceduralMesh.Clear(); for (var j = groupStars.Count - 1; j >= 0; j--) { var star = groupStars[j]; var position = star.Position; var radius = star.Radius; var uv = SgtHelper.CalculateSpriteUV(star.Sprite); var angle = star.Angle / Mathf.PI; ExpandBounds(ref minMaxSet, ref min, ref max, position, radius); SgtProceduralMesh.PushPosition(position, 4); SgtProceduralMesh.PushColor(star.Color, 4); SgtProceduralMesh.PushNormal(-1.0f, 1.0f, angle); SgtProceduralMesh.PushNormal(1.0f, 1.0f, angle); SgtProceduralMesh.PushNormal(-1.0f, -1.0f, angle); SgtProceduralMesh.PushNormal(1.0f, -1.0f, angle); SgtProceduralMesh.PushTangent(star.PulseOffset, star.PulseSpeed, star.PulseRange, 0.0f, 4); SgtProceduralMesh.PushCoord1(uv.x, uv.y); SgtProceduralMesh.PushCoord1(uv.z, uv.y); SgtProceduralMesh.PushCoord1(uv.x, uv.w); SgtProceduralMesh.PushCoord1(uv.z, uv.w); SgtProceduralMesh.PushCoord2(radius, 0.5f); SgtProceduralMesh.PushCoord2(radius, -0.5f); SgtProceduralMesh.PushCoord2(radius, 0.5f); SgtProceduralMesh.PushCoord2(radius, -0.5f); } var bounds = SgtHelper.NewBoundsFromMinMax(min, max); SgtProceduralMesh.SplitQuads(HideFlags.DontSave); var meshCount = SgtProceduralMesh.Count; // Copy meshes for (var j = 0; j < meshCount; j++) { var mesh = SgtProceduralMesh.Pop(); var model = group.Models.Count > j ? group.Models[j] : SgtStarfieldModel.Create(group); mesh.bounds = bounds; model.Mesh = mesh; } } } } }
private void RegenerateMeshes() { var asteroids = default(List <SgtBeltAsteroid>); var pool = default(bool); CalculateAsteroids(out asteroids, out pool); if (asteroids != null) { // Sort asteroids into groups for (var i = asteroids.Count - 1; i >= 0; i--) { var asteroid = asteroids[i]; if (asteroid != null) { var group = GetGroup(asteroid.MainTex, asteroid.HeightTex); group.Asteroids.Add(asteroid); } } // Pool asteroids? if (pool == true) { SgtClassPool <SgtBeltAsteroid> .Add(asteroids); } // Build groups for (var i = groups.Count - 1; i >= 0; i--) { var group = groups[i]; var groupAsteroids = group.Asteroids; var maxWidth = 0.0f; var maxHeight = 0.0f; SgtProceduralMesh.Clear(); for (var j = groupAsteroids.Count - 1; j >= 0; j--) { var asteroid = groupAsteroids[j]; var radius = asteroid.Radius; var distance = asteroid.OrbitDistance; var height = asteroid.Height; var uv = SgtHelper.CalculateSpriteUV(asteroid.MainTex); maxWidth = Mathf.Max(maxWidth, distance + radius); maxHeight = Mathf.Max(maxHeight, height + radius); SgtProceduralMesh.PushPosition(asteroid.OrbitAngle, distance, asteroid.OrbitSpeed, 4); SgtProceduralMesh.PushColor(asteroid.Color, 4); SgtProceduralMesh.PushNormal(-1.0f, 1.0f, 0.0f); SgtProceduralMesh.PushNormal(1.0f, 1.0f, 0.0f); SgtProceduralMesh.PushNormal(-1.0f, -1.0f, 0.0f); SgtProceduralMesh.PushNormal(1.0f, -1.0f, 0.0f); SgtProceduralMesh.PushTangent(asteroid.Angle / Mathf.PI, asteroid.Spin / Mathf.PI, 0.0f, 0.0f, 4); SgtProceduralMesh.PushCoord1(uv.x, uv.y); SgtProceduralMesh.PushCoord1(uv.z, uv.y); SgtProceduralMesh.PushCoord1(uv.x, uv.w); SgtProceduralMesh.PushCoord1(uv.z, uv.w); SgtProceduralMesh.PushCoord2(radius, height, 4); } var bounds = new Bounds(Vector3.zero, new Vector3(maxWidth * 2.0f, maxHeight * 2.0f, maxWidth * 2.0f)); SgtProceduralMesh.SplitQuads(HideFlags.DontSave); var meshCount = SgtProceduralMesh.Count; // Copy meshes for (var j = 0; j < meshCount; j++) { var mesh = SgtProceduralMesh.Pop(); var model = group.Models.Count > j ? group.Models[j] : SgtBeltModel.Create(group); mesh.bounds = bounds; model.Mesh = mesh; } } } }
protected virtual void OnDestroy() { SgtHelper.Destroy(generatedTexture); }
protected virtual void LateUpdate() { // The lights and shadows may have moved, so write them if (Material != null) { SgtHelper.SetTempMaterial(Material); SgtHelper.WriteLights(Lit, Lights, 2, transform.position, transform, null, SgtHelper.Brighten(Color, Brightness), ScatteringStrength); SgtHelper.WriteShadows(Shadows, 2); } }
protected override void EndQuads() { SgtHelper.EndRandomSeed(); }
public static void CreateCloudsphereMenuItem() { var cloudsphere = CreateCloudsphere(null); SgtHelper.SelectAndPing(cloudsphere); }
public static void CreateAdvancedBeltMenuItem() { var belt = CreateAdvancedBelt(null); SgtHelper.SelectAndPing(belt); }
protected override void OnInspector() { var updateMaterial = false; var updateModels = false; DrawDefault("Color", ref updateMaterial); BeginError(Any(t => t.Brightness <= 0.0f)); DrawDefault("Brightness", ref updateMaterial); EndError(); DrawDefault("RenderQueue", ref updateMaterial); DrawDefault("RenderQueueOffset", ref updateMaterial); Separator(); BeginError(Any(t => t.MainTex == null)); DrawDefault("MainTex", ref updateMaterial); EndError(); BeginError(Any(t => t.DepthTex == null)); DrawDefault("DepthTex", ref updateMaterial); EndError(); BeginError(Any(t => t.Radius < 0.0f)); DrawDefault("Radius", ref updateModels); EndError(); DrawDefault("CameraOffset"); // Updated automatically Separator(); DrawDefault("Fade", ref updateMaterial); if (Any(t => t.Fade == true)) { BeginIndent(); BeginError(Any(t => t.FadeTex == null)); DrawDefault("FadeTex", ref updateMaterial); EndError(); BeginError(Any(t => t.FadeDistance <= 0.0f)); DrawDefault("FadeDistance", ref updateMaterial); EndError(); EndIndent(); } Separator(); BeginError(Any(t => t.MeshRadius <= 0.0f)); DrawDefault("MeshRadius", ref updateModels); EndError(); BeginError(Any(t => t.Meshes != null && t.Meshes.Count == 0)); DrawDefault("Meshes", ref updateModels); EndError(); Separator(); DrawDefault("Lit", ref updateModels); if (Any(t => t.Lit == true)) { BeginIndent(); BeginError(Any(t => t.LightingTex == null)); DrawDefault("LightingTex", ref updateMaterial); EndError(); BeginError(Any(t => t.Lights != null && (t.Lights.Count == 0 || t.Lights.Exists(l => l == null)))); DrawDefault("Lights", ref updateMaterial); EndError(); BeginError(Any(t => t.Shadows != null && t.Shadows.Exists(s => s == null))); DrawDefault("Shadows", ref updateMaterial); EndError(); EndIndent(); } if (Any(t => t.DepthTex == null && t.GetComponent <SgtCloudsphereDepth>() == null)) { Separator(); if (Button("Add Depth") == true) { Each(t => SgtHelper.GetOrAddComponent <SgtCloudsphereDepth>(t.gameObject)); } } if (Any(t => t.Lit == true && t.LightingTex == null && t.GetComponent <SgtCloudsphereLighting>() == null)) { Separator(); if (Button("Add Lighting") == true) { Each(t => SgtHelper.GetOrAddComponent <SgtCloudsphereLighting>(t.gameObject)); } } if (Any(t => t.Fade == true && t.FadeTex == null && t.GetComponent <SgtCloudsphereFade>() == null)) { Separator(); if (Button("Add Fade") == true) { Each(t => SgtHelper.GetOrAddComponent <SgtCloudsphereFade>(t.gameObject)); } } if (updateMaterial == true) { DirtyEach(t => t.UpdateMaterial()); } if (updateModels == true) { DirtyEach(t => t.UpdateModels()); } }
private void UpdateMaterialNonSerialized() { SgtHelper.SetTempMaterial(Material); SgtHelper.WriteShadowsNonSerialized(Shadows, 2); }
public static void CreateSingularityMenuItem() { var singularity = CreateSingularity(null); SgtHelper.SelectAndPing(singularity); }
protected override void BuildMesh(Mesh mesh, int asteroidIndex, int asteroidCount) { var positions = new Vector3[asteroidCount * 4]; var colors = new Color[asteroidCount * 4]; var normals = new Vector3[asteroidCount * 4]; var tangents = new Vector4[asteroidCount * 4]; var coords1 = new Vector2[asteroidCount * 4]; var coords2 = new Vector2[asteroidCount * 4]; var indices = new int[asteroidCount * 6]; var maxWidth = 0.0f; var maxHeight = 0.0f; for (var i = 0; i < asteroidCount; i++) { NextQuad(ref SgtBeltAsteroid.Temp, asteroidIndex + i); var offV = i * 4; var offI = i * 6; var radius = SgtBeltAsteroid.Temp.Radius; var distance = SgtBeltAsteroid.Temp.OrbitDistance; var height = SgtBeltAsteroid.Temp.Height; var uv = tempCoords[SgtHelper.Mod(SgtBeltAsteroid.Temp.Variant, tempCoords.Count)]; maxWidth = Mathf.Max(maxWidth, distance + radius); maxHeight = Mathf.Max(maxHeight, height + radius); positions[offV + 0] = positions[offV + 1] = positions[offV + 2] = positions[offV + 3] = new Vector3(SgtBeltAsteroid.Temp.OrbitAngle, distance, SgtBeltAsteroid.Temp.OrbitSpeed); colors[offV + 0] = colors[offV + 1] = colors[offV + 2] = colors[offV + 3] = SgtBeltAsteroid.Temp.Color; normals[offV + 0] = new Vector3(-1.0f, 1.0f, 0.0f); normals[offV + 1] = new Vector3(1.0f, 1.0f, 0.0f); normals[offV + 2] = new Vector3(-1.0f, -1.0f, 0.0f); normals[offV + 3] = new Vector3(1.0f, -1.0f, 0.0f); tangents[offV + 0] = tangents[offV + 1] = tangents[offV + 2] = tangents[offV + 3] = new Vector4(SgtBeltAsteroid.Temp.Angle / Mathf.PI, SgtBeltAsteroid.Temp.Spin / Mathf.PI, 0.0f, 0.0f); coords1[offV + 0] = new Vector2(uv.x, uv.y); coords1[offV + 1] = new Vector2(uv.z, uv.y); coords1[offV + 2] = new Vector2(uv.x, uv.w); coords1[offV + 3] = new Vector2(uv.z, uv.w); coords2[offV + 0] = coords2[offV + 1] = coords2[offV + 2] = coords2[offV + 3] = new Vector2(radius, height); indices[offI + 0] = offV + 0; indices[offI + 1] = offV + 1; indices[offI + 2] = offV + 2; indices[offI + 3] = offV + 3; indices[offI + 4] = offV + 2; indices[offI + 5] = offV + 1; } mesh.vertices = positions; mesh.colors = colors; mesh.normals = normals; mesh.tangents = tangents; mesh.uv = coords1; mesh.uv2 = coords2; mesh.triangles = indices; mesh.bounds = new Bounds(Vector3.zero, new Vector3(maxWidth * 2.0f, maxHeight * 2.0f, maxWidth * 2.0f)); }
protected virtual void LateUpdate() { if (Application.isPlaying == true) { OrbitOffset += Time.deltaTime * OrbitSpeed; } if (Material != null) { Material.SetFloat("_Age", OrbitOffset); } // The lights and shadows may have moved, so write them if (Material != null) { SgtHelper.SetTempMaterial(Material); SgtHelper.WriteLights(Lit, Lights, 2, transform.position, transform, null, SgtHelper.Brighten(Color, Brightness), 1.0f); SgtHelper.WriteShadows(Shadows, 2); } }
private static void CreateSpiralStarfieldMenuItem() { var starfield = CreateSpiralStarfield(null); SgtHelper.SelectAndPing(starfield); }
public static void GenerateMesh(SgtPatch patch) { var terrain = patch.Terrain; if (terrain.Resolution > 0) { var positions = terrain.Positions; var coords1 = terrain.Coords1; var coords2 = terrain.Coords2; var normals = terrain.Normals; var tangents = terrain.Tangents; var quadPoints = terrain.QuadPoints; var quadNormals = terrain.QuadNormals; var quadTangents = terrain.QuadTangents; var indices = terrain.Indices; var res = terrain.Resolution; var resAdd1 = res + 1; var resAdd2 = res + 2; var resAdd3 = res + 3; var resRecip = SgtHelper.Reciprocal(res); var mainVerts = resAdd1 * resAdd1; var skirtVerts = resAdd1 * 4; var mainIndices = res * res * 6; var skirtIndices = res * 24; var vertex = default(int); var vertex2 = default(int); var index = default(int); if (positions.Length != mainVerts + skirtVerts) { terrain.Positions = positions = new Vector3[mainVerts + skirtVerts]; } if (coords1.Length != mainVerts + skirtVerts) { terrain.Coords1 = coords1 = new Vector2[mainVerts + skirtVerts]; } if (coords2.Length != mainVerts + skirtVerts) { terrain.Coords2 = coords2 = new Vector2[mainVerts + skirtVerts]; } if (normals.Length != mainVerts + skirtVerts) { terrain.Normals = normals = new Vector3[mainVerts + skirtVerts]; } if (tangents.Length != mainVerts + skirtVerts) { terrain.Tangents = tangents = new Vector4[mainVerts + skirtVerts]; } if (quadPoints.Length != resAdd3 * resAdd3) { terrain.QuadPoints = quadPoints = new Vector3[resAdd3 * resAdd3]; } if (quadNormals.Length != resAdd2 * resAdd2) { terrain.QuadNormals = quadNormals = new Vector3[resAdd2 * resAdd2]; } if (quadTangents.Length != resAdd2 * resAdd2) { terrain.QuadTangents = quadTangents = new Vector3[resAdd2 * resAdd2]; } // Go through all vertices, but extend the borders by one for (var y = -1; y < resAdd2; y++) { for (var x = -1; x < resAdd2; x++) { var u = x * resRecip; var v = y * resRecip; var pointB = Lerp3(patch.PointBL, patch.PointBR, u); var pointT = Lerp3(patch.PointTL, patch.PointTR, u); var point = Lerp3(pointB, pointT, v).normalized; point *= terrain.GetHeight(point); index = x + 1 + (y + 1) * resAdd3; quadPoints[index] = point; // Is this a main vertex? if (x >= 0 && x < resAdd1 && y >= 0 && y < resAdd1) { var coordB = Lerp2(patch.CoordBL, patch.CoordBR, u); var coordT = Lerp2(patch.CoordTL, patch.CoordTR, u); var coord1 = Lerp2(coordB, coordT, v); vertex = x + y * resAdd1; positions[vertex] = point; coords1[vertex] = coord1; coords2[vertex] = new Vector2(u, v); } } } // Quad normals & tangents for (var y = 0; y < resAdd2; y++) { for (var x = 0; x < resAdd2; x++) { var bl = x + y * resAdd3; var br = x + 1 + y * resAdd3; var tl = x + (y + 1) * resAdd3; var tr = x + 1 + (y + 1) * resAdd3; var b = quadPoints[bl] - quadPoints[br]; var t = quadPoints[tl] - quadPoints[tr]; var l = quadPoints[bl] - quadPoints[tl]; var r = quadPoints[br] - quadPoints[tr]; var h = (b + t).normalized; var v = (l + r).normalized; var i = x + y * resAdd2; quadNormals[i] = Vector3.Cross(h, v); quadTangents[i] = v; } } // Normals & Tangents for (var y = 0; y < resAdd1; y++) { for (var x = 0; x < resAdd1; x++) { var bl = x + y * resAdd2; var br = x + 1 + y * resAdd2; var tl = x + (y + 1) * resAdd2; var tr = x + 1 + (y + 1) * resAdd2; var n = quadNormals[bl] + quadNormals[br] + quadNormals[tl] + quadNormals[tr]; var t = quadTangents[bl] + quadTangents[br] + quadTangents[tl] + quadTangents[tr]; var i = x + y * resAdd1; normals[i] = n.normalized; //normals[i] = positions[i].normalized; tangents[i] = SgtHelper.NewVector4(t.normalized, 1.0f); } } // Skirt vertices var scale = 1.0f - SgtHelper.Divide(terrain.SkirtThickness * Mathf.Pow(0.5f, patch.Depth), 1.0f); for (var i = 0; i < resAdd1; i++) { // Bottom vertex = mainVerts + i; vertex2 = i; positions[vertex] = positions[vertex2] * scale; coords1[vertex] = coords1[vertex2]; coords2[vertex] = coords2[vertex2]; normals[vertex] = normals[vertex2]; tangents[vertex] = tangents[vertex2]; // Top vertex = mainVerts + i + resAdd1; vertex2 = resAdd1 * res + i; positions[vertex] = positions[vertex2] * scale; coords1[vertex] = coords1[vertex2]; coords2[vertex] = coords2[vertex2]; normals[vertex] = normals[vertex2]; tangents[vertex] = tangents[vertex2]; // Left vertex = mainVerts + i + resAdd1 + resAdd1; vertex2 = resAdd1 * i; positions[vertex] = positions[vertex2] * scale; coords1[vertex] = coords1[vertex2]; coords2[vertex] = coords2[vertex2]; normals[vertex] = normals[vertex2]; tangents[vertex] = tangents[vertex2]; // Right vertex = mainVerts + i + resAdd1 + resAdd1 + resAdd1; vertex2 = resAdd1 * i + res; positions[vertex] = positions[vertex2] * scale; coords1[vertex] = coords1[vertex2]; coords2[vertex] = coords2[vertex2]; normals[vertex] = normals[vertex2]; tangents[vertex] = tangents[vertex2]; } // Indices if (indices.Length != mainIndices + skirtIndices) { terrain.Indices = indices = new int[mainIndices + skirtIndices]; // Main for (var y = 0; y < res; y++) { for (var x = 0; x < res; x++) { index = (x + y * res) * 6; vertex = x + y * resAdd1; indices[index + 0] = vertex; indices[index + 1] = vertex + 1; indices[index + 2] = vertex + resAdd1; indices[index + 3] = vertex + resAdd1 + 1; indices[index + 4] = vertex + resAdd1; indices[index + 5] = vertex + 1; } } // Skirt for (var i = 0; i < res; i++) { // Bottom index = mainIndices + (res * 0 + i) * 6; vertex = mainVerts + i; vertex2 = i; indices[index + 0] = vertex; indices[index + 1] = vertex + 1; indices[index + 2] = vertex2; indices[index + 3] = vertex2 + 1; indices[index + 4] = vertex2; indices[index + 5] = vertex + 1; // Top index = mainIndices + (res * 1 + i) * 6; vertex = mainVerts + i + resAdd1; vertex2 = res * resAdd1 + i; indices[index + 0] = vertex2; indices[index + 1] = vertex2 + 1; indices[index + 2] = vertex; indices[index + 3] = vertex + 1; indices[index + 4] = vertex; indices[index + 5] = vertex2 + 1; // Left index = mainIndices + (res * 2 + i) * 6; vertex = mainVerts + i + resAdd1 + resAdd1; vertex2 = i * resAdd1; indices[index + 0] = vertex; indices[index + 1] = vertex2; indices[index + 2] = vertex + 1; indices[index + 3] = vertex2 + resAdd1; indices[index + 4] = vertex + 1; indices[index + 5] = vertex2; // Right index = mainIndices + (res * 3 + i) * 6; vertex = mainVerts + i + resAdd1 + resAdd1 + resAdd1; vertex2 = i * resAdd1 + res; indices[index + 0] = vertex2; indices[index + 1] = vertex; indices[index + 2] = vertex2 + resAdd1; indices[index + 3] = vertex + 1; indices[index + 4] = vertex2 + resAdd1; indices[index + 5] = vertex; } } if (patch.Mesh != null) { patch.Mesh.Clear(); } else { patch.Mesh = SgtObjectPool <Mesh> .Pop() ?? new Mesh(); patch.Mesh.hideFlags = HideFlags.DontSave; } patch.Mesh.vertices = positions; patch.Mesh.uv = coords1; patch.Mesh.uv2 = coords2; patch.Mesh.normals = normals; patch.Mesh.tangents = tangents; patch.Mesh.triangles = indices; patch.Mesh.RecalculateBounds(); patch.MeshCenter = patch.Mesh.bounds.center; } }
protected override void OnInspector() { var updateMaterial = false; var updateModels = false; DrawDefault("Color", ref updateMaterial); BeginError(Any(t => t.Brightness < 0.0f)); DrawDefault("Brightness", ref updateMaterial); EndError(); DrawDefault("RenderQueue", ref updateMaterial); DrawDefault("RenderQueueOffset", ref updateMaterial); Separator(); BeginError(Any(t => t.MainTex == null)); DrawDefault("MainTex", ref updateMaterial); EndError(); BeginError(Any(t => t.DepthTex == null)); DrawDefault("DepthTex", ref updateMaterial); EndError(); Separator(); BeginError(Any(t => t.Sky < 0.0f)); DrawDefault("Sky"); // Updated when rendering EndError(); DrawDefault("Lit", ref updateMaterial); if (Any(t => t.Lit == true)) { BeginIndent(); BeginError(Any(t => t.LightingTex == null)); DrawDefault("LightingTex", ref updateMaterial); EndError(); DrawDefault("Scattering", ref updateMaterial); if (Any(t => t.Scattering == true)) { BeginIndent(); BeginError(Any(t => t.ScatteringTex == null)); DrawDefault("ScatteringTex", ref updateMaterial); EndError(); DrawDefault("ScatteringStrength", ref updateMaterial); EndIndent(); } BeginError(Any(t => t.Lights != null && (t.Lights.Count == 0 || t.Lights.Exists(l => l == null)))); DrawDefault("Lights", ref updateMaterial); EndError(); BeginError(Any(t => t.Shadows != null && t.Shadows.Exists(s => s == null))); DrawDefault("Shadows", ref updateMaterial); EndError(); EndIndent(); } Separator(); BeginError(Any(t => t.MeshRadius <= 0.0f)); DrawDefault("MeshRadius", ref updateModels); EndError(); BeginError(Any(t => t.Meshes != null && t.Meshes.Count == 0)); DrawDefault("Meshes", ref updateModels); EndError(); if (Any(t => t.DepthTex == null && t.GetComponent <SgtJovianDepth>() == null)) { Separator(); if (Button("Add Depth") == true) { Each(t => SgtHelper.GetOrAddComponent <SgtJovianDepth>(t.gameObject)); } } if (Any(t => t.Lit == true && t.LightingTex == null && t.GetComponent <SgtJovianLighting>() == null)) { Separator(); if (Button("Add Lighting") == true) { Each(t => SgtHelper.GetOrAddComponent <SgtJovianLighting>(t.gameObject)); } } if (Any(t => t.Lit == true && t.Scattering == true && t.ScatteringTex == null && t.GetComponent <SgtJovianScattering>() == null)) { Separator(); if (Button("Add Scattering") == true) { Each(t => SgtHelper.GetOrAddComponent <SgtJovianScattering>(t.gameObject)); } } serializedObject.ApplyModifiedProperties(); if (updateMaterial == true) { DirtyEach(t => t.UpdateMaterial()); } if (updateModels == true) { DirtyEach(t => t.UpdateModels()); } }
private void Build(SgtTerrain terrain, Vector3 bestPoint) { if (meshCollider == null) { var gameObject = new GameObject("Plane"); #if UNITY_EDITOR gameObject.hideFlags = HideFlags.DontSave; #endif meshCollider = gameObject.AddComponent <MeshCollider>(); } if (mesh == null) { mesh = SgtObjectPool <Mesh> .Pop() ?? new Mesh(); #if UNITY_EDITOR mesh.hideFlags = HideFlags.DontSave; #endif mesh.name = "Plane"; } var sideE = Detail; var sideP = Detail + 1; var vertexCount = sideP * sideP; var indexCount = sideE * sideE * 6; var rotation = Quaternion.Inverse(terrain.transform.rotation) * Quaternion.LookRotation(bestPoint); var distance = bestPoint.magnitude; var uniformScale = SgtHelper.UniformScale(terrain.transform.lossyScale); var size = Size * SgtHelper.Reciprocal(uniformScale); var step = (size * 2.0f) / Detail; if (positions == null || positions.Length != vertexCount) { positions = new Vector3[vertexCount]; } for (var y = 0; y <= Detail; y++) { for (var x = 0; x <= Detail; x++) { var index = x + y * sideP; var point = rotation * new Vector3(x * step - size, y * step - size, distance); positions[index] = terrain.GetLocalPoint(point); } } // Regen indices? if (indices == null || indices.Length != indexCount) { indices = new int[indexCount]; for (var y = 0; y < sideE; y++) { for (var x = 0; x < sideE; x++) { var index = (x + y * sideE) * 6; var vertex = x + y * sideP; indices[index + 0] = vertex; indices[index + 1] = vertex + 1; indices[index + 2] = vertex + sideP; indices[index + 3] = vertex + sideP + 1; indices[index + 4] = vertex + sideP; indices[index + 5] = vertex + 1; } } mesh.Clear(); } mesh.vertices = positions; mesh.triangles = indices; meshCollider.sharedMesh = mesh; meshCollider.transform.SetParent(terrain.transform, false); }
public void RebuildPatch(SgtPatch patch) { if (Resolution > 0) { var resAdd1 = Resolution + 1; var resAdd2 = Resolution + 2; var resAdd3 = Resolution + 3; var resRecip = SgtHelper.Reciprocal(Resolution); var mainVerts = resAdd1 * resAdd1; var skirtVerts = resAdd1 * 4; var mainIndices = Resolution * Resolution * 6; var skirtIndices = Resolution * 24; var mesh = patch.Mesh; var vertex = default(int); var vertex2 = default(int); var index = default(int); if (positions == null || positions.Length != mainVerts + skirtVerts) { positions = new Vector3[mainVerts + skirtVerts]; } if (coords1 == null || coords1.Length != mainVerts + skirtVerts) { coords1 = new Vector2[mainVerts + skirtVerts]; } if (coords2 == null || coords2.Length != mainVerts + skirtVerts) { coords2 = new Vector2[mainVerts + skirtVerts]; } if (normals == null || normals.Length != mainVerts + skirtVerts) { normals = new Vector3[mainVerts + skirtVerts]; } if (tangents == null || tangents.Length != mainVerts + skirtVerts) { tangents = new Vector4[mainVerts + skirtVerts]; } if (quadPoints == null || quadPoints.Length != resAdd3 * resAdd3) { quadPoints = new Vector3[resAdd3 * resAdd3]; } if (quadNormals == null || quadNormals.Length != resAdd2 * resAdd2) { quadNormals = new Vector3[resAdd2 * resAdd2]; } if (quadTangents == null || quadTangents.Length != resAdd2 * resAdd2) { quadTangents = new Vector3[resAdd2 * resAdd2]; } // Go through all vertices, but extend the borders by one for (var y = -1; y < resAdd2; y++) { for (var x = -1; x < resAdd2; x++) { var u = x * resRecip; var v = y * resRecip; var pointB = Lerp3(patch.PointBL, patch.PointBR, u); var pointT = Lerp3(patch.PointTL, patch.PointTR, u); var point = GetSurfacePositionLocal(Lerp3(pointB, pointT, v)); index = x + 1 + (y + 1) * resAdd3; quadPoints[index] = point; // Is this a main vertex? if (x >= 0 && x < resAdd1 && y >= 0 && y < resAdd1) { var coordB = Lerp2(patch.CoordBL, patch.CoordBR, u); var coordT = Lerp2(patch.CoordTL, patch.CoordTR, u); var coord1 = Lerp2(coordB, coordT, v); var coord2 = new Vector2(u, v); var center = (patch.PointBL + patch.PointBR + patch.PointTL + patch.PointTR) * 0.25f; if (OnCalculateCoord1 != null) { OnCalculateCoord1(point, center, ref coord1); } if (OnCalculateCoord2 != null) { OnCalculateCoord2(point, center, ref coord2); } vertex = x + y * resAdd1; positions[vertex] = point; coords1[vertex] = coord1; coords2[vertex] = coord2; } } } // Quad normals & tangents for (var y = 0; y < resAdd2; y++) { for (var x = 0; x < resAdd2; x++) { var bl = x + y * resAdd3; var br = x + 1 + y * resAdd3; var tl = x + (y + 1) * resAdd3; var tr = x + 1 + (y + 1) * resAdd3; var b = quadPoints[bl] - quadPoints[br]; var t = quadPoints[tl] - quadPoints[tr]; var l = quadPoints[bl] - quadPoints[tl]; var r = quadPoints[br] - quadPoints[tr]; var h = (b + t).normalized; var v = (l + r).normalized; var i = x + y * resAdd2; quadNormals[i] = Vector3.Cross(h, v); quadTangents[i] = v; } } // Normals & Tangents for (var y = 0; y < resAdd1; y++) { for (var x = 0; x < resAdd1; x++) { var bl = x + y * resAdd2; var br = x + 1 + y * resAdd2; var tl = x + (y + 1) * resAdd2; var tr = x + 1 + (y + 1) * resAdd2; var n = quadNormals[bl] + quadNormals[br] + quadNormals[tl] + quadNormals[tr]; var t = quadTangents[bl] + quadTangents[br] + quadTangents[tl] + quadTangents[tr]; var i = x + y * resAdd1; normals[i] = n.normalized; //normals[i] = positions[i].normalized; tangents[i] = SgtHelper.NewVector4(t.normalized, 1.0f); } } // Skirt vertices var scale = 1.0f - SgtHelper.Divide(SkirtThickness * Mathf.Pow(0.5f, patch.Depth), 1.0f); for (var i = 0; i < resAdd1; i++) { // Bottom vertex = mainVerts + i; vertex2 = i; positions[vertex] = positions[vertex2] * scale; coords1[vertex] = coords1[vertex2]; coords2[vertex] = coords2[vertex2]; normals[vertex] = normals[vertex2]; tangents[vertex] = tangents[vertex2]; // Top vertex = mainVerts + i + resAdd1; vertex2 = resAdd1 * Resolution + i; positions[vertex] = positions[vertex2] * scale; coords1[vertex] = coords1[vertex2]; coords2[vertex] = coords2[vertex2]; normals[vertex] = normals[vertex2]; tangents[vertex] = tangents[vertex2]; // Left vertex = mainVerts + i + resAdd1 + resAdd1; vertex2 = resAdd1 * i; positions[vertex] = positions[vertex2] * scale; coords1[vertex] = coords1[vertex2]; coords2[vertex] = coords2[vertex2]; normals[vertex] = normals[vertex2]; tangents[vertex] = tangents[vertex2]; // Right vertex = mainVerts + i + resAdd1 + resAdd1 + resAdd1; vertex2 = resAdd1 * i + Resolution; positions[vertex] = positions[vertex2] * scale; coords1[vertex] = coords1[vertex2]; coords2[vertex] = coords2[vertex2]; normals[vertex] = normals[vertex2]; tangents[vertex] = tangents[vertex2]; } // Indices if (indices == null || indices.Length != mainIndices + skirtIndices) { indices = new int[mainIndices + skirtIndices]; // Main for (var y = 0; y < Resolution; y++) { for (var x = 0; x < Resolution; x++) { index = (x + y * Resolution) * 6; vertex = x + y * resAdd1; indices[index + 0] = vertex; indices[index + 1] = vertex + 1; indices[index + 2] = vertex + resAdd1; indices[index + 3] = vertex + resAdd1 + 1; indices[index + 4] = vertex + resAdd1; indices[index + 5] = vertex + 1; } } // Skirt for (var i = 0; i < Resolution; i++) { // Bottom index = mainIndices + (Resolution * 0 + i) * 6; vertex = mainVerts + i; vertex2 = i; indices[index + 0] = vertex; indices[index + 1] = vertex + 1; indices[index + 2] = vertex2; indices[index + 3] = vertex2 + 1; indices[index + 4] = vertex2; indices[index + 5] = vertex + 1; // Top index = mainIndices + (Resolution * 1 + i) * 6; vertex = mainVerts + i + resAdd1; vertex2 = Resolution * resAdd1 + i; indices[index + 0] = vertex2; indices[index + 1] = vertex2 + 1; indices[index + 2] = vertex; indices[index + 3] = vertex + 1; indices[index + 4] = vertex; indices[index + 5] = vertex2 + 1; // Left index = mainIndices + (Resolution * 2 + i) * 6; vertex = mainVerts + i + resAdd1 + resAdd1; vertex2 = i * resAdd1; indices[index + 0] = vertex; indices[index + 1] = vertex2; indices[index + 2] = vertex + 1; indices[index + 3] = vertex2 + resAdd1; indices[index + 4] = vertex + 1; indices[index + 5] = vertex2; // Right index = mainIndices + (Resolution * 3 + i) * 6; vertex = mainVerts + i + resAdd1 + resAdd1 + resAdd1; vertex2 = i * resAdd1 + Resolution; indices[index + 0] = vertex2; indices[index + 1] = vertex; indices[index + 2] = vertex2 + resAdd1; indices[index + 3] = vertex + 1; indices[index + 4] = vertex2 + resAdd1; indices[index + 5] = vertex; } } if (mesh != null) { mesh.Clear(); } else { mesh = patch.Mesh = SgtObjectPool <Mesh> .Pop() ?? new Mesh(); mesh.name = "Patch"; mesh.hideFlags = HideFlags.DontSave; } mesh.vertices = positions; mesh.uv = coords1; mesh.uv2 = coords2; mesh.normals = normals; mesh.tangents = tangents; mesh.triangles = indices; mesh.RecalculateBounds(); patch.MeshCenter = mesh.bounds.center; } }
private static void CreateAdvancedBeltMenuItem() { var belt = CreateAdvancedBelt(0, SgtHelper.GetSelectedParent()); SgtHelper.SelectAndPing(belt); }
public void MakeEditableCopyContext() { var customBelt = MakeEditableCopy(gameObject.layer, transform.parent, transform.localPosition, transform.localRotation, transform.localScale); SgtHelper.SelectAndPing(customBelt); }
protected override int BeginQuads() { SgtHelper.BeginRandomSeed(Seed); return(AsteroidCount); }
public static void CreateRingMenuItem() { var ring = CreateRing(null); SgtHelper.SelectAndPing(ring); }
public void MakeEditableCopyContext() { var customStarfield = MakeEditableCopy(transform.parent, transform.localPosition, transform.localRotation, transform.localScale); SgtHelper.SelectAndPing(customStarfield); }
public static void CreateProminenceMenuItem() { var prominence = CreateProminence(null); SgtHelper.SelectAndPing(prominence); }
protected bool Button(string text) { var rect = SgtHelper.Reserve(); return(GUI.Button(rect, text)); }