three#Geometry JavaScript Examples
The following examples show how to use
three#Geometry.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: utils.js From cga.js with MIT License | 7 votes |
export function toDisSeg(obj, opts) {
var geometry = new Geometry()
geometry.vertices.push(...obj)
var material = new LineDashedMaterial({
color: 0xff0000,
dashSize: 1,
gapSize: 1,
scale: 1, // 比例越大,虚线越密;反之,虚线越疏
...opts
});
// debugger
// Line.computeLineDistances(geometry);//
var line = new Line(geometry, material);
line.computeLineDistances();
return line;
}
Example #2
Source File: half_edge.js From architect3d with MIT License | 6 votes |
/**
* This generates the invisible planes in the scene that are used for interesection testing for the wall items
*/
generatePlane()
{
var geometry = new Geometry();
var v1 = this.transformCorner(this.interiorStart());
var v2 = this.transformCorner(this.interiorEnd());
var v3 = v2.clone();
var v4 = v1.clone();
// v3.y = this.wall.height;
// v4.y = this.wall.height;
v3.y = this.wall.startElevation;
v4.y = this.wall.endElevation;
geometry.vertices = [v1, v2, v3, v4];
geometry.faces.push(new Face3(0, 1, 2));
geometry.faces.push(new Face3(0, 2, 3));
geometry.computeFaceNormals();
geometry.computeBoundingBox();
this.plane = new Mesh(geometry, new MeshBasicMaterial({visible:true}));
//The below line was originally setting the plane visibility to false
//Now its setting visibility to true. This is necessary to be detected
//with the raycaster objects to click walls and floors.
this.plane.visible = true;
this.plane.edge = this; // js monkey patch
this.computeTransforms(this.interiorTransform, this.invInteriorTransform, this.interiorStart(), this.interiorEnd());
this.computeTransforms(this.exteriorTransform, this.invExteriorTransform, this.exteriorStart(), this.exteriorEnd());
var b3 = new Box3();
b3.setFromObject(this.plane);
this.min = b3.min.clone();
this.max = b3.max.clone();
this.center = this.max.clone().sub(this.min).multiplyScalar(0.5).add(this.min);
}
Example #3
Source File: room.js From architect3d with MIT License | 6 votes |
generateRoofPlane()
{
if(this.roofPlane && this.roofPlane != null)
{
if(this.roofPlane.parent != null)
{
this.roofPlane.parent.remove(this.roofPlane);
}
}
// setup texture
var geometry = new Geometry();
this.corners.forEach((corner) => {
var vertex = new Vector3(corner.x,corner.elevation, corner.y);
geometry.vertices.push(vertex);
});
for (var i=2;i<geometry.vertices.length;i++)
{
var face = new Face3(0, i-1, i);
geometry.faces.push(face);
}
this.roofPlane = new Mesh(geometry, new MeshBasicMaterial({side: DoubleSide, visible:false}));
this.roofPlane.room = this;
}
Example #4
Source File: edge.js From architect3d with MIT License | 6 votes |
buildSideFillter(p1, p2, height, color)
{
var points = [this.toVec3(p1), this.toVec3(p2), this.toVec3(p2, height), this.toVec3(p1, height) ];
var geometry = new Geometry();
points.forEach((p) => {
geometry.vertices.push(p);
});
geometry.faces.push(new Face3(0, 1, 2));
geometry.faces.push(new Face3(0, 2, 3));
var fillerMaterial = new MeshBasicMaterial({color: color,side: DoubleSide});
var filler = new Mesh(geometry, fillerMaterial);
return filler;
}
Example #5
Source File: edge.js From architect3d with MIT License | 6 votes |
buildFillerVaryingHeights(edge, side, color)
{
var a = this.toVec3(edge.exteriorStart(), this.edge.getStart().elevation);
var b = this.toVec3(edge.exteriorEnd(), this.edge.getEnd().elevation);
var c = this.toVec3(edge.interiorEnd(), this.edge.getEnd().elevation);
var d = this.toVec3(edge.interiorStart(), this.edge.getStart().elevation);
// var a = this.toVec3(edge.exteriorStart(), this.wall.getClosestCorner(edge.exteriorStart()).elevation);
// var b = this.toVec3(edge.exteriorEnd(), this.wall.getClosestCorner(edge.exteriorEnd()).elevation);
// var c = this.toVec3(edge.interiorEnd(), this.wall.getClosestCorner(edge.interiorEnd()).elevation);
// var d = this.toVec3(edge.interiorStart(), this.wall.getClosestCorner(edge.interiorStart()).elevation);
var fillerMaterial = new MeshBasicMaterial({color: color,side: side});
var geometry = new Geometry();
geometry.vertices.push(a,b,c,d);
geometry.faces.push(new Face3(0, 1, 2));
geometry.faces.push(new Face3(0, 2, 3));
var filler = new Mesh(geometry, fillerMaterial);
return filler;
}
Example #6
Source File: floor.js From architect3d with MIT License | 6 votes |
buildRoofVaryingHeight()
{
// setup texture
var roofMaterial = new MeshBasicMaterial({side: FrontSide,color: 0xe5e5e5});
var geometry = new Geometry();
this.room.corners.forEach((corner) => {
var vertex = new Vector3(corner.x,corner.elevation, corner.y);
geometry.vertices.push(vertex);
});
for (var i=2;i<geometry.vertices.length;i++)
{
var face = new Face3(0, i-1, i);
geometry.faces.push(face);
}
var roof = new Mesh(geometry, roofMaterial);
// roof.rotation.set(Math.PI / 2, 0, 0);
// roof.position.y = Configuration.getNumericValue(configWallHeight);
return roof;
}
Example #7
Source File: utils.js From cga.js with MIT License | 6 votes |
export function toMesh(obj, materialOption) {
var renderObj = null;
if (obj instanceof cga.Point || obj.isVec3) {
var geometry = new BufferGeometry()
geometry.setAttribute('position', new Float32BufferAttribute([obj.x, obj.y, obj.z], 3));
var material = new PointsMaterial({ size: 5, sizeAttenuation: false, color: 0x0ff0f0, alphaTest: 0.9, transparent: true });
renderObj = new Points(geometry, material);
} else if (obj instanceof cga.Line) {
var geometry = new Geometry()
var v1 = obj.direction.clone().multiplyScalar(10000).add(obj.origin);
var v2 = obj.direction.clone().multiplyScalar(-10000).add(obj.origin);
geometry.vertices.push(v1, v2);
var material = new LineBasicMaterial({ color: 0xffff8f });
renderObj = new Line(geometry, material);
} else if (obj instanceof cga.Ray) {
var geometry = new Geometry()
var v1 = obj.direction.clone().multiplyScalar(10000).add(obj.origin);
geometry.vertices.push(obj.origin, v1);
var material = new LineBasicMaterial({ color: 0xff8fff });
renderObj = new Line(geometry, material);
} else if (obj instanceof cga.Segment) {
var geometry = new Geometry()
geometry.vertices.push(obj.p0, obj.p1);
var material = new LineBasicMaterial({ color: 0x8fffff });
renderObj = new Line(geometry, material);
} else if (obj instanceof cga.Triangle) {
var geometry = new Geometry()
geometry.vertices = [...obj];
geometry.faces.push(new Face3(0, 1, 2))
var material = new MeshBasicMaterial({ color: 0x8f8fff, side: DoubleSide });
renderObj = new Mesh(geometry, material);
}
else if (obj instanceof cga.Polyline) {
var geometry = new Geometry()
geometry.vertices.push(...obj);
var material = new LineBasicMaterial({ color: 0xff8fff });
renderObj = new Line(geometry, material);
} else if (obj instanceof cga.Polygon) {
} else if (obj instanceof cga.Circle) {
var geometry = new Geometry()
var radius = obj.radius;
for (let i = 0; i <= 128; i++) {
var p = new Vector3();
p.x = radius * Math.cos(Math.PI / 64 * i);
p.y = radius * Math.sin(Math.PI / 64 * i);
geometry.vertices.push(p);
}
var quaternion = getQuaternionForm2V(new Vector3(0, 0, 1), obj.normal);
var mat4 = new Matrix4();
mat4.makeRotationFromQuaternion(quaternion);
geometry.applyMatrix(mat4);
geometry.translate(obj.center.x, obj.center.y, obj.center.z);
var material = new LineBasicMaterial({ color: 0x8fffff });
renderObj = new Line(geometry, material);
renderObj.add(new toMesh(obj.center))
renderObj.add(new toMesh(new cga.Ray(obj.center, obj.normal)))
}
else if (obj instanceof cga.Disk) {
var geometry = new CircleGeometry(obj.radius, 128)
var material = new MeshBasicMaterial({ color: 0x8f8fff, side: DoubleSide });
var quaternion = getQuaternionForm2V(new Vector3(0, 0, 1), obj.normal);
var mat4 = new Matrix4();
mat4.makeRotationFromQuaternion(quaternion);
geometry.applyMatrix4(mat4);
geometry.translate(obj.center.x, obj.center.y, obj.center.z);
renderObj = new Mesh(geometry, material);
renderObj.add(new toMesh(obj.center))
renderObj.add(new toMesh(new cga.Ray(obj.center, obj.normal)))
}
return renderObj;
}
Example #8
Source File: hud.js From architect3d with MIT License | 5 votes |
makeLineGeometry(item)
{
var geometry = new Geometry();
geometry.vertices.push(new Vector3(0, 0, 0),this.rotateVector(item));
return geometry;
}
Example #9
Source File: VoxelLoader.js From three-voxel-loader with MIT License | 5 votes |
/**
* Generates a polygon mesh with cubes based on voxel data.
* One cube for each voxel.
* @param {PointOctree} octree Octree with voxel data stored as points in space.
* @returns {Mesh} 3D mesh based on voxel data
*/
generateMesh(octree) {
let mergedGeometry = new Geometry();
const material = this.material;
for (const leaf of octree.leaves()) {
if (leaf.points !== null) {
const pos = new Vector3();
var i;
let min = { x: leaf.points[0].x, y: leaf.points[0].y, z: leaf.points[0].z };
let max = { x: leaf.points[0].x, y: leaf.points[0].y, z: leaf.points[0].z };
for (i = 0; i < leaf.points.length; i++) {
const point = leaf.points[i];
pos.add(point);
min.x = Math.min(min.x, point.x);
min.y = Math.min(min.y, point.y);
min.z = Math.min(min.z, point.z);
max.x = Math.max(max.x, point.x);
max.y = Math.max(max.y, point.y);
max.z = Math.max(max.z, point.z);
}
let width = Math.round((this.voxelSize + (max.x - min.x)) * 100) / 100;;
let height = Math.round((this.voxelSize + (max.y - min.y)) * 100) / 100;;
let depth = Math.round((this.voxelSize + (max.z - min.z)) * 100) / 100;
let voxelGeometry = new BoxGeometry(width, height, depth);
pos.divideScalar(i);
const rgb = leaf.data[0].color;
if (rgb != null) {
const color = new Color().setRGB(rgb.r / 255, rgb.g / 255, rgb.b / 255);
for (var i = 0; i < voxelGeometry.faces.length; i++) {
let face = voxelGeometry.faces[i];
face.color.set(color);
}
}
voxelGeometry.translate(pos.x, pos.y, pos.z);
mergedGeometry.merge(voxelGeometry);
voxelGeometry.translate(-pos.x, -pos.y, -pos.z);
}
}
let bufGeometry = new BufferGeometry().fromGeometry(mergedGeometry);
bufGeometry.computeFaceNormals();
bufGeometry.computeVertexNormals();
var voxels = new Mesh(bufGeometry, material);
return voxels;
}
Example #10
Source File: scene.js From architect3d with MIT License | 4 votes |
/**
* Creates an item and adds it to the scene.
* @param itemType The type of the item given by an enumerator.
* @param fileName The name of the file to load.
* @param metadata TODO
* @param position The initial position.
* @param rotation The initial rotation around the y axis.
* @param scale The initial scaling.
* @param fixed True if fixed.
* @param newItemDefinitions - Object with position and 'edge' attribute if it is a wall item
*/
addItem(itemType, fileName, metadata, position, rotation, scale, fixed, newItemDefinitions)
{
if(itemType == undefined)
{
itemType = 1;
}
var scope = this;
function addToMaterials(materials, newmaterial)
{
for(var i=0;i<materials.length;i++)
{
var mat = materials[i];
if(mat.name == newmaterial.name)
{
return [materials, i];
}
}
materials.push(newmaterial);
return [materials, materials.length-1];
}
var loaderCallback = function (geometry, materials, isgltf=false)
{
// var item = new (Factory.getClass(itemType))(scope.model, metadata, geometry, new MeshFaceMaterial(materials), position, rotation, scale);
var item = new (Factory.getClass(itemType))(scope.model, metadata, geometry, materials, position, rotation, scale, isgltf);
item.fixed = fixed || false;
scope.items.push(item);
scope.add(item);
item.initObject();
scope.dispatchEvent({type:EVENT_ITEM_LOADED, item: item});
if(newItemDefinitions)
{
item.moveToPosition(newItemDefinitions.position, newItemDefinitions.edge);
item.placeInRoom();
}
};
var gltfCallback = function(gltfModel)
{
var newmaterials = [];
var newGeometry = new Geometry();
gltfModel.scene.traverse(function (child) {
if(child.type == 'Mesh')
{
var materialindices = [];
if(child.material.length)
{
for (var k=0;k<child.material.length;k++)
{
var newItems = addToMaterials(newmaterials, child.material[k]);
newmaterials = newItems[0];
materialindices.push(newItems[1]);
}
}
else
{
newItems = addToMaterials(newmaterials, child.material);//materials.push(child.material);
newmaterials = newItems[0];
materialindices.push(newItems[1]);
}
if(child.geometry.isBufferGeometry)
{
var tGeometry = new Geometry().fromBufferGeometry(child.geometry);
tGeometry.faces.forEach((face)=>{
// face.materialIndex = face.materialIndex + newmaterials.length;
face.materialIndex = materialindices[face.materialIndex];
});
child.updateMatrix();
newGeometry.merge(tGeometry, child.matrix);
}
else
{
child.geometry.faces.forEach((face)=>{
// face.materialIndex = face.materialIndex + newmaterials.length;
face.materialIndex = materialindices[face.materialIndex];
});
child.updateMatrix();
newGeometry.mergeMesh(child);
}
}
});
loaderCallback(newGeometry, newmaterials);
// loaderCallback(gltfModel.scene, newmaterials, true);
};
var objCallback = function(object)
{
var materials = [];
var newGeometry = new Geometry();
object.traverse(function (child)
{
if(child.type == 'Mesh')
{
if(child.material.length)
{
materials = materials.concat(child.material);
}
else
{
materials.push(child.material);
}
if(child.geometry.isBufferGeometry)
{
var tGeometry = new Geometry().fromBufferGeometry(child.geometry);
child.updateMatrix();
newGeometry.merge(tGeometry, child.matrix);
}
else
{
child.updateMatrix();
newGeometry.mergeMesh(child);
}
}
});
loaderCallback(newGeometry, materials);
};
this.dispatchEvent({type:EVENT_ITEM_LOADING});
if(!metadata.format)
{
this.loader.load(fileName, loaderCallback, undefined); // third parameter is undefined - TODO_Ekki
}
else if(metadata.format == 'gltf')
{
this.gltfloader.load(fileName, gltfCallback, null, null);
}
else if(metadata.format == 'obj')
{
this.objloader.load(fileName, objCallback, null, null);
}
}