Dynamic lighting using normal maps / SpriteIlluminator

General discussion of game creation, including ai, shaders, and other topics.

Moderators: naturally, ezraanderson

Dynamic lighting using normal maps / SpriteIlluminator

Postby sshukul » Fri Mar 27, 2015 2:24 am

I've been using a lot of third party graphical plugins and tools for enhancing Moai's capabilities and efficiency, like Spriter/Spine, TexturePacker etc. and the other day I got a mail from them about SpriteIlluminator for dynamic lighting using normal maps:

https://www.codeandweb.com/spriteilluminator

It has integrations for Unity, Cocos2D, libgdx and many other major engines. I was wondering if there is a Moai plugin for this or any plans for one, or if there is some capability within the engine to do realistic 2D lighting like this easily.
User avatar
sshukul
 
Posts: 106
Joined: Wed Mar 27, 2013 1:44 am

Re: Dynamic lighting using normal maps / SpriteIlluminator

Postby Vavius » Fri Mar 27, 2015 9:36 am

This is just a custom multitexture shader, nothing more. Couple of lines of lua code.
Vavius
 
Posts: 264
Joined: Mon Apr 29, 2013 6:04 am

Re: Dynamic lighting using normal maps / SpriteIlluminator

Postby sshukul » Sat Mar 28, 2015 1:20 am

Thanks for pointing in the right direction Vavius, guess I'm a noob at shaders so I didn't know it was something that simple. I'll look into and play around with some of the shader code posted around and see if I make some headaway.
User avatar
sshukul
 
Posts: 106
Joined: Wed Mar 27, 2013 1:44 am

Re: Dynamic lighting using normal maps / SpriteIlluminator

Postby Motig » Sat Mar 28, 2015 7:23 am

Would be cool to see what you come up with and how you did it!
Motig
 
Posts: 92
Joined: Thu Sep 05, 2013 7:55 am

Re: Dynamic lighting using normal maps / SpriteIlluminator

Postby Bartho » Sun Mar 29, 2015 10:11 am

Wow, this SpriteIlluminator looks cool! I used TexturePacker in my current project and it is fine. Out of subject maybe but did anyone tried PhysicsEditor? Is it better than RUBE?
User avatar
Bartho
 
Posts: 60
Joined: Mon Jan 19, 2015 4:13 am
Location: France

Re: Dynamic lighting using normal maps / SpriteIlluminator

Postby Vavius » Sun Mar 29, 2015 10:53 am

Bartho wrote:Out of subject maybe but did anyone tried PhysicsEditor? Is it better than RUBE?

PhysicsEditor is just a shape/fixture editor, while RUBE is a full featured world editor.
Vavius
 
Posts: 264
Joined: Mon Apr 29, 2013 6:04 am

Re: Dynamic lighting using normal maps / SpriteIlluminator

Postby monkan » Sun Mar 29, 2015 1:07 pm

I thought this SpriteIlluminator thing looked pretty cool so I thought I'd have a look, I wanted to have a little play around with shaders anyway as it's something I've never really done much. I still find it all a bit confusing. Here's what I got.

main.lua
Code: Select all
  1.  

  2. MOAISim.openWindow ( "test", 800, 600 )

  3.  

  4. viewport = MOAIViewport.new ()

  5. viewport:setSize ( 800, 600 )

  6. viewport:setScale ( 800, 600 )

  7.  

  8. layer = MOAILayer2D.new ()

  9. layer:setViewport ( viewport )

  10. MOAISim.pushRenderPass ( layer )

  11.  

  12. file = assert ( io.open ( 'shader.vsh', mode ))

  13. vsh = file:read ( '*all' )

  14. file:close ()

  15.  

  16. file = assert ( io.open ( 'shader.fsh', mode ))

  17. fsh = file:read ( '*all' )

  18. file:close ()

  19.  

  20. shader = MOAIShader.new ()

  21.  

  22. shader:reserveUniforms ( 11 )

  23. shader:declareUniformSampler( 1, 'diffuseMap', 1 )

  24. shader:declareUniformSampler( 2, 'normalMap', 2 )

  25. shader:declareUniform( 3, 'worldMatrix', MOAIShader.UNIFORM_WORLD )

  26. shader:declareUniform( 4, 'viewProjMatrix', MOAIShader.UNIFORM_VIEW_PROJ )

  27. shader:declareUniform( 5, 'worldViewProjMatrix', MOAIShader.UNIFORM_WORLD_VIEW_PROJ )

  28. shader:declareUniform( 6, 'lightX', MOAIShader.UNIFORM_FLOAT )

  29. shader:declareUniform( 7, 'lightY', MOAIShader.UNIFORM_FLOAT )

  30. shader:declareUniform( 8, 'lightZ', MOAIShader.UNIFORM_FLOAT )

  31.  

  32. shader:declareUniform( 9, 'lightColourR', MOAIShader.UNIFORM_FLOAT )

  33. shader:declareUniform( 10, 'lightColourG', MOAIShader.UNIFORM_FLOAT )

  34. shader:declareUniform( 11, 'lightColourB', MOAIShader.UNIFORM_FLOAT )

  35.  

  36. -- Light position

  37. lightZ = -0.05

  38. shader:setAttr(6, 0.0)

  39. shader:setAttr(7, 0.0)

  40. shader:setAttr(8, lightZ)

  41.  

  42. -- Light colour

  43. shader:setAttr(9, 0.0)

  44. shader:setAttr(10, 1.0)

  45. shader:setAttr(11, 0.0)

  46.  

  47. shader:seekAttr(9, 1.0, 5.0)

  48. shader:seekAttr(10, 0.0, 5.0)

  49. shader:seekAttr(11, 1.0, 5.0)

  50.  

  51. shader:setVertexAttribute ( 1, 'position' )

  52. shader:setVertexAttribute ( 2, 'uv' )

  53. shader:setVertexAttribute ( 3, 'colour' )

  54. shader:load ( vsh, fsh )

  55.  

  56. multitexture = MOAIMultiTexture.new ()

  57. multitexture:reserve ( 2 )

  58.  

  59. diffuse = MOAITexture.new ()

  60. diffuse:load ( "image.png" )

  61. multitexture:setTexture ( 1, diffuse )

  62.  

  63. normal = MOAITexture.new ()

  64. normal:load ( "image_n.png" )

  65. multitexture:setTexture ( 2, normal )

  66.  

  67. gfxQuad = MOAIGfxQuad2D.new ()

  68. gfxQuad:setTexture ( multitexture )

  69. gfxQuad:setRect ( -128, -128, 128, 128 )

  70. gfxQuad:setUVRect ( 0, 1, 1, 0 )

  71.  

  72. prop = MOAIProp2D.new ()

  73. prop:setDeck ( gfxQuad )

  74. prop:setShader ( shader )

  75. prop:moveRot ( 360, 50.0 )

  76. layer:insertProp ( prop )

  77. prop:setLoc(0, 0, 0)

  78.  

  79. function onPointerEvent(x, y)

  80.         x, y = layer:wndToWorld(x, y)

  81.         shader:setAttr(6, x)

  82.         shader:setAttr(7, y)

  83. end

  84. MOAIInputMgr.device.pointer:setCallback(onPointerEvent)

  85.  

  86. function onKeyboardEvent( key, down )

  87.         local keyChar = string.char(key)

  88.        

  89.         if down then

  90.                 if keyChar == "w" or keyChar == "W" then

  91.                         lightZ = lightZ - 0.01

  92.                 elseif keyChar == "s" or keyChar == "S" then

  93.                         lightZ = lightZ + 0.01

  94.                 end

  95.                

  96.                 print('Light Z', lightZ)

  97.                 shader:setAttr(8, lightZ)

  98.         end

  99.  

  100. end

  101. MOAIInputMgr.device.keyboard:setCallback( onKeyboardEvent )

  102.  

  103.  



shader.fsh
Code: Select all
  1.  

  2. varying MEDP vec2 uvVarying;

  3. varying MEDP vec4 positionVarying;

  4. varying MEDP vec4 colourVarying;

  5.  

  6. uniform sampler2D diffuseMap;

  7. uniform sampler2D normalMap;

  8.  

  9. uniform MEDP float lightColourR;

  10. uniform MEDP float lightColourG;

  11. uniform MEDP float lightColourB;

  12.  

  13. uniform mat4 worldMatrix;               // Sum of all MOAITransforms on object

  14. uniform mat4 viewProjMatrix;            // Projection matrix (camera)

  15. uniform mat4 worldViewProjMatrix;       // Projection matrix (camera)

  16.  

  17. uniform MEDP float lightX;

  18. uniform MEDP float lightY;

  19. uniform MEDP float lightZ;

  20.  

  21. //uniform vec3 ambientColor;

  22. vec3 ambientColor = vec3(1.0, 1.0, 1.0);

  23. //uniform float ambientIntensity;

  24. float ambientIntensity = 0.05;

  25. //uniform vec2 resolution;

  26. vec2 resolution = vec2(1.0, 1.0);

  27. //uniform bool useNormals;

  28. bool useNormals = true;

  29. //uniform bool useShadow;

  30. bool useShadow = true;

  31. //uniform vec3 attenuation;

  32. vec3 attenuation = vec3(1.0, 1.0, 1.0);

  33.  

  34. void main()

  35. {

  36.         vec4 light = vec4(lightX, lightY, lightZ, 1.0) * viewProjMatrix;

  37.         vec3 lightColour = vec3(lightColourR, lightColourG, lightColourB);

  38.  

  39.         //sample color & normals from our textures

  40.         vec4 color = texture2D(diffuseMap, uvVarying);

  41.         vec3 nColor = texture2D(normalMap, uvVarying).rgb;

  42.  

  43.         //normals need to be converted to [-1.0, 1.0] range and normalized

  44.         vec3 normal = normalize(nColor * 2.0 - 1.0);

  45.  

  46.         //here we do a simple distance calculation

  47.         vec3 deltaPos = vec3( (light.xy - positionVarying.xy) / resolution.xy, light.z );

  48.  

  49.         vec3 lightDir = normalize(deltaPos);

  50.         float lambert = useNormals ? clamp(dot(normal, lightDir), 0.0, 1.0) : 1.0;

  51.  

  52.         //now let's get a nice little falloff

  53.         float d = sqrt(dot(deltaPos, deltaPos));      

  54.         float att = useShadow ? 1.0 / ( attenuation.x + (attenuation.y*d) + (attenuation.z*d*d) ) : 1.0;

  55.    

  56.         vec3 result = (ambientColor * ambientIntensity) + (lightColour.rgb * lambert) * att;

  57.         result *= color.rgb;

  58.  

  59.         gl_FragColor = colourVarying * vec4(result, color.a);

  60. }

  61.  



shader.vsh
Code: Select all
  1.  

  2. attribute vec4 position;

  3. attribute vec2 uv;

  4. attribute vec4 colour;

  5.  

  6. varying vec4 colourVarying;

  7. varying vec2 uvVarying;

  8. varying vec3 positionVarying;

  9.  

  10. uniform mat4 worldMatrix;               // Sum of all MOAITransforms on object

  11. uniform mat4 viewProjMatrix;            // Projection matrix (camera)

  12. uniform mat4 worldViewProjMatrix;       // Projection matrix (camera)

  13.  

  14. void main () {

  15.         uvVarying = uv;

  16.         colourVarying = colour;

  17.  

  18.         positionVarying = position;

  19.     gl_Position = position;

  20.        

  21. }

  22.  

  23.  



I got the shader code from here - http://www.java-gaming.org/index.php?topic=27516.0

The light seems to work and looks pretty cool. SpriteIlluminator looks like a pretty cool program.

Does anyone know if there is a way / how to use MOAIMultitextures with SpriteSheets? Currently I'm using TexturePacker to make a sprite sheet and loading it into a MOAIGfxQuadDeck2D. Is there a way to use both methods?

Cheers.
monkan
 
Posts: 76
Joined: Sat Jul 14, 2012 1:05 am

Re: Dynamic lighting using normal maps / SpriteIlluminator

Postby Vavius » Sun Mar 29, 2015 11:02 pm

monkan wrote:Does anyone know if there is a way / how to use MOAIMultitextures with SpriteSheets? Currently I'm using TexturePacker to make a sprite sheet and loading it into a MOAIGfxQuadDeck2D. Is there a way to use both methods?

If all your textures have identical layout (all sprites on all textures have the same uv), then you can use MOAIGfxQuadDeck2D.

In more complex Multitexture scenarios you may want to have different uv sets. This means non-default vertex format. I've used a custom TexturePacker loader that writes positions and uv into vertex attributes for MOAIMesh.
Vavius
 
Posts: 264
Joined: Mon Apr 29, 2013 6:04 am

Re: Dynamic lighting using normal maps / SpriteIlluminator

Postby RurouniGPL » Thu Apr 02, 2015 3:56 am

AwesomeBump is Open Source and maybe it can be used to create normal maps:

http://awesomebump.besaba.com/gallery/

But I think "Unflattener" is easier and more similar to SpriteIlluminator, also Open Source:

https://github.com/dbohdan/unflattener/
RurouniGPL
 
Posts: 244
Joined: Wed Jun 27, 2012 11:54 am

Re: Dynamic lighting using normal maps / SpriteIlluminator

Postby sshukul » Sat Apr 18, 2015 1:37 am

Wow I hadn't checked this thread but instead asked nosheet for help with the calculations in the meantime and made an alternate one. Thanks for the shader code monkan, will try it out as well.

The one I got is very similar, a simple point + ambient lighting 2D shader, but without any normal or bump maps yet. Here are the results with it:

Image

Apparently it's a lot like the lighting system used by Broken Age. Here's the final shader function if anyone else is interested. It's adapted to plug and play directly for props and quads rather than meshes, so it works nicely out of the box with frameworks like RapaNui and Hanappe:

Code: Select all
  1.  

  2. function point_lighting_shader(lightx, lighty, lightz, intensity, range, red, green, blue, am_red, am_green, am_blue)  

  3.   local vsh = [[  

  4.     attribute vec4 v_pos;

  5.     attribute vec2 v_uv;

  6.     attribute vec4 v_col;

  7.     // Note: NO NORMALS!

  8.  

  9.     // UNIFORMS

  10.     // X, Y and Z position of the point light.

  11.     uniform float light_x, light_y, light_z;

  12.     // Light range is a distance at which the light intensity will decay to zero.

  13.     uniform float light_intensity, light_range;

  14.     // Point light color

  15.     uniform float light_red, light_green, light_blue;

  16.     // Ambient light color

  17.     uniform float ambient_red, ambient_green, ambient_blue;

  18.  

  19.     // VARYINGS

  20.     varying vec4 frag_color;

  21.     varying vec2 frag_uv;

  22.  

  23.     void main () {

  24.       //vec4 vertex_position = v_pos

  25.    

  26.       // Find the distance between the vertex and the light source

  27.       float vertex_dist = length (vec3( light_x, light_y, light_z ) - vec3( v_pos.x, v_pos.y, v_pos.z ));

  28.  

  29.       // Decay light factor depending on the vertex distance from light source

  30.       float decay = max( 0.0, (light_range - vertex_dist )/ light_range );

  31.  

  32.       // Color of the point light

  33.       vec4 light_color = vec4( light_red, light_green, light_blue, 1.0 );

  34.  

  35.       // Color of the ambient light

  36.       vec4 ambient_color = vec4( ambient_red, ambient_green, ambient_blue, 1.0 );

  37.       frag_color = (v_col * light_color * light_intensity * decay) + ambient_color;

  38.      

  39.       // Alpha channel

  40.       frag_color.w = v_col.w;

  41.  

  42.       // Output UV

  43.       frag_uv = v_uv;

  44.      

  45.       // Multiply vertex position with projection matrix to get its on-screen position.

  46.       gl_Position = v_pos;

  47.     }

  48.   ]]

  49.  

  50.   local fsh = [[

  51.     // UNIFORMS

  52.     uniform sampler2D map_diffuse;

  53.  

  54.     // VARYINGS

  55.     varying LOWP vec4 frag_color;

  56.     varying MEDP vec2 frag_uv;

  57.  

  58.     void main() {

  59.       gl_FragColor = frag_color * ( texture2D( map_diffuse, frag_uv ) );

  60.     }

  61.   ]]

  62.    

  63.   local shader = MOAIShader.new ()

  64.   shader:reserveUniforms ( 11 )

  65.  

  66.   shader:declareUniformFloat( 1, 'light_x', lightx )

  67.   shader:declareUniformFloat( 2, 'light_y', lighty )

  68.   shader:declareUniformFloat( 3, 'light_z', lightz )

  69.   shader:declareUniformFloat( 4, 'light_intensity', intensity )

  70.   shader:declareUniformFloat( 5, 'light_range', range )

  71.   shader:declareUniformFloat( 6, 'light_red', red )

  72.   shader:declareUniformFloat( 7, 'light_green', green )

  73.   shader:declareUniformFloat( 8, 'light_blue', blue )

  74.   shader:declareUniformFloat( 9, 'ambient_red', am_red )

  75.   shader:declareUniformFloat( 10, 'ambient_green', am_green )

  76.   shader:declareUniformFloat( 11, 'ambient_blue', am_blue )

  77.  

  78.   shader:setVertexAttribute( 1, 'v_pos' )

  79.   shader:setVertexAttribute( 2, 'v_uv' )

  80.   shader:setVertexAttribute( 3, 'v_col' )

  81.  

  82.   shader:load ( vsh, fsh )

  83.   return shader

  84. end

  85.  



Since it's wrapped in a function, it's just a simple couple of lines in your main lua code instead of having to set attributes every time.

Code: Select all
  1.  

  2. shader = point_lighting_shader(0.48, 0.17, 0, 1.5, 1.5, 0.77, 0.80, 1.0, 0.001, 0.19, 0.50)

  3. myProp:setShader(shader)

  4.  



I might try to work in Monkan's normal map bit as well.
User avatar
sshukul
 
Posts: 106
Joined: Wed Mar 27, 2013 1:44 am

Re: Dynamic lighting using normal maps / SpriteIlluminator

Postby Motig » Sat Apr 18, 2015 5:08 am

Looking good! Thanks for sharing the code, that'll be helpful I'm sure!
Motig
 
Posts: 92
Joined: Thu Sep 05, 2013 7:55 am

Re: Dynamic lighting using normal maps / SpriteIlluminator

Postby sshukul » Sun Jun 14, 2015 5:03 am

I switched to the normal maps shader with SpriteIlluminator posted here after some tweaks, and it definitely looks a lot better and more realistic with the folds and texturing compared to the vanilla lighting shader I had.

The only problem is that this shader seems to flip around whenever I flip the sprite around by multiplying the x-scale by -1. This is not desired behavior, as it then looks like the light is suddenly coming from the opposite direction whenever the player changes direction. This was not the case in the vanilla lighting shader, which seems to flip the character without flipping around the shader. Is it because the old one was doing the calculations in the vertex shader and the normal maps one is doing them in the fragment shader? Both the codes are posted here, anyone have any ideas?

Just to make it clear, here's what I mean with the flipping (please ignore the rough normal map, intend to fine tune later):

Correct directional lighting here
Image
Flipped around and the light source seems to flip with it:
Image

But not happening with old shader
Image
Image
User avatar
sshukul
 
Posts: 106
Joined: Wed Mar 27, 2013 1:44 am

Re: Dynamic lighting using normal maps / SpriteIlluminator

Postby andreiS » Mon Oct 26, 2015 3:30 pm

Hi, i'm a total noob when it comes to shaders, I've just had the first encounter with them actually. I also got the shader code from here: http://www.java-gaming.org/index.php?topic=27516.0 but i don't know how to add more textures/sprites and more light sources, I can only use one texture at a time. Can anyone help me?
andreiS
 
Posts: 1
Joined: Mon Oct 26, 2015 3:25 pm

Re: Dynamic lighting using normal maps / SpriteIlluminator

Postby sshukul » Tue Feb 27, 2018 10:23 pm

Resurrecting thread as per discussion with @naturally on Slack.

Code: Select all
  1.  

  2. --- Simulates light coming onto the sprite from a single point, with additional options to simulate

  3. --- ambient background light, coloring a sprite black before lighting (eg for shadows), or simulating

  4. --- a light that moves with the sprite for example light from a torch that a character is carrying that

  5. --- moves with it

  6. -- @param lightx    The x coord of the light origin point

  7. -- @param lighty    The y coord of the light origin point

  8. -- @param lightz    The y coord of the light origin point

  9. -- @param intensity The brightness of the light.

  10. -- @param range     How far the light travels before it starts to taper off in brightness

  11. -- @param red       The red component of light color

  12. -- @param green     The green component of light color

  13. -- @param blue      The blue component of light color

  14. -- @param am_red    The red component of ambient background light color

  15. -- @param am_green  The green component of ambient background light color

  16. -- @param am_blue   The blue component of ambient background light color

  17. -- @param move_with_prop   Does the light source point move along with the sprite or stay stationary?

  18. -- @param is_black  Should we color the sprite black before lighting it (this is useful for simulating shadows etc.)

  19.  

  20. function point_lighting_shader_desktop(lightx, lighty, lightz, intensity, range, red, green, blue, am_red, am_green, am_blue, move_with_prop, is_black)  

  21.   local fsh = [[  

  22.     varying MEDP vec2 uvVarying;

  23.     varying MEDP vec4 positionVarying;

  24.     varying MEDP vec4 colourVarying;

  25.    

  26.     varying MEDP vec4 light;

  27.     varying MEDP vec3 lightColour;

  28.  

  29.     uniform sampler2D diffuseMap;

  30.     uniform sampler2D normalMap;

  31.  

  32.     uniform float light_intensity, light_range;

  33.    

  34.     uniform MEDP float ambientColourR;

  35.     uniform MEDP float ambientColourG;

  36.     uniform MEDP float ambientColourB;

  37.    

  38.     uniform MEDP float is_black;

  39.    

  40.     uniform mat4 worldMatrix;               // Sum of all MOAITransforms on object

  41.    

  42.     vec2 resolution = vec2(1.0, 1.0);

  43.     vec3 attenuation = vec3(1.0, 1.0, 1.0);

  44.    

  45.  

  46.     void main()

  47.     {

  48.       //sample color & normals from our textures

  49.       vec4 color = texture2D(diffuseMap, uvVarying);

  50.       vec3 nColor = texture2D(normalMap, uvVarying).rgb;

  51.  

  52.       //normals need to be converted to [-1.0, 1.0] range and normalized

  53.       vec3 normal = normalize(nColor * 2.0 - 1.0);

  54.  

  55.       //here we do a simple distance calculation

  56.       vec3 deltaPos = vec3(light.xy - positionVarying.xy, light.z );

  57.      

  58.       float vertex_dist = length (deltaPos);

  59.      

  60.       //gl_FragColor = vec4(deltaPos, light.a);

  61.       // Decay light factor depending on the vertex distance from light source

  62.       float decay = max( 0.0, (light_range - vertex_dist )/ light_range ) * 2.0;

  63.  

  64.       vec3 lightDir = normalize(deltaPos);

  65.       float lambert = clamp(dot(normal, lightDir), 0.0, 1.0);

  66.  

  67.       //now let's get a nice little falloff

  68.      float d = sqrt(dot(deltaPos, deltaPos));      

  69.      float att = ( attenuation.x + (attenuation.y*d) + (attenuation.z*d*d) );

  70.  

  71.      vec3 ambientColor = vec3(ambientColourR, ambientColourG, ambientColourB);

  72.      

  73.      vec3 result = ambientColor + (lightColour.rgb * lambert * light_intensity * decay) * att;

  74.      result *= color.rgb;

  75.      if(is_black == 1.0)

  76.        result *= vec3(.35, .35, .35);

  77.    

  78.      vec3 resCol = colourVarying.rgb * result.rgb;

  79.      gl_FragColor = vec4(colourVarying.rgb * result.rgb, colourVarying.a * color.a);

  80.    }

  81.  ]]

  82.  

  83.  local vsh = [[

  84.    attribute vec4 position;

  85.    attribute vec2 uv;

  86.    attribute vec4 colour;

  87.    

  88.    uniform MEDP float lightX;

  89.    uniform MEDP float lightY;

  90.    uniform MEDP float lightZ;

  91.  

  92.    uniform MEDP float lightColourR;

  93.    uniform MEDP float lightColourG;

  94.    uniform MEDP float lightColourB;

  95.    

  96.    uniform MEDP float moveWithProp;

  97.  

  98.    uniform mat4 worldMatrix;               // Sum of all MOAITransforms on object

  99.    

  100.    varying vec2 uvVarying;

  101.    varying MEDP vec4 colourVarying;

  102.    varying vec4 positionVarying;  

  103.    varying vec4 light;

  104.    varying vec3 lightColour;

  105.    

  106.    void main () {

  107.      uvVarying = uv;

  108.        

  109.      positionVarying = position;

  110.      if(moveWithProp > 0.0)

  111.        light = positionVarying + vec4(lightX, lightY, lightZ, 1.0); // * worldMatrix;

  112.      else

  113.        light = vec4(lightX, lightY, lightZ, 1.0); // * worldMatrix;

  114.        

  115.      lightColour = vec3(lightColourR, lightColourG, lightColourB);

  116.  

  117.      colourVarying = colour;

  118.      

  119.      gl_Position = position;          

  120.    }

  121.  ]]

  122.  

  123.  local program = MOAIShaderProgram.new ()

  124.  

  125.  program:setVertexAttribute ( 1, 'position' )

  126.  program:setVertexAttribute ( 2, 'uv' )

  127.  program:setVertexAttribute ( 3, 'colour' )

  128.  

  129.  program:reserveUniforms ( 16 )  

  130.  program:declareUniform( 1, 'diffuseMap', MOAIShaderProgram.UNIFORM_TYPE_INT )

  131.  program:declareUniform( 2, 'normalMap', MOAIShaderProgram.UNIFORM_TYPE_INT )

  132.  program:declareUniform( 3, 'lightX', MOAIShaderProgram.UNIFORM_TYPE_FLOAT )

  133.  program:declareUniform( 4, 'lightY', MOAIShaderProgram.UNIFORM_TYPE_FLOAT )

  134.  program:declareUniform( 5, 'lightZ', MOAIShaderProgram.UNIFORM_TYPE_FLOAT )

  135.  program:declareUniform( 6, 'lightColourR', MOAIShaderProgram.UNIFORM_TYPE_FLOAT )

  136.  program:declareUniform( 7, 'lightColourG', MOAIShaderProgram.UNIFORM_TYPE_FLOAT )

  137.  program:declareUniform( 8, 'lightColourB', MOAIShaderProgram.UNIFORM_TYPE_FLOAT )

  138.  program:declareUniform( 9, 'light_intensity', MOAIShaderProgram.UNIFORM_TYPE_FLOAT )

  139.  program:declareUniform( 10, 'light_range', MOAIShaderProgram.UNIFORM_TYPE_FLOAT )

  140.  program:declareUniform( 11, 'ambientColourR', MOAIShaderProgram.UNIFORM_TYPE_FLOAT )

  141.  program:declareUniform( 12, 'ambientColourG', MOAIShaderProgram.UNIFORM_TYPE_FLOAT )

  142.  program:declareUniform( 13, 'ambientColourB', MOAIShaderProgram.UNIFORM_TYPE_FLOAT )

  143.  program:declareUniform( 14, 'moveWithProp', MOAIShaderProgram.UNIFORM_TYPE_FLOAT )

  144.  program:declareUniform( 15, 'isBlack', MOAIShaderProgram.UNIFORM_TYPE_FLOAT )

  145.  program:declareUniform( 16, 'worldMatrix', MOAIShaderProgram.UNIFORM_TYPE_FLOAT, MOAIShaderProgram.UNIFORM_WIDTH_MATRIX_4X4 )  

  146.  

  147.  program:load ( vsh, fsh )

  148.  program:reserveTextures ( 2 )

  149.  program:setTexture ( 1, 1, 1 )

  150.  program:setTexture ( 2, 2, 2 )

  151.  

  152.  program:reserveGlobals(1)  

  153.  program:setGlobal ( 1, MOAIShaderProgram.GLOBAL_MODEL_TO_WORLD_MTX , 16, 1 )

  154.  

  155.  local shader = MOAIShader.new ()

  156.  shader:setProgram(program)    

  157.  shader:setUniform( 1, 0 )

  158.  shader:setUniform( 2, 1 )

  159.  shader:setUniform( 3, lightx )

  160.  shader:setUniform( 4, lighty )

  161.  shader:setUniform( 5, lightz )

  162.  shader:setUniform( 6, red)

  163.  shader:setUniform( 7, green )

  164.  shader:setUniform( 8, blue )

  165.  shader:setUniform( 9, intensity / 5)

  166.  shader:setUniform( 10, range )

  167.  shader:setUniform( 11, am_red)  

  168.  shader:setUniform( 12, am_green )

  169.  shader:setUniform( 13, am_blue )

  170.  

  171.  if move_with_prop ~= nil then

  172.    shader:setUniform( 14, 1.0)

  173.  else

  174.    shader:setUniform( 14, 0.0)

  175.  end

  176.  

  177.  if is_black then

  178.    shader:setUniform( 15, 1.0)

  179.  else

  180.    shader:setUniform( 15, 0.0)

  181.  end

  182.  

  183.  return shader

  184. end

  185.  



So here's the final shader I had for point lighting with normal maps, in case anyone needs it. It worked really well pre Moai 1.6 but some of the rendering pipeline stuff changed in 1.6 so it still works but I had to comment out the // * worldMatrix; multiplication as it is broken now.

What this means is that right now the lighting looks great but if you do things like flipping the sprite, then the lighting point flips with it instead of adjusting accordingly based on the GLOBAL_MODEL_TO_WORLD_MTX.

If someone could get this to work again, I would really appreciate as I've been trying my best and failing with my limited knowledge of shaders and the Moai rendering pipeline.

The final result with the normal maps (generated using SpriteIlluminator) looks like this:

Image
User avatar
sshukul
 
Posts: 106
Joined: Wed Mar 27, 2013 1:44 am


Return to Game Development

Who is online

Users browsing this forum: No registered users and 1 guest

cron

x