Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by zero3growlithe · Sep 23, 2012 at 11:47 AM · colorlightmappixelbeastget

How to get pixel color using raycast on object with Lightmap created by "Beast"?

I want to change my ship's color depending from lightmap color (got using raycast under this ship) of object under this ship with mesh collider attached to it. Lightmap is created using "Beast" lightmapper. Simply I want to make lightmap shadows to affect my ship as if they were real time shadows. How do I do that?

Right now I'm trying to achieve that using code below but I can't access object's lightmap to get point color pointed with raycast: (Note this is part of code so there might be something missing)

 // Check Ground Color
                 // Just in case, also make sure the collider also has a renderer material and texture. Also we should ignore primitive colliders.
                 var rend : Renderer = hit.collider.renderer;
                 var meshCollider = hit.collider as MeshCollider;
                 if (rend == null || rend.sharedMaterial == null || rend.sharedMaterial.mainTexture == null || meshCollider == null || rend.LightmapSettings.lightmaps[rend.lightmapIndex] == null) {}
                 else{
                     // Get lightmap color, and apply to object 
                     var tex : Texture2D = rend.LightmapSettings.lightmaps[rend.lightmapIndex];
                     if (tex.format == TextureFormat.RGB24 || tex.format == TextureFormat.ARGB32) {
                         var pixelUV = hit.textureCoord2;
                         surfaceColor = tex.GetPixel(pixelUV.x * tex.width, pixelUV.y * tex.height);
                             if (SceneLight){SceneLight.light.color = surfaceColor;}
                     }
                 }

Any ideas?

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bunny83 · Sep 26, 2012 at 07:29 PM 1
Share

Hmm, never tried that. What exactly doesn't work? Do you get an error? If so which one, if not what parts get executed (some Debug.Logs)?

Usually the texture doesn't need to have RGB24 or ARGB32 format to use GetPixel / GetPixels / GetPixelBilinear on it. You can't use SetPixel on a comressed texture, but reading should be no problem as long as the texture is readable.

I would remove the format check and see if it works. Also GetPixelBilinear is probably more suited for this case. It takes real uv coordinates (0-1) and does a bilinear look up which gives you a much smoother result.

1 Reply

· Add your reply
  • Sort: 
avatar image
3
Best Answer

Answer by Bunny83 · Sep 27, 2012 at 03:27 AM

Ok, i finally had some time to create a simple test scene and baked some lightmaps. Here's the result.

It's quite large due to the lightmaps ;) I change the builders color to the sampled lightmap color. The problem is that the lightmaps uses both, the rgb color and the alpha channel. At some spots it looks a bit strange, but basically it works ;)

This is done in Unity free, so no pro features are used.

Here's the important part of the script:

         var rend = hit.collider.renderer;
         if (rend == null || rend.lightmapIndex > 253)
             return;

         var lightMapInfo = LightmapSettings.lightmaps[rend.lightmapIndex];
         var tex = lightMapInfo.lightmapNear;
         var pixelUV = hit.lightmapCoord;
         var surfaceColor = tex.GetPixelBilinear(pixelUV.x, pixelUV.y);

Here are the major parts that you messed up or forgot:

  • First you have to make the lightmap textures readable. Just select the textures in the project view (you can select all at once) and change the texture type to "advanced". Now you can tick the "isReadable" flag.

  • The lightmapped object has to use a MeshCollider. You only get uv information from the raycast when you use a MeshCollider which of course has to use the same Mesh as collider.

  • The lightmapIndex has to be checked against it's special meaning. If no lightmap is available this number is either 255 or 254. In this case you can't use it as index into the lightmaps array.

  • The LightmapSettings is a static class. You tried to use it like a member of Renderer. It holds all lightmaps for all renderers at this central point.

  • LightmapSettings.lightmaps is not an array of Texture2D. It holds LightmapData classes. Those have two textures, a near and a far texture.

  • textureCoord2 really holds the lightmap uvs, but those coordinates are scaled and offsetted by the renderers lightmapTilingOffset since each lightmap is actually atexture atlas. Unity has a special property which already does this calculation for you: lightmapCoord.

  • If you want to sample the color of an arbitary point on a texture you should use GetPixelBilinear instead of GetPixel. It takes uv coordinates instead of pixel coordinates. This even simplifies the usage. Furthermore it does a bilinear filtering which smoothes the returned color a bit.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image zero3growlithe · Sep 28, 2012 at 06:45 AM 0
Share

Wow! Thanks for detailed answer :D Though I've used GetPixel ins$$anonymous$$d of Bilinear one cos it works better for me :P

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

10 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Vertex colour transparency and Beast lightmapping? 3 Answers

Remove Temp/Beast folder 0 Answers

Transparent glsl shader is still opaque in the lightmap. 0 Answers

Get Average Colour From Texture? 1 Answer

How many terrain lightmaps are generated with beast? 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges