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 Gary Jackson · Mar 10, 2011 at 03:13 PM · raycastterrainaicharactercontrollercollisiondetection

Extracting RGB component data from a textured plane positioned below a 3D terrain

I have a simple model that I am trying to test as a proof of concept.

1) Terrain located at 0,0,0 2) Textured plane at 0,-10,0 (below the terrain and unseen) currently greyscale; but would like to read RGB eventually. Sized to match extents of terrain. Lets call it a 'data-field' (literally).

The character controller neeeds to extract data from the data-field (raycast?) and 'print it'.

Extracted data might contain useful information for AI use (e.g. good place for cover; look up; wait here if under fire) so we would be returning multiple 0-255 rankings from multiple data-fields for subsequent processing by the AI. Obviously RGB data-planes have 3 x the bang for the same buck, if you separate the components!

This has got to be better than waypoints as a basic wayfinding strategy; websearch 'isovist fields' if you want more info on this theory applied to permeability/pathfinding.

In principle, this is an opportunity to generate complex response data from multiple painted planes (which could be layered off...???/unrendered???)

This is the beginning of potential spatial data discussion, but my brain is not there yet.

G

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

3 Replies

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

Answer by yoyo · Mar 10, 2011 at 04:03 PM

Here's one way ...

  • associate a Collider with your textured plane
  • use Collider.Raycast to cast vertical rays (direction = Vector3.down) down into the plane from the world position of the character
  • use the textureCoord of the raycast hit and feed it into Texture2D.GetPixel to read the RGB value from the texture

EDIT: The following script seems to do the trick.

To use it ...

  1. Save the script as DataField.cs in your project.
  2. Select Game Object > Create Other > Plane from the Unity menu
  3. Attach a material to your plane, and a texture to the material.
  4. Check the texture importer settings on the texture and make sure Read/Write enable is set.
  5. Attach the DataField component to your Plane object.
  6. Find an object in your scene, or create an empty one, that will move across the plane. Attach it to the ExampleCharacterObject property of the DataField component.
  7. Run your game, and watch the ColorUnderCharacter property of the DataField as the object moves across it. It should match the texture color under the character.

Note that another component in your scene can now have a property of type DataField, and then call the GetColorData method on it directly -- the ExampleCharacterObject lookup in the Update method is just for demonstration purposes.

using UnityEngine;

public class DataField : MonoBehaviour { // Attach something to this in the inspector and move it around // to demonstrate the data field lookup in action. public Transform ExampleCharacterObject = null;

 // Don't modify this, it will be updated as the character object moves around.
 public Color ColorUnderCharacter = Color.black;

 private Texture2D mTexture = null;

 // Check for required components on startup, and find the texture to be
 // used for the data field.
 void Start ()
 {
     if (collider == null)
     {
         Debug.LogError("collider required for DataField to perform texture lookups", gameObject);
     }
     if ((renderer == null) || (renderer.material == null) || (renderer.material.mainTexture == null))
     {
         Debug.LogError("renderer with a material and a main texture required for DataField to perform texture lookups", gameObject);
     }

     mTexture = renderer.material.mainTexture as Texture2D;
     if (mTexture == null)
     {
         Debug.LogError("Texture2D required for DataField to perform texture lookups", gameObject);
     }

     // Note that you must turn on Read/Write enable in the import settings for the
     // texture or else GetPixel will fail.
 }

 // Update demonstrates the use of GetColorData, but you can call
 // it from elsewhere too (this behaviour doesn't need an Update
 // method, this is just for illustration).
 void Update ()
 {
     if (ExampleCharacterObject != null)
     {
         ColorUnderCharacter = GetColorData(ExampleCharacterObject.position);
     }
 }

 // Find the color data under a given position.
 public Color GetColorData(Vector3 position)
 {
     // Default to black if we find no data.
     Color colorData = Color.black;

     // Create a down-pointing ray at the position.
     Ray ray = new Ray(position, Vector3.down);
     RaycastHit hit;

     // Check for a hit, using some arbitrarily long ray length.
     if (collider.Raycast(ray, out hit, 10000.0f))
     {
         colorData = mTexture.GetPixelBilinear(hit.textureCoord.x, hit.textureCoord.y);
     }

     return colorData;
 }

}

Comment
Add comment · Show 8 · 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 Gary Jackson · Mar 10, 2011 at 08:05 PM 0
Share

$$anonymous$$any thanks.

G

avatar image yoyo · Mar 10, 2011 at 08:09 PM 0
Share

No prob -- I've done something similar in my game, so if you run into problems let me know and can give more details.

avatar image Gary Jackson · Mar 10, 2011 at 11:57 PM 0
Share

O$$anonymous$$. Have built the terrain; mesh and just tried to write above. Writing this is a bit too complex at the moment - going to have to spend a few days working through more basic scripts first. Generally, I don't code!

I get what you are saying (conceptually) - just feeling a bit consciously incompetant vs .js

Architecture, Urban Design, Acupuncture, Daoist meditation, 3d $$anonymous$$odelling - I got licked. Damned if a bit of code is going to stop me...

avatar image Gary Jackson · Mar 11, 2011 at 01:05 PM 0
Share

Thank you. All works brillantly!! $$anonymous$$ultiple Textured Planes being read by the script and returning colour values. Interestingly you can turn off the mesh render and the data is still pulled.

This means one can push spatial data to variables based on a 'painted plane'. An AI can the 'weight' the returned values and feed it into decision making.

I think we have just changed the face of urban design analysis... but I need to think.

avatar image Gary Jackson · Mar 11, 2011 at 01:08 PM 0
Share

Would you be interested in pursuign this further - it very edgy RND on my part; but I think it might create a useful AI demo for the Community as whole. I would love to see this influencing a squad of soldiers moving through an obstacle field.

Show more comments
avatar image
0

Answer by rolandcahen · Feb 25, 2012 at 03:12 AM

Hello Gary and "yoyo".

Thanks for this good help. However I have a bug I cannot solve : Only one half of the texture is read, the rest of it stays at value (0,0,0,0). Have you any idea why this happens ? Thanks a lot.

See example http://www.rolandcahen.eu/UNITY/DataFieldReaderTest.unitypackage

RoCa

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

Answer by rolandcahen · Feb 25, 2012 at 03:11 AM

I found the solution. Only half of the texture was calcuated Not for negative x, z positions because the reader was at y = 0. for some reasons the raycast could not lookup. Once the texture reader is a 1m everything is fine

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

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

1 Person is following this question.

avatar image

Related Questions

Debug.DrawRay Issue? 0 Answers

Help Understanding Raycast 2 Answers

Character floating after walking up higher terrain/object 1 Answer

Help with AI 1 Answer

Linecast & Raycast won't hit the outer surface of a CharacterController 1 Answer


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