[SOLVED]grabpass completely desaturate

hey,

I’m trying to make an object look like it consists of static. ive found a way to get the outside to do that, the object is transparant with static over it. the second thing after that is that i want to make everything inside the object rendered as a greyscale.

now so far i think the only way to do this is to do grabpass and than turn that into a greyscale image and rerender that… however im really bad at programming shaders.

this is what i got so far.

Shader "Custom/Enemy" {
Properties {
    _Color ("Color Tint", Color) = (1,1,1,1)
    _Rate ("Oscillation Rate", Range (1, 300)) = 300
}

SubShader {
   
    ZWrite Off
    Tags { "Queue" = "Transparent" }
    
	Blend One One
	GrabPass { }

	Pass {
		
		
CGPROGRAM
#pragma vertex vert

#include "UnityCG.cginc"

sampler2D _GrabTexture : register(s0);

struct v2f {
	float4 position : POSITION;
	float4 color : COLOR;
};

v2f vert (appdata_base v)
{
	v2f o;
	o.position = v.vertex;
	return o;
}

ENDCG 

	SetTexture [_GrabTexture] { combine texture}

	}

	
	
    Pass {
//static


CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma fragmentoption ARB_fog_exp2
#include "UnityCG.cginc"

float4 _Color;
float _Rate;

struct v2f {
    V2F_POS_FOG;
    float4 texcoord : TEXCOORD0;
};

v2f vert (appdata_base v)
{
    v2f o;
    PositionFog( v.vertex, o.pos, o.fog );
    o.texcoord = v.texcoord;
    return o;
}

half4 frag (v2f i) : COLOR
{
    float3 color;
    float m;
    m = _Time[0]*_Rate + ((i.texcoord[0]+i.texcoord[1])*5000000*_Color.a*_Color.a);
    m = sin(m) * 0.5;
    color = float3(m*_Color.r, m*_Color.g, m*_Color.b);
    return half4( color, 1 );
}
ENDCG

    }
	
}
Fallback "Transparent/Diffuse"
}

thanx for any help :slight_smile:

That sounds like it would work. I don’t have Unity here to test your shader out, but which part are you having trouble with? It looks like you aren’t attempting to desaturate the grabpass results at all. To do that you’ll have to convert the RGB value to HSL, and then reduce S to zero and convert back.

Or you can just use the Luminance function given to you by including UnityCG.cginc :wink:

Yes, that would be much easier.

the static is indeed already working, im missing the desaturation part. the problem is CG programming is completely new to me, i pretty good at javascript, php, coldfusion, actionscript and whatnot… none of these however even closely resemble CG so far :stuck_out_tongue:

so my question symplified would be how would i use the grabbed pass, apply the luminance function on it and reattach it. im still trying all sorts of things but i get confused by all the terms ive never used.

thanks for your replies :slight_smile:

well i finally worked out how to do that as well, its pretty simple, but hard to find what every word means in CG

here’s what i came up with, it might have lots of things it doesn’t actually need to work but it works :slight_smile:

Shader "Custom/Enemy" {
Properties {
    _Color ("Color Tint", Color) = (1,1,1,1)
    _Rate ("Oscillation Rate", Range (1, 300)) = 300
}

SubShader {
   
    ZWrite Off
    Tags { "Queue" = "Transparent" }

	GrabPass {	
		TextureScale 0.5
		TextureSize 2048
 		}

	Pass {
CGPROGRAM
#pragma fragment frag
#include "UnityCG.cginc"


samplerRECT _GrabTexture : register(s0);
float4 _GrabTexture_TexelSize;


struct v2f {
	float4 uvgrab : TEXCOORD0;
};



half4 frag( v2f i ) : COLOR
{
	half4 test = texRECTproj( _GrabTexture, i.uvgrab.xyw );
	test.xyz = Luminance(test.xyz);
	return test;
}

ENDCG
	}
	

	Pass{
	Blend One One
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma fragmentoption ARB_fog_exp2
#include "UnityCG.cginc"

float4 _Color;
float _Rate;

struct v2f {
    V2F_POS_FOG;
    float4 texcoord : TEXCOORD0;
};

v2f vert (appdata_base v)
{
    v2f o;
    PositionFog( v.vertex, o.pos, o.fog );
    o.texcoord = v.texcoord;
    return o;
}

half4 frag (v2f i) : COLOR
{
    float3 color;
    float m;
    m = _Time[0]*_Rate + ((i.texcoord[0]+i.texcoord[1])*5000000*_Color.a*_Color.a);
    m = sin(m) * 0.5;
    color = float3(m*_Color.r, m*_Color.g, m*_Color.b);
    return half4( color, 1 );
}
ENDCG


	}
}

}

Nicely done. There are indeed two extraneous bits of code I can see:

It doesn’t look like you’re using _GrabTexture_TexelSize, so you can delete that declaration.

You don’t need to put register semantics into Cg any more.

You do sometimes, so it’s not a bad habit to have. Most of the time it “just works” though.