Latest Updates: unity RSS

  • Real time blend 2 textures in Unity

    Ellen 08:40 on September 6, 2012 | 7 Permalink | Reply
    Tags: , shaders, unity

    I had a hard time changing the texture of a game object in real-time where I tried out a movie fading between textures and using Lerp on the material. I wanted it to be dynamic and just be able to pass an argument to my script defining which texture to animate to. Many game objects had different shaders so it didn’t work just to Lerp the current material. So I create custom shaders for different objects that contained 2 textures “_MainTex” and “_Texture2″. I also added the blend option the the shader order to be able to control the blend of the texture from outside. Just adding the shader you can change the blend slider to see the result.

    See example of one diffuse shader with normal map.

    In my javascript I could then pass in which texture to change to and which one to change from.
    I set the textures I want to change between. These are set in the inspector of athouring environment of Unity and passed to the script.

    var firstTexture:Texture;
    var secondTexture:Texture;
    var thirdTexture:Texture;

    The script starts out with setting the default texture of the chosen game object. The game object could have been passed to the script as well. :-)

    function Start () {
        textureObject = gameObject.Find("myTextureObject");
        textureObject.renderer.material.SetFloat( "_Blend", 1 ); // Setting the current texture to be 100% visible
    }

    The public function changeTexture can be accessed from outside to recieve arguments for new textures. newTexture is used to store the new texture as soon as it is time to animate.

    public function changeTexture(myArg : float) {
      yield WaitForSeconds(0.3);

      if(myArg == 1) {
          newTexture = firstTexture;
      } else if (myArg == 2){
          newTexture = secondTexture;
      } else if (myArg == 3){
          newTexture = thirdTexture;
      }

      textureObject.renderer.material.mainTexture = newTexture;
      triggerChange = true;
    }

    In the update function the script always checks if it should change to a new texture. If changeTexture was called it starts blending to the new texture. At the end it sets the old texture to the new one and resets the blend:

    function Update () {
         if(triggerChange == true) {
              changeCount = changeCount - 0.05;

              textureObject.renderer.material.SetFloat( "_Blend", changeCount );
              if(changeCount <= 0) {
                   triggerChange = false;
                   changeCount = 1.0;
                   textureObject.renderer.material.SetTexture ("_Texture2", newTexture);
                   textureObject.renderer.material.SetFloat( "_Blend", 1);
              }

         }
    }

    BlendTextures.js is the entire javascript file of the script above.

    To implement this in Unity follow these steps:
    1. Add your script to the scene. Select the textures and normalmap you defined as public variables in BlendTextures.js.
    2. Create a material and choose your custom shader.
    3. Add the material to your game object.

    blend_textures_unity

    Read up on how to create your custom shaders in the Unity Documentation.

  • Unity + Processing Demo

    Ellen 10:11 on July 2, 2012 | 5 Permalink | Reply
    Tags: osc, , , unity

    Test your Unity project with the Processing Osc Demo. Send your events through the Processing app and see them appear in the Unity project. There is also a timer in Processing sending events at all time changing the width of a cube in Unity. Download the Unity OSC example here.
    unity_processing_demo

  • Unity + Processing + Arduino

    Ellen 10:32 on May 31, 2012 | 18 Permalink | Reply
    Tags: , , , , unity

    I did a test connecting Arduino and Unity with the help of Processing and a Open Sound Control library. When I am pushing the Flexi Force sensor the values affect a cube within Unity. To get this setup do the following:

    Arduino
    Load the Standard Firmdata on to your Arduino. Check out a full Arduino + Processing tutorial to get the setup.

    Processing
    Download the oscP5 processing library and put it in your Processing sketch folder in the folder libraries.

    Initialize oscP5 library and broadcast data to port 12000. The port Unity will listen to.
    Add the remote location to listen to on port 3200. If Unity would broadcast event in this example, the broadcast port would be set to 3200. Initialise Arduino to be used later on.

    oscP5 = new OscP5(this,12000);
      myRemoteLocation = new NetAddress("127.0.0.1",3200);
      arduino = new Arduino(this, Arduino.list()[0], 57600);

    In the draw function we read the analoge pin 0 to get sensor values from the Flexi Force sensor. Set the OSC message to flexiforce. Unity will listen to this label. Add the message of the sensor value into the osc message with add method. This method can take numbers, strings and byte data.

    float flexiforceSensor = arduino.analogRead(0);
    OscMessage oscMess = new OscMessage("/flexiforce");
    oscMess.add(flexiforceSensor);

    oscP5.send(oscMess, myRemoteLocation);

    The entire Processing sketch can be seen here.

    Unity
    In Unity you can import the Osc.cs and UDPPacketIO.cs to broadcast and listen to UDP data.

    Set up the corresponding host and ports in your Unity script.

    public var OSCHost : String = "127.0.0.1";
    public var SendToPort : int = 3200;
    public var ListenerPort : int = 12000;

    Import the UDPPacketIO and Osc components and feed in the host and ports. Add your specific label of your osc message and what method to call when receiving this event from Processing.

    var udp : UDPPacketIO = GetComponent("UDPPacketIO");
    udp.init(OSCHost, SendToPort, ListenerPort);
           
    handler = GetComponent("Osc");
    handler.init(udp);
    handler.SetAddressHandler("/flexiforce", AffectObject);

    In the receiving method you can print out the label and message values. In this case the sensor value from the Flexi Force. The cube will change width when getting a new value from Processing.

    public function AffectObject(oscMessage : OscMessage) : void
    {      
            Debug.Log("Event name: " + Osc.OscMessageToString(oscMessage));
            Debug.Log("Event data: " + oscMessage.Values[0]);
           
            var myCube = GameObject.Find("Cube");
            var boxWidth:int = 8 - ( oscMessage.Values[0]);
            myCube.transform.localScale = Vector3(boxWidth,5,5);
    }

c
compose new post
j
next post/next comment
k
previous post/previous comment
r
reply
e
edit
o
show/hide comments
t
go to top
esc
cancel