Folding rectangles to form a cube using three.js - javascript

I am trying to make a cube with given 6 faces lying on the surface as a cube net with one face movable. Something like this:
In the above picture, there are 6 faces, one face ( blue one) is movable.
One can rotate them up together along their edges to form a “net”.
Once they think they are finished, they can press a “fold it” button – all edges turn up 90 degrees to create the cube (or may not be a cube if he hasn't joined the blue face at proper position.)
Below is intermediate status after pressing "fold it" button.
After the faces are folded it should like this:
The corresponding animation is given here: http://www.mathematikus.de/10/
(somehow that link is not working on mac)
I am not sure how to go about this. Any help is appreciated.
Thanking you in advance.

You can use hierarchy of objects.
var obj1 = new THREE.Mesh(...);
var obj2 = new THREE.Mesh(...);
obj1.add(obj2);
There's a good example of it.
So, using this principle, I made animation for folding the cube, given in your question. Of course, this is not the ultimate solution, this is just a starting point.
jsfiddle example
upd: I've updated the fiddle. You can start folding by clicking the PressMe button. Animation made with Tween.js (see the foldTheCube() function)

Related

Apply a glow effect onto a THREE.TextGeometry object in Three.js

I have done quite a few hours of google search to see if this can be done
within the three.js environment.
I want to create a glow effect based on a TextGeometry and not a simple primitive like a sphere or cube object similar to the one in Lee StemKoski - shadow glow example below.
I have been reading and experimenting with Lee StemKoski examples
In particular with his Shader Glow example which as stated works well with simple objects but not with complex geometric shapes. Therefore I was wondering if there are any work arounds to this or other suggestions to how one could create a glow effect around text.
regards
w9914420
UPDATE: I decided to go with the mapping concept by attaching an image to the text I can create a glow effect. In this example I used a circle, but should not be too difficult to do a map for the text that I need.
UPDATE: 20/3/17 - So I had an attempt at using the theeX.atmosphere material to create that elusive text glow effect. Here are some of the results.
As you can see not quite there yet - the issue that I have is I am not able to smooth the vertices of the outer glow text more. I am not sure if it is possible any advice appreciated.
Native code used to create effect.
//normal text created
var geometry2 = threeLab.asset.simpleText('hello','my_font');
var materialme = new THREE.MeshNormalMaterial();
var mesh = new THREE.Mesh( geometry2, materialme );
this.scene.add( mesh );
// we create a secondary text object to use as glow
var geometry = threeLab.asset.simpleText('hello','my_font');
// clone for manipulation
var geoclone = geometry.clone();
//Thes functions are need to make it work in this case
geoclone.mergeVertices();
//geoclone.computeCentroids();
geoclone.computeVertexNormals();
geoclone.computeFaceNormals();
// part of threex library to make the glow scale and expand
THREEx.dilateGeometry(geoclone, 0.5);
var materialz = THREEx.createAtmosphereMaterial();
var meshHalo = new THREE.Mesh(geoclone, materialz );
//we have now our glow
this.scene.add( meshHalo );
So In the end I increased the bevelsegments of my secondary TextGeometry object which helped to smooth it. I was then able to encase my first text into the second like so:
With further tweaking I will be able to get the glow effect more subtle, but for now the effect works.
Thank you all for the input :)

Constrain a sprite within a polygon area in Phaser

I am creating a click and point adventure game. I have a character walking in the area but I'm unsure as to how to constrain where his feet can venture to. In the attached image, I have his feet in yellow, but I must not allow him to go outside the blue polygon area on the floor.
Any ideas on how to make this work? Please bear in mind, I'm creating a tween for the character to follow when the user clicks the screen. If he bumps into an area outside of the polygon, it will make the player (pink rectangle) stop.
I've tried arcade physics but that's only for square based sprites. I guess I have to use p2 physics? Can I use p2 physics on shapes or maybe a simple line like the polygon area?
You don't need to use any of Phasers physics engines to handle this, you can do it using the Polygon geometry object and just check if the click is within the polygon or not.
Here's an online example: http://phaser.io/examples/v2/geometry/polygon-contains
But the code is quite simple. First create a polygon (be aware about overlapping points and the way the poly winds, see docs for details):
poly = new Phaser.Polygon([ new Phaser.Point(200, 100), new Phaser.Point(350, 100), new Phaser.Point(375, 200), new Phaser.Point(150, 200) ]);
and then just check if they've clicked within it:
if (poly.contains(game.input.x, game.input.y))
{
// allow walk
}

Highstock.js diagram is working strange?

The problem is that if I turn off TB1S1 off before I turn TB2S1 and TB3S1 the dynamic line changes its shape, very strange. But if I let TB1S1 be on, and turn off TB2S1 and TB3S1, then the dynamic line stays perfect.
To see the problem, just turn off TB1S1, and look at the lines TB2S1 and TB3S1 changing its shape.
How can I solve this problem that the diagram holds its shape without being attached to TB1S1..?
[JSFiddle - Demo][1]
[1]: http://jsfiddle.net/2jccfson/1/
Kind regards,
Dler H.

Three.js custom geometry raycaster catches wrong object

I need to use my own gemetry since the default cube does not look like it should in wireframe mode (now it is made of triangles instead of squares).
So I made my own geometry and it looks ok, but the raycaster does not work as good with my own objects as it does with the built-in cubes.
var cube = new THREE.Line( getCube( 5,5, 5), new THREE.LineDashedMaterial( { color: 0x000000,dashSize: 1, gapSize: 0.1, linewidth: 2 } ),THREE.LinePieces );
where getCube() returns
var geometry = new THREE.Geometry()
See example:
http://jsfiddle.net/QHjSM/12/
6 colour filled box on the top are the defalt THREE.CubeGeometry boxes, and selecting them with raycaster works perfect, 6 wireframe are my custom geometry.
Issues:
If you try to click outside the box, but pretty close to it it will catch the box, and if you click inside the box (in the middle of it) it will not catch it neither.
But the most annoying thing is that if you click inside one box, but close to another one sometimes it catches not the wrong one.
I'm not sure can it be done better, tried all the geometry.compute... methods, but still no effect.
Good day, your custom cubes are not in fact cubes. They are just a stack of lines with no cooresponding faces. Your selection is not returning as expected due to the fact that your "cubes" indeed have gapping holes right threw them. What you can do is in your getCube function after you've built the vertices you can then build all your faces in a similar way.
Have a look at this example: Issue with custom geometry and face normal
Generally you'll need to carefully pattern out every 3 set of vertices so that when you build the faces there in a clock-wise direction so that the normals will be computerd correctly. Here's a basic example of adding a face.
geometry.faces.push(new THREE.Face3(1,2,3));
BUT! Note that this will result in the same aforementioned diagonal lines through your wireframe. So, for your use case why not simply use both the basic cube mesh with picking and remove the wireframe then overlay the line drawn boxes as your custom wireframe. Best of both worlds.
FYI, you probably already know but Face4 is gone, so you'll need to use Face3 and some sort of custom wireframe to do this anyway.
Edit:
I did a little test to your fiddle, noticed something strange. Using the CanvasRender, even with the wireframe off the default cube you still see the diagonal lines! I try WebGLRenderer and it's fine. I'll have to look into that one further.
CanvasRenderer
http://jsfiddle.net/QHjSM/13/
WebGLRenderer
http://jsfiddle.net/QHjSM/14/
Me again, hmm it appears those ghosted face lines are visible in all the CanvasRenderer examples that use a MeshBasicMaterial on the Three.js site. The only thing I was able to do was simply reduce the opacity of the cube mesh material to 0.1 to lessen the effect. I suppose the only other method is to switch to the WebGLRenderer but I look forward to being wrong on this :) Here's the last test
http://jsfiddle.net/QHjSM/16/

How to implement sketch brush, like in deviantART muro?

deviantART muro has a set of brilliant tools of painting. And I'm very curious how to implement these brushes like Sketch and Paintbrush, arithmetically?
Using any normal programming language to explain is okay, though I prefer C++ or JavaScript. I think it's better than read their JS source code.
I'd say it works something like:
Track mouse movement
On captured mouse movement, draw your desired brush from saved "Old mouse position" to captured "New mouse position", iterating at a pixel's distance at a time
If you move the mouse too fast for the script to capture, it will just look like a computed long straight line (which is what it looks like Muro is doing). If you want to get real fancy you can calculate the trajectory from previous mouse positions and draw that instead for a "smoother" line.
Since you specified Javascript you'd probably want to draw it in a canvas object.
EDIT 1:
Sketch specifically seems to save mouse movements and then loop through, say the 20 latest mouse movements for each mouse movement and draw a bezier curve from that point to the current point.
So, something like (pseudo code)
Object mousemovements = [];
on.mousemove(event)
{
if (mousemovements.length > 20)
{
mousemovements.removeLast();
}
mousemovements.insertAtBeginning([ event.mouseX, event.mouseY ]);
for-each (movement in mousemovements)
{
drawBeziercurveFromTo(movement.mouseX, movement.mouseY,
event.mouseX, event.mouseY);
}
}
Jquery/Canvas DEMO based on the above pseudo code
EDIT 2:
I had a closer look at how "Sketch" worked and it seems that they update the mouse pointer positions, moving the older points closer to the newer points. Something like this:
This DEMO works pretty much like the sketch brush

Categories