How to hide and show scenes using three.js - three.js

I am creating scene using Three.js webgl library. I am having a texture in the first scene. Clicking on this texture, i am trying to navigate to the next scene. I have written a function to handle this scenario to move to the next scene. Clicking on this texture, it is moving to the second scene as expected, but it shows on top of the first scene. So, i would like to know How to programmatically show or hide scenes.

You can use something like:
function setVisibility (parent, node)
{
node.visible = parent.visible;
}
function traverseVisibility (parent, callback)
{
var child, i, num_children = parent.children.length;
for (i = num_children - 1; i >= 0; i--)
{
child = parent.children[i];
callback (parent, child);
traverseVisibility (child, callback);
}
}
yourNode.visible = false; // or true
traverseVisibility (yourNode, setVisibility);

Related

Is there a way to implement the Three.js effect composer into Aframe with VR mode disabled?

Has anyone had any luck implementing bloom effects or other post-processing effects from https://threejs.org/docs/#examples/en/postprocessing/EffectComposer into a scene?
I’ve come across this a few times and tried to use it but can’t seem to make it work with the latest version of Aframe https://github.com/wizgrav/aframe-effects
I know it's not possible in VR yet and depends on Three work, but is there a way to use them if I'm not intending on my scene being used in VR or AR mode?
If you don't need VR or AR mode, you can use the effect composer "directly" (check out Don McCurdys gist):
Create a THREE.EffectComposer object,
add passes, and
call the composers render on each renderloop .
The "hacky" part is in 3, because you need to override a-frames default behavior.
// assuming this is a component
init: function() {
const renderer = this.sceneEl.renderer;
this.composer = new THREE.EffectComposer(renderer);
// add some passes ..
// (...)
// override `render`
this.bind();
},
// we need to keep the timestamps for the composer.render() function
tick: function (t, dt) {
this.t = t;
this.dt = dt;
},
// Bind the EffectComposer to the A-Frame render loop.
bind: function () {
const renderer = this.sceneEl.renderer;
const render = renderer.render;
const system = this;
let isDigest = false;
renderer.render = function () {
if (isDigest) {
render.apply(this, arguments);
} else {
isDigest = true;
system.composer.render(system.dt);
isDigest = false;
}
};
Check it out in this glitch;
As mentioned, all credit to Don and his gist.
Keep in mind - a-frame does not have most stuff from the threejs examples, so you may need to include them separately (like i did with the passes and shaders in my glitch)

How to do Zooming individually with two PerspectiveCameras with their two objects?

I have two objects in right and left side of window.
I want to zoom those objects individually when I hover it.
var itsLeftControls, itsRightControls;
itsRightControls = new THREE.TrackballControls(itsRightCamera);
itsLeftControls = new THREE.TrackballControls(itsLeftCamera);
document.getElementById('SubContainerLeft').onmouseover = function () {
aMouseOverActivate(itsLeftControls);
aMouseOverDeactivate(itsRightControls);
};
document.getElementById('SubContainerRight').onmouseover = function () {
aMouseOverActivate(itsRightControls);
aMouseOverDeactivate(itsLeftControls);
};
function aMouseOverActivate(theControl)
{
theControl.zoomSpeed = 0.8;
}
function aMouseOverDeactivate(theControl)
{
theControl.zoomSpeed = 0.0;
}
function animateLeft()
{
requestAnimationFrame(animateLeft);
renderLeft();
}
function renderLeft()
{
itsLeftControls.update();
itsLeftRenderer.render(itsLeftScene, itsLeftCamera);
}
function animateRight()
{
requestAnimationFrame(animateRight);
renderRight();
}
function renderRight()
{
itsRightControls.update();
itsRightRenderer.render(itsRightScene, itsRightCamera);
}
if I hover in left side and try to zoom with mouse scrolling wheel, it is working fine. after that when I hover in right side, I can see that same zooming effect in right side also without scrolling mouse.
How to fix this?
TrachballControls take a optional second argument that is the dom element onto which it will attach the mouse event listeners.
If this argument is not supplied, it will attach the event listeners to the document.
This means that both your trackball controls are listening for events on the document (rather than their respective containers which I think you want.)
So just send your scene container divs as the second arguments to TrackballControlls and you should be good to go.
var leftContainer = document.getElementById('SubContainerLeft');
var rightContainer = document.getElementById('SubContainerRight');
var itsRightControls = new THREE.TrackballControls(itsRightCamera, rightContainer);
var itsLeftControls = new THREE.TrackballControls(itsLeftCamera, leftContainer);

Stop rotating for a click right on the game object

I have a unity game and in it, a rotating game object, which increases its speed when it is clicked.
My problem is that the game does not work as I want it to. Right now, if I click any part of the screen, it increases the game object's speed of rotation. On the other hand, if I keep my finger on the screen, the game object starts to slow down and then starts rotating in the opposite direction.
I want the rotation of the object to increase when I click on it, not just if I click on any part of the screen. Furthermore, I don't know why holding down reverses the direction of rotation.
var speed = 1;
var click = 0;
Screen.orientation = ScreenOrientation.LandscapeLeft;
function Update (){
{
transform.Rotate(0,0,speed);
}
if(Input.GetMouseButton(0))
{
if(speed != 0)
{
speed = 0;
} else {
click++;
speed = click;
}
You must use Input.GetMouseButtonUp or Input.GetMouseButtonDown, NOT A Input.GetMouseButton, this method used for clamping.
Try use this code:
var speed = 1;
var click = 0;
Screen.orientation = ScreenOrientation.LandscapeLeft;
function Update (){
{
transform.Rotate(0,0,speed);
}
if(Input.GetMouseButtonDown(0))
{
if(speed != 0)
{
speed = 0;
} else {
click++;
speed = click;
}
A few issues here:
Firstly:
To increase speed upon clicking on the object only, use raycasting from camera, and check if it hits your object. Your object needs need a collider component on it for this to work.
RaycastHit hit;
Ray ray = camera.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit))
{
Transform objectHit = hit.transform;
objectHit.Rotate(0,0,speed);
}
Refer to https://docs.unity3d.com/Manual/CameraRays.html -> Raycasting section, for more information.
Secondly:
Input.GetMouseButtonDown(..) // returns true at the instance your mouse button transits from up to down
Input.GetMouseButton(..) // returns true as long as your mouse button is held down
Use Input.GetMouseButtonDown(..) in your Update() method if you want to to do something when you click on it.

Tracking frames and/or time during an animation

Can I call a function(one that will make another object visible/invisible) on a specific animation frame or time? I would like to have arrows describe the movement of the animation at certain times during the animation. While I can just make them visible when I start the animation and make them invisible when the animation stops, I would like to specify ranges inside the animation to do this
playPatientAnim: function (anim, callback) {
var pending = 1;
var me = this;
var finish = callback ? function () {
if (pending && !--pending) {
callback.call(me, anim);
}
} : null;
me.currentPatient.skinned.forEach(function (mesh) {
mesh.animations.forEach(function(anim){
anim.stop();
});
});
me.currentPatient.skinned.forEach(function (mesh) {
var animation = mesh.animations[anim];
animation.stop();
if (animation) {
pending++;
animation.onComplete = finish;
animation.play();
}
});
if (finish) {
finish();
}
}
You can make a mesh visible or invisible ( mesh.visible = false; //or true ). To change visibility at certain time you could use timestamp:
new Date().getTime() and calculate how you want to do the sequence of your animation.

How to transform a gameObject to a desired position and return to starting position?

I'm new to UnityScript and I have the following code that does a forward movement of a gameObject on "Z" axis, but needs some refinement. Let me explain.
The script runs when a GUI.Button is clicked. The gameObject starts moving...till infinity.
I have tried to make it move till a desired pos, (e.g. an empty gameObject pos) but didn't work at all.
How should I refine this code snippet to move the gameObject to a desired position on a first GUI.Button click and return to starting position on back click?
Furthermore, is it possible to have this movement made step by step on same GUI.Button clicks?
Here is the code snippet:
#pragma strict
// member variables (declared outside any function)
var startPos: Vector3;
var endPos : Vector3;
var cachedTransform : Transform;
private static var isforward = false;
// save pos before moving:
startPos = transform.position;
// make the gameObject transform, then restore the initial position when needed:
transform.position = startPos;
function Awake() {
startPos = transform.localPosition;
}
function Start() {
cachedTransform = transform;
startPos = cachedTransform.position;
}
function FixedUpdate() {
if(isforward){
var translation : float;
if (cachedTransform.position.x == endPos)
{
cachedTransform.position = startPos;
}
else
{
translation = Time.deltaTime * 2;
cachedTransform.Translate(0, 0, translation);
cachedTransform.Translate(Vector3.forward * translation);
}
}
}
static function doforward ()
{
isforward = !isforward;
}
Thank you all in advance for your answers.
You can easily move a game object if you attach a script to it, then (in JavaScript)
#pragma strict
var startPosition;
// Record the starting position when the scene loads
function Start () {
startPosition = gameObject.transform.position;
}
// Call this to move you object to wherever
function moveObject () {
var newPos = new Vector3 (10,20,0); //(where ever you need it to go)
gameObject.transform.position = newPos;
}
// Call this to move the object to starting position, using variable we made at start
function moveToStart () {
gameObject.transform.position = startPosition;
}
Then you just call those functions when you need to move the object around.
You might want to look into the Vector3.Lerp method. This takes two vectors (points) and a float as parameters, then gives you back a point that's a fraction of the way between them. So for example, Lerp(from, to, 0.3f) will give you a point 30% of the way between the two points. Then once you have this, all you need to do is set your object's transform.

Resources