Jump to content

Getting Planet Texture


Recommended Posts

Hi I'm working on my first project and I've hit a bit of a block. I'm trying to build something like the chem cam on the Mars rover (it fires a high power lazer at rock and analyses the rock based on the spectrum). I've built my part model and it imports into ksp properly and I'm now working on the code.

So far I've got my animations working and I can get it to draw a lazer when it's within range (20m) of a planet.

I'm using a raycast to detect if I'm in range of the planet and then looking at the name of the transform of the associated collider mesh to find the name of the planet, then checking from a list of planets to see what the ray is colliding with.

The problem I'm having is that I wanted to use colour of the texture at the pixel where the ray collides to generate the composition of the rock (so that for example if it was on duna and the colour was orange it would be silicon/iron based or it was white then it would ice or something), using something like this. I can get a reference to the texture using hitInfo.collider.renderer.sharedMaterial.mainTexture but when I then try and access the pixel data from it I get an error:

UnityException: Texture 'KerbinScaledSpace300' is not readable, the texture memory can not be accessed from scripts. You can make the texture readable in the Texture Import Settings.

So it seems that my method isn't going to work. Does anyone have any idea of how else I might get the colour of the planet texture where my ship is? I'm thinking if I can't get it work I could use the latitude and logitude and make a low-res map of the minerals of a planet, but this will bloat the mod and probably won't give great results.

This is the code I've got so far, the error comes at the line: Color col=tex.GetPixel(x,y);


private void fireChemCam()
{
if (camState == ChemCamState.Open)
{
RaycastHit hitInfo;
if (Physics.Raycast(lazerRenderer.transform.position, lazerRenderer.transform.forward, out hitInfo))
{
string target = hitInfo.collider.gameObject.name;
int planetId = PlanetNames.IndexOf(target);
print("Collided With: " + target);
Texture2D tex = hitInfo.collider.renderer.sharedMaterial.mainTexture as Texture2D;
print("Texture Dims: "+tex.width.ToString()+", "+tex.height.ToString());
int x=tex.width*hitInfo.textureCoord.x;
int y=tex.height*hitInfo.textureCoord.y;
Color col=tex.GetPixel(x,y);
print("R:"+col.r.ToString()+",G:"+col.g.ToString()+",B:"+col.b.ToString());
if (planetId>-1&&hitInfo.distance < 20 && (GetAvailableResource("ElectricCharge") >= 200))
{
camState = ChemCamState.Firing;
RequestResource("ElectricCharge", 200);
lazerRenderer.enabled = true;
_firing = 6;
}
}
else _print("no Hit");
}
}

Any ideas would be greatly appreciated,

Thanks in advance,

tobyb121

Link to comment
Share on other sites

  • 1 month later...

The problem I'm having is that I wanted to use colour of the texture at the pixel where the ray collides to generate the composition of the rock (so that for example if it was on duna and the colour was orange it would be silicon/iron based or it was white then it would ice or something)

Your biggest problem is that ice is not an element.

The rest of it seems pretty easy to solve if you approach it from the right mindset- you just need to incorporate an "if" statement after Color col=tex.GetPixel(x,y);

Hope you figure it out soon!

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