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 Brady · May 09, 2011 at 10:06 PM · lightmapassetpostprocessorsetpixels

How to write pixels to a lightmap

I'm trying to do some post-processing of Beast-generated lightmaps, but I'm running into the problem that after writing out pixels (using SetPixels()) to the lightmap and Apply()'ing the changes, the lightmap remains unchanged. I've tried it both as a pure editor script, and also as an asset importer (where the post-processing is performed in a custom postprocessor).

Out of desperation just to see if my pixels were getting written in any form, I tried writing the lightmap back out as a PNG (using EncodeToPNG()), but I still don't get any modified result.

Any ideas?

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Kryptos · Aug 17, 2011 at 07:41 AM

Did you ensure that your texture has property 'Read/Write Enabled' checked? (To see this property, you have to choose 'Advanced' as texture type in the TextureImporter component of your texture asset)

IMHO SetPixels() is just ignored if this property is not enabled.

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 Brady · Aug 18, 2011 at 12:19 AM 0
Share

Yes, that is set correctly.

avatar image
0

Answer by Kryptos · Aug 18, 2011 at 07:26 AM

I tried yesterday to write to the lightmap and I didn't get any issue. Can you give a link to a simple project with this issue or post your code?

Here is the code sample I used:

 void LateUpdate()
 {
     // Let's assume we get these coordinates by raycasting
     // Coordinates retrieved that way are normalized, i.e. between 0 and 1
     Vector2 lightmapCoord = GetCoordRaycast();
     // I have a single lightmap in my scene, therefore it is the first occurence in the lightmap array.
     Texture2D lightmap = LightmapSettings.lightmaps[0].lightmapFar;
     // Get the current pixel (filtered)
     Color lightmapColor =
         lightmap.GetPixelBilinear( lightmapCoord.x, lightmapCoord.y );
     Debug.Log(lightmapColor);

     // Set a blood pixel ;)
     lightmap.SetPixel( lightmapCoord.x*lightmap.width, lightmapCoord.y*lightmap.height, Color.red );
     lightmap.Apply();
 }

As a result, some kind of blooded tracks were following my character.

Comment
Add comment · Show 2 · 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 Brady · Aug 18, 2011 at 11:16 PM 0
Share

I guess I should clarify, I'm trying to actually write to the lightmap on disk in edit mode as opposed to at runtime. Nothing I do seems to "stick", in other words, the lightmaps remain unaffected.

Strangely, however, I tried your code snippet at runtime (using UV coords that begin at the lower-left and step toward the upper-right over time), and I couldn't get it to work either. The lightmap texture is marked as writable and its format is RGBA32. There has to be another element I'm missing... Do you happen to have a simple package you could post that demonstrates this? Perhaps I could look at that and figure out what the difference is between a working example and my test case. It could be that whatever is preventing it from working in edit mode is also preventing it at runtime, since your example should definitely work.

avatar image Brady · Aug 19, 2011 at 12:24 AM 0
Share

Never $$anonymous$$d, I just realized the reason it wasn't working for me was a float-to-int conversion problem (since SetPixel() requires ints). Your example is working for me now. Still not sure on the edit-time issue... thanks for your help!

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

2 People are following this question.

avatar image avatar image

Related Questions

AssetPostprocessor using configuration stored in asset 2 Answers

Lighting on a room-by room basis 1 Answer

Toggling specific lightmaps for individual lights? 0 Answers

Disabling dynamic shadows in deferred rendering (Unity 5) 1 Answer

Unity - Stuck in Build 7/11 Light Transport - 1 Job 13 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