Shaders’ Outpost

Discussion and information relevant to creating special missions, new ships, skins etc.

Moderators: winston, another_commander

User avatar
Griff
Oolite 2 Art Director
Oolite 2 Art Director
Posts: 2483
Joined: Fri Jul 14, 2006 12:29 pm
Location: Probably hugging his Air Fryer

Post by Griff »

Ahruman, is the uniform binding throwingSparks working OK?
i'm getting this error when trying to bind it to a shader:

Code: Select all

oolite.exe[2252] [shader.uniform.bind.failed]: Could not bind uniform "nearly_dead" to -[ShipEntity ?] (no uniform of that name could be found).
in shipdata.plist i've got:

Code: Select all

uniforms = { 
	time = "universalTime"; 
	engine_power = "speedFactor";
	laser_heat_level = "laserHeatLevel";
	nearly_dead = "throwingSparks"; 
	hull_heat_level = "hullHeatLevel";
	}; 


in the fragment shader i've got:

Code: Select all

uniform bool   nearly_dead;
and what i'm doing with it is:

Code: Select all

if (nearly_dead = true)
      { 
      color += cyanGlow(effectsMap.r * Pulse(min(engine_power, 1.0), 1.0)) * Blink_on_off(Pulse(1.0, 1.0));
      color += effectsMap.a * diffuseMap * Blink_on_off(Pulse(1.0, 0.6));
      }  
   else 
      { 
       color += cyanGlow(effectsMap.r * Pulse(min(engine_power, 1.0), 1.0));
       color += effectsMap.a * diffuseMap * 3.0;
      }
that bit of code flickers the exhaust glow effects off and off a bit like how the exhaust plume flickers.

what i'm seeing in oolite is the flickering effect as if the nearly_dead bool is true, even though the ship is not in the throwingSparks state, what's odd is if i edit the shader and change if (nearly_dead = true) to if (nearly_dead = false) i don't see the flickering effect, so it's as if the if else stuff is being acted on.


Edit: fixed by Cmdr James, i had the shader code wrong

Also, does hullHeatLevel always have a slightly positive value even when ships are just milling around aimlessly in space?
i've added in the hull heat glow effect from the shady cobra to the griff_krait shader and now all the kraits are glowing slightly red, as if hullHeatLevel starts off at some value such as 0.1 - this is for non-player craft, i haven't tested it with a player ship
Image
Last edited by Griff on Thu May 08, 2008 8:09 pm, edited 5 times in total.
User avatar
Cmdr James
Commodore
Commodore
Posts: 1357
Joined: Tue Jun 05, 2007 10:43 pm
Location: Berlin

Post by Cmdr James »

Im not t all a javascript expert, but that isnt the way Id test the nearly_dead boolean in any other language.

I think it should be

Code: Select all

if (nearly_dead) {
and

Code: Select all

if(!nearly_dead) {
I think your

Code: Select all

if (nearly_dead = true)
is setting the value of nearlydead, rather than testing it.[/quote]
User avatar
Griff
Oolite 2 Art Director
Oolite 2 Art Director
Posts: 2483
Joined: Fri Jul 14, 2006 12:29 pm
Location: Probably hugging his Air Fryer

Post by Griff »

thanks Cmdr James! that fixed it, i changed the shader code to

Code: Select all

if (nearly_dead)
      { 
      color += cyanGlow(effectsMap.r * Pulse(min(engine_power, 1.0), 1.0)) * Blink_on_off(Pulse(1.0, 1.0));
      color += effectsMap.a * diffuseMap * Blink_on_off(Pulse(1.0, 0.6));
      }  
if (!nearly_dead) 
      { 
       color += cyanGlow(effectsMap.r * Pulse(min(engine_power, 1.0), 1.0));
       color += effectsMap.a * diffuseMap * 3.0;
      }
and it's working great!
User avatar
Frame
---- E L I T E ----
---- E L I T E ----
Posts: 1477
Joined: Fri Mar 30, 2007 8:32 am
Location: Witchspace

Post by Frame »

for reference in code terms

== test´s Equality

= sets varible to be the value that follows...

Not being an Java coder, but i bet this also works like this in java script, like it does in C++
Bounty Scanner
Number 935
User avatar
Cmdr James
Commodore
Commodore
Posts: 1357
Joined: Tue Jun 05, 2007 10:43 pm
Location: Berlin

Post by Cmdr James »

And of course, if its a boolean to start with, its a bit odd to test if its equal to true, as the test returns a boolean.

And, if you want to be pedantic, java is the same as C/C++/objective-C in the equality testing sense, but we are talking about javascript, which is no more like java than vb :p
User avatar
Eric Walch
Slightly Grand Rear Admiral
Slightly Grand Rear Admiral
Posts: 5536
Joined: Sat Jun 16, 2007 3:48 pm
Location: Netherlands

Post by Eric Walch »

Griff wrote:
Also, does hullHeatLevel always have a slightly positive value even when ships are just milling around aimlessly in space?
i've added in the hull heat glow effect from the shady cobra to the griff_krait shader and now all the kraits are glowing slightly red, as if hullHeatLevel starts off at some value such as 0.1 - this is for non-player craft, i haven't tested it with a player ship
At least until 1.70 there was just one ship temperature and that never dropped below a certain value of 60. A meaningless number. But in relative temperature with 1.0 as value were heat damage starts, this minimum value is 0.23 i don't know if 1.71 now also has a hull temperature different from cabin temperature.
User avatar
Griff
Oolite 2 Art Director
Oolite 2 Art Director
Posts: 2483
Joined: Fri Jul 14, 2006 12:29 pm
Location: Probably hugging his Air Fryer

Post by Griff »

ah, that explains it, thanks eric! with a hullheat level of 0.23 the ship has already started to begin glowing, i think a few tweaks to the shader should sort this.
edit i suppose just subtracting 0.23 away from the hullheatlevel in the shader code should do the trick, the max command should clamp it to 0.0 if the value falls below that

Code: Select all

max(hull_heat_level - 0.23, 0.0)
below is an image of the hull heat at 0.0, 0.23, 0.50, 0.75, 1.0
Image
seems to be working ok, haven't tried it in oolite yet, i've tied the hull heat effect into the specularity map just to break up the effect a bit, it doesn't look that great but i don't want to have to add another texture map just to control this effect
User avatar
JensAyton
Grand Admiral Emeritus
Grand Admiral Emeritus
Posts: 6657
Joined: Sat Apr 02, 2005 2:43 pm
Location: Sweden
Contact:

Post by JensAyton »

Griff wrote:
Does Oolite support Cubemaps? (textureCube)
No.
launch, there's something odd going on with the environment map though, it streaks a lot along some of the polygons, maybe something to do with the topology of the model?
That’s pretty much the expected result for flat surfaces. It works better on doughnuts. :-)
User avatar
JensAyton
Grand Admiral Emeritus
Grand Admiral Emeritus
Posts: 6657
Joined: Sat Apr 02, 2005 2:43 pm
Location: Sweden
Contact:

Post by JensAyton »

Cmdr James wrote:
And of course, if its a boolean to start with, its a bit odd to test if its equal to true, as the test returns a boolean.

And, if you want to be pedantic, java is the same as C/C++/objective-C in the equality testing sense, but we are talking about javascript, which is no more like java than vb :p
But we should be talking about GLSL, which also uses C syntax for tests and assignments. :-)

As for the heat glow thing, I’d strongly recommend using the same heat glow function as the default shader, so all ships behave the same:

Code: Select all

// uHullHeatLevel bound to hullHeatLevel
// uTime bound to timeElapsedSinceSpawn

#ifndef OO_REDUCED_COMPLEXITY
// Irregular flickering function.
float Pulse(float value, float timeScale)
{
	float t = uTime * timeScale;   
	
	float s0 = t;
	s0 -= floor(s0);
	float sum = abs( s0 - 0.5);
	
	float s1 = t * 0.7 - 0.05;
	s1 -= floor(s1);
	sum += abs(s1 - 0.5) - 0.25;
	
	float s2 = t * 1.3 - 0.3;
	s2 -= floor(s2);
	sum += abs(s2 - 0.5) - 0.25;
	
	float s3 = t * 5.09 - 0.6;
	s3 -= floor(s3);
	sum += abs(s3 - 0.5) - 0.25;
	
	return (sum * 0.1 + 0.9) * value;
}


// Colour ramp from black through reddish brown/dark orange to yellow-white.
vec4 TemperatureGlow(float level)
{
	vec4 result = vec4(0);
	
	result.r = level;
	result.g = level * level * level;
	result.b = max(level - 0.7, 0.0) * 2.0;
	result.a = 1.0;
	
	return result;	
}
#endif

// ...

#ifndef OO_REDUCED_COMPLEXITY
	// Heat glow
	float hullHeat = max(uHullHeatLevel - 0.5, 0.0) * 2.0;
	hullHeat = Pulse(hullHeat * hullHeat, 0.1);
	totalColor += TemperatureGlow(hullHeat);
#endif
User avatar
Griff
Oolite 2 Art Director
Oolite 2 Art Director
Posts: 2483
Joined: Fri Jul 14, 2006 12:29 pm
Location: Probably hugging his Air Fryer

Post by Griff »

well, everything works better with doughnuts, even a trumble infestation (if you can get to them before they do)
User avatar
Cmdr. Maegil
Sword-toting nut-job
Sword-toting nut-job
Posts: 1294
Joined: Tue Feb 27, 2007 10:28 pm
Location: On the mend in Western Africa

Post by Cmdr. Maegil »

YAH! YAH!
You know those who, having been mugged and stabbed, fired, dog run over, house burned down, wife eloped with best friend, daughters becoming prostitutes and their countries invaded - still say that "all is well"?
I'm obviously not one of them.
User avatar
Griff
Oolite 2 Art Director
Oolite 2 Art Director
Posts: 2483
Joined: Fri Jul 14, 2006 12:29 pm
Location: Probably hugging his Air Fryer

Post by Griff »

good point about the hull heat effect Ahruman, i've put your example code into the shader.

I've been playing about with the 'environment shader effect' a few posts above and worked it into a sort of 'Active Camouflage' effect that kicks in when a ship cloaks, basically the ships texturemap colour & specular effects are muted by multiplying them by 0, and a starfield texture is mapped onto the model instead, the starfild texture co-ords are being calculated using the vertex fragment position in eye space and LightSource[1].position.xyz, this gives the effect of the model moving 'through' it's texture rather than the texture being a static painting on the ships hull

Image
now you see me......................................now you dont (well except for the engine glow & hull lights)
it's downloadable here
http://www.box.net/shared/bmgecao84c

edit: hmm, thinking about it, a shader binding to 'hasHostileTarget' would probably work better than a shader binding to 'isCloaked'.
here's a version that does that: http://www.box.net/shared/pvc6p2dwsg fire a few shots into them to get them to switch to their camouflage mode
User avatar
Griff
Oolite 2 Art Director
Oolite 2 Art Director
Posts: 2483
Joined: Fri Jul 14, 2006 12:29 pm
Location: Probably hugging his Air Fryer

Post by Griff »

guys, i've got a programming question and i was wondering if some of you code wizzards could turn your mighty intellects towards the problem briefly, you don't have to go into detail just an overview of a good programming method would be cool, basically i'm trying to get a bit fancy with a shader effect, what i want to do is fade a texture in whilst fading another texture out depending on whether a bool is true or not.

this is what it've got so far, please feel free to point and laugh

Code: Select all

/* check if the ship is cloaked, if so:
   1. fade out the colour & lighting thus making the ship a non-specular-reflecting black colour and
   2. fade in the environment map as a replacement texture 
   The float 't' counts down and the float 'tt' counts up, the fract command throws away the integer and keeps only the
   numbers after the decimal point somewhere in the range of .9999 to .0000 */ 
   if (is_cloaked)
      { 
   float t = fract(-time * 0.5);
   float tt = fract(time * 0.5);
   vec4 c = color * t;
   color = vec4(c);
   color += envMap * tt * diffuseMap.a;
      }  

   if (!is_cloaked) 
      { 
   float t = fract(time * 0.5);
   float tt = fract(-time * 0.5);
   vec4 c = color * t;
   color = vec4(c);
   color += envMap * tt * diffuseMap.a;  
      } 
I've got a terrible sinking feeling that i'm barking up the wrong tree with this method, the time variable obviously keeps marching onwards so the fade effects are completing, then resetting to maximum, then counting up/down again, then resetting to maximum, then counting up/down again etc. what do you recommend for fixing the values of t and tt when they get near to either .0000 or .9999? would you do some sort of 'do while' loop?
do
{
block of code;
}
while (test condition);

edit: do while loops not supported on older cards :(
User avatar
Cmdr James
Commodore
Commodore
Posts: 1357
Joined: Tue Jun 05, 2007 10:43 pm
Location: Berlin

Post by Cmdr James »

I dont quite understand the problem, but I can offer some randomish thoughts.
You can almost always replace a

Code: Select all

do{ } while();
with a simple

Code: Select all

while() {}
. If you have to, you can do something like

Code: Select all

while (firstRun || condition) {
  doStuff();//do your fading and stuff
  firstRun = false;
}
If you are unable to use a while, you can probably use a

Code: Select all

for()
, I would imagine that any programming language without some kind of looping of this kind would be more or less impossible (year, right, try using Transact-SQL and being forced to use GOTO :lable :D )

if I understand right, then you can at least remove some duplication in your code

Code: Select all

float t = fract(-time * 0.5);
  if (is_cloaked) {
    t = -t;
  } 
vec4 c = color * t;
color = vec4(c);
color += envMap * -t * diffuseMap.a;
I think instead of using time, and taking the fractional part, you should maybe use

Code: Select all

elasped = time - startTime
and then stopping once elapsed is greater or equal to some value (1.00, I guess)


Its possible that I have completely missed the point though, and Im not too sure what most of the variables you are using actually are, so take it with a pinch of salt :)
User avatar
Griff
Oolite 2 Art Director
Oolite 2 Art Director
Posts: 2483
Joined: Fri Jul 14, 2006 12:29 pm
Location: Probably hugging his Air Fryer

Post by Griff »

Thanks for taking the time to pick through my crappy code Cmdr James!
Please don't tax yourself too much with it though, i'm posting questions about stuff i don't understand, and the shader does pretty much what i want it to now - this fading in and out stuff is just extra frills.

When the bool isCloaked = true
i think i need a little counter routine that counts up from 0.00 to 1.00 over a period of about say, 4 seconds, and when the counter gets to 1.00 it stays at that value until bool isCloaked = false, upon which it counts back down from 1.00 to 0.00

i want to use the counter value to control a % mixture of two texture maps when a ship switches in or out of a cloaked state

[ship becomes cloaked]
start_time=0.0 (to reset the clock)
time_elapsed = start_time + time
mix texture_map_2 to texture_map_1 using time_elapsed as a mix percentage
when time_elapsed =1.0 fix it at this value until ship becomes uncloaked, then count back down from 1.00 to 0.00 using the decreasing values as a texture mix % so the ship returns to it's normal texture map
Post Reply