topical media & game development

talk show tell print

lib-unity-demo-shader-Assets-Common-Assets-RenderDepth.js / js



  @script RequireComponent(Camera)
  
  var depthShader : Shader;
  
  private var renderTexture : RenderTexture;
  private var shaderCamera : GameObject;
  
  function Start() {
          if (!SystemInfo.SupportsRenderTextureFormat(RenderTextureFormat.Depth)) {
                  enabled = false;
                  return;
          }
          if (!depthShader || !depthShader.isSupported) {
                  enabled = false;
                  return;
          }
  }
  
  function OnDisable() {
          DestroyImmediate(shaderCamera);
  }
  
  function OnPreCull()
  {
          if (!enabled || !gameObject.active)
                  return;
          renderTexture = RenderTexture.GetTemporary (camera.pixelWidth, camera.pixelHeight, 24, RenderTextureFormat.Depth);
          if (!shaderCamera) {
                  shaderCamera = new GameObject("ShaderCamera", Camera);
                  shaderCamera.camera.enabled = false;
                  shaderCamera.hideFlags = HideFlags.HideAndDontSave;
          }
          
          var cam = shaderCamera.camera;
          cam.CopyFrom (camera);
          cam.backgroundColor = Color(1,1,1,1);
          cam.clearFlags = CameraClearFlags.SolidColor;
          cam.targetTexture = renderTexture;
          cam.RenderWithShader (depthShader, "RenderType");
          
          Shader.SetGlobalTexture ("_GlobalDepthTexture", renderTexture);
          Shader.SetGlobalVector ("_GlobalDepthTextureSize", Vector4(renderTexture.width, renderTexture.height, 0, 0));
  }
  
  function OnPostRender()
  {
          if (!enabled || !gameObject.active)
                  return;
          RenderTexture.ReleaseTemporary (renderTexture);
  }
  


(C) Æliens 04/09/2009

You may not copy or print any of this material without explicit permission of the author or the publisher. In case of other copyright issues, contact the author.