Advertisement
Guest User

Untitled

a guest
Jun 19th, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.76 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. [ExecuteInEditMode]
  6. public class Poly_Clouds : MonoBehaviour {
  7.  
  8.     public bool Generate;
  9.     [Range(1,500)]
  10.     public int CloudDensity;
  11.     [Range(1,30)]
  12.     public int CloudPartCount;
  13.     [Range(1,10000)]
  14.     public float CloudSize;
  15.     [Range(0,1)]
  16.     public float CloudSizeVariation;
  17.     [Range(1,10000)]
  18.     public float CloudHeight;
  19.  
  20.    
  21.     // Here u must Set a PrefabModel ( i use a basic Icosphere out of Blender )
  22.    
  23.     public GameObject CloudPrefab;
  24.     private MeshFilter MF;
  25.     private MeshRenderer MR;
  26.  
  27.     void Start () {
  28.         enabled = false;
  29.     }
  30.  
  31.     void Update () {
  32.         if(Generate ){
  33.             Generate = false;
  34.             CreatePlanetClouds();
  35.         }      
  36.     }
  37.  
  38.     public void CreatePlanetClouds() {
  39.         if( !MR ) {
  40.             MR = GetComponent<MeshRenderer>();
  41.             if( !MR ) {
  42.                 MR = gameObject.AddComponent<MeshRenderer>();
  43.             }
  44.         }
  45.         GameObject SenorCloud = CreateSingleCloud( transform.position, 1f );
  46.         for( int i = 0; i < CloudDensity; i++ ) {
  47.             SenorCloud.transform.rotation = Quaternion.Euler( Random.Range( 0, 360 ), Random.Range( 0, 360 ), Random.Range( 0, 360 ) );
  48.             Vector3 Pos = transform.position + ( SenorCloud.transform.up * CloudHeight );
  49.             GameObject Cloud = CreateCloud( Pos, CloudSize, CloudPartCount );
  50.             Cloud.transform.rotation = SenorCloud.transform.rotation;
  51.             Cloud.transform.parent = transform;
  52.         }
  53.         DestroyImmediate( SenorCloud );
  54.         if( MF ) {
  55.             if( MF.sharedMesh ) {
  56.                 MF.sharedMesh.Clear();
  57.             }
  58.             DestroyImmediate( MF );
  59.         }
  60.         MeshFilter[] Meshes = GetComponentsInChildren<MeshFilter>();
  61.         CombineInstance[] combine = new CombineInstance[Meshes.Length];
  62.         for( int i = 0; i < Meshes.Length; i++ ) {
  63.             combine[i].mesh = Meshes[i].sharedMesh;
  64.             combine[i].transform = Meshes[i].transform.localToWorldMatrix;
  65.         }
  66.         if( !MF ) {
  67.             MF = GetComponent<MeshFilter>();
  68.             if( !MF ) {
  69.                 MF = gameObject.AddComponent<MeshFilter>();
  70.             }
  71.         }
  72.  
  73.         MF.sharedMesh = new Mesh();
  74.         MF.sharedMesh.name = "Poly_Cloud";
  75.         MF.sharedMesh.CombineMeshes( combine );
  76.         clean();
  77.     }
  78.  
  79.     void clean() {
  80.         foreach( MeshFilter MFilter in GetComponentsInChildren<MeshFilter>() ) {
  81.             if( MFilter != GetComponent<MeshFilter>() && MFilter ) {
  82.                 DestroyImmediate( MFilter.gameObject );
  83.             }
  84.         }
  85.     }
  86.  
  87.     GameObject CreateCloud( Vector3 Pos, float Size, int Count ) {
  88.         float variSize = Size * Random.Range( 1f - CloudSizeVariation, 1f + CloudSizeVariation );
  89.         GameObject Cloud = CreateSingleCloud( Pos, variSize );
  90.         GameObject LastCloud = Cloud;        
  91.         for( int i = 0; i < Random.Range( 1, Count ); i++ ) {
  92.             LastCloud.transform.localRotation = Quaternion.Euler( 0, Random.Range( 0, 360 ), 0 );
  93.  
  94.             float variSize2 = Size * Random.Range( 1f - CloudSizeVariation, 1f + CloudSizeVariation );
  95.             GameObject SingleCloud = CreateSingleCloud( LastCloud.transform.position + ( LastCloud.transform.forward * ( variSize + 1f) / 7f ), variSize2 );
  96.             SingleCloud.transform.parent = LastCloud.transform;
  97.             LastCloud = SingleCloud;
  98.         }
  99.         return Cloud;
  100.     }
  101.  
  102.     GameObject CreateSingleCloud( Vector3 Pos, float Size ) {
  103.         GameObject SingleCloud = (GameObject)Instantiate( CloudPrefab , Pos, Quaternion.identity );
  104.         SingleCloud.transform.localScale = Vector3.one * (Size) ;
  105.         return SingleCloud;
  106.     }
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement