Jump to content

Spawning a mesh (part?)


cicatrix

Recommended Posts

Greetings, everyone.

Since I am new to modmaking, I want to sort out 2 questions about whether or not is it possible.

Basically, I want something 'to be found' on the surface of some (random) planet. That 'something' is a Unity mesh that has to be spawned at some given coordinates. Is it possible? Ideally, this has to be something sort of detectable anomaly on the surface.

The second question is that I want to generate a texture for that mesh 'on the fly', i.e. procedurally. Will that be possible?

Link to comment
Share on other sites

The mod Kerbal kostructs is a good backbone for objects on planets and as far as I know the best way to implement them. As for procedural textures I'm not sure if the game is set up to allow that but others who know more than me would be more helpful

Link to comment
Share on other sites

Procedural textures are possible, of course. Just depends how much time and work you want to put into coding it. You'd have to dive into the Unity API dealing with the Texture2D stuff - I'm afraid I can't help you there.

Spawning mesh is not hard. xEvilReeperx posted this little partmodule as a proof of concept a while back, and I'm sure won't mind if I share it again:


class GoldenShower : MonoBehaviour
{
private Rect _windowRect = new Rect(400, 400, 128f, 1f);
private GameObject _coinPrefab;
private const int ShowerCoinCount = 800;


void Start()
{
_coinPrefab = GameObject.CreatePrimitive(PrimitiveType.Sphere); // note: comes with sphere collider
_coinPrefab.transform.localScale = new Vector3(0.04f, 0.01f, 0.04f);


// KSP/Diffuse apparently needs a texture, can't just set a color like regular Diffuse shader
var goldTexture = new Texture2D(1, 1);
goldTexture.SetPixels32(new Color32[] { XKCDColors.GoldenYellow });
goldTexture.Apply();


_coinPrefab.renderer.material = new Material(Shader.Find("KSP/Diffuse")) { mainTexture = goldTexture };


var rb = _coinPrefab.AddComponent<Rigidbody>();
rb.mass = 0.01f;
rb.angularDrag = 5f;


_coinPrefab.collider.material = new PhysicMaterial
{
frictionCombine = PhysicMaterialCombine.Maximum,
bounceCombine = PhysicMaterialCombine.Minimum,
bounciness = 0.45f,
dynamicFriction = 0.05f,
staticFriction = 0.25f
};
_coinPrefab.SetActive(false);
}


void OnGUI()
{
_windowRect = KSPUtil.ClampRectToScreen(GUILayout.Window(123, _windowRect, DrawWindow, "Menu"));
}


private void DrawWindow(int winid)
{
GUILayout.BeginVertical();
//GUILayout.Label(string.Format("Gravity: {0}"));
GUILayout.Label(string.Format("Accel: {0}", Physics.gravity.magnitude));
if (GUILayout.Button("Increase monetary wealth?"))
StartCoroutine(CoinShower());
GUILayout.EndVertical();
GUI.DragWindow();
}


private float Random360()
{
return UnityEngine.Random.Range(0f, 360f);
}


private System.Collections.IEnumerator CoinShower()
{
print("Let there be wealth!");
var vessel = FlightGlobals.ActiveVessel;
float start = Time.realtimeSinceStartup;


for (int i = 0; i < ShowerCoinCount; ++i)
{
var spawn = vessel.GetWorldPos3D() + FlightGlobals.upAxis * 20f;
var coin = (GameObject)Instantiate(_coinPrefab, spawn + UnityEngine.Random.insideUnitSphere * 2f,
Quaternion.Euler(new Vector3(Random360(),
Random360(),
Random360())));


coin.rigidbody.velocity = vessel.rigidbody.velocity; // else if in orbit, coins will miss


// impart a bit of force to get it spinning
coin.rigidbody.AddTorque(new Vector3(Random360() * 0.1f, Random360() * 0.1f, Random360() * 0.1f), ForceMode.Impulse);
coin.SetActive(true);


// we might need to spawn more than [fps] coins per second if we're to reach ShowerCoinCount in
// two seconds
// so delay here if we're ahead of schedule, otherwise continue dumping coins
while ((Time.realtimeSinceStartup - start) / 2f <= (float)i / ShowerCoinCount)
yield return 0;
}
print("Wealth complete");
}
}

To spawn mesh from a .mu file, you can use:


GameDatabase.Instance.GetModel(KerbalFoundries/Blade/Rock); //ommit the .mu extenstion from the end of the file name

You'll have to add colliders, rigidbody etc when you spawn the GO too, and it will NOT be persistent as it is.

If you want to see how to spawn parts (which will be persistent without extra work), I'd suggest looking up the source to Extra-Planetary Launchpads.

HTH.

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...