Reply To: Projector and Unity

Home Forums Syphon Syphon Implementations – User Projector and Unity Reply To: Projector and Unity

#4600
pymenvert
Participant

Thanks Brian for this way…

I’ve modified the SyphonClientBridge like that :

using UnityEngine;
using System.Collections;
using System.Runtime.InteropServices;

public class SyphonClientBridge : MonoBehaviour
{
        public int desiredWidth = 1024;
        public int desiredHeight = 768;
        private Texture2D _texture;

        // Frees the Syphon Server and clears our Unity Plugin's GL resources
        [DllImport ("SyphonUnityPlugin")]
	private static extern void syphonClientDestroyResources();

	// Syphon Client Update Texture takes in a texture from Unity, and populates it with the contents of a
	// Syphon Server. This also lazily inits the server for you, ensuring the proper OpenGL context is set.
	[DllImport ("SyphonUnityPlugin")]
	private static extern void syphonClientUpdateTexture(int nativeTexture, int width, int height);

	// Use this for initialization
	void Awake ()
	{
		_texture = new Texture2D(desiredWidth, desiredHeight, TextureFormat.ARGB32, false);
		//_texture = new Cubemap (128, TextureFormat.ARGB32, false);
		_texture.Apply(false);

		Projector proj = gameObject.GetComponent<Projector>();

		proj.material.mainTexture = _texture;
		//proj.material.mainTexture.cookie = _texture;
	/*	if(renderer) {
			renderer.material.mainTexture = _texture;
			//proj.material.mainTexture = _texture;
		}*/

	}

	static protected Material lineMaterial;
	static protected void CreateLineMaterial() {
		if( !lineMaterial ) {
			lineMaterial = new Material( "Shader "Lines/Colored Blended" {" +
				"SubShader { Pass { " +
				" Blend SrcAlpha OneMinusSrcAlpha " +
				" ZWrite Off "  +
				"} } }" );
			lineMaterial.hideFlags = HideFlags.HideAndDontSave;
			lineMaterial.shader.hideFlags = HideFlags.HideAndDontSave;
		}
	}        

        // YOU MUST USE ON RENDER OBJECT FOR UNITY CLIENT.
        void OnRenderObject ()
        {
                // Unity 3.0 has GetNativeTextureID, which returns the texture ID.
                // 2.x requires us to call GetInstanceID instead.
//              #if UNITY_3     

                CreateLineMaterial();
                lineMaterial.SetPass(0);

                        syphonClientUpdateTexture(_texture.GetNativeTextureID(), _texture.width, _texture.height);
//              #elif UNITY_2
//                      syphonClientUpdateTexture(_texture.GetInstanceID());
//              #endif
        }

        void OnPostRender() {
                CreateLineMaterial();
                lineMaterial.SetPass( 0 );
        }

        public void cleanup(){
                        syphonClientDestroyResources();
        }

        void OnDisable ()
        {
                GL.InvalidateState();
                Destroy(_texture);
                cleanup();
        }

}

Now, it works with a projector’s GameObject but I don’t know if it’s a clean code.

But, an instance of syphon client is mapped on each face of Cube.
like this screen shot :
http://www.heberger-image.fr/images/67697_Syphon_cube.png.html

I won’t that.
I would like to have one instance of Syphon Client mapped on all the structure (2 Cubes for example).

I don’t know if the problem come from projector’s shaders or SyphonClientBridge.

I’m trying to include mainTexture in the projector>light shaders but no way.
Perhaps create a syphon>projector shaders…
But I don’t understand how works the syphon shaders.

Can you help me please..

Thanks in advance
pym