Shield Hit Effect (JME2)

  1. Create a sphere in Blender.
  2. Divide the work space and change the second view to UV Mapping
  3. Select the top view in the modeling window (7 on the key pad)
  4. Press ‘Tab’ to go into mesh edit mode, press ‘A’ once or twice until all vertices are selected and then press the ‘U’ key > ‘Project from View’



    In the UV Editor:
  5. Turn ‘Snap to Pixels’ on and adjust the vertices the are horizontal until they are evenly spaced.
  6. Turn off ‘Snap to Pixels’ and rotate the UV mapping until the next divisions are horizontals.
  7. Repeat steps 5 and 6 until the entire sphere is flattened.



    In the Button Window:
  8. Add your image to the material and switch mapping to UV.



    From the main menu:
  9. Use ogre exporter to export your model with the UV mapping.



    There are a bazillion tutorials on Blender on the web and it is very easy to learn in a day or three, if you have the inclination.



    Christy

So I take it there is no way you can do this in JME2? I saw that Texture objects have a setTextureCoords() method which gets a FloatBuffer. Could one use this to gain the same effect?

If not I guess I’ll dig into blender after all g. Thanks for your how-to!

I think the biggest issue is actually generating the texture coordinates. Assuming you know the collision point relative to the sphere origin, generate texture coordinates such that point 0.5, 0.5 is at the center of the collision.

1 Like

It seems everyone thinks I have a clue about these texture coordinates and how to generate them, but thats not the case ^^. I tried to google me into it and read about on wikipedia, but I just don’t understand it. So is it possible to do the same thing t0neg0d posted in JME2 code? If yes, how? ^^"

well…at some point the baker has to learn how to make his dough…

UV mapping is very widely used in game industry, you won’t waste your time learning about it.

1 Like

You’re probably right :slight_smile:

I’ll get into UV mapping at a later point. Right now I’m more concerned about the alpha enhancing effect. I mean that the color of the shield should be stronger at the collision and become weaker the more one gets away from the collision point. I tried to just put a PointLight at the location where the collision takes place (I attached it the the LightState of my rootNode of the whole scene), but this just won’t work. I just set the diffuse colors of the material and the lights, is this right or do I need to set other colors, too?

Or is this behaviour only possible with shaders here? I tried to look into it, but I think I found a limitation with shaders: How to tell it where the collision took place? Yeah you can give the shader these uniform variable from the outside, but these are supposed to be constant all the time, right? And these collisions won’t happen at the same points of course. And what about multiple collisions at the same time? How can a shader handle this?

The uniform variables are not constant, not per frame or object at least. Also, you can create an additional sphere for each impact, this is somewhat easier than rendering multiple impacts in the shader.

1 Like

I’m currently trying to implement your idea. So I’ve written these simple shaders:

vert:

[java]

uniform vec3 collision;

varying float dist;



void main(void) {

gl_Position = ftransform();

vec3 pos = vec3(gl_Position.x, gl_Position.y, gl_Position.z);

dist = distance(pos, collision);

}

[/java]



frag:

[java]

varying float dist;



void main(void) {

gl_FragColor = gl_FrontMaterial.diffuse;



float alpha = 1.0;

if(dist > 20.0) alpha = 0.0;

else if(dist > 1.0) alpha = 1.0 - (dist / 20.0);



gl_FragColor.a = alpha;

}

[/java]



But it seems that the distance is not computed the way I want it to. Looks like gl_Position is not in the same coordinate system like the collision vector, which I compute in java in world coordinates. But I have no idea what to do now. I just tried to change collision in to local coordinates, but it won’t work either. Any suggestions? :slight_smile:

See this page:

https://wiki.jmonkeyengine.org/legacy/doku.php/jme3:advanced:jme3_shaders

1 Like

Thanks a lot for this link!

I figured the easiest way to get it work is to use the local coordinates for the collision location, since this I can compute in java.

The shader works now:

vert:

[java]

uniform vec3 collision;

varying float dist;

void main(void) {

gl_Position = ftransform();

dist = distance(gl_Vertex, collision);

}

[/java]

frag:

[java]

uniform float maxDist;

uniform float degradation;

varying float dist;

void main(void) {

gl_FragColor = gl_FrontMaterial.diffuse;

float alpha = 1.0;

if(dist > maxDist) alpha = 0.0;

else if(dist > 0.0) alpha = 1.0 - (dist / maxDist);

alpha = alpha - degradation;

gl_FragColor.a = alpha;

}

[/java]

maxDist depends on the size of the ship (i.e. the radius of the shield). degradation is managed by a Controller, so the effect gets weaker over time and after 2 seconds is completely gone. Looks quite nice. The next step would be to add some grid like texture, but I’m still struggling with these texture coordinates… :smiley: