FlatWorldUI.cs 12.6 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441
//--------------------------------------
//               PowerUI
//
//        For documentation or 
//    if you have any issues, visit
//        powerUI.kulestar.com
//
//    Copyright © 2013 Kulestar Ltd
//          www.kulestar.com
//--------------------------------------

using System;
using UnityEngine;
using System.Collections;
using System.Collections.Generic;


namespace PowerUI{
	
	/// <summary>
	/// Flat WorldUI's render to textures. Normal worldUI's are better for performance but flat worldUI's are more flexible. 
	/// Free/Indie users should use normal WorldUI's instead for performance purposes, however this can still be used.
	/// The difference here is the texture can be e.g. applied to a curved surface.
	/// </summary>
	
	public partial class FlatWorldUI:WorldUI{
		
		/// <summary>Global position offset on z.</summary>
		private static float GlobalOffset=-1000f;
		
		/// <summary>The default layer to use for rendering FlatWorldUI's. If not set the PowerUI layer is used, Change by using yourWorldUI.Layer.</summary>
		private int DefaultLayer=-1;
		
		/// <summary>A delegate called when the Update event is fired, right before the WorldUI redraws.</summary>
		public UpdateMethod OnUpdate;
		/// <summary>The raw texture. If this changes because you resized your FlatWorldUI, 
		/// an imagechange event will be fired on the window (type of Dom.Event).</summary>
		public RenderTexture Texture;
		/// <summary>The internal camera being used to render it flat.</summary>
		public Camera SourceCamera;
		/// <summary>The internal camera's gameobject being used.</summary>
		public GameObject CameraObject;
		/// <summary>A filter to apply.</summary>
		private Loonim.SurfaceTexture Filter_;
		/// <summary>The draw info which draws the filter.</summary>
		private Loonim.DrawInfo FilterDrawInfo;
		
		
		/// <summary>Creates a new Flat World UI with 100x100 pixels of space and a name of "new World UI".
		/// The gameobjects origin sits at the middle of the UI by default. See <see cref="PowerUI.WorldUI.SetOrigin"/>. 
		/// By default, 100 pixels are 1 world unit. See <see cref="PowerUI.WorldUI.SetResolution"/>.</summary>
		public FlatWorldUI():this("new World UI",100,100){}
		
		/// <summary>Creates a new Flat World UI with 100x100 pixels of space and the given name.
		/// The gameobjects origin sits at the middle of the UI by default. See <see cref="PowerUI.WorldUI.SetOrigin"/>. 
		/// By default, 100 pixels are 1 world unit. See <see cref="PowerUI.WorldUI.SetResolution"/>.</summary>
		/// <param name="name">The name for the UI's gameobject.</param>
		public FlatWorldUI(string name):this(name,100,100){}
		
		/// <summary>Creates a new Flat World UI with the given pixels of space and a name of "new World UI".
		/// The gameobjects origin sits at the middle of the UI by default. See <see cref="PowerUI.WorldUI.SetOrigin"/>. 
		/// By default, 100 pixels are 1 world unit. See <see cref="PowerUI.WorldUI.SetResolution"/>.</summary>
		/// <param name="widthPX">The width in pixels of this UI.</param>
		/// <param name="heightPX">The height in pixels of this UI.</param>
		public FlatWorldUI(int widthPX,int heightPX):this("new World UI",widthPX,heightPX){}
		
		/// <summary>Creates a new Flat World UI with the given pixels of space and a given name.
		/// The gameobjects origin sits at the middle of the UI by default. See <see cref="PowerUI.WorldUI.SetOrigin"/>. 
		/// By default, 100 pixels are 1 world unit. See <see cref="PowerUI.WorldUI.SetResolution"/>.</summary>
		/// <param name="name">The name for the UI's gameobject.</param>
		/// <param name="widthPX">The width in pixels of this UI.</param>
		/// <param name="heightPX">The height in pixels of this UI.</param>
		public FlatWorldUI(string name,int widthPX,int heightPX):base(name,widthPX,heightPX){
			
			// It's a flat UI:
			Flat=true;
			
			// Create camera gameobject:
			CameraObject=new GameObject();
			
			CameraObject.name=name+"-#camera";
			
			// Parent the camera to the root:
			CameraObject.transform.parent=gameObject.transform;
			CameraObject.transform.localPosition=Vector3.zero;
			
			// Add a camera:
			SourceCamera=CameraObject.AddComponent<Camera>();
			
			// Put it right at the back:
			SourceCamera.depth=-9999;
			
			// Set the clear flags:
			SourceCamera.clearFlags=CameraClearFlags.Color;
			SourceCamera.backgroundColor=new Color(1f,1f,1f,0f);
			
			// Make it forward rendered (it deals with transparency):
			SourceCamera.renderingPath=RenderingPath.Forward;
			
			// Disable the camera object so we 
			// can manually redraw at the UI rate:
			CameraObject.SetActive(false);
			
			float zSpace=UI.GetCameraDistance();
			
			// Setup the cameras distance:
			SetCameraDistance(zSpace);
			
			// Call the camera creation method:
			UI.CameraGotCreated(SourceCamera);
			
			// Make it orthographic:
			SourceCamera.orthographic=true;
			
			// Set the orthographic size:
			SetOrthographicSize();
			
			// Next it's time for the texture itself!
			// We now always have a RenderTexture.
			
			// Update the render texture:
			ChangeImage(widthPX,heightPX);
			
			// Change the layer of the gameobject and also the camera.
			
			// Set the culling mask:
			if(DefaultLayer==-1){
			
				Layer=UI.Layer;
				
			}else{
				
				Layer=DefaultLayer;
				
			}
			
			gameObject.transform.position=new Vector3(0f,-150f,GlobalOffset);
			GlobalOffset+=zSpace+1f;
			
		}
		
		/// <summary>Updates the render texture.</summary>
		private void ChangeImage(int width,int height){
			
			if(Texture!=null){
				
				// Destroy existing one:
				GameObject.Destroy(Texture);
			
			}
			
			// Create tex:
			Texture=new RenderTexture(width,height,16,RenderTextureFormat.ARGB32);
			
			// Hook it up:
			SourceCamera.targetTexture=Texture;
			
			// If we have a filter, update its input:
			if(Filter_==null){
				
				// Fire an imagechange event into the window:
				Dom.Event e=new Dom.Event("imagechange");
				e.SetTrusted(false);
				document.window.dispatchEvent(e);
				
			}else{
				// Connect up the filter
				// (which will fire an imagechange for us):
				ConnectFilter();
			}
			
		}
		
		/// <summary>Connects a filter to the camera's output.
		/// Fires an imagechange event too as this updates Texture to the filtered version.</summary>
		private void ConnectFilter(){
			
			if(Filter_!=null){
				
				if(FilterDrawInfo==null){
					FilterDrawInfo=new Loonim.DrawInfo();
				}
				
				// Update draw info:
				FilterDrawInfo.SetSize(pixelWidth,pixelHeight);
				
				// Update source:
				Filter_.Set("source0",SourceCamera.targetTexture);
				
				// Reallocate the filter:
				Filter_.PreAllocate(FilterDrawInfo);
				
				// Grab the main output (always a RT):
				Texture=Filter_.Texture as RenderTexture;
				
				if(Texture==null){
					
					// This isn't a valid filter!
					// It either had no nodes or e.g. e.g. a solid colour.
					Debug.Log("Invalid filter was set to a FlatWorldUI - removed it.");
					Filter_=null;
					
				}
				
			}
			
			if(Filter_==null){
				
				// Clear draw info:
				FilterDrawInfo=null;
				
				// Revert to the camera's output:
				Texture=SourceCamera.targetTexture;
				
			}
			
			// Fire an imagechange event into the window:
			Dom.Event e=new Dom.Event("imagechange");
			e.SetTrusted(false);
			document.window.dispatchEvent(e);
			
		}
		
		/// <summary>Alias for Texture.</summary>
		public RenderTexture texture{
			get{
				return Texture;
			}
		}
		
		/// <summary>This FlatWorldUI will accept input from the given collider. Note that it will use the textureCoord's of the hit point
		/// (this will work with mesh colliders too).</summary>
		public void AcceptInputFrom(Collider collider){
			AcceptInputFrom(collider.transform);
		}
		
		/// <summary>This FlatWorldUI will accept input from a gameobject with one or more colliders.
		/// Note that it will use the textureCoord's of the hit point
		/// (this will work with mesh colliders too).</summary>
		public void AcceptInputFrom(GameObject colliderObject){
			AcceptInputFrom(colliderObject.transform);
		}
		
		/// <summary>This FlatWorldUI will accept input from a gameobject with one or more colliders.
		/// Note that it will use the textureCoord's of the hit point
		/// (this will work with mesh colliders too).</summary>
		public void AcceptInputFrom(Transform colliderTransform){
			
			if(transform!=null){
				// Remove from the physics lookup if already in there:
				if(PhysicsLookup!=null){
					PhysicsLookup.Remove(transform);
				}
			}
			
			// Set transform:
			transform=colliderTransform;
			
			// Accept input (which will add it to the physics lookup):
			AcceptInput=true;
			
		}
		
		/// <summary>Applies Texture to the sharedMaterial on the gameobject.</summary>
		public void ApplyTo(GameObject gameObject){
			
			MeshRenderer renderer=gameObject.GetComponent<MeshRenderer>();
			Material material;
			
			if(renderer==null){
				
				// Try skinned:
				SkinnedMeshRenderer sme=gameObject.GetComponent<SkinnedMeshRenderer>();
				
				if(sme==null){
					throw new Exception("That gameObject doesn't have a mesh renderer or a skinned mesh renderer.");
				}
				
				// Get the material:
				material=sme.sharedMaterial;
				
			}else{
				
				// Get the material:
				material=renderer.sharedMaterial;
				
			}
			
			// Apply it:
			material.mainTexture=Texture;
			
		}
		
		/// <summary>Applies Texture to material.mainTexture.</summary>
		public void ApplyTo(Material material){
			
			// Apply it:
			material.mainTexture=Texture;
			
		}
		
		/// <summary>Creates a material with the given shader and applies texture to its mainTexture property.</summary>
		public Material CreateMaterial(Shader shader){
			
			Material material=new Material(shader);
			
			material.mainTexture=Texture;
			
			return material;
			
		}
		
		/// <summary>Creates a Standard Shader material and applies texture to its mainTexture property.</summary>
		public Material CreateMaterial(){
			
			Material material=new Material(Shader.Find("Standard"));
			
			material.mainTexture=Texture;
			
			return material;
			
		}
		
		public override void RenderWithCamera(int layer){
			base.RenderWithCamera(layer);
			SourceCamera.cullingMask=(1<<layer);
		}
		
		/// <summary>Sets the distance of the camera, essentially defining the amount of z-index available.</summary>
		/// <param name="distance">The distance of the camera from the origin along the z axis in world units.</param>
		public void SetCameraDistance(float distance){
			SourceCamera.farClipPlane=distance*2f;
			CameraObject.transform.localPosition=new Vector3(0f,0f,-distance);
		}
		
		public override void SetOrigin(float x,float y){
			x=0.5f;
			y=0.5f;
			base.SetOrigin(x,y);
		}
		
		public override void SetResolution(float pp){
		}
		
		public override void SetResolution(int x,int y){
		}
		
		public override bool SetDimensions(int x,int y){
			
			// Check for changes:
			if(pixelWidth==x && pixelHeight==y){
				return false;
			}
			
			// Set the base dimensions:
			base.SetDimensions(x,y);
			
			if(Texture!=null){
				
				// Recreate:
				ChangeImage(x,y);
				
			}
			
			// Set the orthographic size:
			SetOrthographicSize();
			
			return true;
		}
		
		private void SetOrthographicSize(){
			
			if(SourceCamera==null){
				return;
			}
			
			// Grab the world per pixel size:
			SourceCamera.orthographicSize=pixelHeight / 2f * WorldPerPixel.y;
			
		}
		
		/// <summary>A filter to apply to this FWUI's output.</summary>
		public Loonim.SurfaceTexture Filter{
			get{
				return Filter_;
			}
			set{
				Filter_=value;
				
				if(SourceCamera.targetTexture!=null){
					
					// Already got a texture - hook up the filter now:
					ConnectFilter();
					
				}
			}
		}
		
		public override void Update(){
			
			if(OnUpdate!=null){
				OnUpdate();
			}
			
			base.Update();
			
			if(Renderer==null){
				return;
			}
			
			// Apply aspect ratio - we do this in update as 
			// changing the screen size affects our FWUI cams too:
			SourceCamera.aspect=Ratio;
			
			// Render now!
			SourceCamera.Render();
			
			// If we have Loonim filters, redraw those too.
			if(Filter_!=null){
				Filter_.ForceDraw(FilterDrawInfo);
			}
			
		}
		
		public override void Destroy(){
			
			base.Destroy();
			
			if(Texture!=null){
				
				// Destroy it:
				GameObject.Destroy(Texture);
				Texture=null;
				
			}
			
		}
		
	}
	
}