Dynamic Light Extension

Hi,

after a game require it and a lot of time to search, finally i found some source and here is some of my work about dynamic light. it’s old but still work and easy to use.

it’s fix version of avalon’s sample on github. here is how to use it.

shadowCaster = Sprite::create("test.png");
shadowCaster->retain();
shadowCaster->setPosition(Vec2(visibleSize.width / 2 + origin.x, visibleSize.height / 2 + origin.y));
this->addChild(shadowCaster, 10);

lightSize = 256;

dynLight = DynamicLight::create();
dynLight->retain();
//dynLight->debugDrawEnabled = true;
dynLight->setShadowCasters(shadowCaster);
dynLight->setColor(Color4B::WHITE);
dynLight->setPosition({ 300,150 });
this->addChild(dynLight, 9);

that all.

Link: https://github.com/namkazt/DynamicLight

4 Likes

hi namkazt
dynLight->setColor(Color4B::WHITE); it’s invalid

finalShadowMapSprite->setColor({ 255, 255, 255 }) in DynamicLight.cpp
it should be replaced by finalShadowMapSprite->setColor({ color.r, color.g, color.b })

I’m going to try to get this to run on v2.2.6 and see how it does.

Finding a Cocos2d-x equivalent lighting system, one that compete with LibGDX, has been a long time dream of mine.

thanks @unity2d i’ll fix that.

wow thanks for this. Been looking for this. Will try it out

Im getting an error:

cocos2d: fullPathForFilename: No file found at shaders/pass.vsh. Possible missing file.
cocos2d: fullPathForFilename: No file found at shaders/pass.vsh. Possible missing file.
Get data from file(shaders/pass.vsh) failed, error code is 3
cocos2d: fullPathForFilename: No file found at shaders/shadowMap.fsh. Possible missing file.
cocos2d: fullPathForFilename: No file found at shaders/shadowMap.fsh. Possible missing file.
Get data from file(shaders/shadowMap.fsh) failed, error code is 3
OpenGL error 0x0502 in ..\renderer\CCGLProgram.cpp cocos2d::GLProgram::initWithByteArrays 182

Error linking shader program: 'Vertex shader(s) failed to link, fragment shader(s) failed to link.
ERROR: error(#280) Not all shaders have valid object code
ERROR: error(#280) Not all shaders have valid object code
'

Error linking shader program: 'Vertex shader(s) failed to link, fragment shader(s) failed to link.
ERROR: error(#280) Not all shaders have valid object code
ERROR: error(#280) Not all shaders have valid object code
'

cocos2d: fullPathForFilename: No file found at shaders/pass.vsh. Possible missing file.
cocos2d: fullPathForFilename: No file found at shaders/pass.vsh. Possible missing file.
Get data from file(shaders/pass.vsh) failed, error code is 3
cocos2d: fullPathForFilename: No file found at shaders/shadowRender.fsh. Possible missing file.
cocos2d: fullPathForFilename: No file found at shaders/shadowRender.fsh. Possible missing file.
Get data from file(shaders/shadowRender.fsh) failed, error code is 3
OpenGL error 0x0502 in ..\renderer\CCGLProgram.cpp cocos2d::GLProgram::initWithByteArrays 182

Error linking shader program: 'Vertex shader(s) failed to link, fragment shader(s) failed to link.
ERROR: error(#280) Not all shaders have valid object code
ERROR: error(#280) Not all shaders have valid object code
'

Error linking shader program: 'Vertex shader(s) failed to link, fragment shader(s) failed to link.
ERROR: error(#280) Not all shaders have valid object code
ERROR: error(#280) Not all shaders have valid object code
'

OpenGL error 0x0502 in ..\renderer\CCTexture2D.cpp cocos2d::Texture2D::initWithMipmaps 640

'Pods To Riches.exe' (Win32): Loaded 'C:\Windows\SysWOW64\mciwave.dll'. Symbols loaded.
The thread 0x698 has exited with code 0 (0x0).
'Pods To Riches.exe' (Win32): Unloaded 'C:\Windows\SysWOW64\mciwave.dll'
cocos2d: warning: Uniform not found: resolution
cocos2d: warning: Uniform not found: upScale
cocos2d: warning: Uniform not found: accuracy
cocos2d: warning: Uniform not found: resolution
cocos2d: warning: Uniform not found: softShadows
cocos2d: warning: Uniform not found: u_texture
cocos2d: warning: Uniform not found: u_texture
cocos2d: warning: Uniform not found: u_texture2
OpenGL error 0x0502 in ..\renderer\CCTextureAtlas.cpp cocos2d::TextureAtlas::drawNumberOfQuads 689

using cocos2d-x 3.3

you need to copy shaders in resource at github to your project.

thank you @namkazt :smile:

another error :frowning: where can I find u_texture?

cocos2d: warning: Uniform not found: u_texture

hi @jvfiel please post all error logs

my source:

	Sprite * basicProjectile = Sprite::create();
	basicProjectile->setSpriteFrame(Final_ProjectileFileName);
	basicProjectile->setScale(GETSCALEX);
	basicProjectile->setName(projectile_name);
	this->addChild(basicProjectile, 2);

        basicProjectile->setPosition(winSize.width/2,winSize.height/2);

	//DYNAMIC LIGHT
	auto dynLight = DynamicLight::create();
	dynLight->retain();
	dynLight->setShadowCasters(basicProjectile);
	dynLight->setColor(Color4B::RED);
	dynLight->setPosition(basicProjectile->getPosition());
	this->addChild(dynLight, 9);

the only error is : cocos2d: warning: Uniform not found: u_texture

and I’m not seeing any dynamic light.
Is my implementation correct?

you missing that :

basicProjectile->retain();

my projectile is actually showing. My problem is the dynamic light

opss!!

if you mean your basic Projectile is a lighter
ShadowCasters is all struct that receice light and cast shadows ( and isn’t basicProjectile )
and dylight is just a point light ( if same post with shadowncaster mean it’s inside so there is no shadow )

please look at my helloworld demo scene.

1 Like

Oh ok, thanks again :smiley:

@namkazt can this extension achieve this kind of dynamic lighting?
https://www.youtube.com/watch?v=DLBxMd1naT4 skip to 0:28

@jvfiel yes, but must edit something like blur texture shadow result …

@namkazt Thanks for this
Your github demo is running fine but with one warning

cocos2d: warning: Uniform not found: u_texture