Aframe FPS adjustment using tick handler - aframe

A newbie here. I am trying to adjust the FPS from default 60, 72 90 to 30 using the tick handler. After spending some substantial time trying to understand game loops, FPS and in this case the tick handler i still cannot grasp on how to achieve this. This is the code i have been fiddling with but no joy.
AFRAME.registerComponent('FPS-30', {
init: function () {
this.tick = AFRAME.utils.throttleTick(this.tick, 33, this);
},
tick: function (t, dt) {}
});
and
AFRAME.registerComponent('FPS-30', {
tick: utils.throttleTick(function(t, dt){}, 33);
});
As documented in here, and also here. I have attached the component to the a-scene element and also a-entity but no success so far! Can someone shine some light on how to achieve this? Thanks!

You cannot lock FPS to a specific value. A-Frame render loop fires at a rate defined by requestAnimationFrame (usually 60) in 2D mode and the native refresh rate of the available headset in VR mode (60, 72, 90...). Not advised for applications to interfere because there are time sensitive operations like frame submission, re-projections or sensor readings that might not work as expected if not properly synced.
You can though control the rate that a component tick method fires via throttleTick. Below an example of it working. Notice console messages printing the time elapsed between tick calls in ms:
https://glitch.com/edit/#!/congruous-weak-thumb?path=index.html%3A12%3A10

Related

Hi, I need an algorithm to tell a sprite to change the image when the in-game text finishes appearing (in GameMaker studio 1.4)

I need an algorithm to tell a sprite to end as soon as the text finishes appearing, basically I need to make a cutscene which describes the beginning of a story within the game, but since I'm using gamemaker, I don't know how to do it, can someone help me?
For cutscenes and automated image sequences you usually have some sort of variables, that handle the states and counters for displaying sprite's sub-images.
But firstly I like to use mainly two main time-related variables in my controller object usually named sys (counter and counterTime):
c = 0; // and for stepEvent: c += 1; // so it goes +1 per step
ct = 0; // and for stepEvent: ct+= 1 / room_speed; // so it goes +1 per second
During the cutscene you might want to stop everything else that moves:
is_cutscene = false; // objects might not be able to move when this is true
// you still have to implement that by yourself
// player can't move if is_cutscene, nothing happens!!
(except cutscene buttons etc...)
So now when the player gets to a 'cutscene'y object and triggers some cutscene_1 = true eg. you can have the image/text/sound/animation object come to life/create/active... That object might have in the create event:
duration = 6; // 6 seconds the scene lasts
start_time = sys.ct // and in Step: if sys.ct > (start_time + duration)
// then -> cutscene advance/destroy
Ofcourse - this is super simple and now you could only say implement:
walk close to a pop-up object
show an image for 6 seconds
it dissappears
And it might not be big trouble to implement it... BUT THIS may also be the foundation for more advanced multi-step sequence and cuts.

Particle system does not get updated in aframe

I'm trying to create a simulation for an X-Ray Tube, where you can change the current or voltage and see the effects on the electrones and photons.
I've setup the electrones as a particle system with the aframe-particle-system-component. (https://www.npmjs.com/package/aframe-particle-system-component/v/1.1.3) Now I'm trying to change the particle system with a slider.
The problem is that the input of the slider gets registered, but it has no effect on the particle system in the scene.
The weird thing for me is, that the particle system will get changed, when I call the setAttribute() function outside of the EventListener. But than it changes only once at the beginning obviously, which does not help me.
The DOM of the entity does get updated and shows the new value of the size, when i inspect it in the console. Even the aframe inspector shows the updated size. But the actual particle system in the scene still shows the size, that i set in the html file.
Here is the js code that I'm using for this:
var elektron = document.querySelector('#ParticleSystem');
var scene = document.querySelector('a-scene');
//Var for the two sliders
var rangeCurrent = document.querySelector('#rangeCurrent');
var rangeVoltage = document.querySelector('#rangeVoltage');
var current = rangeCurrent.value;
var voltage = rangeVoltage.value;
//EventListener for the slider
rangeCurrent.addEventListener('change', function(){
current = rangeCurrent.value;
elektron.setAttribute('particle-system', {size: current});
});
There are no error messages.
I've been stuck with this problem the whole day and would be really thankful if someone could help me out!
I finally got it to work!!
In the update() function of the particle-system-component script they don't initiate the new ParticleSystem with the new data you define with setAttribute. I tried to fix it by myself for a while but couldn't get it to work. So then I used the earlier version 1.12 of the particle-system-component and now it works.

Game Maker - Turn On/Off Sound

What I have
I have two rooms: rm_home and rm_options. I have a sound: snd_Bgm. And, I have three objects: obj_bgm, obj_BtnOnClicked, and obj_BtnOffClicked.
What I want is very simple
Player can turn on/off the snd_Bgm.
What I have done
In obj_bgm, I have these events:
Create Event: set var global.sound to 1
Room Start: stop sound snd_Bgm; if global.sound == 1 then play sound snd_Bgm
In obj_BtnOnClicked, I have these events:
Left-Pressed Event: play sound snd_Bgm; set var global.sound to 1
In obj_BtnOffClicked, I have these events:
Left-Pressed Event: stop sound snd_Bgm; set var global.sound to 0
I put obj_BtnOnClicked, and obj_BtnOffClicked in rm_options, which can be accessed from rm_home. Then, I put obj_bgm in rm_home.
This is the problem
When game start, it will show rm_home and plays the snd_bgm. I go to rm_options, then click the obj_BtnOffClicked to turn off the sound, so the sound is off. But, when I go back to rm_home, the sound is on again.
What I think
I shouldn't put Create Event: set var global.sound to 1 in obj_bgm, because when rm_home start, it takes the value of var global.sound from Create Event. But, if I put Create Event in obj_BtnOnClicked or obj_BtnOffClicked, it shows a Get Error.
What should I do? Please explain your answer. Thanks.
Are your rooms and your variables persistent ?
If rm_home is not persistent, it will restart every time you leave it. So every object placed in that room will be reset, including obj_bgm, which sets your sound variable to 1. I think this is the reason the sound restarts when you come back to rm_home.
If you still want to reset the room but not this particular object, you can make it persistent. You can do it either by ticking the box in the object or through code.
If you put the create event in a button object, it will not be read until you go to the options room. So when the obj_bgm tries to set it to 1, it does not exist yet. I believe it causes the error.
I make these remarks on assumptions, but I would need to see your code or your error message to help you further.

Why Direct2D only runs at an FPS of 30 after unplugging the external power supply of my laptop?

I am new to Direct2D and recently I've found a wierd problem. When external power supply is plugged, my program runs at a steady FPS of 60, which I know may be a result of VSync; but after unplugging the external power supply for a while, my program drops to a steady FPS of 30 (I outputed the time interval between every two renderings and it shows 32ms).
And even if I plugged the power supply again, it still remains at 30 FPS until I reboot the computer.
Is it because my laptop shut down something when powered by battery which cuts the FPS to its half? If true, what can I do for it?
My laptop's OS is Windows 8.1.
Here are some code that may be helpful.
HRESULT hr = S_OK;
if (!m_pRenderTarget)
{
RECT rc;
GetClientRect(m_hwnd, &rc);
D2D1_SIZE_U size = D2D1::SizeU(rc.right - rc.left, rc.bottom - rc.top);
D2D1_HWND_RENDER_TARGET_PROPERTIES render_target_properties = D2D1::HwndRenderTargetProperties(m_hwnd, size);
//render_target_properties.presentOptions = D2D1_PRESENT_OPTIONS_IMMEDIATELY;
//↑ Tried this and it doesn't work, don't know why
// Create a Direct2D render target.
hr = m_pDirect2dFactory->CreateHwndRenderTarget(
D2D1::RenderTargetProperties(),
render_target_properties,
&m_pRenderTarget
);
}
return hr;
The mainloop looks like this:
while(msg.message!=WM_QUIT)
{
if(PeekMessage(&msg,0,0,0,PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
now_time = timeGetTime();
if(now_time - last_time >= 1000/MAX_FPS)
{
OutputDebugPrintf("%lf\n", now_time - last_time);
application->Update(now_time - last_time);
application->OnRender();
last_time = now_time;
}
}
I'm sure it takes little time in Update() and it can run at a steady frame rate of 60, so there seems to be no problem in OnRender().
Thank you!
I wasn't aware of this myself but you already guessed right: many (all?) laptops drop their refresh rate when running on battery. See google. And if vsync runs slower, so will your loop. There appear to be solutions to disable this, depending on your hardware (e.g. for Intel).
Bit late but your problem has nothing the do with the code i think. Many laptops with an nvidia video card have a system called "Battery Boost". When enabled, your can drag a slider to an fps count. When charging the battery games get vsynced to 60 fps. When not charging games get vsynced to the fps indicated by the slider:
With the top-right toggle you can disable this feature.
This menu can be found in the nvidia experience program -> settings -> games.

Measuring time taken to display an object in Flex

For e.g.
function showIt():void {
something.visible = true;
}
function init():void {
time1 = flash.utils.getTimer();
showIt();
time2 = flash.utils.getTimer();
}
<mx:st id="something" visible="false"/>
<mx:Button click="init()"/>
In the above code, I want to measure the time taken to display st on screen.
Is it enough to compute time2 - time1?
Or should I put an event handler on render?
Or some other way?
You should try out Grant Skinner's AS3 Performance Test Harness.
Here's a simple demo of it in action (from his blog):
––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
performancetests.GraphicsTests (5 iterations)
Testing different approaches for drawing.
––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
method...................................................ttl ms...avg ms
tare [3] 2 0.40
drawPath 242 48.40
drawPathShort 171 34.20
fullPath 182 36.40
reference 127 25.40
shortReference 129 25.80
withGraphics 1154 230.80
––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
In the download he has a class called "RenderTest.as", which he describes with:
Represents a render test, which times how long it takes to draw a specified DisplayObject to a BitmapData instance.
I use it all the time, it's great.
Hope that helps,
Lance
you can draw it into a BitmapData to force rendering and measure the time required.
greetz
back2dos
Using the Profiler will probably be more interesting to compare such operations.
time2 - time1 will just give you the time required to set the visible property of the UIComponent to true - which might be just a couple of lines of code. The real rendering happens at regular intervals when the UIComponent fires enterFrame event. I don't know of any method to measure the time taken to render one particular component, and I don't think you can do that.
One idea that comes to mind is to listen for ENTER_FRAME and calculate the time difference between two consecutive events (or even better: take average over, say, 100 frames) to get an estimate of the time taken for rendering the whole stage. Now repeat the experiment after removing the particular component from stage. The difference between the two timings will give you an idea of the time taken to render your component.

Resources