Advertisement
Guest User

InfiniteStarfield

a guest
Jan 28th, 2016
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.38 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class InfinteStarfield : MonoBehaviour {
  5.     ParticleSystem.Particle[] points;
  6.    
  7.     public int starsMax = 100;
  8.     public float starSize = 1;
  9.     public float starDistance = 10;
  10.     public float starClipDistance = 1;
  11.     float starDistanceSqr;
  12.     float starClipDistanceSqr;
  13.    
  14.     void Start () {
  15.         starDistanceSqr = starDistance * starDistance;
  16.         starClipDistanceSqr = starClipDistance * starClipDistance;
  17.     }
  18.  
  19.     private void CreateStars() {
  20.         points = new ParticleSystem.Particle[starsMax];
  21.         for (int i = 0; i < starsMax; i++) {
  22.             points[i].position = Random.insideUnitSphere * starDistance + transform.position;
  23.             points[i].color = new Color(1,1,1, 1);
  24.             points[i].size = starSize;
  25.         }
  26.     }
  27.    
  28.     void Update () {
  29.         if ( points == null ) CreateStars();
  30.         for (int i = 0; i < starsMax; i++) {
  31.             if ((points[i].position - transform.position).sqrMagnitude > starDistanceSqr) {
  32.                 points[i].position = Random.insideUnitSphere.normalized * starDistance + transform.position;
  33.             }
  34.            
  35.             if ((points[i].position - transform.position).sqrMagnitude <= starClipDistanceSqr) {
  36.                 float percent = (points[i].position - transform.position).sqrMagnitude / starClipDistanceSqr;
  37.                 points[i].color = new Color(1,1,1, percent);
  38.                 points[i].size = percent * starSize;
  39.             }
  40.         }
  41.         GetComponent<ParticleSystem>().SetParticles ( points, points.Length );
  42.     }
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement