EdgeDetectEffectNormals.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. @script ExecuteInEditMode
  2. @script RequireComponent (Camera)
  3. @script AddComponentMenu ("Image Effects/Edge Detection (Geometry)")
  4. enum EdgeDetectMode {
  5. Thin = 0,
  6. Thick = 1,
  7. }
  8. class EdgeDetectEffectNormals extends PostEffectsBase {
  9. public var mode : EdgeDetectMode = EdgeDetectMode.Thin;
  10. public var sensitivityDepth : float = 1.0;
  11. public var sensitivityNormals : float = 1.0;
  12. public var edgesOnly : float = 0.0;
  13. public var edgesOnlyBgColor : Color = Color.white;
  14. public var edgeDetectShader : Shader;
  15. private var _edgeDetectMaterial : Material = null;
  16. function CreateMaterials ()
  17. {
  18. _edgeDetectMaterial = CheckShaderAndCreateMaterial (edgeDetectShader,_edgeDetectMaterial);
  19. }
  20. function Start () {
  21. CreateMaterials ();
  22. CheckSupport (true);
  23. }
  24. function OnEnable () {
  25. GetComponent.<Camera>().depthTextureMode |= DepthTextureMode.DepthNormals;
  26. }
  27. function OnRenderImage (source : RenderTexture, destination : RenderTexture)
  28. {
  29. CreateMaterials ();
  30. var sensitivity : Vector2;
  31. sensitivity.x = sensitivityDepth;
  32. sensitivity.y = sensitivityNormals;
  33. source.filterMode = FilterMode.Point;
  34. _edgeDetectMaterial.SetVector ("sensitivity", Vector4 (sensitivity.x, sensitivity.y, 1.0, sensitivity.y));
  35. _edgeDetectMaterial.SetFloat("_BgFade", edgesOnly);
  36. var vecCol : Vector4 = edgesOnlyBgColor;
  37. _edgeDetectMaterial.SetVector("_BgColor", vecCol);
  38. if (mode == EdgeDetectMode.Thin) {
  39. Graphics.Blit (source, destination, _edgeDetectMaterial, 0);
  40. }
  41. else {
  42. Graphics.Blit (source, destination, _edgeDetectMaterial, 1);
  43. }
  44. }
  45. }