Home > Back-end >  Mapping color change to VR controller?
Mapping color change to VR controller?

Time:07-14

I'm currently creating a VR musical experience using Unity.

What I'm trying to achieve, is having some sphere's material being controlled through a Vector2 that returns the values of the HTC Vive trackpad, then use that number to control the color of said sphere (the higher the number, the fuller the colour, and it would be transparent when at the lowest possible value, I'd be using the Y axis for that).

So far I've got his code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Valve.VR;

public class Intensity : MonoBehaviour
{
  


    public SteamVR_Action_Vector2 touchPadAction;


    void Update()
    {
        
        Vector2 touchpadValue = touchPadAction.GetAxis(SteamVR_Input_Sources.Any);

        if (touchpadValue != Vector2.zero)
        {
            print(touchpadValue);
        }

        Debug.Log(touchpadValue);
    }
}

After that, I'm completely lost! The most suitable function I have found is Lerp but can't figure out how to map the value change to the trackpad, and also how to connect it to the Material parameters.

I'm not looking for anyone to write the whole script for me, but pointing me to the concepts I should learn in order to be able to code this would be really helpful.

Thanks!

CodePudding user response:

I'll explain what I understand: if I swipe up on the trackpad, the material should become less transparent, while if I swipe down it should become more and more transparent. If that's correct, you can do this: Take the Y value of "touchpadValue" and assign it to the alpha property of the material.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Valve.VR;

public class Intensity : MonoBehaviour
{
    public SteamVR_Action_Vector2 touchPadAction;
    public Material sphereMaterial;

    void Update()
    {
        Vector2 touchpadValue = touchPadAction.GetAxis(SteamVR_Input_Sources.Any);
        sphereMaterial.color = new Color(sphereMaterial.color.r, sphereMaterial.color.g, sphereMaterial.color.b, touchpadValue.y);
    }
}

P.S. I have read the documentation, but I have not found the maximum and minimum values ​​of the Vector. I assumed it's between 0 and 1. If it's between -1 and 1, do this:

sphereMaterial.color = new Color(sphereMaterial.color.r, sphereMaterial.color.g, sphereMaterial.color.b, (touchpadValue.y   1) / 2);

While if it is any other value starting from 0, just divide touchpadValue.y by the maximum value.

Remember that if you change the color of that material, all objects with that material will change color, so I recommend that you create a specific one for the sphere.

if you think my answer helped you, you can mark it as accepted. I would very much appreciate it :)

  • Related