Jump to content

Adding Fog/Smoke/Clouds


Recommended Posts

Hello, 

   I'm rather new to modding KSP and Unity in general. I am attempting to add fog and clouds to KSP and I have really no clue where to begin. I've searched Google high and low as well as the forums and all I could find is incomplete code snippets so small that they really don't help much. If someone has the time and knowledge, would you lend me a hand. Below is my code so far. I think I am on the right track with making a gameobject, assigning it's .maintexture to be my texture I choose, adding a mesh, creating vertices, triangles, etc. but I really have no idea if this is correct and if it is, where do I go now. 

 

using System;
using System.IO;
using System.Collections;

using UnityEngine;
using KSP;

namespace Clouds
{
    public class Clouds_Main : MonoBehaviour
    {
        bool debug_output = false;
        Texture2D white_clouds = null, storm_clouds = null;
        private float lastFixedUpdate = 0.0f, logInterval = 2.5f;

        Mesh light_cloud_mesh = new Mesh();
        Mesh dark_cloud_mesh = new Mesh();

        public Material light_cloud_material;
        public Material dark_cloud_material;

        GameObject dark_cloud_gameObject;
        GameObject light_cloud_gameObject;



        /* Caution: as it says here: http://docs.unity3d.com/Documentation/ScriptReference/MonoBehaviour.Awake.html,
         use the Awake() method instead of the constructor for initializing data because Unity uses
        Serialization a lot. */   

        /* Called after the scene is loaded. */
        void Awake()
        {
            if (white_clouds == null)
                white_clouds = LoadPNG(@"C:\Program Files (x86)\Steam\SteamApps\common\Kerbal Space Program\GameData\Wind Mod\S_Cloud4.png"); // Load the S_Cloud4.png Icon image into memory
            if (storm_clouds == null)
                storm_clouds = LoadPNG(@"C:\Program Files (x86)\Steam\SteamApps\common\Kerbal Space Program\GameData\Wind Mod\D_Cloud1.png"); // Load the D_Cloud1.png Icon image into memory

            light_cloud_material.SetTexture(0, white_clouds);
            dark_cloud_material.SetTexture(1, storm_clouds);

        }

        /* Called next. */
        void Start()
        {
            dark_cloud_gameObject.AddComponent<MeshRenderer>().material.mainTexture = dark_cloud_material.GetTexture(1);
            dark_cloud_mesh = dark_cloud_gameObject.AddComponent<MeshFilter>().mesh;
            dark_cloud_mesh.Clear();
            dark_cloud_mesh.vertices = new Vector3[] { new Vector3(-0.5f, 0, -0.5f), new Vector3(0.5f, 0, -0.5f), new Vector3(0.5f, 0, 0.5f), new Vector3(-0.5f, 0, 0.5f) };
            dark_cloud_mesh.uv = new Vector2[] { new Vector2(0, 0), new Vector2(0, 1), new Vector2(1, 1), new Vector2(1, 0) };
            dark_cloud_mesh.triangles = new int[] { 0, 1, 2, 2, 3, 0 };


            light_cloud_gameObject.AddComponent<MeshRenderer>().material.mainTexture = light_cloud_material.GetTexture(0);
            light_cloud_mesh = light_cloud_gameObject.AddComponent<MeshFilter>().mesh;
            light_cloud_mesh.Clear();
            light_cloud_mesh.vertices = new Vector3[] { new Vector3(-0.5f, 0, -0.5f), new Vector3(0.5f, 0, -0.5f), new Vector3(0.5f, 0, 0.5f), new Vector3(-0.5f, 0, 0.5f) };
            light_cloud_mesh.uv = new Vector2[] { new Vector2(0, 0), new Vector2(0, 1), new Vector2(1, 1), new Vector2(1, 0) };
            light_cloud_mesh.triangles = new int[] { 0, 1, 2, 2, 3, 0 };



        }

        /*  Called every frame */
        void Update()
        { 
        
        
        }

        /* Called at a fixed time interval determined by the physics time step. */
        void FixedUpdate()
        {
            if ((Time.time - lastFixedUpdate) > logInterval)
            {

            }
        }

        /* Called when the game is leaving the scene (or exiting). Perform any clean up work here. */
        void OnDestroy()
        { }


        Texture2D LoadPNG(string filePath) // Converts a .png image into a BYTE array that Unity needs in order to display as a GUI image
        {
            Texture2D temp_texture = null; // Ensure the texture is always reset
            byte[] fileData;

            if (File.Exists(filePath))
            {
                fileData = File.ReadAllBytes(filePath);
                temp_texture = new Texture2D(2, 2);
                temp_texture.LoadImage(fileData); //..this will auto-resize the texture dimensions.
            }
            else
            {
                if (debug_output)
                    Debug.Log("[WIND MOD : Gui Draw] Icon file does not exist in location: " + filePath);
            }
            return temp_texture;
        }
    }
}

I would greatly appreciate any help you can give. Thanks in advance. 

Link to comment
Share on other sites

This thread is quite old. Please consider starting a new thread rather than reviving this one.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...