OpenJSCAD: Can't find command for 3D to 2D Projection - projection

In openSCAD there is a command to project a 3D model to a 2D surface.
I can't find anything about this command in the JSCAD Documentation.
Is there a way to do the same in openJSCAD.

Yes, it can be done, let me demonstrate with a simple cube:
function main () {
var z0basis = CSG.OrthoNormalBasis.Z0Plane();
return (cube({size: 10, center: true})).projectToOrthoNormalBasis(z0basis);
}

Related

HERE MAP add new point in clicked place on polyline

I really beg about help because I'm trying to do some things for a few days, and I really don't know how. I want to do road manipulate by hold and drag . I think I need to start with adding new point in polyline geometry exaclly in the pointer place (or linestring, linstring points and polyline geometry seems to be almost the same). But it seems to be impossible.
Of course, there is no problem with getting coordinates, but adding this new coordinates to polyline or linestring in appropriate place.
There is no code, because every here map methods (pushPoints, setGeopetry) seems to be usless, and give points in the wrong place.
See please this example: https://demo.support.here.com/examples/v3/fleet - you can drag-n-drop there
and src code of it : https://demo.support.here.com/javascripts/examples/v3/fleet.js
Search there in the code the variable clipedPolyline and func clipPolyline:
var clipPolyline = function(polyline, viewportX, viewportY, bboxSize){
var pleft = viewportX - bboxSize,
pright = viewportX + bboxSize,
ptop = viewportY - bboxSize,
pbottom = viewportY + bboxSize,
coordLeftTop = map.screenToGeo(pleft, ptop),
coordRigthBottom = map.screenToGeo(pright, pbottom),
rect = new H.geo.Rect(coordLeftTop.lat,coordLeftTop.lng,coordRigthBottom.lat,coordRigthBottom.lng),
clipedPolyline = polyline.clip(rect);
return clipedPolyline;
};
Read please documentation for method clip on:
https://developer.here.com/documentation/maps/3.1.31.0/api_reference/H.map.Polyline.html#clip

How can I merge geometries in A-Frame without losing material information?

I have a large set of block objects using a custom geometry, that I am hoping to merge into a smaller number of larger geometries, as I believe this will reduce rendering costs.
I have been following guidance here: https://aframe.io/docs/1.2.0/introduction/best-practices.html#performance which has led me to the geometry-merger component here:
https://github.com/supermedium/superframe/tree/master/components/geometry-merger/
The A-Frame docs say:
"You can use geometry-merger and then make use a three.js material with vertex colors enabled. three.js geometries keep data such as color, uvs per vertex."
The geometry-merger component also says:
"Useful if using vertex or face coloring as individual geometries' colors can still be manipulated individually since this component keeps a faceIndex and vertexIndex."
However I have a couple of problems.
If I set vertexColors on my material (as suggested by the A-Frame docs), then this ruins the appearance of my blocks.
Whether or not I set vertexColors on my material, all material information seems to be lost when the geometries are merged, and everything just ends up white.
See this glitch for a demonstration of both problems.
https://tundra-mercurial-garden.glitch.me/
My suspicion is that the A-Frame geometry-merger component just won't do what I need here, and I need to implement something myself using the underlying three.js functions.
Is that right, or is there a way that I could make this work using geometry-merger?
For the vertexColors to work, you need to have your vertices coloured :)
More specifically - the BufferGeometry expects an array of rgb values for each vertex - which will be used as color for the material.
In this bit of code:
var geometry = new THREE.BoxGeometry();
var mat = new THREE.MeshStandardMaterial({color: 0xffffff, vertexColors: THREE.FaceColors});
var mesh = new THREE.Mesh(geometry, mat);
The mesh will be be black unless the geometry contains information about the vertex colors:
// create a color attribute in the geometry
geometry.setAttribute('color', new THREE.BufferAttribute(new Float32Array(vertices_count), 3));
// grab the array
const colors = this.geometry.attributes.color.array;
// fill the array with rgb values
const faceColor = new THREE.Color(color_hex);
for (var i = 0; i < vertices_count / 3; i += 3) {
colors[i + 0] = faceColor.r; // lol +0
colors[i + 1] = faceColor.g;
colors[i + 2] = faceColor.b;
}
// tell the geometry to update the color attribute
geometry.attributes.color.needsUpdate = true;
I can't make the buffer-geometry-merger component work for some reason, but It's core seems to be valid:
AFRAME.registerComponent("merger", {
init: function() {
// replace with an event where all child entities are ready
setTimeout(this.mergeChildren.bind(this), 500);
},
mergeChildren: function() {
const geometries = [];
// traverse the child and store all geometries.
this.el.object3D.traverse(node => {
if (node.type === "Mesh") {
const geometry = node.geometry.clone();
geometry.applyMatrix4(node.parent.matrix);
geometries.push(geometry)
// dispose the merged meshes
node.parent.remove(node);
node.geometry.dispose();
node.material.dispose();
}
});
// create a mesh from the "merged" geometry
const mergedGeo = THREE.BufferGeometryUtils.mergeBufferGeometries(geometries);
const mergedMaterial = new THREE.MeshStandardMaterial({color: 0xffffff, roughness: 0.3, vertexColors: THREE.FaceColors});
const mergedMesh = new THREE.Mesh(mergedGeo, mergedMaterial);
this.el.object3D.add(mergedMesh)
}
})
You can check it out in this glitch. There is also an example on using the vertex colors here (source).
I agree it sounds like you need to consider other solutions. Here are two different instances of instancing with A-Frame:
https://github.com/takahirox/aframe-instancing
https://github.com/EX3D/aframe-InstancedMesh
Neither are perfect or even fully finished, but can hopefully get you started as a guide.
Although my original question was about geometry merging, I now believe that Instanced Meshes were a better solution in this case.
Based on this suggestion I implemented this new A-Frame Component:
https://github.com/diarmidmackenzie/instanced-mesh
This glitch shows the scene from the original glitch being rendered with just 19 calls using this component. That compares pretty well with > 200 calls that would have been required if every object were rendered individually.
https://dull-stump-psychology.glitch.me/
A key limitation is that I was not able to use a single mesh for all the different block colors, but had to use one mesh per color (7 meshes total).
InstancedMesh can support different colored elements, but each element must have a single color, whereas the elements in this scene had 2 colors each (black frame + face color).

Automatic bezier edges in Cytoscape.js

I would like to create nice curved edges in my Cotoscape.js graph using the unbundled-bezier style. According to the database I have to set the control-point-distance(s) automatically, so I came up with following code:
{
selector: 'edge',
css: {
'curve-style': 'unbundled-bezier',
'target-arrow-shape': 'triangle',
'control-point-weights': '0.25 0.75.',
'control-point-distance': function( ele ){
console.log(ele.source().position());
var pos1 = ele.source().position().y;
var pos2 = ele.target().position().y;
var str = '' + Math.abs(pos2-pos1) + 'px -' + Math.abs(pos2-pos1) + 'px';
console.log(pos1, pos2, str);
return str;
}
}
}
My problem is, that the graph is rendered with straight lines ant the curvy line appears only when I click on some. Also, when I move the nodes the curve moves nicely with the node, but the node positions (ele.source().position().y) does not change
A style function ought to be a pure function. Yours is technically not: It depends on state outside of the edge's data.
The only way an arbitrary function could be used to specify style is if the function is continuously polled. That would be hacky and prohibitively expensive.
You must use a pure function if you want to use a custom function. Either rewrite your function to rely on only the edge's data or use a passthrough data() mapping and change the edge's data whenever you want to modify the edge.

HaxeFlixel tilemap collisions

I am developing a game in Haxe with the HaxeFlixel Framework.
I decided to split the map in chunks so i can load new areas of the map at runtime (without loading screen). For that i put every chunk in an instance of FlxTilemap.
Now I noticed that, when i try to move a FlxTilemap (by changing its x and y properties) the collision detection (with FlxG.collide(hero, map)) does not work right.
To test why the collision detection doesn't work, I simply added a FlxTilemap to the scene and collided it with my hero:
map = new FlxTilemap();
var mapData = "";
for (y in 0...8) {
for (x in 0...8) {
mapData += "0,";
}
mapData += "\n";
}
map.loadMap(mapData, AssetPaths.tuxemon_sprites__png, 16, 16);
for (x in 0...8) {
map.setTile(x, 6, SpriteSheet.TILES.FENCE.LOOSE_1_RIGHT);
}
for (y in 0...8) {
map.setTile(6, y, SpriteSheet.TILES.FENCE.LOOSE_1_RIGHT);
}
map.setPosition(
map.x - map.width / 2,
map.y - map.height / 2
);
add(map);
Collision detection is handeled in the update() method of the state:
override public function update():Void
{
super.update();
FlxG.collide(hero, map);
}
Am I doing it the wrong way or did I simply miss something?
EDIT:
There seems to be a problem in the HaxeFlixel collision detection.
The collision will only be detected when the x and y properties of the FlxObjects are positive.
I want to have negative x/y positions as well.
Does anyone know a fix or workaround for this problem?
Try changing the bounds of your world space. Specifically, FlxG.worldBounds.

instantiate plane with texture between two objects(vectors)

First a little context.
I am attempting to build a little fence-builder functionality.
Below is a sketch showing idea/how I see it approached.
Ohh not enough rep apparently: link here: [http://imgur.com/ZougnQi][1]
Two objects are instantiated(Pole1 & Pole2)
As I in runtime move those two poles around the terrain, I wish to
a: create a plane with a texture between those poles (a tileable transparent fence thingy)
which in update ofcourse scales according to the distance between those two points/poles.
b: instantiate new poles according to distance between the two poles.
So far I have per pictures linked two poles I can drag around and a third pole being placed exactly between those two.
I found another post: Instantiate gameobject between 2 objects in unity 3d
But couldn't get code from "SlxS" to work, and it does not really aid in me creating a textured plane/cube between the poles.
Code that works:
function Update ()
{
poles.transform.position = 0.5f*(pole1.transform.position + pole2.transform.position);
}
var pole1 : Transform;
var pole2 : Transform;
var poles : GameObject;
var selectedDistance : int = 5;
function Build()
{
var distance = Vector3.Distance(pole1.transform.position,pole2.transform.position);
print(distance);
poles.transform.position = 0.5f*(pole1.transform.position + pole2.transform.position);
if((pole1.transform.position - pole2.transform.position).magnitude < selectedDistance)
{
print("more than 5 apart");
}
else
{
print("less than 5 apart");
}
}
Hope to get some input :)
Best,
[1]: http://imgur.com/ZougnQi
SOLVED: Thanks to https://stackoverflow.com/users/685314/joetjah
Code:
function Update ()
{
poles.transform.position = 0.5f*(pole1.transform.position + pole2.transform.position);
}
var pole1 : Transform;
var pole2 : Transform;
var poles : Transform;
function Build2() {
var poleDistance : Vector2;
poles.localScale.z = Vector3.Distance(pole1.position,pole2.position); // Find the distance between 2 points
print(poles.localScale.z);
poles.LookAt(pole2);
}

Resources