[REQ] Spotlight Shader

Posted By: Rei_Ayanami

[REQ] Spotlight Shader - 06/09/10 19:06

Hello laugh ,

I need a really(really, really) good Spotlight
shader - it will be the most important part of
the game, so vertex lightning is much to bad.

I know that there is a Spotlight shader by Emre,
who already helped me once adding Dynamic Lights.
But I get more and more problems with that Shader...

It looks good, but I need some things to be
changed/a new one. Here is how it looks at
the moment(Its a bit racked... but just because i
got a new monitor today..):


I applied the object shader to the view...
Invisible Objects sometimes get Visible,
Translucent things aren't translucent and
no static lights...

EDIT: Here is the code for the shader (at the moment)
Code:
// By Joseph Duffey Jan 2008
// For the GameStudio Community
// 
// Projective Texturing Spotlight + Fog
//

const float4x4 matWorldViewProj;                                          // World*view*projection matrix.
const float4x4 matMtl;                                                    // custom matrix passed via the material
const float4x4 matWorld;                                                  // World matrix.
const float4 vecViewPos;                                                  // Current view position passed from application
const float4 vecFog;                                                      // Vector calculated from the current view's clip and fog parameters
const float4 vecAmbient;                                                  // Ambient color, passed by the engine.
const float4 vecSkill1;                                                   // spotlights pos (xyz) and range (w) from application
const float4 vecSkill5;                                                   // spotlights normalized direction vector from aplication
static const float SpecularIntensity = 0.5f;                              // The intensity of the specular light. 
static const float SpecularPower = 2.0f;                                  // The specular power, used as 'glossiness' factor. 

texture entSkin1;                                                         // bitmap of object material applied to
texture mtlSkin1;                                                         // projection bitmap defined in material

sampler BaseTex = sampler_state
{
    Texture = <entSkin1>;
    MinFilter = Linear;
    MagFilter = Linear;
    MipFilter = Linear;
    AddressU = wrap;
    AddressV = wrap;
};

sampler ProjTex = sampler_state
{
    Texture = <mtlSkin1>;
    MinFilter = Linear;
    MagFilter = Linear;
    MipFilter = Linear;
    AddressU = clamp;
    AddressV = clamp;
};

struct VS_INPUT
{
    float4 Pos    : POSITION;
    float3 Normal : NORMAL;
    float4 Tex    : TEXCOORD0;
};

struct VS_OUTPUT
{
    float4 Pos       : POSITION;
    float4 Tex       : TEXCOORD0;
    float4 Tex1      : TEXCOORD1;
    float4 LightVec  : TEXCOORD2;
    float3 Normal    : TEXCOORD3;
    float3 ViewDir   : TEXCOORD4; 
    float Fog        : FOG;
};

struct PS_INPUT
{
    float4 Tex      : TEXCOORD0;
    float4 Tex1     : TEXCOORD1;
    float4 LightVec : TEXCOORD2;
    float3 Normal   : TEXCOORD3;
    float4 ViewDir  : TEXCOORD4;
};


VS_OUTPUT MAIN_VS( VS_INPUT In ) 
{
    VS_OUTPUT Out = (VS_OUTPUT) 0;

    Out.Pos = mul(In.Pos, matWorldViewProj);                                 // Transform vertex position to screen space:
    Out.Normal = normalize(mul(In.Normal, matWorld));                        // Transform the normal from object space to world space:
    Out.Tex = In.Tex;                                                        // Copy color texture coordinates through
    Out.Tex1 = mul( mul(In.Pos,matWorld), matMtl);                      // Calc the projected texture coordinates
    Out.LightVec.xyz = mul(In.Pos, matWorld) - vecSkill1;                    // light to position vector
    Out.LightVec.w = length(Out.LightVec.xyz)/vecSkill1.w;                   // light range
    Out.ViewDir = vecViewPos - mul(In.Pos, matWorld);                        // Calculate a vector from the vertex to the view, for specular  
  
    float3 worldPos = mul(In.Pos, matWorld);                                 // Transform the vertex to world space
    Out.Fog = 1 - (distance(worldPos, vecViewPos) - vecFog.x) * (vecFog.z);  // Calculate Fog
    return Out;
}

float4 MAIN_PS( PS_INPUT In ) : COLOR 
{
    In.LightVec.xyz = normalize(In.LightVec.xyz);                            // normalize light pos xyz, but not w (range) 
    In.Normal = normalize(In.Normal);
    In.ViewDir = normalize(In.ViewDir);

    float4 LightDir = normalize(vecSkill5);                                  // Normalize spotlight direction

    float4 Attn = 1 - saturate(dot(In.LightVec.w, In.LightVec.w));           // Calculate attenuation
    float4 Color = tex2D( BaseTex, In.Tex );                                 // Base texture lookup
 
    float projIntensity = saturate(dot(-In.LightVec, In.Normal));            // The more perpendicular the polygon to the light, the more light PPL
    float4 projColor = In.Tex1.w < 0.0 ? 0.0 : tex2Dproj( ProjTex, In.Tex1 ); // look up projection texture only in facing dir (avoid back proj)
    float4 proj = projColor * projIntensity;                                 // multiply the proj color with the amount of intensity

    float3 R = normalize(2 * dot(In.Normal, -LightDir) * In.Normal + LightDir); // Calculate the reflection vector:
    float Specular = 0;//(pow(saturate(dot(R, In.ViewDir)), SpecularPower) * SpecularIntensity);    // Calculate the speculate component only in spotlight

    Specular *= saturate(proj);                                              // only allow specular in projection area

    return (Color * vecAmbient) +  Color * ((proj + Specular)* Attn);        // final color
}

//////////////////////////////
// ADD POINTLIGHT
//////////////////////////////
float4 vecLightPos[8];
float4 vecLightColor[8];
float4x4 matWorldInv;	
struct PointOut
{
	float4 Position     : POSITION;
	float2 Tex0    : TEXCOORD0; 
	float4 Color		: COLOR0; 
	float  Fog     : FOG;

};
PointOut PointVS(float4 inPos : POSITION, float2 inTex0 : TEXCOORD0,float3 inNormal: NORMAL0)
{
	PointOut Out;
	Out.Position = mul(inPos, matWorldViewProj);
	Out.Tex0 = inTex0;
	Out.Color        = float4(0,0,0,0);
	//Add point lights
	for(int i = 0; i <= 5; i++)
	{
		// Diffuse lighting
		float4 objLightPos  = mul(float4(vecLightPos[i].xyz, 1), matWorldInv);
		float4 objLightDir  = objLightPos - inPos;
		float4 objLightDirN = normalize(objLightDir);
		float  diffLight    = max(dot(objLightDirN, inNormal), 0);
		
		// Calculate attenuation factor
		float falloffFactor = 0;
		if(vecLightPos[i].w > 0)
		{
			float linearDistance = length(objLightDir)/vecLightPos[i].w;
			if(linearDistance < 1) falloffFactor = 1 - linearDistance;
		}
		diffLight *= falloffFactor;
		
		// Add to final result
		Out.Color.rgb  += vecLightColor[i].rgb * diffLight;
	}
	float3 worldPos =  mul(inPos, matWorld);
	Out.Fog = 1 - (distance(worldPos, vecViewPos) - vecFog.x) * (vecFog.z);
	return Out;
}
float4 PointPS(PointOut In) : COLOR0
{
	float4 FinalColor;
	FinalColor = tex2D(BaseTex,In.Tex0);
	FinalColor *=(In.Color)*3;
	return FinalColor;
}
technique PP_Spot
{
	pass p0 
	{
		VertexShader = compile vs_1_1 MAIN_VS();
		PixelShader  = compile ps_2_0 MAIN_PS();
	}
	pass P1
	{
		alphablendenable=true;
		srcblend=one;
		destblend=one;
		VertexShader = compile vs_2_0 PointVS();
		PixelShader = compile ps_2_0 PointPS();
	}
}



So, but the shader I need, needs:
  • Pointlights
  • Use Models Ambient Value to make Object brighter
  • Static Lights (pre-compiled with wed)
  • Let Translucent Objects Translucent
  • Let Invisible Objects Invisible
  • Should apply to all objects
  • Not be tooo slow...not so important, A8 Pro will make with BSP faster laugh
  • Size should be adjustable
  • Lightrange should be adjustable
  • Make use of FOG


Thanks for any input laugh

I know it is really, really, really much
what I want, but I am such a noob at
programming shaders...


Thanks for advance and best regards,
Rei
Posted By: Rei_Ayanami

Re: [REQ] Spotlight Shader - 06/11/10 14:41

I know this would require much much work ...


But if anyone tries it would be nice laugh
Posted By: bart_the_13th

Re: [REQ] Spotlight Shader - 06/12/10 08:59

Adapted from XDiv10's projected texture (http://www.opserver.de/ubb7/ubbthreads.php?ubb=showflat&Number=315503&page=1)
Code:
//Apply this material to levelblocks
MATERIAL* mtl_pTexBlock = 
{
	effect = "
	////////////////////////////////////////////////////////
	// Projected texture (FFP) by Xd1Vo
	// Adapted for level blocks by bart_the_13th
	////////////////////////////////////////////////////////
	
	float4x4 matEffect3;
	
	texture mtlSkin1;
	texture entSkin1;
	texture entSkin2;
	texture StencilMap;//I was planning to modulate the flashlight to the stencilmap too, but my A7E version doesnt allow it :P
	
	technique proj_tex
	{
	
		pass p0
		{
			Texture[0] = <entSkin1>; // texture
			Texture[1] = <entSkin2>; // light map
			Texture[2] = <mtlSkin1>; // projected shadow/flashlight
			
			
			FogEnable=1;
			ColorArg1[0] = Texture;  
			ColorArg2[0] = Diffuse;
			ColorOp[0]   = Modulate;  //use selectArg1 if not using dynamic lights
			ResultArg[0] = Temp;      //Temp = texture + diffuse
	
			ColorArg1[1] = Texture;
			ColorArg2[1] = Current;
			ColorOp[1]   = Modulate2X;
			
			ColorArg1[2] = Texture;
			ColorArg2[2] = Current;
			ColorOp[2]   = Add;
			magfilter[2] = linear;
			minfilter[2] = linear;
			mipfilter[2] = linear;
			
			AddressU[2] = BORDER;
			AddressV[2] = BORDER;
			BorderColor[2] = 0x000001;  
			
			texcoordindex[2] = cameraspaceposition ; 
			TextureTransformFlags[2] = count3 |projected;
			TextureTransform[2] = <matEffect3>;
	
			ColorArg1[3] = temp;     // (shadow map + vertex lighting)
			ColorArg2[3] = Current;  // (color map + detail map)
			ColorOp[3]   = Modulate2X; // modulate (color map + detail map) with (shadow map + vertex lighting)
			magfilter[3] = linear;
			minfilter[3] = linear;
			mipfilter[3] = linear;
		}
		
	}
	";
}

/* Add these lines to main function

if(flashlite!=NULL)
{
	create_dxmat(mat_effect3,flashlite.x,flashlite.pan,focus,mtl_pTexBlock.skin3); //eagle
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////
*/


Posted By: Rei_Ayanami

Re: [REQ] Spotlight Shader - 06/12/10 12:26

Thanks laugh

But I don't know how apply it...


At the moment I just apply it to the camera.material , and I use this code for adapting:
Code:
//
// By Joseph Duffey Jan 2008
// Edited by Dennis van den Broek Feb 2010
// For the GameStudio Community
//
// Projective Texturing Spotlight
#define PRAGMA_PATH "GFX"
#define PRAGMA_PATH "Scripts"

BMAP* projectiontex = "Spotlight.jpg"; // texture to be projected
var flashlight_energy;
var flashlight_stat;
//=======================================================================================
// MATERIAL FOR OBJECTS LIT BY SPOTLIGHT
//=======================================================================================

MATERIAL* mtlSpotlit =
{
	ambient_red = 18;			   // The ambient color - a dark grey.
	ambient_green = 18;
	ambient_blue = 18;
	skin1 = projectiontex;
	effect = "PT_Spot.fx";	// The effect file containing the vertex shader, pixel shader and technique.
	flags |= OVERRIDE;
}

MATERIAL* mtlSpotlitObj =
{
	ambient_red = 100;			   // The ambient color - a dark grey.
	ambient_green = 100;
	ambient_blue = 100;
	skin1 = projectiontex;
	effect = "PT_Spot.fx";	// The effect file containing the vertex shader, pixel shader and technique.
	flags |= OVERRIDE;
}

MATERIAL* mtlNoSpotLight =
{
	effect = "technique DepthTechnique { pass p0 {	}}";
	flags = OVERRIDE;
}

//=======================================================================================
// PROJECTION
//=======================================================================================

MATERIAL* mtlProjView =               
{
effect = "technique DepthTechnique { pass p0 {	}}";   // must have an effect or crash!
	event = mtlProjection_event;     // transform and adjust views view matrix
	flags = ENABLE_VIEW;		         // call the event before view rendering
}

VIEW* ProjectionView =              // Put at projectors position during rendering to get its view matrix
{
	material = mtlProjView;
	flags = SHOW | UNTOUCHABLE; 
}

function mtlProjection_event()  // adjust matrices to size of bitmaps and pass to "matMtl" and "matEffect"
{
	// create a transformation matrix 
	float pTexAdj[16] = { 0.5, 0.0, 0.0, 0.0, 0.0,-0.5,	0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0 }; 
	// Adjust projection matrix for texture size 
	pTexAdj[12] = 0.5 + (0.5/(float)bmap_width(projectiontex)*2.0);// store bitmap diminsions into matrix
	pTexAdj[13] = 0.5 + (0.5/(float)bmap_height(projectiontex)*2.0);
	// Store the transformation matrix to the materials then adjust the matrix with the bitmap size 
	mat_set(mtlSpotlit.matrix,matViewProj);	  // store the transformation matrix to the material
	mat_multiply(mtlSpotlit.matrix,pTexAdj);  // adjust the matrix with the bitmap size
	mat_set(mtlSpotlitObj.matrix,matViewProj);	  // store the transformation matrix to the material
	mat_multiply(mtlSpotlitObj.matrix,pTexAdj);  // adjust the matrix with the bitmap size
}

//=======================================================================================
// SPOTLIGHT ACTION
//=======================================================================================

function Spotlight()
{
	diag_var("\nSpotlight started at %.3f\n", total_ticks/16);
	SOUND* flashlight_snd = "flashlight.ogg";
	VECTOR temp_vec;  // for temporary vectors
	VECTOR temp_vec2, temp_vec3;
	ENTITY* temp_ent = ent_create(NULL, nullvector, NULL);
	ProjectionView.aspect = 0.77;       // adjust views aspect to desired ratio, squash or stretch 
	wait(10); 
	mtlSpotlit.skill4 = floatv(1.0); // spotlights range
	mtlSpotlitObj.skill4 = floatv(1.0); // spotlights range
	while(!player) wait(1);
	while(player)
	{
		// move me with keyboard keys
		//c_move(me, vector((key_w - key_s) * time_step * 8,0,0), nullvector, IGNORE_PASSABLE | GLIDE);
		//c_rotate(me, vector((key_cul - key_cur)*time_step*8, (key_cuu-key_cud)*time_step*8,0), IGNORE_PASSABLE);
		
		// Put projection view at my pos and dir
		if(mouse_mode != 1)
		{
			vec_set(temp_ent.x, camera.x);
			temp_ent.z -= player.skill100*1.5;
			vec_set(temp_ent.pan, camera.pan);
			c_move(temp_ent, vector(0,-10,0), nullvector, IGNORE_MODELS | IGNORE_MAPS);
			vec_set(ProjectionView.x, temp_ent.x);
			vec_set(ProjectionView.pan, camera.pan);
		}
		else
		{
			vec_set(temp_ent.x, camera.x);
			temp_ent.z -= player.skill100*1.5;
			vec_set(temp_ent.pan, camera.pan);
			vec_set(ProjectionView.x, temp_ent.x);
			temp_vec2.x = mouse_pos.x;
			temp_vec2.y = mouse_pos.y;
			temp_vec2.z = 50000;
			vec_for_screen(temp_vec2, camera);
			you = player;
			c_trace(camera.x, temp_vec2, IGNORE_YOU);
			vec_set(temp_vec3, target.x);
			vec_sub(temp_vec3, camera.x);
			vec_to_angle(ProjectionView.pan, temp_vec3);
		}
		
		// send my position to shader (vecSkill1.xyzw)
		mtlSpotlit.skill1 = floatv(camera.x);
		mtlSpotlit.skill2 = floatv(camera.z-player.skill100*1.5); // z and y must be reversed!
		mtlSpotlit.skill3 = floatv(camera.y);
		mtlSpotlitObj.skill1 = floatv(camera.x);
		mtlSpotlitObj.skill2 = floatv(camera.z-player.skill100*1.5); // z and y must be reversed!
		mtlSpotlitObj.skill3 = floatv(camera.y);
		if(flashlight_stat == 1)
		{
			if(flashlight_energy > 500)
			{
				mtlSpotlit.skill4 = floatv(2000); // spotlights range
				mtlSpotlitObj.skill4 = floatv(2000); // spotlights range
			}
			else
			{
				if(flashlight_energy > 0)
				{
					mtlSpotlit.skill4 = floatv(2000-flashlight_energy/2); // spotlights range
					mtlSpotlitObj.skill4 = floatv(2000-flashlight_energy/2); // spotlights range
				}
				else
				{
					flashlight_stat = 0;
					mtlSpotlit.skill4 = floatv(1.0); // spotlights range
					mtlSpotlitObj.skill4 = floatv(1.0); // spotlights range
				}
			}
			flashlight_energy -= time_step/2;
		}
		if(key_f)
		{
			if(flashlight_energy > 0)
			{
				if(flashlight_stat == 1) 
				{
					mtlSpotlit.skill4 = floatv(1.0); // spotlights range
					mtlSpotlitObj.skill4 = floatv(1.0); // spotlights range
					player.skill1 = 0;
					flashlight_stat = 0;
				}
				else 
				{
					if(flashlight_energy > 500)
					{
						mtlSpotlitObj.skill4 = floatv(2000); // spotlights range
						mtlSpotlit.skill4 = floatv(2000); // spotlights range
					}
					else
					{
						if(flashlight_energy > 0)
						{
							mtlSpotlit.skill4 = floatv(2000-flashlight_energy/2); // spotlights range
							mtlSpotlitObj.skill4 = floatv(2000-flashlight_energy/2); // spotlights range
						}
						else
						{
							flashlight_stat = 0;
							mtlSpotlit.skill4 = floatv(1); // spotlights range
							mtlSpotlitObj.skill4 = floatv(1); // spotlights range
						}
					}
					player.skill1 = 1;
					flashlight_stat = 1;
					snd_play(flashlight_snd,100, 0);
				}
				while(key_f) 
				{
					// Put projection view at my pos and dir
					if(mouse_mode != 1)
					{
						vec_set(temp_ent.x, camera.x);
						vec_set(temp_ent.pan, camera.pan);
						temp_ent.z -= player.skill100*1.5;
						c_move(temp_ent, vector(0,-10,0), nullvector, IGNORE_MODELS | IGNORE_MAPS);
						vec_set(ProjectionView.x, temp_ent.x);
						vec_set(ProjectionView.pan, camera.pan);
					}
					else
					{
						vec_set(temp_ent.x, camera.x);
						vec_set(temp_ent.pan, camera.pan);
						temp_ent.z -= player.skill100*1.5;
						vec_set(ProjectionView.x, temp_ent.x);
						temp_vec2.x = mouse_pos.x;
						temp_vec2.y = mouse_pos.y;
						temp_vec2.z = 50000;
						vec_for_screen(temp_vec2, camera);
						you = player;
						c_trace(camera.x, temp_vec2, IGNORE_YOU);
						vec_set(temp_vec3, target.x);
						vec_sub(temp_vec3, camera.x);
						vec_to_angle(ProjectionView.pan, temp_vec3);
					}
					
					// send my position to shader (vecSkill1.xyzw)
					mtlSpotlit.skill1 = floatv(camera.x);
					mtlSpotlit.skill2 = floatv(camera.z-player.skill100*1.5); // z and y must be reversed!
					mtlSpotlit.skill3 = floatv(camera.y);
					vec_for_angle(temp_vec.x, vector((camera.pan), camera.tilt, camera.roll)); //turn my angles into a dir vector
					mtlSpotlit.skill5 = floatv(temp_vec.x);
					mtlSpotlit.skill6 = floatv(temp_vec.z); // z and y must be reversed!
					mtlSpotlit.skill7 = floatv(temp_vec.y);
					wait(1);
				}
			}
		}
		
		// send my direction to shader (vecSkill2.xyzw)
		vec_for_angle(temp_vec.x, ProjectionView.pan); //turn my angles into a dir vector
		mtlSpotlit.skill5 = floatv(temp_vec.x);
		mtlSpotlit.skill6 = floatv(temp_vec.z); // z and y must be reversed!
		mtlSpotlit.skill7 = floatv(temp_vec.y);
		mtlSpotlitObj.skill5 = floatv(temp_vec.x);
		mtlSpotlitObj.skill6 = floatv(temp_vec.z); // z and y must be reversed!
		mtlSpotlitObj.skill7 = floatv(temp_vec.y);
		
		wait(1);
	}
}

//=======================================================================================
// ACTION FOR OBJECTS LIT BY SPOTLIGHT
//=======================================================================================

action Spotlit_object()
{
	my.material = mtlSpotlit;
}

//=======================================================================================
// FUNCTION FOR SETTING STAGE RENDERING
//=======================================================================================

// this should be done immediatly after level load
function init_stages()
{
	ProjectionView.stage = camera; // so projection is not lagging one frame behind
}




Can you please help me again?

It is already sooo great that you helped me till now laugh

Best regards,
Rei
Posted By: Rei_Ayanami

Re: [REQ] Spotlight Shader - 06/12/10 22:24

C'mon guys...

Do you really want to tell me bart_the_13th is the only one who can help me?
Posted By: bart_the_13th

Re: [REQ] Spotlight Shader - 06/13/10 03:36

Well, you can apply it by include the code into your main script and select all blocks and apply it to the level block.
Posted By: Rei_Ayanami

Re: [REQ] Spotlight Shader - 06/13/10 10:08

Thanks for answering laugh


I will try that laugh
Posted By: bart_the_13th

Re: [REQ] Spotlight Shader - 06/14/10 09:16

Oh, I forgot to mention that since that shader is an adaptation from XD1v0, you still need the projMain.c from this wiki:
http://www.opserver.de/wiki/index.php/ProjectionTexture
Posted By: Rei_Ayanami

Re: [REQ] Spotlight Shader - 06/15/10 17:06

Thanks, I got it working. laugh

But I am having a problem:

Why the light is not in the middle of the screen (when setting the pan as camera.pan and the pos to camera.x) ?

It is at the lower right blue line, and not in the middle of them all...

Thanks in advance,
Rei
Posted By: Grafton

Re: [REQ] Spotlight Shader - 09/09/10 21:35

Oops, sorry, my bad.
Doh, I should have read the whole post before replying.
Posted By: EvilSOB

Re: [REQ] Spotlight Shader - 09/09/10 23:17

Hi Rei.

Give this one a try... Its VERY MUCH a work-in-progress though.
Its just a pixel shader so all you do is attach it to the camera and set
up a few material skills.

This STAND-ALONE demo should explain all you need.
Just give it a level in the start of "main()" and run it...

Code:
#include <acknex.h>
#include <default.c>

MATERIAL*	mTorch =
{	effect = "
	Texture TargetMap;	
	float4 vecViewPort, vecSkill1, vecSkill5, vecSkill9;
	sampler2D TexMap = sampler_state	{	Texture = <TargetMap>;	};
	//---------------------------------------------------------------------------------------
	//---------------------------------------------------------------------------------------
	float4 PS_Torch(	float2 tex:TEXCOORD0	):COLOR
	{
		float	 ratio = vecViewPort.x/vecViewPort.y;
		float2 dir   = vecSkill1.xy - tex;
		float  r_dist  = sqrt( pow(dir.x*ratio, 2) + pow(dir.y,2) ) ;
		float4 color = lerp(0, tex2D(TexMap,tex), saturate((r_dist-vecSkill1.w)*vecSkill5.w));
		return saturate(tex2D(TexMap,tex) + color * vecSkill5/64);
	} 
	//---------------------------------------------------------------------------------------
	technique Torch {	pass p0	{	PixelShader = compile ps_2_0 PS_Torch();		}	}
	//---------------------------------------------------------------------------------------
			";
}
VIEW*		vTorch =	{	material = mTorch;		flags=CHILD|PROCESS_TARGET;		}


function main()
{	wait(1);	level_load("testlevel.wmb");	wait(5);
	
	//Position and Size Settings
	var density=0.5, radius=0.35, mouse_x=0, mouse_y=0;
	mTorch.skill1 = floatv(mouse_x);	//X_POS   == focus X position	(range  0 to 1)
	mTorch.skill2 = floatv(mouse_y);	//Y_POS   == focus Y position	(range  0 to 1)
	mTorch.skill3 = floatv(density);	//DENSITY == small, around 0.5	(range -1 to 1)
	mTorch.skill4 = floatv(radius);	//RADIUS  == small, around 0.2	(range  0 to 1)
	

	//Contrast color Settings
	var speed=-15;
	mTorch.skill5 = floatv(125);		//Torch "contrast" color Red component
	mTorch.skill6 = floatv(125);		//Torch "contrast" color Green component
	mTorch.skill7 = floatv(125);		//Torch "contrast" color Blue component
	mTorch.skill8 = floatv(speed);	//FADESPEED == around 0.25		(range  0 to 5)
	
	while(1)
	{
		if(key_space)	camera.stage = NULL;			//hold space to DISABLE torch
		else				camera.stage = vTorch;

		mouse_x = clamp(mouse_cursor.x, 0, screen_size.x)/screen_size.x;
		mouse_y = clamp(mouse_cursor.y, 0, screen_size.y)/screen_size.y;
			DEBUG_VAR( mouse_x, 50);
			DEBUG_VAR( mouse_y, 70);

		if(key_cuu)	density += 0.01;
		if(key_cud)	density -= 0.01;
			DEBUG_VAR( density, 	90);

		if(key_cur)	radius  += 0.01;
		if(key_cul)	radius  -= 0.01;
			DEBUG_VAR( radius, 		110);

		mTorch.skill1 = floatv(mouse_x);
		mTorch.skill2 = floatv(mouse_y);
		mTorch.skill3 = floatv(density);
		mTorch.skill4 = floatv(radius);

		if(key_pgup)	speed  += 0.01;
		if(key_pgdn)	speed  -= 0.01;
			DEBUG_VAR( speed, 		150);
		mTorch.skill8 = floatv(speed);

		wait(1);
	}
}


Posted By: Rei_Ayanami

Re: [REQ] Spotlight Shader - 09/10/10 14:13

Thanks EvilSOB,

it is really easy to understand. I will implant it in the game to see how much faster it will be laugh

I will give you a feedback as soon as I am ready laugh

Thanks again and best regards,
Rei
Posted By: Rei_Ayanami

Re: [REQ] Spotlight Shader - 09/11/10 11:23

Hiya,

I really like it, because it is fast. But I also have a problem:

As it takes the before rendered camera view, it does not make black (or really dark) parts brighter. As there are scenes in the game, where nearly everything should be black (of course not the flash-lighted parts), this is not so good.

Another problem I have, is that I get problems when changing the resolutions. I have no idea what the problem could be..


So thank you very much and I'd like to know which improvements you want to make laugh

Best regards,
Rei
Posted By: EvilSOB

Re: [REQ] Spotlight Shader - 09/12/10 04:57

I agree on both points, but cant do anything about them.
I just hoped this "simplistic" shader would be 'good enough'.
A PP shader can only work with the rendered result, and therefore cannot
'extract' the real colors from an 'un-lit' or darkened view.
(UNLESS the PP shader is "creating" the darkness, rather than creating a light)
Ah well... That never looks to good IMHO anyway...


But I must question why arent you just using a dynamic-light for the torch?
Like this?

Code:
ENTITY* torch = NULL;

void torch_startup()
{
	while(!level_ent)	wait(1);		//wait till level is loaded.
	if(torch==NULL)	{	torch = me = ent_create(0,0,0);	}
	//
	//  torch DEFAULT settings
	my.skill1 = 1;			//default to torch ON
	my.skill2 = 1000;		//default to lightrange 1000
	//
	set(me, PASSABLE|INVISIBLE|SPOTLIGHT);
	vec_set(d3d_spotlightcone, vector(15,15,1));	//set torch cone-shape
	while(torch)
	{
		if(my.skill1==1)	 my.lightrange = my.skill2;
		else					 my.lightrange=0;
		//
		vec_set(my.x, camera.x);		//follow camera position
		vec_set(my.pan, camera.pan);	//follow camera direction
		//
		wait(1);
	}
}



The problem with the center-position moving on screen-changes,
I THINK, is more my demo code rather than my shader.
(But thats beside the point now)
Posted By: bart_the_13th

Re: [REQ] Spotlight Shader - 09/12/10 08:48

Maybe you can add the spotlight shader and the stencil buffer to create shadow. Or maybe combine it with shadow mapping shader.
He didnt want to use the dynamic light with spotlight flag because it still use tesselation wink
© 2024 lite-C Forums