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): // update object position to shader v1.0 - mgear - http://unitycoder.com/blog #pragma strict //var obj:Transform; private var radius:float=2; function Update () { // get mouse pos var ray = Camera.main.ScreenPointToRay (Input.mousePosition); var hit : RaycastHit; if (Physics.Raycast (ray, hit, Mathf.Infinity)) { // renderer.material.SetVector("_ObjPos", Vector4(obj.position.x,obj.position.y,obj.position.z,0)); renderer.material.SetVector("_ObjPos", Vector4(hit.point.x,hit.point.y,hit.point.z,0)); // convert hit.point to our plane local coordinates, not sure how to do in shader.. IN.pos.. ?? // var hitlocal = transform.InverseTransformPoint(hit.point); // renderer.material.SetVector("_ObjPos",Vector4(hitlocal.x,hitlocal.y,hitlocal.z,0)); } // box rotation for testing.. if (Input.GetKey ("a")) { transform.Rotate(Vector3(0,30,0) * Time.deltaTime); } if (Input.GetKey ("d")) { transform.Rotate(Vector3(0,-30,0) * Time.deltaTime); } // mousewheel for radius if (Input.GetAxis("Mouse ScrollWheel")!=0) { radius +=Input.GetAxis("Mouse ScrollWheel")*0.8; renderer.material.SetFloat( "_Radius", radius); } } the shaders code is: Code (csharp): // xray mouse pos shader test v1.0 - mgear - http://unitycoder.com/blog Shader "mShaders/XRay1" { Properties { _MainTex ("Base (RGB)", 2D) = "white" {} _ObjPos ("ObjPos", Vector) = (1,1,1,1) _Cutoff ("Alpha cutoff", Range(0,1)) = 0.5 _Radius ("HoleRadius", Range(0.1,5)) = 2 } SubShader { Tags {"Queue"="AlphaTest" "IgnoreProjector"="False" "RenderType"="TransparentCutout"} Cull Off // draw backfaces also, comment this line if no need for backfaces AlphaTest Greater [_Cutoff] CGPROGRAM // #pragma target 3.0 #pragma surface surf Lambert // #include "UnityCG.cginc" struct Input { float2 uv_MainTex; // float3 depth; // float4 pos; // float3 viewDir; float3 worldPos; // float3 worldRefl; // float3 worldNormal; // float4 screenPos; // INTERNAL_DATA }; sampler2D _MainTex; uniform float4 _ObjPos; uniform float _Radius; void surf (Input IN, inout SurfaceOutput o) { half3 col = tex2D (_MainTex, IN.uv_MainTex).rgb; float dx = length(_ObjPos.x-IN.worldPos.x); float dy = length(_ObjPos.y-IN.worldPos.y); float dz = length(_ObjPos.z-IN.worldPos.z); float dist = (dx*dx+dy*dy+dz*dz)*_Radius; dist = clamp(dist,0.5,1); o.Albedo = col; // color is from texture o.Alpha = dist; // alpha is from distance to the mouse } ENDCG } FallBack "Diffuse" } Any help would be much appreciated thanks in advance
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
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 ??