/// <summary> /// Queries the shader the material should use /// </summary> private Shader GetShader() { return(useNewShader ? ShaderLoader.GetShader(NEW_SHADER) : Shader.Find(unlit ? UNLIT_SHADER : DIFFUSE_SHADER)); }
/// <summary> /// Builds the Ring /// </summary> public void BuildRing() { // Prepare GameObject parent = transform.parent.gameObject; List <Vector3> vertices = new List <Vector3>(); List <Vector2> Uvs = new List <Vector2>(); List <int> Tris = new List <int>(); List <Vector3> Normals = new List <Vector3>(); // Mesh wrapping for (float i = 0f; i < 360f; i += (360f / steps)) { // Rotation Vector3 eVert = Quaternion.Euler(0, i, 0) * Vector3.right; // Inner Radius vertices.Add(eVert * (innerRadius * (1f / parent.transform.localScale.x))); Normals.Add(Vector3.left); Uvs.Add(Vector2.one); // Outer Radius vertices.Add(eVert * (outerRadius * (1f / parent.transform.localScale.x))); Normals.Add(Vector3.left); Uvs.Add(Vector2.zero); } for (float i = 0f; i < 360f; i += (360f / steps)) { // Rotation Vector3 eVert = Quaternion.Euler(0, i, 0) * Vector3.right; // Inner Radius vertices.Add(eVert * (innerRadius * (1f / parent.transform.localScale.x))); Normals.Add(Vector3.left); Uvs.Add(Vector2.one); // Outer Radius vertices.Add(eVert * (outerRadius * (1f / parent.transform.localScale.x))); Normals.Add(Vector3.left); Uvs.Add(Vector2.zero); } // Tri Wrapping int Wrapping = steps * 2; for (int i = 0; i < Wrapping; i += 2) { Tris.Add((i) % Wrapping); Tris.Add((i + 1) % Wrapping); Tris.Add((i + 2) % Wrapping); Tris.Add((i + 1) % Wrapping); Tris.Add((i + 3) % Wrapping); Tris.Add((i + 2) % Wrapping); } for (int i = 0; i < Wrapping; i += 2) { Tris.Add(Wrapping + (i + 2) % Wrapping); Tris.Add(Wrapping + (i + 1) % Wrapping); Tris.Add(Wrapping + (i) % Wrapping); Tris.Add(Wrapping + (i + 2) % Wrapping); Tris.Add(Wrapping + (i + 3) % Wrapping); Tris.Add(Wrapping + (i + 1) % Wrapping); } // Update Rotation transform.localRotation = rotation; // Update Scale and Layer transform.localScale = parent.transform.localScale; transform.position = parent.transform.localPosition; gameObject.layer = parent.layer; // Create MeshFilter MeshFilter meshFilter = gameObject.AddComponent <MeshFilter>(); // Set mesh meshFilter.mesh = new Mesh(); meshFilter.mesh.vertices = vertices.ToArray(); meshFilter.mesh.triangles = Tris.ToArray(); meshFilter.mesh.uv = Uvs.ToArray(); meshFilter.mesh.RecalculateNormals(); meshFilter.mesh.RecalculateBounds(); meshFilter.mesh.Optimize(); meshFilter.sharedMesh = meshFilter.mesh; // Set texture ringMR = gameObject.AddComponent <MeshRenderer>(); Renderer parentRenderer = parent.GetComponent <Renderer>(); if (useNewShader) { ringMR.material = new Material(ShaderLoader.GetShader("Kopernicus/Rings")); } else if (unlit) { ringMR.material = new Material(Shader.Find("Unlit/Transparent")); } else { ringMR.material = new Material(Shader.Find("Transparent/Diffuse")); } ringMR.material.SetTexture("_MainTex", texture); ringMR.material.SetFloat("innerRadius", innerRadius); ringMR.material.SetFloat("outerRadius", outerRadius); if (useNewShader) { ringMR.material.SetFloat("planetRadius", planetRadius); ringMR.material.SetFloat("penumbraMultiplier", penumbraMultiplier); } ringMR.material.color = color; ringMR.material.renderQueue = 3003; }