Jump to content

Traffic light "switch" model in Editor


njord
 Share

Recommended Posts

Hello comrades.

 

As you know, there is a traffic light model called "Switch" in Editor files. I can load it in C++ and use setkey functions to make it glow red or green.

I extended its functionality to use it in car lights, brakelights, signals etc (thanks to Flexman's answer before forum breakdown).

 

However, since (correct me if I'm wrong) only entity color is unique across hardware instances of a mesh, color is used as the parameter to set a switch's glowing state.

so in switch's lua script there is this chunk of code :

 

		if self.active==1 then
		self.model:SetColor(Vec4(1,0,0,0),1)
	else
		self.model:SetColor(Vec4(0,0,0,0),1)
	end

 

and in switch.frag shader :

 

gl_FragData[3] = texture2D(texture2,texcoord)*fragcolor.x + texture2D(texture3,texcoord)*(1.0-fragcolor.x);

 

which uses x (Red) component of the entity color as a ratio variable to either make texture2 or texture3 highlighted areas of the model "glowing".

 

Lets say switch is a truck. "Red light" state turns its headlights glowing on, where "Green light" state turns its rearlights glowing on. Also I have a white spotlight entity attached to it. However each time I change the truck's color,I get the spot light painted in its parent's color. I tried to disable recursion when setting color by changing

 

self.model:SetColor(Vec4(1,0,0,0),1)

 

to

 

self.model:SetColor(Vec4(1,0,0,0),0)

 

but this did not help. Actually both headlights and rearlights on the truck start glowing without regards to color parameter when "self.model:SetColor(Vec4(1,0,0,0),0)" is used.

 

Summing it up : I still want to control which texture to glow on my model, but I dont want my attached light entities get affected by their parent color.

 

Thanks in advance!

 

PS : I had this same getting painted problem with transparent surfaces on my vehicle too (not only child light entities).

Here is my vehicle lights frag shader and vehicle lua script. I tried sending the state parameters only via alpha channel where color is always set to 1 1 1. That way at least my lights stay white all the time.

 

frag shader :

#define LW_DIFFUSE texture0
#define LW_BUMPMAP texture1
#define LW_BLOOM texture2
uniform sampler2D texture3;
uniform sampler2D texture4;
uniform sampler2D texture5;

#extension GL_ARB_draw_buffers : enable

uniform vec3 cameraposition;
uniform vec2 buffersize;
uniform vec2 camerarange;

include "abstract::greyscale.txt"
include "abstract::DepthToZPosition.frag"

#ifdef LW_DETAIL
uniform sampler2D LW_DETAIL;
#endif

#ifdef LW_DIFFUSE
uniform sampler2D LW_DIFFUSE;
#endif

#ifdef LW_SPECULARMAP
uniform sampler2D LW_SPECULARMAP;
#endif

#ifdef LW_BUMPMAP
uniform sampler2D LW_BUMPMAP;
#endif

#ifdef LW_BLOOM
uniform sampler2D LW_BLOOM;
#endif

#ifdef LW_CUBEMAP
uniform samplerCube LW_CUBEMAP;
#endif

#ifdef LW_PARALLAXMAP
uniform sampler2D LW_PARALLAXMAP;
varying vec3 eyevec;
#endif

#ifdef LE_REFRACTION
uniform sampler2D LE_REFRACTION;
uniform sampler2D LE_DEPTHBUFFER;
uniform float refractionstrength = 0.01;
#endif

#ifdef LW_POMMAP
vec3 vLightTS=vec3(0.577,0.577,0.577);
varying vec3 eyevec;
float depthP = .15;
float nMinSamples = 20;
float nMaxSamples = 50;	
#endif

#ifdef LW_ALPHABLEND
uniform sampler2D LW_ALPHABLEND_INCOMINGCOLOR;
uniform sampler2D LW_ALPHABLEND_INCOMINGNORMAL;
#endif

varying vec3 vertexposition;
varying vec3 T,B,N;
varying vec2 texcoord0;
varying vec2 texcoord1;
varying vec3 cubecoord;
varying vec4 modelvertex;
varying vec4 fragcolor;

float fOcclusionShadow = 1.0;

uniform sampler2D texture14;
uniform float terrainsize;
uniform vec3 terrainscale;
uniform float bumpscale;
uniform float specular;
uniform float gloss;

//Terrain color map
uniform sampler2D texture12;

void main(void) {
vec4 diffuse = vec4(1);//fragcolor;
vec3 normal;
float shininess = 0.0;
vec2 texcoord=texcoord0;// only use this because of parallax mapping

vec2 terraincoord;
float terrainresolution;

#ifdef LW_PARALLAXMAP
	texcoord += (texture2D(LW_PARALLAXMAP,texcoord).x * 0.04 - 0.036) * normalize(eyevec).xy;
#endif

#ifdef LW_POMMAP
	// for POM, the heightmap is in the alpha of the diffuse so save ur diffuse with DXT5 I chose this because the alpha of DXT5 is higher precision
	//Include "POM.frag"
#endif

#ifdef LW_DIFFUSE
	diffuse *= texture2D(LW_DIFFUSE,texcoord)*fOcclusionShadow;
#endif

#ifdef LW_ALPHATEST
	if (diffuse.w<0.5) {
		discard;
	}
#endif


normal = N;

#ifdef LW_BUMPMAP

	#ifdef LW_TERRAINNORMALS
		//Use terrain normals
		terraincoord=vec2(vertexposition.x,-vertexposition.z) / terrainsize + 0.5;
		terrainresolution = terrainsize / terrainscale.x;
		terraincoord += 0.5 / terrainresolution;
		vec3 worldNormal = ((texture2D(texture14,terraincoord).xyz - 0.5) * 2.0).xyz;
		normal = normalize(gl_NormalMatrix*worldNormal);
	#else
		vec4 bumpcolor = texture2D(LW_BUMPMAP,texcoord);
		normal = bumpcolor.xyz * 2.0 - 1.0;
	#endif

	#ifdef LW_DETAIL
		normal += texture2D(LW_DETAIL,texcoord * 4.0).xyz * 2.0 - 1.0;
	#endif
	normal.z /= bumpscale;
	normal = T * normal.x + B * normal.y + N * normal.z;
	normal = normalize(normal);
	#ifdef LW_SPECULAR
		shininess = bumpcolor.a*fOcclusionShadow*specular;
	#endif
	#ifdef LW_SPECULARMAP
		shininess = texture2D(LW_SPECULARMAP,texcoord).x*fOcclusionShadow*specular;
	#endif
#endif

#ifdef LW_TERRAINNORMALS
	#ifndef LW_BUMPMAP
		//Use terrain normals
		terraincoord=vec2(vertexposition.x,-vertexposition.z) / terrainsize + 0.5;
		terrainresolution = terrainsize / terrainscale.x;
		terraincoord += 0.5 / terrainresolution;
		//vec4 normsample=((texture2D(texture14,terraincoord).xyz - 0.5) * 2.0).xyz;
		vec3 worldNormal = ((texture2D(texture14,terraincoord).xyz - 0.5) * 2.0).xyz;
		normal = normalize(gl_NormalMatrix*worldNormal);
		//shininess = normsample.w;
	#endif
#endif

#ifdef LW_TERRAINCOLOR
	//Use terrain color
	terraincoord=vec2(vertexposition.x,-vertexposition.z) / terrainsize + 0.5;
	terrainresolution = terrainsize / terrainscale.x;
	terraincoord += 0.5 / terrainresolution;
	vec4 terraincolor = texture2D(texture12,terraincoord);
	diffuse = vec4( greyscale(diffuse.xyz) * 2.0 * terraincolor.xyz,diffuse.w);
	shininess = terraincolor.w;
#endif

#ifdef LE_REFRACTION
	diffuse.a=0.25;
	vec4 refractionvector = vec4( gl_FragCoord.x/buffersize.x, gl_FragCoord.y/buffersize.y, gl_FragCoord.z, 1.0 );
	vec4 refractionvector2 = refractionvector + refractionstrength * vec4(normal,0.0);		
	if (gl_FragCoord.z<DepthToZPosition(texture2DProj(LE_DEPTHBUFFER,refractionvector2).x)) {
		refractionvector=refractionvector2;
	}
	vec4 transparency = texture2DProj(LE_REFRACTION,refractionvector);
	diffuse = transparency * diffuse;
#endif

vec3 adjustednormal = normal*0.5+0.5;
float adjustedgloss = gloss;

gl_FragData[0] = diffuse;
gl_FragData[1] = vec4(adjustednormal,1.0);
gl_FragData[2] = vec4(shininess,gloss,0.0,diffuse.w);


float lights = floor((fragcolor.w+0.49f) / 8.0f);
float brakes = floor((fragcolor.w+0.49f) / 4.0f) - lights*2.0f;
float leftsignals = floor((fragcolor.w+0.49f) / 2.0f) - lights*4.0f - brakes*2.0f;
float rightsignals = floor(fragcolor.w+0.49f) - lights*8.0f - brakes*4.0f - leftsignals*2.0f;
gl_FragData[3] = texture2D(texture2,texcoord)*lights + texture2D(texture3,texcoord)*brakes + texture2D(texture4,texcoord)*leftsignals + texture2D(texture5,texcoord)*rightsignals;


//gl_FragData[3] = texture2D(texture2,texcoord)*fragcolor.x + texture2D(texture3,texcoord)*fragcolor.y + texture2D(texture4,texcoord)*fragcolor.z + texture2D(texture5,texcoord)*fragcolor.w;

gl_FragData[3].w = 0.0;
}

 

lua script:

require("scripts/class")

local class=CreateClass(...)

function class:InitDialog(grid)
self.super:InitDialog(grid)
group=grid:AddGroup("Chevrolet_Blazer")
group:AddProperty("lights", PROPERTY_FLOAT, "0|0.0,5","lights")
group:AddProperty("brakelights", PROPERTY_FLOAT, "0|0.0,5","brakelights")
group:AddProperty("leftsignals", PROPERTY_FLOAT, "0|0.0,5","leftsignals")
group:AddProperty("rightsignals", PROPERTY_FLOAT, "0|0.0,5","rightsignals")
group:Expand(1)
end

function class:CreateObject(model)
local object=self.super:CreateObject(model)

object.lights = 0
object.brakelights = 0
object.leftsignals = 0
object.rightsignals = 0

function object:UpdateLights()
	--self.model:SetColor( Vec4(self.lights, self.brakelights, self.leftsignals, self.rightsignals), 1 )
	self.model:SetColor( Vec4(1, 1, 1, 8 * self.lights + 4 * self.brakelights + 2 * self.leftsignals + self.rightsignals), 1 )
end

function object:SetKey(key,value)
	if key=='color' then --overrides color setting
	elseif key=='lights' then
		self.lights = tonumber(value)
		if self.lights < 1.0 and self.lights > 0.0 then
			self.lights = 1.0
		end
	elseif key=='brakelights' then
		self.brakelights = tonumber(value)
		if self.brakelights < 1.0 and self.brakelights > 0.0 then
			self.brakelights = 1.0
		end
	elseif key=='leftsignals' then
		self.leftsignals = tonumber(value)
		if self.leftsignals < 1.0 and self.leftsignals > 0.0 then
			self.leftsignals = 1.0
		end
	elseif key=='rightsignals' then
		self.rightsignals = tonumber(value)
		if self.rightsignals < 1.0 and self.rightsignals > 0.0 then
			self.rightsignals = 1.0
		end
	else
		return self.super:SetKey(key,value)
	end
	self:UpdateLights()
	return 1
end

object:UpdateLights()
end

Link to comment
Share on other sites

  • 2 weeks later...
  • 1 month later...

Very sorry for the late reply. I have a working traffic light model, its script and its shader working. If you want, I can send it to you or upload it somewhere.

 

To clarify my problem : I use color to pass parameters to different instances of a mesh. Specifically, I use the color parameter to switch between texture reads to make some parts of the model glow (emissive) in the dark.

 

My problem is, the lights attached to the mesh, and transparent surfaces of the mesh also get painted when i use color as a parameter. How can I setcolor of a mesh, but not set the color of a light attached to it ?

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...