Points Intersection of BufferGeometry - three.js

I'm trying to adapt this example or this discussion, but when I use new library, there is no more face and vertices in geometry properties.
The structures is differenet, so I confuse to use the Index->array and Attrirube->Position to change this code:
`
obj.geometry.faces.forEach(function(face, idx) {
obj.localToWorld(a.copy(obj.geometry.vertices[face.a]));
obj.localToWorld(b.copy(obj.geometry.vertices[face.b]));
obj.localToWorld(c.copy(obj.geometry.vertices[face.c]));
lineAB = new THREE.Line3(a, b);
lineBC = new THREE.Line3(b, c);
lineCA = new THREE.Line3(c, a);
console.log("lineAB", lineAB);
console.log("lineBC", lineBC);
console.log("lineCA", lineCA);
setPointOfIntersection(lineAB, mathPlane, idx);
setPointOfIntersection(lineBC, mathPlane, idx);
setPointOfIntersection(lineCA, mathPlane, idx);
});
`
Any idea how to do it? Please help.. Thanks in advance~

Since r125, there is no Geometry class anymore. All geometries are BufferGeometry now. Thus, vertices are stored in geometry.attributes.position.
BufferGeometry can be indexed or non-indexed:
Indexed means that faces defined with triplets of incides of
vertices.
Non-indexed means that faces defined with triplets of
vertices.
So, this part of code:
var a = new THREE.Vector3(),
b = new THREE.Vector3(),
c = new THREE.Vector3();
obj.geometry.faces.forEach(function(face) {
obj.localToWorld(a.copy(obj.geometry.vertices[face.a]));
obj.localToWorld(b.copy(obj.geometry.vertices[face.b]));
obj.localToWorld(c.copy(obj.geometry.vertices[face.c]));
lineAB = new THREE.Line3(a, b);
lineBC = new THREE.Line3(b, c);
lineCA = new THREE.Line3(c, a);
setPointOfIntersection(lineAB, mathPlane);
setPointOfIntersection(lineBC, mathPlane);
setPointOfIntersection(lineCA, mathPlane);
});
needs some changes.
var a = new THREE.Vector3(),
b = new THREE.Vector3(),
c = new THREE.Vector3();
var isIndexed = obj.geometry.index != null; // if geometry is indexed or non-indexed
var pos = obj.geometry.attributes.position; // attribute with positions
var idx = obj.geometry.index; // index
var faceCount = (isIndexed ? idx.count : pos.count) / 3; // amount of faces
for(let i = 0; i < faceCount; i++) {
let baseIdx = i * 3;
let idxA = baseIdx + 0;
a.fromBufferAttribute(pos, isIndexed ? idx.getX(idxA) : idxA);
// .fromBufferAttribute is a method of Vector3
// .getX is a method of BufferAttribute
let idxB = baseIdx + 1;
b.fromBufferAttribute(pos, isIndexed ? idx.getX(idxB) : idxB);
let idxC = baseIdx + 2;
c.fromBufferAttribute(pos, isIndexed ? idx.getX(idxC) : idxC);
obj.localToWorld(a);
obj.localToWorld(b);
obj.localToWorld(c);
lineAB = new THREE.Line3(a, b);
lineBC = new THREE.Line3(b, c);
lineCA = new THREE.Line3(c, a);
setPointOfIntersection(lineAB, mathPlane);
setPointOfIntersection(lineBC, mathPlane);
setPointOfIntersection(lineCA, mathPlane);
});
PS Haven't tested this snippet, changes from scratch. Possible typos.

Related

Geometry intersection converting from direct geometry to buffergeometry

I am using Three.js. Found a really good Decal library written by Benpurdy. It's very easily modifiable and also used the techniques described here
However, the technique uses Geometry. The project I am on, uses BufferGeometry. I traced the code which does the geometry intersects and can't figure out the conversion from faces and vertices to attributes.
this.createGeometry = function(matrix, mesh) {
var geom = mesh.geometry;
var decalGeometry = new THREE.Geometry();
var projectorInverse = matrix.clone().getInverse(matrix);
var meshInverse = mesh.matrixWorld.clone().getInverse(mesh.matrixWorld);
var faces = [];
for(var i = 0; i < geom.faces.length; i++){
var verts = [geom.faces[i].a, geom.faces[i].b, geom.faces[i].c];
var pts = [];
var valid = false;
for(var v = 0; v < 3; v++) {
var vec = geom.vertices[verts[v]].clone();
vec.applyMatrix4(mesh.matrixWorld);
vec.applyMatrix4(matrix);
if((vec.z > 1) || (vec.z < -1) || (vec.x > 1) || (vec.x < -1) || (vec.y > 1) || (vec.y < -1)) {
} else {
valid = true;
}
pts.push(vec);
}
if(valid) {
var uv = [];
for(var n = 0; n < 3; n++){
uv.push(new THREE.Vector2( (pts[n].x + 1) / 2, (pts[n].y + 1) / 2));
pts[n].applyMatrix4(projectorInverse);
pts[n].applyMatrix4(meshInverse);
decalGeometry.vertices.push( pts[n] );
}
// update UV's
decalGeometry.faceVertexUvs[0].push(uv);
var newFace = geom.faces[i].clone();
newFace.a = decalGeometry.vertices.length - 3;
newFace.b = decalGeometry.vertices.length - 2;
newFace.c = decalGeometry.vertices.length - 1;
decalGeometry.faces.push(newFace);
}
}
return decalGeometry;
}
Appreciate if anyone could shed some light on how to go about pursuing this? Thanks.
I ended up solving the problem by writing another function to compute intersections with buffergeometry. Took me a while trying to understand the original buffer geometry code.
this.createGeometryFromBufferGeometry = function(matrix, mesh) {
var geom = mesh.geometry;
var decalGeometry = new THREE.Geometry();
var projectorInverse = matrix.clone().getInverse(matrix);
var meshInverse = mesh.matrixWorld.clone().getInverse(mesh.matrixWorld);
var faces = [];
for(var i = 0; i < geom.attributes.position.array.length; i+=9){
var pts = [];
var valid = false;
for(var v = 0; v < 9; v+=3) {
var vec = new THREE.Vector3(geom.attributes.position.array[i+v],geom.attributes.position.array[i+v+1],geom.attributes.position.array[i+v+2]);
console.log((i+v) + " " + (i+v+1) + " " + (i+v+2) );
console.log(vec);
vec.applyMatrix4(mesh.matrixWorld);
vec.applyMatrix4(matrix);
if((vec.z > 1) || (vec.z < -1) || (vec.x > 1) || (vec.x < -1) || (vec.y > 1) || (vec.y < -1)) {
} else {
valid = true;
}
pts.push(vec);
}
if(valid) {
var uv = [];
for(var n = 0; n < 3; n++){
uv.push(new THREE.Vector2( (pts[n].x + 1) / 2, (pts[n].y + 1) / 2));
pts[n].applyMatrix4(projectorInverse);
pts[n].applyMatrix4(meshInverse);
decalGeometry.vertices.push( pts[n] );
}
decalGeometry.faceVertexUvs[0].push(uv);
var newFace = new THREE.Face3()
newFace.a = decalGeometry.vertices.length - 3;
newFace.b = decalGeometry.vertices.length - 2;
newFace.c = decalGeometry.vertices.length - 1;
decalGeometry.faces.push(newFace);
}
}
return decalGeometry;
}
BufferGeometry() has a method .fromGeometry(). Populates this BufferGeometry with data from a Geometry object.
var geom = new THREE.BoxGeometry(1,1,1);
var bufGeom = new THREE.BufferGeometry().fromGeometry(geom);
UPD. You can use the other way round.
var bufGeom = new THREE.BoxBufferGeometry(1,1,1);
var geom = new THREE.Geometry().fromBufferGeometry(bufGeom);
Quick and dirty solution is to create geometry from bufferGeometry and after calculating dispose created geometry
this.compute = function()
{
this.geometry = mesh.geometry
if(this.geometry.attributes)
{
this.geometry = new THREE.Geometry().fromBufferGeometry(this.geometry);
this.computeDecal();
this.geometry.dispose();
}
else
{
this.computeDecal();
}
}

How to calculate transformed skin vertices?

The code I tried:
var transformedSkinVertex = function (skin, index) {
var skinIndices = (new THREE.Vector4 ()).fromAttribute (skin.geometry.getAttribute ('skinIndex'), index);
var skinWeights = (new THREE.Vector4 ()).fromAttribute (skin.geometry.getAttribute ('skinWeight'), index);
var skinVertex = (new THREE.Vector3 ()).fromAttribute (skin.geometry.getAttribute ('position'), index).applyMatrix4 (skin.bindMatrix);
var result = new THREE.Vector3 (), temp = new THREE.Vector3 (), tempMatrix = new THREE.Matrix4 (); properties = ['x', 'y', 'z', 'w'];
for (var i = 0; i < 4; i++) {
var boneIndex = skinIndices[properties[i]];
tempMatrix.multiplyMatrices (skin.skeleton.bones[boneIndex].matrixWorld, skin.skeleton.boneInverses[boneIndex]);
result.add (temp.copy (skinVertex).multiplyScalar (skinWeights[properties[i]]).applyMatrix4 (tempMatrix));
}
return result.applyMatrix4 (skin.bindMatrixInverse);
};
This works for T pose:
But with arms lowered some parts explode into angel-like shape:
Here is arms placed slightly differently:
My current theory is that this happens when there are > 2 bones. But... why?
All weights correctly add up to 1, as you can see above three.js renders the skin correcty.
Resolved.
Correct line is
result.add (temp.copy (skinVertex).applyMatrix4 (tempMatrix).multiplyScalar (skinWeights[properties[i]]));
I did not think the order of multiplication by scalar would matter.

Ordering of transparent ParticleSystem with BufferGeometry in Threejs

Related to this question : Z-buffer issue with BufferGeometry in ParticleSystem
The given solution does not work for me, I made my own shader for rendering as I added custom attributes for size and UV. Everythinf works fine with the buffer geometry except the particle ordering for transparency.
If Activated > squared texture are partly hiding the other particles.
If Deactivated (depthTest : false) > particles looks fine but are not ordered.
Thanks for your answer, the subject has been raised several times but nothing worked for me yet.
Using Three.js 61
particleMaterial = new THREE.ShaderMaterial({
fragmentShader : document.getElementById("sectorPointFragment").textContent,
vertexShader : document.getElementById("sectorPointVertex").textContent,
uniforms : uniforms,
attributes : attributes,
transparent : true,
alphaTest : 0.5
});
_this.particles = new THREE.ParticleSystem(geometry, particleMaterial);
_this.particles.sortParticles = true;
So here is the solution I took, creating a new array with value each time. Seems to work, tested with 1000 particles not more
this.updateShaders = function() {
if(clock.getElapsedTime() - _lastZOrdering <= 0.2) {
return;
}
// z-ordering
var distances = [],
attributes = this.particles.geometry.attributes,
nbParticle = attributes.position.numItems / 3,
tmpPos = new THREE.Vector3(0, 0, 0);
for (var i = 0; i < nbParticle; ++i) {
tmpPos.set(
attributes.position.array[i * 3],
attributes.position.array[i * 3 + 1],
attributes.position.array[i * 3 + 2]
);
distances[i] = [this.controls.object.position.distanceTo(tmpPos), i];
}
distances.sort(function(a, b){
return b[0] - a[0];
});
var index, indexSrc, indexDst, tmpTab;
for (var val in attributes) {
tmpTab = new Float32Array(attributes[val].itemSize * nbParticle);
for(i = 0; i < nbParticle; ++i){
index = distances[i][1];
for(j = 0; j < attributes[val].itemSize; ++j){
indexSrc = index * attributes[val].itemSize + j;
indexDst = i * attributes[val].itemSize + j;
tmpTab[indexDst] = attributes[val].array[indexSrc];
}
}
attributes[val].array = tmpTab;
attributes[val].needsUpdate = true;
}
_lastZOrdering = clock.getElapsedTime();
}

Away3D 4.1 performance issues - how to optimise a scene with many identical meshes?

I have created a simple test using Away3D 4.1 (2500 cubes) but performance is a lot lower than i expected - only 10 FPS.
I assume i am making a noob mistake (being a noob and all) so here are relevant pieces of code:
Lighting:
var light1:DirectionalLight = new DirectionalLight();
light1.position = new Vector3D(400, 300, -200);
light1.lookAt(new Vector3D());
light1.color = 0xFFFFFF;
light1.ambient = 0.25;
lightPicker = new StaticLightPicker([light1]);
Creating cubes:
var material:ColorMaterial = new ColorMaterial(0x999999);
material.lightPicker = lightPicker;
material.specular = 0;
var mesh:Mesh = new Mesh(new CubeGeometry(50, 50, 50), material);
for (var i:uint = 0; i < 50; i++)
{
for (var j:uint = 0; j < 50; j++)
{
var cube:Mesh = Mesh(mesh.clone());
cube.x = 100*(i-25);
cube.y = 25;
cube.z = 100*(j-25);
scene.addChild(cube);
}
}
And the camera:
camera = new Camera3D();
camera.position = new Vector3D(0, 1000, -5000);
camera.lookAt(new Vector3D(0, 0, 0));
camera.lens.far = 10000;
Stage3D output in Scout shows that there are many calls between each drawTriangles call and my basic understanding tells me that drawTriangle calls should be 'batched'.
I know that some other frameworks have batch methods but i havent been able to find anything related to Away3D.
Thanks in advance for any help.
It looks like Merge (thanks to Varnius for spotting that) is the recommended way to do it with previous versions, but it doesn't work in 4.1 (see away3d forum thread).
However, user kurono posted a solution in the forum that works (at least for my scenario) so i reproduce it here in case anyone else has the same problem:
var material:ColorMaterial = new ColorMaterial(0x999999);
material.lightPicker = lightPicker;
material.specular = 0;
var mesh:Mesh = new Mesh(new CubeGeometry(50, 50, 50));
var meshes:Vector.<Mesh> = new Vector.<Mesh>();
for (var i:uint = 0; i < 50; i++)
{
for (var j:uint = 0; j < 50; j++)
{
var cube:Mesh = Mesh(mesh.clone());
cube.x = 100*(i-25);
cube.y = 25;
cube.z = 100*(j-25);
meshes.push(cube);
}
}
var bigMesh:Mesh = doMerge(meshes, material);
scene.add(bigMesh);
The magic is in the doMerge() method:
function doMerge(meshes:Vector.<Mesh>, material:MaterialBase):Mesh
{
var isub:ISubGeometry;
var rawVertsAll:Vector.<Number> = new Vector.<Number>();
var rawIndicesAll:Vector.<uint> = new Vector.<uint>();
var rawUVsAll:Vector.<Number> = new Vector.<Number>();
var rawNormalsAll:Vector.<Number> = new Vector.<Number>();
var rawTangentsAll:Vector.<Number> = new Vector.<Number>();
var offset:uint = 0;
var verts:Vector.<Number>;
var normals:Vector.<Number>;
var tangents:Vector.<Number>;
var uvs:Vector.<Number>;
var indices:Vector.<uint>;
var i:uint;
var j:uint;
var k:uint;
for (k = 0; k < meshes.length; k++)
{
var m:Mesh = meshes[k];
isub = m.geometry.subGeometries[0].cloneWithSeperateBuffers();
isub.applyTransformation(m.transform.clone());
verts = new Vector.<Number>();
normals = new Vector.<Number>();
tangents = new Vector.<Number>();
uvs = new Vector.<Number>();
indices = isub.indexData;
for (i = 0; i < isub.numVertices; i++)
{
verts.push(isub.vertexData[i * isub.vertexStride + isub.vertexOffset]);
verts.push(isub.vertexData[i * isub.vertexStride + isub.vertexOffset + 1]);
verts.push(isub.vertexData[i * isub.vertexStride + isub.vertexOffset + 2]);
normals.push(isub.vertexNormalData[i * isub.vertexNormalStride + isub.vertexNormalOffset]);
normals.push(isub.vertexNormalData[i * isub.vertexNormalStride + isub.vertexNormalOffset + 1]);
normals.push(isub.vertexNormalData[i * isub.vertexNormalStride + isub.vertexNormalOffset + 2]);
tangents.push(isub.vertexTangentData[i * isub.vertexTangentStride + isub.vertexTangentOffset]);
tangents.push(isub.vertexTangentData[i * isub.vertexTangentStride + isub.vertexTangentOffset + 1]);
tangents.push(isub.vertexTangentData[i * isub.vertexTangentStride + isub.vertexTangentOffset + 2]);
uvs.push(isub.UVData[i * isub.UVStride + isub.UVOffset]);
uvs.push(isub.UVData[i * isub.UVStride + isub.UVOffset + 1]);
}
for (j = 0; j < indices.length; j++)
{
indices[j] += offset;
}
offset += isub.numVertices;
rawVertsAll = rawVertsAll.concat(verts);
rawNormalsAll = rawNormalsAll.concat(normals);
rawTangentsAll = rawTangentsAll.concat(tangents);
rawUVsAll = rawUVsAll.concat(uvs);
rawIndicesAll = rawIndicesAll.concat(indices);
}
var geometry:Geometry = new Geometry();
var subGeometry:SubGeometry = new SubGeometry();
subGeometry.updateVertexData(rawVertsAll);
subGeometry.updateIndexData(rawIndicesAll);
subGeometry.updateUVData(rawUVsAll);
subGeometry.updateVertexNormalData(rawNormalsAll);
subGeometry.updateVertexTangentData(rawTangentsAll);
geometry.subGeometries.push(subGeometry);
return new Mesh(geometry, material);
}
And voila! 10fps becomes 60fps
Yeah, you should batch your draw calls. I don't have much experience with Away3D but after a quick look through their API reference it seems that away3d.tools.commands.Merge should help you to merge all those cubes into one large batched mesh.
Are you getting 10 fps in the debug player or the release version?
I ran your code and get 20 fps in the debug player but 50+ fps in the release version.
I tried merging and didn't see any improvements. Plus, if you want to access the individual cubes, merging will make that quite complicated :)

Actionscript - randomly drop from moving plane MC

I wasn't quite sure how to describe my problem in the subject. I have a plane MC and a crate MC. The plane only flies along the y axis from the bottom of the screen to top. Along the way I want it to randomly drop the crate MC. My code is below. The problem is that the crates spontaneously keep spawning and not near the plane.
function movePlane():void
{
var tempY:Number;
var tempX:Number;
var tempCrate:MovieClip;
var tempPlane:MovieClip;
for (var j:int =planes.length-1; j>=0; j--)
{
tempPlane = planes[j];
tempPlane.y += tempPlane.planeSpeed;
tempCrate = new Crate();
tempY = Math.floor(Math.random() * tempPlane.y);
tempX = Math.floor(Math.random() * tempPlane.x);
}
tempCrate.y = tempY;
tempCrate.x = tempX;
addChild(tempCrate);
}
Edited answer:
To make a crate drop on each plane once you can create this behavior by creating a timer on each plane with a random time value. Like this:
function addRandomCreation():void{
var animationTime:Number = 5000; //The time the planes will be animating in ms
for(var i:int = 0; i < planes.length; i++){
var planeTimer:Timer = new Timer(Math.round(animationTime * Math.random()));
planeTimer.addEventListener(TimerEvent.TIMER, timerComplete(i));
planeTimer.start();
}
}
function timerComplete(planeID:int):function{
return function(event:TimerEvent):void{
event.target.stop();
event.target.removeEventListener(event.type, arguments.callee);
var tempCrate:MovieClip = new Crate();
tempY = Math.round(Math.random() * planes[planeID].y);
tempCrate.y = tempY;
tempCrate.x = planes[planeID].x;
addChild(tempCrate);
}
}
Edited answer:
This will create a crate on the same x axis as the plane it's being created by.
function movePlane():void
{
var tempY:Number;
var tempX:Number;
var tempCrate:MovieClip;
var tempPlane:MovieClip;
for (var j:int =planes.length-1; j>=0; j--)
{
tempPlane = planes[j];
tempPlane.y += tempPlane.planeSpeed;
tempCrate = new Crate();
tempY = Math.floor(Math.random() * tempPlane.y);
tempCrate.y = tempY;
tempCrate.x = tempPlane.x;
addChild(tempCrate);
}
}
You have have to use addChild each time you create a new Crate otherwise it will just create a lot of crates which only the last one will be added to the stage. To do this you have to move the addChild into the loop.
function movePlane():void
{
var tempY:Number;
var tempX:Number;
var tempCrate:MovieClip;
var tempPlane:MovieClip;
for (var j:int =planes.length-1; j>=0; j--)
{
tempPlane = planes[j];
tempPlane.y += tempPlane.planeSpeed;
tempCrate = new Crate();
tempY = Math.floor(Math.random() * tempPlane.y);
tempX = Math.floor(Math.random() * tempPlane.x);
tempCrate.y = tempY;
tempCrate.x = tempX;
addChild(tempCrate);
}
}

Resources