www.jpct.net

jPCT-AE - a 3d engine for Android => Support => Topic started by: kiffa on August 01, 2012, 08:36:40 am

Title: Does jPCT-AE support shadow?
Post by: kiffa on August 01, 2012, 08:36:40 am
There is a ShadowHelper  class in jPCT but not in jPCT-AE.

I want to use shadow in my world with jPCT-AE, is there any simple way to do this?

Thanks!
Title: Re: Does jPCT-AE support shadow?
Post by: EgonOlsen on August 01, 2012, 08:43:41 am
The short answer would be: No, not yet. There's no official support for shadow mapping in jPCT-AE. The long answer would be: Yes, but...you would have to write the shaders for that by yourself (which might not be the "simple way" that you are looking for).
There is the option to render the depth map into a texture encoded in it's ARGB values, but there are no built-in shaders yet to take this texture and use it for shadow mapping. This is about to be added someday, but ATM, it'll be too slow on anything but the high end anyway.
Title: Re: Does jPCT-AE support shadow?
Post by: Thomas. on August 01, 2012, 09:02:47 pm
Take in account Mali-400 does not support high precision floating point number in fragment shader. So if you have some device with Mali-400 or you want full compatibility with this GPUs, you can not use default depth shader. But its easy rewrite them (my rewritten shader is little bit faster). If you want, I can share them. And if you want some numbers, one per-pixel point light, one per-pixel spot light, post-processing image effects (distortion), one rendering of depth buffer, one rendering of motion vectors and motion blur effect (4 samples)... game is running 49fps on Samsung Galaxy S3, without motion blur (and everything needed for this effect) it is 60fps. So your app can running smooth with shadows too. But take in account, these numbers come from high-end phone.
Title: Re: Does jPCT-AE support shadow?
Post by: kiffa on August 02, 2012, 04:53:20 am
Thanks, Egon! But i really hope you can support this in jPCT-AE. ;D I think it may be helpful for someone.


Take in account Mali-400 does not support high precision floating point number in fragment shader. So if you have some device with Mali-400 or you want full compatibility with this GPUs, you can not use default depth shader. But its easy rewrite them (my rewritten shader is little bit faster). If you want, I can share them. And if you want some numbers, one per-pixel point light, one per-pixel spot light, post-processing image effects (distortion), one rendering of depth buffer, one rendering of motion vectors and motion blur effect (4 samples)... game is running 49fps on Samsung Galaxy S3, without motion blur (and everything needed for this effect) it is 60fps. So your app can running smooth with shadows too. But take in account, this numbers comes from high-end phone.

Thanks, Thomas! Please share them! You can update them to a public-web-store, or send to me by email: kiffa.gx@.gmail.com. Thanks again!
Title: Re: Does jPCT-AE support shadow?
Post by: EgonOlsen on August 02, 2012, 07:34:13 am
I wasn't aware that the default depth shader doesn't work on Mali-crapsets. It would like to see the working version too!
Title: Re: Does jPCT-AE support shadow?
Post by: Thomas. on August 02, 2012, 07:26:30 pm
Here you are. Depth is packet into RGBA and have to be unpacked. I see I wanted to test performance of smoothstep(...)... Keep in mind, this is the simplest part of shadows. jPCT-AE not support projectors ATM, you will have to use Camera and write shaders for shadows.

vertex shader
Code: [Select]
uniform mat4 modelViewProjectionMatrix;

attribute vec4 position;

varying float depth;

uniform float nearPlane;
uniform float farPlane;

void main() {
vec4 pos = modelViewProjectionMatrix * position;
depth = (pos.z - nearPlane) / (farPlane - nearPlane); // TODO performance test of smoothstep(min, max, x)
gl_Position = pos;
}

fragment shader
Code: [Select]
precision highp float;

varying float depth;

const vec4 PACK_FACTORS = vec4(256.0 * 256.0 * 256.0, 256.0 * 256.0, 256.0, 1.0);
const vec4 BIT_MASK = vec4(0.0, 1.0 / 256.0, 1.0 / 256.0, 1.0 / 256.0);

void main() {
vec4 packedValue = fract(PACK_FACTORS * depth);
packedValue -= packedValue * BIT_MASK;
gl_FragColor = packedValue;
}

unpack
Code: [Select]
const vec4 UNPACK_FACTORS = vec4(1.0 / (256.0 * 256.0 * 256.0), 1.0 / (256.0 * 256.0), 1.0 / 256.0, 1.0);
... main
highp float depth = dot(texture2D(textureUnit0, texCoord), UNPACK_FACTORS);

I wasn't aware that the default depth shader doesn't work on Mali-crapsets. It would like to see the working version too!

Wokrs, but with a lot of artefacts.

Edit: I tested smoothstep(...) now. Ten times rendering of depth and fps is 22 with linear interpolation and also with smoothstep. And Egons shader is on 29 (with artefacts), but before it was slower ???

Eidt2: Oh before I used full resolution of depth texture (now I'm using half), so it was four times more fragment calculations, whereas vertex calculations are still same for any resolution...
Title: Re: Does jPCT-AE support shadow?
Post by: EgonOlsen on August 02, 2012, 11:09:43 pm
Thanks. I guess i'll have to update the depth shader code with these changes then.
Title: Re: Does jPCT-AE support shadow?
Post by: raft on March 06, 2013, 10:35:00 am
The short answer would be: No, not yet. There's no official support for shadow mapping in jPCT-AE.
any near plans to implement official shadow support? current hardware is quite fast and even getting faster.

I'm making a desktop version of sky maze and will soon add shadows. will be great if same code works for android too.

furthermore, android based game console* OUYA  (http://www.ouya.tv/devs/) will be released on june which possibly will have more than enough power. specs are at the bottom of that page. (I'm still not a HW guy, so specs dont make much sense to me ;)) I'm also planning to publish sky maze on that platform.

*thanks to paul for tip :) 
Title: Re: Does jPCT-AE support shadow?
Post by: EgonOlsen on March 06, 2013, 11:03:35 am
I'm still waiting to Thomas. to create a version that he considers to be fine... ;) IIRC, the last version still had some issues. I've roughly scheduled shadows for 1.26, which should be ready in may or june. Depending on how it goes, there might be some beta earlier. At least that's the current plan...

Regarding OUYA's hardware: It's a NVidia Tegra 3, which is the same thing that i have in my Nexus 7. To be honest, i'm not very impressed with what NVidia delivered with this chip. It's depth buffer accuracy is laughable by default, it's rendering accuracy is below everything else that i've seen on a mobile device and it's performance is fine but nothing to get crazy about. It should be fast enough to do basic shadow mapping though.
Title: Re: Does jPCT-AE support shadow?
Post by: raft on March 06, 2013, 11:14:30 am
cool, glad to hear that it's in the roadmap :)
Title: Re: Does jPCT-AE support shadow?
Post by: Thomas. on March 06, 2013, 04:42:06 pm
I'm still waiting to Thomas. to create a version that he considers to be fine... ;)

Oh, shadows have currently the lowest priority for me, first I have to complete app to work and projects to school, then I wanted to look at the native Bullet and make support for jPCT... I can look at the shadows next week. There was a bug when everything outside of the shadow projector frustum are darker. This is visible in the screenshots and video of my game.
Title: Re: Does jPCT-AE support shadow?
Post by: raft on June 20, 2013, 10:56:38 pm
I guess shadows won't be ready at OUYA launch (which is 25th of June), right?
Title: Re: Does jPCT-AE support shadow?
Post by: EgonOlsen on June 21, 2013, 07:42:29 am
I guess shadows won't be ready at OUYA launch (which is 25th of June), right?
No, they won't, i'm afraid. 1.26 got already bigger than it was supposed to be. I'll try to release it within the next 1 or 2 weeks and postpone the shadows to 1.27.
Title: Re: Does jPCT-AE support shadow?
Post by: drdla on April 27, 2016, 11:17:44 am
It's few years from thic conversation.

Is there now any way to create shadows in jPCT-AE?

Thank you!
Title: Re: Does jPCT-AE support shadow?
Post by: EgonOlsen on April 27, 2016, 12:11:40 pm
Yes, but it's still in early beta and (maybe) not finished yet. It's also not documented ATM. However, in my tests, it does what it's supposed to do, so it might not change that much before the official release. If you want to play around with it, you can grab the beta here: http://jpct.de/download/beta/jpct_ae.jar (http://jpct.de/download/beta/jpct_ae.jar)
It includes a ShadowHelper-class in the util-package. If you want to combine shadows with your own shaders, you can do that as well, but it's not documented yet. You can look into the default shaders in the jar to get an idea of how to do it though.
But as said, things might change before the next official release.
Title: Re: Does jPCT-AE support shadow?
Post by: drdla on April 27, 2016, 12:33:34 pm
Thank you.
I understand that it's not documented, but is there any sample of use shadows?
 
And is there any way to use it with Vuforia and get shadow on real table for example?
Something like
Code: [Select]
mShadowHelper.addReceiver(table);
And how I can initialize buffer whitch is parametr of ShadowHelper?
This example from wiki donīt work.
Code: [Select]
buffer = new FrameBuffer(800, 600, FrameBuffer.SAMPLINGMODE_NORMAL);
buffer.disableRenderer(IRenderer.RENDERER_SOFTWARE);
buffer.enableRenderer(IRenderer.RENDERER_OPENGL, IRenderer.MODE_OPENGL);
buffer.setPaintListener(this);

Thank you!
Title: Re: Does jPCT-AE support shadow?
Post by: EgonOlsen on April 27, 2016, 12:47:27 pm
Yes, there is an example, but I can't access it right now. I'll post it later.

About the table...yes, in theory. But it would require that you model the table in the scene. You can't project a shadow on nothing and a table in a 2D camera image is actually nothing to a 3D engine.

The FrameBuffer is the normal FrameBuffer of your app. You just have to make sure to use OpenGL ES 2.0, not 1.x.
Title: Re: Does jPCT-AE support shadow?
Post by: drdla on May 05, 2016, 09:22:44 am
Can you post an example please?

Thank you.
Title: Re: Does jPCT-AE support shadow?
Post by: EgonOlsen on May 05, 2016, 09:37:36 pm
This should help: http://www.jpct.net/forum2/index.php/topic,4333.msg32272.html#msg32272 (http://www.jpct.net/forum2/index.php/topic,4333.msg32272.html#msg32272)
Title: Re: Does jPCT-AE support shadow?
Post by: maggie on May 13, 2016, 05:13:12 pm
About the table...yes, in theory. But it would require that you model the table in the scene. You can't project a shadow on nothing and a table in a 2D camera image is actually nothing to a 3D engine.

In vuforia case, is it necessary to set the orientation of the projector for the shadow?
Title: Re: Does jPCT-AE support shadow?
Post by: EgonOlsen on May 13, 2016, 05:36:11 pm
It's always needed to set a proper orientation for the projector. That's not related to Vuforia.
Title: Re: Does jPCT-AE support shadow?
Post by: maggie on May 14, 2016, 12:42:25 am
I mean in this particular example, projector orientation is not set explicitly. Perhaps because it's already set by the lib?
This should help: http://www.jpct.net/forum2/index.php/topic,4333.msg32272.html#msg32272 (http://www.jpct.net/forum2/index.php/topic,4333.msg32272.html#msg32272)

So generally, how the projector should be treated to make the shadow works with vuforia marker/image target?
Title: Re: Does jPCT-AE support shadow?
Post by: EgonOlsen on May 14, 2016, 10:28:29 am
It should point to the object IMHO. Think of it as a spot light.
Title: Re: Does jPCT-AE support shadow?
Post by: maggie on May 17, 2016, 08:48:05 pm
So I tried to integrate the shadow with vuforia, without any advanced customization. Just like how the example does it, I put the projector in the sun's location and look at to the ground.

Code: [Select]
public Constructor() {
   sun = new Light(world);
   sun.setIntensity(250, 250, 250);
   
   ...
   ...

   object = Object3D.mergeAll(Loader.loadOBJ(
mActivity.getAssets().open("teapot.obj"), null, 250f));
   object.build();

   world.addObject(object);
   
   // Shadow receiver
   ground = ExtendedPrimitives.createPlane(100, 10);
   ground.setAdditionalColor(new RGBColor(255, 255, 255));
   ground.setTransparency(20);
   ground.build();
   ground.rotateX( (float) Math.PI/2f );
   
   world.addObject(ground);
   cam = world.getCamera();

   SimpleVector sv = new SimpleVector();
   sv.set(ground.getTransformedCenter());
   sv.x += 50;
   sv.z += 400;
   sun.setPosition(sv);
   
}

public void onDrawFrame(GL10 gl) {
   ...
   sh.updateShadowMap(fb, world);
   ...
}

public void onSurfaceChanged(GL10 gl, int width, int height) {
        ...   
       
     // set projector and shadow
    Projector projector = new Projector();
projector.setClippingPlanes(0.75f, 3000f);
float fov = projector.convertDEGAngleIntoFOV(90);
projector.setFOV(fov);
projector.setYFOV(fov);

projector.setPosition( sun.getPosition() );
projector.lookAt(ground.getTransformedCenter());

sh = new ShadowHelper(fb, projector, 1024);
sh.setAmbientLight(new RGBColor(50, 50, 50));
sh.addCaster(object);
sh.addReceiver(ground);

    ...
}

Here is the screen capture:
(http://s32.postimg.org/aast52el1/upload.png)

It looks like not mapped correctly.. any idea what possibly went wrong?
Title: Re: Does jPCT-AE support shadow?
Post by: EgonOlsen on May 17, 2016, 11:19:16 pm
Looks like as if that teapot model isn't closed at the top. Try ShadowHelper.setCullingMode(true); and see if that helps.
Title: Re: Does jPCT-AE support shadow?
Post by: maggie on June 10, 2016, 08:58:13 pm
Hi again, just have time now to follow up this one ;D
So I modified my code a bit, by adding sh.setCullingMode(true) as suggested, and set the sun+projector position 400 unit above the ground, while (x,y) remains (0,0). Having this value, I think the shadow should be exactly below the teapot. But here is what I got on Xperia M:
(http://s33.postimg.org/eck040oe7/xperiam.png)

Also, with the same code, I'm not sure why I got different shadow behaviour when I run my code on Nexus 9 tablet:
(http://s33.postimg.org/5kqz3bwjj/nexus9.png)

any idea why?  :-\
Title: Re: Does jPCT-AE support shadow?
Post by: EgonOlsen on June 11, 2016, 09:48:21 am
I think this has something to do with the fact that you are doing this camera/AR thing somehow...I don't know how it's related, but the shadow calculations themselves are deterministic, so if the input parameters are actually the same, the output should be as well. Try to position the projector based on the teapots Position in world space every frame and see if that changes something.