Author Topic: Merging Code - Custom Shaders  (Read 11653 times)

I've been trying to figure out how to integrate this shader code into Blockland's code, changing certain variables to fit. But I have yet to figure out what file to put it in, which command, where in which command, and how to change some parts to work. I have been told there is no uniform for worldTime, and was going to opt for converting the vec camPos to an int for the purpose of the script. Could someone figure out how to integrate this into the shaders?

Code: [Select]
vec4 position = gl_ModelViewMatrix * gl_Vertex;
float distanceSquared = position.x * position.x + position.z * position.z;
position.y += 5*sin(distanceSquared*sin(float(worldTime)/143.0)/1000);
float y = position.y;
float x = position.x;

float z = position.z * ( (sin(float(worldTime)/256.0) + 1.75) / 2);
float om = sin(distanceSquared*sin(float(worldTime)/256.0)/5000) * sin(float(worldTime)/200.0);
position.y = x*sin(om)+y*cos(om);
position.x = x*cos(om)-y*sin(om);
position.z = z;

gl_Position = gl_ProjectionMatrix * position;

This is the best I could figure trying to put it into renderVsm_vert.glsl:

Code: [Select]
worldTime = camPos.x + camPos.y + camPos.z;
float distanceSquared = vPos.x * vPos.x + vPos.z * vPos.z;
vPos.y += 5*sin(distanceSquared*sin(float(worldTime)/143.0)/1000);
float y = vPos.y;
float x = vPos.x;

float z = vPos.z * ( (sin(float(worldTime)/256.0) + 1.75) / 2);
float om = sin(distanceSquared*sin(float(worldTime)/256.0)/5000) * sin(float(worldTime)/200.0);
vPos.y = x*sin(om)+y*cos(om);
vPos.x = x*cos(om)-y*sin(om);
vPos.z = z;

I have no idea if it'd even go in there, or if this would even work in the end. But so far, I've definitely been coding this wrong. I wish Port would have not contradicted himself by first editing the code and then saying he does not understand it. Maybe you just switched some numbers, but you know more than me.

Edit: I managed to sort of get it working, although it ends up looking very buggy: http://www.youtube.com/watch?v=jZ7QOvw9P1Y Here is the code I used in generateVsm_frag.glsl and renderVsm_frag.glsl:

Code: [Select]
float distanceSquared = vPos.x * vPos.x + vPos.z * vPos.z;
int worldTime = int(camPos.x + camPos.y + camPos.z);
int oScale = int(256.0/2);
int eScale = int(200.0/2);
vPos.y += 5*sin(distanceSquared*sin(float(worldTime)/143.0)/1000);
float y = vPos.y;
float x = vPos.x;

float z = vPos.z * ( (sin(float(worldTime)/oScale) + 1.75) / 2);
float om = sin(distanceSquared*sin(float(worldTime)/oScale)/5000) * sin(float(worldTime)/eScale);
vPos.y = x*sin(om)+y*cos(om);
vPos.x = x*cos(om)-y*sin(om);
vPos.z = z;

Edit2: I tried halving more of the values to see if it was a scale issue. It seemed to make things work somewhat better, although it also allows things to skew the hell out suddenly: http://www.youtube.com/watch?v=RcPCYHLWouk Here is the code I currently have:

Code: [Select]
float distanceSquared = vPos.x * vPos.x + vPos.z * vPos.z;
int worldTime = int(camPos.x + camPos.y + camPos.z);
int aScale = int(256.0/2);
int bScale = int(200.0/2);
int cScale = int(143.0/2);
int dScale = int(1000/2);
int eScale = int(5000/2);
vPos.y += 5*sin(distanceSquared*sin(float(worldTime)/cScale)/dScale);
float y = vPos.y;
float x = vPos.x;

float z = vPos.z * ( (sin(float(worldTime)/aScale) + 1.75) / 2);
float om = sin(distanceSquared*sin(float(worldTime)/aScale)/eScale) * sin(float(worldTime)/bScale);
vPos.y = x*sin(om)+y*cos(om);
vPos.x = x*cos(om)-y*sin(om);
vPos.z = z;

I'm putting them as separate ints instead of changing them internally so I can adjust the scaling on the original code without forgetting etc. etc.
« Last Edit: August 10, 2012, 02:02:15 PM by MegaScientifical »

This is awesome! Will be looking into shader programming soon! great proof of concept!

Edit2: I tried halving more of the values to see if it was a scale issue. It seemed to make things work somewhat better, although it also allows things to skew the hell out suddenly: http://www.youtube.com/watch?v=RcPCYHLWouk Here is the code I currently have:

Code: [Select]
float distanceSquared = vPos.x * vPos.x + vPos.z * vPos.z;
int worldTime = int(camPos.x + camPos.y + camPos.z);
int aScale = int(256.0/2);
int bScale = int(200.0/2);
int cScale = int(143.0/2);
int dScale = int(1000/2);
int eScale = int(5000/2);
vPos.y += 5*sin(distanceSquared*sin(float(worldTime)/cScale)/dScale);
float y = vPos.y;
float x = vPos.x;

float z = vPos.z * ( (sin(float(worldTime)/aScale) + 1.75) / 2);
float om = sin(distanceSquared*sin(float(worldTime)/aScale)/eScale) * sin(float(worldTime)/bScale);
vPos.y = x*sin(om)+y*cos(om);
vPos.x = x*cos(om)-y*sin(om);
vPos.z = z;

I'm putting them as separate ints instead of changing them internally so I can adjust the scaling on the original code without forgetting etc. etc.

Maybe it'd be better if someone rebuilt the code to work with Blockland, instead of adjusting pre-existing code to fit into the game. I do hope Badspot adds a uniform int worldTime; of some sort so the effect can go along with the current world time.

After messing around a bit, I ended up with some neat effects.

For the first one, go into you blockland folder and there should be a shaders folder.

http://www.youtube.com/watch?v=ad-n6DT1IHc
While Blockland is open, insert this code into renderVsm_vert.glsl after the line that says "vPos = world2Cam * vec4(worldPos, 1.0f);":
Code: [Select]
vPos.z += sin(worldPos.z) * 3.14159 / 3;Save it and change the shader quality in blockland to apply it.


The video for this one will be up later.
Put this code in common.glsl right at the bottom before the line "return fragColor;":
Code: [Select]
fragColor.x += (fragColor.x + fragColor.y - fragColor.z)/vPos.x;
fragColor.y += (fragColor.x + fragColor.y - fragColor.z)/vPos.y;
fragColor.z += (fragColor.x + fragColor.y - fragColor.z)/vPos.z;
If for some reason you don't know how to get it back to normal, just restart blockland and the launcher will download the defaults.

It's fun to mess around with these, even though I barely know what the forget I'm doing.
« Last Edit: August 15, 2012, 08:23:12 PM by Scars75 »

Wow, that's interesting. I'm still curious if we'll be given the ability to pack our own shaders. I thought that was confirmed, but it is probably not very high up on the to-do list.

Greyscale bricks!


In renderVsm_vert.glsl replace gl_FrontColor = gl_Color with these two lines:
Code: [Select]
float grey = (0.3333 * gl_Color.r) + (0.3333 * gl_Color.g) + (0.3333 * gl_Color.b);
gl_FrontColor = vec4(grey, grey, grey, gl_Color.a);

now I just need to figure out making it work with colored light.

EDIT: new version, put this code on common.glsl right before return fragColor
Code: [Select]
float grey = (0.3333 * fragColor.r) + (0.3333 * fragColor.g) + (0.3333 * fragColor.b);
fragColor = vec4(grey, grey, grey, fragColor.a);
Works for everything except skybox and light flares.
« Last Edit: August 15, 2012, 10:53:54 PM by Tammy »

Its cool how people are making custom shaders for blockland.
Hopefully Badspot will make it able for us to load custom shaders while ingame, or atleast not have to modify the current shaders to get custom shaders. :)

EDIT: new version, put this code on common.glsl right before return fragColor
Code: [Select]
float grey = (0.3333 * fragColor.r) + (0.3333 * fragColor.g) + (0.3333 * fragColor.b);
fragColor = vec4(grey, grey, grey, fragColor.a);
Works for everything except skybox and light flares.

Quote
Watermelon: gl_FragColor = vec4(vec3((fragColor.r + fragColor.g + fragColor.b)*0.33333), fragColor.a);
...
Watermelon: fragColor.rgb = vec3((fragColor.r + fragColor.g + fragColor.b)*0.3333);

Tried to make a sepia shader, didn't turn out too well.
« Last Edit: August 16, 2012, 10:51:22 AM by Scars75 »

Sepia filter is a little bit more advanced.

here is my attempt:

Code: [Select]
vec3 beforesepia = vec3(fragColor);
fragColor.r = dot(beforesepia, vec3(.393, .769, .189));
fragColor.g = dot(beforesepia, vec3(.349, .686, .168));
fragColor.b = dot(beforesepia, vec3(.272, .534, .131));


without sepia:




with sepia:


As you see its working just fine. (first sepia shader for blockland YAY)

Sepia filter is a little bit more advanced.

here is my attempt:

Code: [Select]
vec3 beforesepia = vec3(fragColor);
fragColor.r = dot(beforesepia, vec3(.393, .769, .189));
fragColor.g = dot(beforesepia, vec3(.349, .686, .168));
fragColor.b = dot(beforesepia, vec3(.272, .534, .131));


without sepia:
[I MG width=600]http://i.imgur.com/XUUH9.jpg[/img]



with sepia:
[ IMG width=600]http://i.imgur.com/91ECH.jpg[/img]

As you see its working just fine. (first sepia shader for blockland YAY)
Yeah, I was going for the effect in this image:




Acid trip.
In renderVsm_vert.glsl, add this after vPos = world2Cam * vec4(worldPos, 1.0f);:
Code: [Select]

float distance = (worldPos.x - vPos.x) + (worldPos.z - vPos.z) + (worldPos.y - vPos.y);
vPos.x += sin(distance)/5;
vPos.y += cos(distance)/5;
« Last Edit: August 16, 2012, 03:37:35 PM by Scars75 »

Not sure what to call this.
Again, right before return fragColor;
Code: [Select]
   fragColor.r = dot(vec3(fragColor), vec3(dirLightColor.r-0.15+sin(viewDelta.z)+sin(viewDelta.x)/20, dirLightColor.g-0.45, .59));
   fragColor.g = dot(vec3(fragColor), vec3(dirLightColor.g-0.2+sin(viewDelta.z)-cos(viewDelta.x)/20, .53, .13));
   fragColor.b = dot(vec3(fragColor), vec3(dirLightColor.b-0.5+sin(viewDelta.z), .23, .13));

What does that do, Scars? No screenshots?

What does that do, Scars? No screenshots?
Not really sure how to explain it.


This little bit here:
const int numlights = 8;
Should change the max number of lights you can see in-game.