• 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 /
  • Help Room /
avatar image
0
Question by Fezpin · Jul 18, 2018 at 12:34 PM · colortransform.position

Convert A color into a float

I need to get a float value to change the position of a cube in my scene depending on the color's value,

I have a private Color[] camLight; which gets its value from a webcamtexture. and I have a Public GameObject testCube;

I need to do something like this:

void Update() { testCube.transform.position = new Vector3(0f, camLight, 0f); }

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

1 Reply

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

Answer by Hellium · Jul 18, 2018 at 01:41 PM

This is a custom conversion YOU have to define according to your needs. There are an infinite number of ways to do it.

 Color color = new Color( 1, 0, 0, 1 ) ;
 System.Func<Color, float> red = c => c.r ;
 System.Func<Color, float> green = c => c.g ;
 System.Func<Color, float> blue = c => c.b ;
 System.Func<Color, float> alpha = c => c.a ;
 System.Func<Color, float> sum = c => c.r + c.g + c.b + c.a ;
 System.Func<Color, float> meanRGB = c => ( c.r + c.g + c.b ) / 3f ;
 System.Func<Color, float> luminance = c =>0.2126f * c.r + 0.7152f * c.g + 0.0722f * c.b ;

 void Update()
 {
     float y = luminance( color ) ; // or red( color ) or meanRGB( color ), or ....
     testCube.transform.position = new Vector3(0f, y, 0f);
 }


EDIT : Complete script, including the suggested snippets, with the given script you have provided.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 
 public class PhoneCameraScript : MonoBehaviour
 {
     private WebCamTexture frontCam;
     private Texture defaultBackground;
     public RawImage background;
     public AspectRatioFitter fit;
     public GameObject testCube;
     
     System.Func<Color, float> red = c => c.r;
     System.Func<Color, float> green = c => c.g;
     System.Func<Color, float> blue = c => c.b;
     System.Func<Color, float> alpha = c => c.a;
     System.Func<Color, float> sum = c => c.r + c.g + c.b + c.a;
     System.Func<Color, float> meanRGB = c => (c.r + c.g + c.b) / 3f;
     System.Func<Color, float> luminance = c => 0.2126f * c.r + 0.7152f * c.g + 0.0722f * c.b;
         
     private bool CamAvailable
     {
         get { return frontCam != null ;}
     }
 
     private void Start()
     {
         defaultBackground = background.texture;
         frontCam = GetWebCamTexture();
         
         if( CamAvailable )
         {
             frontCam.Play();
             background.texture = frontCam;
         }
         else
         {
             Debug.LogWarning("No front-facing camera detected");
         }
     }   
     
     private void Update()
     {
         if (!CamAvailable)        
             return;
         
         float ratio = (float)frontCam.width / (float)frontCam.height;
         fit.aspectRatio = ratio;
 
         float scaleY = frontCam.videoVerticallyMirrored ? -1f: 1f;
         background.rectTransform.localScale = new Vector3(1f, scaleY, 1f);
 
         int orient = -frontCam.videoRotationAngle;
         background.rectTransform.localEulerAngles = new Vector3(0, 0, orient);
 
         float y = GetElevation(frontCam.GetPixels());
         testCube.transform.position = new Vector3(0f, y, 0f);
     }
     
     /// <summary>
     /// Gets the webcam texture
     /// </summary>
     /// <returns>The texture of the front face webcam or <c>null</c> if no eligible device has been found</returns>
     private WebCamTexture GetWebCamTexture()
     {
         WebCamDevice[] devices = WebCamTexture.devices;
         
         for (int i = 0; i < devices.Length; i++)
         {
             if (devices[i].isFrontFacing)
                 return new WebCamTexture(devices[i].name, Screen.width, Screen.height);
         }
         return null ;
     } 
     
     /// <summary>
     /// Gets the elevation of an object according to the input colors
     /// </summary>
     /// <returns>The elevation of an object according to the input colors</returns>
     private float GetElevation( params Color[] colors )
     {
         int length = colors.Length ;
         Color averageColor = Color.clear ;
         
         // Compute average color of the input colors
         for( int index = 0 ; index < length ; ++index )
             averageColor += colors[index] ;
         averageColor /= length ;
         
         return luminance( averageColor ) ;  // or red( color ) or meanRGB( color ), or custom one ....
     }
 }
Comment
Add comment · Show 23 · 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 Fezpin · Jul 21, 2018 at 04:29 PM 0
Share

This has made the cube move, but it only seems to move by 0.2126. could you explain how this script works in simpler term? my apologies I am a novice coder and am incredibly grateful for you assistance :>

avatar image Hellium Fezpin · Jul 21, 2018 at 04:49 PM 0
Share

The System.Func represents the functions you can use to convert a color into a float.

 float y = lu$$anonymous$$ance( color ) ; // or red( color ) or meanRGB( color ), or ....

Converts the color into a float value using the lu$$anonymous$$ance function

 testCube.transform.position = new Vector3(0f, y, 0f);

Assigns the position to the cubeTransform with the computed elevation.

Your cube does not move because the color does not change (with the code I have provided)

If you declare the color public, and change it in the inspector, you will see your cube move.

  // Change the color in the inspector to see your cube move!
 public Color color = new Color( 1, 0, 0, 1 ) ;

I arbitrarily chose the lu$$anonymous$$ance function, but you can use another one I've created, or make your own!

avatar image Fezpin Hellium · Jul 22, 2018 at 09:56 AM 0
Share

ok that sounds awesome, so presumably I can change the colour that is used to move the cube to the main colour co$$anonymous$$g from my webcam texture right? so is there a way of using the color[] camLight as the input color in the calculation?

Show more comments

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

149 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Color.Lerp over HP 1 Answer

Colors Look off in iOS 0 Answers

I want a little help on my unity Project 0 Answers

Sprite Alpha Color Won't Change Via Script 1 Answer

Error in script? 0 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