Jump to content


Photo

2D Normal Mapping

requested gm:studio shader

  • Please log in to reply
18 replies to this topic

#1 Xor

Xor

    @XorDev

  • GMC Member
  • 1346 posts
  • Version:GM:Studio

Posted 21 January 2015 - 10:26 PM

RMMO8l7.png
Hello. I made a basic 3D lighting system, so I thought, "why not make a 2D one".
This is a deffered rendering system with simple scripts that make it simple to customize. 
Suggestions are welcome to help improve this.
 
Features:
  • 8 Point lights with x, y, range and color settings
  • Easy scripts for adding textures to the diffuse map, normal map, or specular map
  • Ambient light colors
  • Multiple objects with new example object
  • NM_wiggle() script for make the light range wiggle
  • Rotated objects and objects with alpha
Vertex code:
Spoiler

 
Fragment code:
Spoiler

 
Download Normal Mapping Example

I also made a simple 2D lighting example without the normal mapping for learning purposes:
LqYOt4y.png
Features :
  • Multiple point lights
  • Point light colors
  • Ambient light colors
  • Easy to use scripts
Download Simple Lighting Example

Edited by Xor, 12 February 2016 - 03:26 PM.

  • 9
I'm on Twitter as of March 2016 (@XorDev).

#2 jaimitoes

jaimitoes

    GMC Member

  • GMC Member
  • 247 posts
  • Version:GM:Studio

Posted 22 January 2015 - 12:38 AM

Very cool and compatible with mobile devices... (tried in HTC one S and NEXUS 7 2013)


  • 1

#3 JimmyBegg

JimmyBegg

    GMC Member

  • GMC Member
  • 111 posts
  • Version:GM:Studio

Posted 22 January 2015 - 09:53 AM

Nice job!

You should probably use a for loop in the fragment

int i = 0;
vec3 total = lighting(Spec,Norm,Dif.rgb,v_vPosition,vec4(lights[i*3],lights[i*3+1],24.0,lights[i*3+2]),vec3(lcolor[i*3],lcolor[i*3+1],lcolor[i*3+2]));
for(i=1; i<LN; i++) {
        total += lighting(Spec,Norm,Dif.rgb,v_vPosition,vec4(lights[i*3],lights[i*3+1],24.0,lights[i*3+2]),vec3(lcolor[i*3],lcolor[i*3+1],lcolor[i*3+2]));
}

That way it works with any amount of lights :)

lights.jpg


  • 1

logoleapgmsig.pngparacs.png


#4 Xor

Xor

    @XorDev

  • GMC Member
  • 1346 posts
  • Version:GM:Studio

Posted 22 January 2015 - 02:54 PM

Spoiler

I did originally, but it doesn't work cross platform. I also lose about 900 FPS with 16 lights.


Edited by Xor, 22 January 2015 - 03:29 PM.

  • 0
I'm on Twitter as of March 2016 (@XorDev).

#5 jaimitoes

jaimitoes

    GMC Member

  • GMC Member
  • 247 posts
  • Version:GM:Studio

Posted 22 January 2015 - 03:10 PM

Nice job!

You should probably use a for loop in the fragment

int i = 0;
vec3 total = lighting(Spec,Norm,Dif.rgb,v_vPosition,vec4(lights[i*3],lights[i*3+1],24.0,lights[i*3+2]),vec3(lcolor[i*3],lcolor[i*3+1],lcolor[i*3+2]));
for(i=1; i<LN; i++) {
        total += lighting(Spec,Norm,Dif.rgb,v_vPosition,vec4(lights[i*3],lights[i*3+1],24.0,lights[i*3+2]),vec3(lcolor[i*3],lcolor[i*3+1],lcolor[i*3+2]));
}

That way it works with any amount of lights :)

lights.jpg

yes I can assure with a for statement, the device mobile of 2010 does not work, is because for a shader, the devices do not calculate proper in shader model 2.0 because is dinamyc branch, dinamyc branch is only supported in 3.0.. i test the previous shader with a for statement and crash my Htc one s and on a Nexus 7 2013 works very slow.... without the "for" you have a boost performance.


Edited by jaimitoes, 22 January 2015 - 03:30 PM.

  • 0

#6 Xor

Xor

    @XorDev

  • GMC Member
  • 1346 posts
  • Version:GM:Studio

Posted 22 January 2015 - 03:37 PM

Update!

 

Added features:

  • Multiple objects with new example object
  • NM_wiggle() script for make the light range wiggle

Edited by Xor, 22 January 2015 - 03:37 PM.

  • 0
I'm on Twitter as of March 2016 (@XorDev).

#7 JimmyBegg

JimmyBegg

    GMC Member

  • GMC Member
  • 111 posts
  • Version:GM:Studio

Posted 22 January 2015 - 09:18 PM

Ah damn, would there be another way around this?


  • 0

logoleapgmsig.pngparacs.png


#8 jaimitoes

jaimitoes

    GMC Member

  • GMC Member
  • 247 posts
  • Version:GM:Studio

Posted 22 January 2015 - 09:27 PM

i try to learning all aspects but at the moment im noob with this...

 

searching about open gl ES 2.0 :

 

http://aras-p.info/b...all-over-again/

 

you have more examples seaching in google... waiting for progress. Good luck!! this is awesome feature.


Edited by jaimitoes, 22 January 2015 - 09:30 PM.

  • 0

#9 Xor

Xor

    @XorDev

  • GMC Member
  • 1346 posts
  • Version:GM:Studio

Posted 23 January 2015 - 07:59 PM

Update!

 

Added rotated objects and placement for rocks in the example.


  • 0
I'm on Twitter as of March 2016 (@XorDev).

#10 Consumer

Consumer

    GMC Member

  • GMC Member
  • 143 posts
  • Version:Unknown

Posted 24 January 2015 - 10:36 AM

Great work! It works immensely well, and I suppose adjusting an upper limit of light sources would help adapt for different purposes. I'll throw in some long shots of suggestions in case you wanted to bite.

 

Self-shadowing would be interesting as long as it's practical, from I imagine just the normal map.

 

And since I haven't seen normal mapping done with lighting/shadowing in GM yet, fusing the two to occlude normal map illumination with mesh-based occluders would be cool and very valuable.

 

Some observations with more initial testing:

- Possible memory problems; adding rocks increases memory use according to the system though the debugger doesn't notice that problem. I didn't see any memory leaks in the code, but I don't think this is normal GM behavior.

- Renamed "upsec" to "uspec", likely mispelling on global variable initialization, though no problem so far since it's only used locally within the NM controller instance. Regardless, you may want to consider making variables that don't need to be global as local instead.


Edited by Consumer, 24 January 2015 - 11:14 AM.

  • 1

#11 Xor

Xor

    @XorDev

  • GMC Member
  • 1346 posts
  • Version:GM:Studio

Posted 24 January 2015 - 03:06 PM

Great work! It works immensely well, and I suppose adjusting an upper limit of light sources would help adapt for different purposes. I'll throw in some long shots of suggestions in case you wanted to bite.

 

Self-shadowing would be interesting as long as it's practical, from I imagine just the normal map.

 

And since I haven't seen normal mapping done with lighting/shadowing in GM yet, fusing the two to occlude normal map illumination with mesh-based occluders would be cool and very valuable.

 

Some observations with more initial testing:

- Possible memory problems; adding rocks increases memory use according to the system though the debugger doesn't notice that problem. I didn't see any memory leaks in the code, but I don't think this is normal GM behavior.

- Renamed "upsec" to "uspec", likely mispelling on global variable initialization, though no problem so far since it's only used locally within the NM controller instance. Regardless, you may want to consider making variables that don't need to be global as local instead.

Thanks. I do have my 2D light and shadow engine which I may try to merge with this in the future, but for now, if you want shadows, you'll have to use that.

Rock memory problem may be do to the fact that rotated objects must be run thru "shdr_rotate", before it gets added. The more rocks you have, the more times it has to be run.

Thanks for pointing out "upsec". I'll try to update this soon.


  • 0
I'm on Twitter as of March 2016 (@XorDev).

#12 Xor

Xor

    @XorDev

  • GMC Member
  • 1346 posts
  • Version:GM:Studio

Posted 09 February 2015 - 12:20 AM

Just fixed rotation script (it was rotating backwards). Make sure you update! You can redownload or change the "NM_normal" script code to:

Spoiler

  • 1
I'm on Twitter as of March 2016 (@XorDev).

#13 ThePC007

ThePC007

    Chickenator

  • GMC Member
  • 1062 posts
  • Version:GM:Studio

Posted 31 August 2015 - 02:13 PM

Very cool and compatible with mobile devices... (tried in HTC one S and NEXUS 7 2013)

 

I tried it on my Galaxy Note II and got not more than ~7 fps. How much did you get?


  • 0

#14 jaimitoes

jaimitoes

    GMC Member

  • GMC Member
  • 247 posts
  • Version:GM:Studio

Posted 02 September 2015 - 01:14 PM

Sure more than 7fps. How big is your view space?
  • 0

#15 Pro Pro.

Pro Pro.

    GMC Member

  • GMC Member
  • 739 posts

Posted 19 January 2016 - 11:00 PM

How do you get an ambient light source? and how would u change its intensity?

 

Also, the original colors of the sprite dont seem to show up at all, even under white light


Edited by Pro Pro., 19 January 2016 - 11:06 PM.

  • 0

#16 Xor

Xor

    @XorDev

  • GMC Member
  • 1346 posts
  • Version:GM:Studio

Posted 19 January 2016 - 11:29 PM

Hello Pro Pro. You should find a script for setting the ambient light in the script group. Just use the script and set the color. The brightness of the light determines the intensity. If you have a black light for example, it won't light anything, but a white light would be much brighter.

As for the color change, I'm not sure what would make the issue. Please provide more info like screenshots or code.
  • 1
I'm on Twitter as of March 2016 (@XorDev).

#17 Pro Pro.

Pro Pro.

    GMC Member

  • GMC Member
  • 739 posts

Posted 20 January 2016 - 12:01 AM

Thank you for the fast response.

 

The ambient light solved most of the issues, but here is what i am trying to do with my project, maybe you can help.

 

Basically, i am trying to have a somewhat cartoonish look where a lot of things wont use this engine at all, but certain structures and the player and enemy sprites will use this.  I want the effect of the engine pretty soft in a way i can have a light source as the sun which determines where the sprite is brighter than the rest (and darker on the other side).  Not nearly as dramatic as it is in the example.

 

Would i have to do this with the normal and spec maps? (i use crazybump). Or do i need to tackle this from some sort of combination of the ambient light settings and the light source (sun) settings?

 

For instance, if i set ambient to 155,155,155,  none of the light sources are hardly visible at all and don't brighten up the surfaces of objects when i try to use it currently.

 

When ambient is set to 100,100,100 you can see other light sources, but they are "grayed out" and the otherside of objects are grey instead of just a darker version of the original


Edited by Pro Pro., 20 January 2016 - 12:05 AM.

  • 0

#18 Xor

Xor

    @XorDev

  • GMC Member
  • 1346 posts
  • Version:GM:Studio

Posted 20 January 2016 - 12:54 AM

Thank you for the fast response.

 

The ambient light solved most of the issues, but here is what i am trying to do with my project, maybe you can help.

 

Basically, i am trying to have a somewhat cartoonish look where a lot of things wont use this engine at all, but certain structures and the player and enemy sprites will use this.  I want the effect of the engine pretty soft in a way i can have a light source as the sun which determines where the sprite is brighter than the rest (and darker on the other side).  Not nearly as dramatic as it is in the example.

 

Would i have to do this with the normal and spec maps? (i use crazybump). Or do i need to tackle this from some sort of combination of the ambient light settings and the light source (sun) settings?

 

For instance, if i set ambient to 155,155,155,  none of the light sources are hardly visible at all and don't brighten up the surfaces of objects when i try to use it currently.

 

When ambient is set to 100,100,100 you can see other light sources, but they are "grayed out" and the otherside of objects are grey instead of just a darker version of the original

 

If I am understanding you right you want a directional light for the sun and an ambient light, but you have no need for point lights? If this is the case I'll edit the code a bit because the code doesn't currently doesn't have directional lights.

 

The issue of the light color still baffles me. How are you setting the color? I'm assuming you are using make_colour_rgb(). Also does my example work for you?


  • 0
I'm on Twitter as of March 2016 (@XorDev).

#19 Pro Pro.

Pro Pro.

    GMC Member

  • GMC Member
  • 739 posts

Posted 20 January 2016 - 05:53 PM

The example is what i am testing.  If i have nothing on but a low ambient light... say, 100,100,100, everything is a shade of gray, instead of being a darker version of itself is what i mean.  Then any light sources you added are also grayed out from the ambient light.

 

Doesn't there need to be an intensity setting? That way i can have a weak white light instead of a strange gray light?


  • 0





Also tagged with one or more of these keywords: requested, gm:studio, shader