Created
August 26, 2018 09:18
-
-
Save CyraGames/30351d0f973a545dd84e21ccfd637ede to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Globalization; | |
/* | |
THIS SCRIPT IS WRITTEN BY | |
THOMAS KOLE | |
www.thomaskole.com | |
THIS SCRIPT IS RELEASED UNDER A | |
Creative Commons Attribution-NonCommercial | |
LISENCE, MEANING THAT YOU CAN USE IT AS LONG | |
AS YOU ATTRIBUTE ME. | |
FOR COMMERCIAL USE, PLEASE CONTACT ME. | |
---- | |
THIS SCRIPT READS FROM THE HYG DATABASE | |
http://astronexus.com/node/34 | |
A PROCESSED VERSION CAN BE FOUND IN THE | |
.unitypackage, | |
OR ON MY WEBSITE | |
https://thomaskole.wordpress.com/ | |
FOR IT'S USAGE, PLEASE SEE THE ASTRONEXUS WEBSITE. | |
---- | |
PLEASE FEEL FREE TO MODIFY ANY PART OF THIS SCRIPT | |
AS MUCH AS YOU WANT! | |
Modified by HJ. | |
*/ | |
namespace sp1 | |
{ | |
public class AnalyzeStars : MonoBehaviour | |
{ | |
// Public | |
public int maxParticles = 100; | |
public TextAsset starCSV; | |
public Material matVertex; | |
// Private | |
private Vector3[] points; | |
private Color[] colors; | |
private GameObject pointGroup; | |
Camera thisCamera; | |
private bool IsStarInitialized; | |
void Awake() | |
{ | |
} | |
void Start() | |
{ | |
IsStarInitialized = false; | |
float farClipPlane = 30000.0f; // hard-coded for now, problem with Camera.main not defined | |
Tools.Log("reading star files..."); | |
string[] lines = starCSV.text.Split('\n'); | |
int lineCount = lines.Length-1; | |
if (lineCount < maxParticles) | |
maxParticles = lineCount - 1; | |
points = new Vector3[maxParticles]; | |
colors = new Color[maxParticles]; | |
for (int i = 0; i < maxParticles; i++) | |
{ | |
string[] components = lines[i].Split(','); | |
points[i] = new Vector3(float.Parse(components[1], CultureInfo.InvariantCulture), | |
float.Parse(components[3], CultureInfo.InvariantCulture), | |
float.Parse(components[2], CultureInfo.InvariantCulture)).normalized * farClipPlane * 0.5f; | |
float magnitude = float.Parse(components[0], CultureInfo.InvariantCulture); | |
float intensity = (1.0f - ((magnitude + 1.44f) / 16.0f)); | |
colors[i] = Color.white * intensity; | |
} | |
InstantiateMesh(maxParticles); | |
} | |
public void OnEnable() | |
{ | |
// register the callback when enabling object | |
Camera.onPreRender += MyPreRender; | |
MSCameraController.OnCameraChange += OnCameraChange; | |
} | |
public void MyPreRender(Camera cam) | |
{ | |
Debug.Log("PreRender " + gameObject.name + " from camera " + cam.gameObject.name); | |
thisCamera = cam; | |
Camera.onPreRender -= MyPreRender; | |
} | |
public void OnCameraChange(CameraType cam) | |
{ | |
//Debug.Log("OnCameraChange " + gameObject.name + " from camera " + cam._camera.name); | |
thisCamera = cam._camera; | |
} | |
void Update() | |
{ | |
if (IsStarInitialized) | |
{ | |
// | |
// make the stars blink a bit | |
// | |
pointGroup.GetComponent<Renderer>().material.SetFloat("_AnimationTime", Time.frameCount / 10.0f); | |
// | |
// Update the positions of the star cloud ; it remains centered on the camera view | |
// | |
if (pointGroup != null && thisCamera != null) | |
pointGroup.transform.SetPositionAndRotation(thisCamera.transform.position, Quaternion.identity); | |
} | |
} | |
private void InstantiateMesh(int nPoints) | |
{ | |
// Create Mesh | |
pointGroup = new GameObject("StarField"); | |
pointGroup.AddComponent<MeshFilter>(); | |
pointGroup.AddComponent<MeshRenderer>(); | |
pointGroup.GetComponent<Renderer>().material = matVertex; | |
pointGroup.GetComponent<MeshFilter>().mesh = CreateMesh(nPoints); | |
pointGroup.SetActive(true); | |
IsStarInitialized = true; | |
} | |
private Mesh CreateMesh(int nPoints) | |
{ | |
Mesh mesh = new Mesh(); | |
int[] indecies = new int[nPoints]; | |
for (int i = 0; i < nPoints; ++i) | |
{ | |
indecies[i] = i; | |
} | |
mesh.vertices = points; | |
mesh.colors = colors; | |
mesh.SetIndices(indecies, MeshTopology.Points, 0); | |
mesh.uv = new Vector2[nPoints]; | |
mesh.normals = new Vector3[nPoints]; | |
return mesh; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment