Search Unity

X ray shader problems need help

Discussion in 'Shaders' started by parryt, Jun 25, 2013.

  1. parryt

    parryt

    Joined:
    Dec 4, 2012
    Posts:
    25
    Hi I am trying to learn shaders and I'm waiting for a book on CG but at the moment I'm finding different shaders that have already been made but i have found a problem with a shaders that I would like to use the shaders link is:

    http://unitycoder.com/blog/2012/02/22/x-ray-cutout-shader-with-mouse/

    The problem that I am getting is that this shaders works on different game objects for example a standard cube and capsule within unity but if i add this shaders and script to an obj or to a fbx the whole object turns black and because of my lack of knowledge in shaders i don't know whats wrong.

    The javascript code is:


    Code (csharp):
    1. // update object position to shader v1.0 - mgear - http://unitycoder.com/blog
    2.  
    3. #pragma strict
    4.  
    5. //var obj:Transform;
    6. private var radius:float=2;
    7.  
    8.  
    9. function Update ()
    10. {
    11.  
    12.     // get mouse pos
    13.     var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
    14.     var hit : RaycastHit;
    15.     if (Physics.Raycast (ray, hit, Mathf.Infinity))
    16.     {
    17. //      renderer.material.SetVector("_ObjPos", Vector4(obj.position.x,obj.position.y,obj.position.z,0));
    18.         renderer.material.SetVector("_ObjPos", Vector4(hit.point.x,hit.point.y,hit.point.z,0));
    19.  
    20.         // convert hit.point to our plane local coordinates, not sure how to do in shader.. IN.pos.. ??
    21. //      var hitlocal = transform.InverseTransformPoint(hit.point);
    22. //      renderer.material.SetVector("_ObjPos",Vector4(hitlocal.x,hitlocal.y,hitlocal.z,0));
    23.        
    24.     }
    25.  
    26.    
    27.     // box rotation for testing..
    28.     if (Input.GetKey ("a"))
    29.     {
    30.         transform.Rotate(Vector3(0,30,0) * Time.deltaTime);
    31.     }
    32.     if (Input.GetKey ("d"))
    33.     {
    34.         transform.Rotate(Vector3(0,-30,0) * Time.deltaTime);
    35.     }
    36.    
    37.     // mousewheel for radius
    38.     if (Input.GetAxis("Mouse ScrollWheel")!=0)
    39.     {
    40.         radius +=Input.GetAxis("Mouse ScrollWheel")*0.8;
    41.         renderer.material.SetFloat( "_Radius", radius);
    42.     }
    43. }

    the shaders code is:

    Code (csharp):
    1. // xray mouse pos shader test v1.0 - mgear - http://unitycoder.com/blog
    2.  
    3. Shader "mShaders/XRay1"
    4. {
    5.     Properties {
    6.         _MainTex ("Base (RGB)", 2D) = "white" {}
    7.         _ObjPos ("ObjPos", Vector) = (1,1,1,1)
    8.         _Cutoff ("Alpha cutoff", Range(0,1)) = 0.5
    9.         _Radius ("HoleRadius", Range(0.1,5)) = 2
    10.     }
    11.     SubShader {
    12.         Tags {"Queue"="AlphaTest" "IgnoreProjector"="False" "RenderType"="TransparentCutout"}
    13.         Cull Off // draw backfaces also, comment this line if no need for backfaces
    14.         AlphaTest Greater [_Cutoff]
    15.        
    16.         CGPROGRAM
    17. //      #pragma target 3.0
    18.         #pragma surface surf Lambert
    19. //      #include "UnityCG.cginc"
    20.  
    21.         struct Input
    22.         {
    23.             float2 uv_MainTex;
    24.            
    25. //          float3 depth;
    26. //          float4 pos;
    27. //          float3 viewDir;
    28.            
    29.             float3 worldPos;
    30. //          float3 worldRefl;
    31. //          float3 worldNormal;
    32. //          float4 screenPos;
    33. //          INTERNAL_DATA
    34.         };
    35.        
    36.         sampler2D _MainTex;
    37.         uniform float4 _ObjPos;
    38.         uniform float _Radius;
    39.  
    40.         void surf (Input IN, inout SurfaceOutput o)
    41.         {
    42.        
    43.             half3 col = tex2D (_MainTex, IN.uv_MainTex).rgb;
    44.  
    45.             float dx = length(_ObjPos.x-IN.worldPos.x);
    46.             float dy = length(_ObjPos.y-IN.worldPos.y);
    47.             float dz = length(_ObjPos.z-IN.worldPos.z);
    48.             float dist = (dx*dx+dy*dy+dz*dz)*_Radius;
    49.             dist = clamp(dist,0.5,1);
    50.            
    51.             o.Albedo = col; // color is from texture
    52.             o.Alpha = dist;  // alpha is from distance to the mouse
    53.         }
    54.         ENDCG
    55.     }
    56.     FallBack "Diffuse"
    57. }
    58.  

    Any help would be much appreciated

    thanks in advance
     
  2. Kragh

    Kragh

    Joined:
    Jan 22, 2008
    Posts:
    657
    The object needs to have a collider attached to it, for the raycasting to work. Maybe that is what you are missing? Other than that, I can't see anything which would make primitives work, but not your mesh
     
  3. parryt

    parryt

    Joined:
    Dec 4, 2012
    Posts:
    25
    i have put a mesh collider onto the object because it is the shape of a 3D plane but the object still turns black but i have also tested this out by creating a cube in blender and then just putting a simple box collider onto the object and it still sturns the object black any ideas ??
     
  4. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,448
    Maybe problem with UV mapping?
    Test it by adding some basic uv mapping to the box in blender.
     
  5. parryt

    parryt

    Joined:
    Dec 4, 2012
    Posts:
    25
    i have tried adding the uv mapping this did not work any other ideas??
     
  6. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,448
    If you can put the model for download, i can test it here also.