fix 3dm draw

This commit is contained in:
zaccaria.majid
2023-10-23 10:42:49 +02:00
parent 99175b166f
commit c8aa50ed20
5 changed files with 396 additions and 356 deletions
+6 -3
View File
@@ -22,12 +22,15 @@
_dimX : X,
_dimY: Y,
_fileName: "Ex3.3dm",
_showLimitedGrid: true,
_showInfiniteGrid: true,
_showGrid: false,
_isOrtographic: true,
_modelsDir: "./src/test3/",
_fileType: '3DM',
// _showFrame: true,
// _showGrid: true
_showFrame: true,
_frameType: 2,
_frameLen: 1500, //mm
_frameFat: 10 //mm
}
console.log(options._modelsDir + options._fileName)
+4 -4
View File
@@ -10,7 +10,7 @@
"license": "MIT",
"dependencies": {
"vue": "^2.5.11",
"webgl-visualizer": "^1.2.231019-beta.1506"
"webgl-visualizer": "^1.2.231023-beta.950"
},
"devDependencies": {
"babel-core": "^6.26.0",
@@ -9296,9 +9296,9 @@
}
},
"node_modules/webgl-visualizer": {
"version": "1.2.231019-beta.1506",
"resolved": "https://nexus.steamware.net/repository/npm-group/webgl-visualizer/-/webgl-visualizer-1.2.231019-beta.1506.tgz",
"integrity": "sha512-iIe/E75g6IVmBSLDwSW0qVLVo890SH7U04dqFENds4dzC4ULIbhQ/GjGhW0mhjA9HrvBAgSOfPArVrRGzf7Rzw==",
"version": "1.2.231023-beta.950",
"resolved": "https://nexus.steamware.net/repository/npm-group/webgl-visualizer/-/webgl-visualizer-1.2.231023-beta.950.tgz",
"integrity": "sha512-p69VybrVh+vYyRRQlYL+bASi3GdBPDIQowUAJNbfZwKUrw3XwUvy0yexIMzB3+eJZVZcVqg9NGikPXkAUZPvSA==",
"license": "ISC"
},
"node_modules/webpack": {
+1 -1
View File
@@ -11,7 +11,7 @@
},
"dependencies": {
"vue": "^2.5.11",
"webgl-visualizer": "^1.2.231019-beta.1506"
"webgl-visualizer": "^1.2.231023-beta.950"
},
"browserslist": [
"> 1%",
+385 -348
View File
@@ -9,18 +9,19 @@ import { Rhino3dmLoader } from '../jsm/loaders/3DMLoader.js';
var m_divStartDimX = 500;
var m_divStartDimY = 400;
var fName = "Cubo.3mf";
var fName = "";
//var fileType = "3MF";
var fileType = '3DM';
var m_nApproxIndex = 5;
var m_bShowDir = true;
// *** camera settings ***
var m_bZoom = true;
var m_bPan = true;
var m_bRot = true;
var m_nCamPos = 0;
var m_nCameraStartPos = 8 ;
var m_bIsOrtographic = true ;
// *** index blender ***
@@ -55,13 +56,18 @@ var layer_list = [];
const GLBox3D = new THREE.Box3(); // Box3D globale delle geometrie
//init();
class webgl_original {
async initcall({ _dimX, _dimY, _fileName, _showInfiniteGrid, _fileType, _modelsDir}) {
async initcall({ _dimX, _dimY, _fileName, _showInfiniteGrid, _fileType, _modelsDir, _showGrid, _showFrame, _frameType, _frameLen, _frameFat}) {
m_divStartDimX = _dimX;
m_divStartDimY = _dimY;
m_bShowGrid = _showGrid;
fName = _fileName;
m_bInfGrid = _showInfiniteGrid;
modelsDir = _modelsDir;
fileType = _fileType;
m_bShowFrame = _showFrame;
m_bFrameType = _frameType;
m_lFrameLen = _frameLen;
m_dFrameFat = _frameFat;
//console.log(fName)
setLoaders();
if(fileType.toUpperCase() == '3MF')
@@ -255,183 +261,189 @@ function init3dm(){
return new Promise(() => {
setTimeout(() => {
loader.setLibraryPath( 'https://cdn.jsdelivr.net/npm/rhino3dm@7.15.0/' ) ;
loader.load( modelsDir + fName , m_nApproxIndex, function ( object ) {
object.traverse( function( child) {
child.castShadow = true ;
}) ;
// Ref systems
const Mat_GlobToLoc = new THREE.Matrix4() ; //<---- rotations for XY plane from Extrusion
const Mat_LocToGlob = new THREE.Matrix4() ; //<---- rotations for Extrusion from XY plane
const Mat_ThreeJs_Frame = new THREE.Matrix4() ; // <----- rotations from XYZ ref system to ThreeJS system
Mat_ThreeJs_Frame.set( 1, 0, 0, 0,
0, 0, 1, 0,
0, -1, 0, 0,
0, 0, 0, 1) ;
var Mat_ToScene = new THREE.Matrix4() ;
// Extrusion settings for curves
const extrudeSettings = {
steps: 1,
depth: 0,
bevelEnabled: false,
bevelThickness: 1,
bevelSize: 1,
bevelOffset: 0,
bevelSegments: 1
} ;
// loading entities...
for ( var i = 0 ; i < object.children.length ; ++ i) {
// getting i-th object
var child = object.children[i] ;
//console.log( child);
loader.load( modelsDir + fName, m_nApproxIndex, function ( object ) {
object.traverse( function( child) {
child.castShadow = true ;
}) ;
// Ref systems
const Mat_GlobToLoc = new THREE.Matrix4() ; //<---- rotations for XY plane from Extrusion
const Mat_LocToGlob = new THREE.Matrix4() ; //<---- rotations for Extrusion from XY plane
const Mat_ThreeJs_Frame = new THREE.Matrix4() ; // <----- rotations from XYZ ref system to ThreeJS system
Mat_ThreeJs_Frame.set( 1, 0, 0, 0,
0, 0, 1, 0,
0, -1, 0, 0,
0, 0, 0, 1) ;
var Mat_ToScene = new THREE.Matrix4() ;
// Extrusion settings for curves
const extrudeSettings = {
steps: 1,
depth: 0,
bevelEnabled: false,
bevelThickness: 1,
bevelSize: 1,
bevelOffset: 0,
bevelSegments: 1
} ;
// loading entities...
for ( var i = 0 ; i < object.children.length ; ++ i) {
// getting i-th object
var child = object.children[i] ;
//console.log( child);
// READING A POINT TYPE -----------------------------------------------------------------
if ( child.type == 'Points') {
try {
if ( child.name != 'Orig') {
console.log( 'Not a valid origin') ;
return false ;
}
// start layer
nStartLayer = parseInt( child.userData.attributes.layerIndex) ;
// ORIGIN ------------------------
var coord = child.userData.attributes.geometry.location ;
var orig_x = parseFloat( coord[0]) ;
var orig_y = parseFloat( coord[2]) ;
var orig_z = - parseFloat( coord[1]) ;
ORIG = new THREE.Vector3( orig_x, orig_y, orig_z) ;
// EXTRUSION ---------------------
var bOk_ext = false ;
if ( child.userData.attributes.userStrings[1][0] == 'SlicingDir') {
var sExtr = child.userData.attributes.userStrings[1][1] ;
if ( sExtr != "" ) {
let sExtr_arr = sExtr.split(',') ;
if ( sExtr_arr.length == 3) {
var dExtr_x = parseFloat( sExtr_arr[0]) ;
var dExtr_y = parseFloat( sExtr_arr[1]) ;
var dExtr_z = parseFloat( sExtr_arr[2]) ;
vtExtr = new THREE.Vector3( dExtr_x, dExtr_y, dExtr_z) ;
bOk_ext = true ;
}
}
}
if ( ! bOk_ext) {
console.log( ' Not a valid Extrusion') ;
return false ;
}
var Z_AX = new THREE.Vector3( vtExtr.x, vtExtr.y, vtExtr.z) ;
var X_AX = new THREE.Vector3() ;
var Y_AX = new THREE.Vector3() ;
if ( ! Z_AX.equals( new THREE.Vector3( 0, 0, 1))) {
X_AX.crossVectors( new THREE.Vector3( 0, 0, 1), Z_AX) ;
Y_AX.crossVectors( Z_AX, X_AX) ;
}
else {
X_AX = new THREE.Vector3( 1, 0, 0) ;
Y_AX = new THREE.Vector3( 0, 1, 0) ;
}
Mat_GlobToLoc.set( X_AX.x, X_AX.y, X_AX.z, 0,
Y_AX.x, Y_AX.y, Y_AX.z, 0,
Z_AX.x, Z_AX.y, Z_AX.z, 0,
0, 0, 0, 1) ;
Mat_LocToGlob.copy( Mat_GlobToLoc) ;
Mat_LocToGlob.invert() ;
Mat_ToScene.multiplyMatrices( Mat_ThreeJs_Frame, Mat_LocToGlob) ;
// STRAND -------------------------
if ( child.userData.attributes.userStrings[0][0] == 'StrandH') {
dStrand = - parseFloat( child.userData.attributes.userStrings[0][1]) ;
extrudeSettings.depth = dStrand ;
}
else {
console.log( ' Not a valid Strand') ;
return false ;
}
}
catch( error) {
console.log( 'Orig, Extrusion or Strand missing or not implemented') ;
return false ;
}
}
// READING A LINE -----------------------------------------------------------------------
else if ( child.type == 'Line') {
// Parsing the extrusion and the Layer of the curve
try {
var nLayer = parseInt( child.userData.attributes.layerIndex) - nStartLayer ;
if ( nLayer < 0)
return false ;
var points_array = child.geometry.attributes.position.array ;
//var ptPrec ;
const shape = new THREE.Shape() ;
for ( var v = 0 ; v < points_array.length ; v += 3) {
var Zact ;
var vtCoord = new THREE.Vector4( points_array[v], points_array[v+1], points_array[v+2], 1) ;
vtCoord.applyMatrix4( Mat_GlobToLoc) ;
if ( v == 0) {
shape.moveTo( vtCoord.x, vtCoord.y) ;
Zact = vtCoord.z ;
}
else
shape.lineTo( vtCoord.x, vtCoord.y) ;
}
var cCol = child.material.color.clone() ;
if ( nLayer%2 == 0) {
cCol.lerp( new THREE.Color( 0x000000 ), 0.25) ;
}
const geometry = new THREE.ExtrudeGeometry( shape, extrudeSettings) ;
const material = new THREE.MeshLambertMaterial({ color: cCol,
wireframe : false,
shadowSide : THREE.DoubleSide}) ;
const mesh = new THREE.Mesh( geometry, material) ;
mesh.translateZ( Zact) ;
//mesh.applyMatrix4( Mat_LocToGlob) ;
//mesh.applyMatrix4( Mat_ThreeJs_Frame) ;
mesh.applyMatrix4( Mat_ToScene) ;
mesh.nLayer = nLayer ;
mesh.isArrow = false ;
layer_list.push( mesh) ;
} catch( err) {
console.log( ' Not a valid Layer for paths') ;
return false ;
}
}
// READING ARROW'S MESH
else if ( child.type == 'Mesh') {
try {
if ( child.name != 'Arrow') {
//console.log( 'Not a valid Arrow direction') ;
//return false ;
child.nLayer = 1 ;
layer_list.push( child) ;
continue ;
}
var nLayer = parseInt( child.userData.attributes.layerIndex) - nStartLayer ;
if ( nLayer < 0)
return false ;
child.isArrow = true ;
child.applyMatrix4( Mat_ThreeJs_Frame) ;
child.nLayer = nLayer ;
child.material.color = new THREE.Color( 0xff0000) ;
layer_list.push( child) ;
} catch( e) {
console.log( 'Not a valid Arrow direction') ;
return false ;
}
}
// READING OTHER ENTITY --------------------
else
continue ;
// READING A POINT TYPE -----------------------------------------------------------------
if ( child.type == 'Points') {
try {
if ( child.name != 'Orig') {
console.log( 'Not a valid origin') ;
return false ;
}
// start layer
nStartLayer = parseInt( child.userData.attributes.layerIndex) ;
// ORIGIN ------------------------
var coord = child.userData.attributes.geometry.location ;
var orig_x = parseFloat( coord[0]) ;
var orig_y = parseFloat( coord[2]) ;
var orig_z = - parseFloat( coord[1]) ;
ORIG = new THREE.Vector3( orig_x, orig_y, orig_z) ;
// EXTRUSION ---------------------
var bOk_ext = false ;
if ( child.userData.attributes.userStrings[1][0] == 'SlicingDir') {
var sExtr = child.userData.attributes.userStrings[1][1] ;
if ( sExtr != "" ) {
let sExtr_arr = sExtr.split(',') ;
if ( sExtr_arr.length == 3) {
var dExtr_x = parseFloat( sExtr_arr[0]) ;
var dExtr_y = parseFloat( sExtr_arr[1]) ;
var dExtr_z = parseFloat( sExtr_arr[2]) ;
vtExtr = new THREE.Vector3( dExtr_x, dExtr_y, dExtr_z) ;
bOk_ext = true ;
}
}
}
if ( ! bOk_ext) {
console.log( ' Not a valid Extrusion') ;
return false ;
}
var Z_AX = new THREE.Vector3( vtExtr.x, vtExtr.y, vtExtr.z) ;
var X_AX = new THREE.Vector3() ;
var Y_AX = new THREE.Vector3() ;
if ( ! Z_AX.equals( new THREE.Vector3( 0, 0, 1))) {
X_AX.crossVectors( new THREE.Vector3( 0, 0, 1), Z_AX) ;
Y_AX.crossVectors( Z_AX, X_AX) ;
}
else {
X_AX = new THREE.Vector3( 1, 0, 0) ;
Y_AX = new THREE.Vector3( 0, 1, 0) ;
}
Mat_GlobToLoc.set( X_AX.x, X_AX.y, X_AX.z, 0,
Y_AX.x, Y_AX.y, Y_AX.z, 0,
Z_AX.x, Z_AX.y, Z_AX.z, 0,
0, 0, 0, 1) ;
Mat_LocToGlob.copy( Mat_GlobToLoc) ;
Mat_LocToGlob.invert() ;
Mat_ToScene.multiplyMatrices( Mat_ThreeJs_Frame, Mat_LocToGlob) ;
// STRAND -------------------------
if ( child.userData.attributes.userStrings[0][0] == 'StrandH') {
dStrand = - parseFloat( child.userData.attributes.userStrings[0][1]) ;
extrudeSettings.depth = dStrand ;
}
else {
console.log( ' Not a valid Strand') ;
return false ;
}
}
catch( error) {
console.log( 'Orig, Extrusion or Strand missing or not implemented') ;
return false ;
}
}
// READING A LINE -----------------------------------------------------------------------
else if ( child.type == 'Line') {
// Parsing the extrusion and the Layer of the curve
try {
var nLayer = parseInt( child.userData.attributes.layerIndex) - nStartLayer ;
if ( nLayer < 0)
return false ;
var points_array = child.geometry.attributes.position.array ;
//var ptPrec ;
const shape = new THREE.Shape() ;
for ( var v = 0 ; v < points_array.length ; v += 3) {
var Zact ;
var vtCoord = new THREE.Vector4( points_array[v], points_array[v+1], points_array[v+2], 1) ;
vtCoord.applyMatrix4( Mat_GlobToLoc) ;
//if ( v != 0 && v < points_array.length - 3 &&
// (( vtCoord.x - ptPrec.x) * ( vtCoord.x - ptPrec.x) +
// ( vtCoord.y - ptPrec.y) * ( vtCoord.y - ptPrec.y) +
// ( vtCoord.z - ptPrec.z) * ( vtCoord.z - ptPrec.z)) < 10)
// continue ;
if ( v == 0) {
shape.moveTo( vtCoord.x, vtCoord.y) ;
Zact = vtCoord.z ;
}
else
shape.lineTo( vtCoord.x, vtCoord.y) ;
//ptPrec = new THREE.Vector4( vtCoord.x, vtCoord.y, vtCoord.z, 1) ;
}
var cCol = child.material.color.clone() ;
if ( nLayer%2 == 0) {
cCol.lerp( new THREE.Color( 0x000000 ), 0.25) ;
}
const geometry = new THREE.ExtrudeGeometry( shape, extrudeSettings) ;
const material = new THREE.MeshLambertMaterial({ color: cCol,
wireframe : false,
shadowSide : THREE.DoubleSide}) ;
const mesh = new THREE.Mesh( geometry, material) ;
mesh.translateZ( Zact) ;
//mesh.applyMatrix4( Mat_LocToGlob) ;
//mesh.applyMatrix4( Mat_ThreeJs_Frame) ;
mesh.applyMatrix4( Mat_ToScene) ;
mesh.nLayer = nLayer ;
mesh.isArrow = false ;
layer_list.push( mesh) ;
} catch( err) {
console.log( ' Not a valid Layer for paths') ;
return false ;
}
}
// READING ARROW'S MESH
else if ( child.type == 'Mesh') {
try {
if ( child.name != 'Arrow') {
//console.log( 'Not a valid Arrow direction') ;
//return false ;
child.nLayer = 1 ;
layer_list.push( child) ;
continue ;
}
var nLayer = parseInt( child.userData.attributes.layerIndex) - nStartLayer ;
if ( nLayer < 0)
return false ;
child.isArrow = true ;
child.applyMatrix4( Mat_ThreeJs_Frame) ;
child.nLayer = nLayer ;
child.material.color = new THREE.Color( 0xff0000) ;
layer_list.push( child) ;
} catch( e) {
console.log( 'Not a valid Arrow direction') ;
return false ;
}
}
// READING OTHER ENTITY --------------------
else
continue ;
}
AdaptSeceneToContest() ;
let endTime = performance.now();
let timeElapsed = endTime - startTime;
console.log('init3dm in : ' + timeElapsed)
// console.log(new Date()) ;
}) ;
}
AdaptSeceneToContest() ;
console.log(new Date()) ;
}) ;
let endTime = performance.now();
let timeElapsed = endTime - startTime;
console.log('init3dm in : ' + timeElapsed)
}, 0)
})
@@ -439,8 +451,29 @@ function init3dm(){
function refreshElement(nStart, nEnd) {
//console.log(parseInt(nStart));
for (var i = 0; i < layer_list.length; ++i) {
if (parseInt(layer_list[i].children[0].nLayer) >= parseInt(nStart) &&
m_nStartL = nStart;
m_nEndL = nEnd;
if(fileType == "3DM"){
for ( var i = 0 ; i < layer_list.length ; ++ i) {
if ( parseInt( layer_list[i].nLayer) >= nStart &&
parseInt( layer_list[i].nLayer) <= nEnd) {
if ( layer_list[i].isArrow)
layer_list[i].visible = m_bShowDir ;
else
layer_list[i].visible = true ;
}
else {
layer_list[i].visible = false ;
}
}
}
else if (fileType == "3MF"){
for (var i = 0; i < layer_list.length; ++i) {
if (parseInt(layer_list[i].children[0].nLayer) >= parseInt(nStart) &&
parseInt(layer_list[i].children[0].nLayer) <= parseInt(nEnd)) {
layer_list[i].visible = true;
}
@@ -448,6 +481,7 @@ function refreshElement(nStart, nEnd) {
layer_list[i].visible = false;
}
}
}
render();
}
@@ -462,13 +496,13 @@ function AdaptSeceneToContest() {
// Global Box ----------
if(fileType == "3DM"){
for ( var i = 0 ; i < layer_list.length ; ++ i) {
const Objbox = new THREE.Box3().setFromObject( layer_list[i]);
for ( var i = 0 ; i < layer_list.length ; ++ i) {
const Objbox = new THREE.Box3().setFromObject( layer_list[i]);
GLBox3D.union( Objbox) ;
layer_list[i].material.side = THREE.DoubleSide ;
layer_list[i].castShadow = true
if ( ! layer_list[i].isArrow || m_bShowPrintDir)
scene.add( layer_list[i]) ;
scene.add( layer_list[i]) ;
}
}
else if(fileType == "3MF"){
@@ -497,8 +531,8 @@ function AdaptSeceneToContest() {
//scene.add( BoxHelper);
// orthographic and perspective camera ------
var perspectiveCamera = new THREE.PerspectiveCamera( 35, window.innerWidth / window.innerHeight, 10, 300000 ) ;
var orthographicCamera = new THREE.OrthographicCamera( window.innerWidth / - 2, window.innerWidth / 2,
perspectiveCamera = new THREE.PerspectiveCamera( 35, window.innerWidth / window.innerHeight, 10, 300000 ) ;
orthographicCamera = new THREE.OrthographicCamera( window.innerWidth / - 2, window.innerWidth / 2,
window.innerHeight / 2, window.innerHeight / - 2,
-300000, 300000) ;
if ( ! m_bIsOrtographic)
@@ -555,14 +589,14 @@ function AdaptSeceneToContest() {
// Orig conversion -----
if(fileType == "3DM"){
const points_array = [ new THREE.Vector3( 0, 0, 0)] ;
var ORIG_Geometry = new THREE.BufferGeometry() ;
const points_array = [ new THREE.Vector3( ORIG.x, ORIG.y, ORIG.z)] ;
var ORIG_Geometry = new THREE.BufferGeometry() ;
ORIG_Geometry.setFromPoints( points_array) ;
var ORIG_Material = new THREE.PointsMaterial({ size: 5, sizeAttenuation : true, color : new THREE.Color( 0x000000)});
var ORIG_Material = new THREE.PointsMaterial({ size: 5, sizeAttenuation : true, color : new THREE.Color( 0x000000)}) ;
var ptORIG = new THREE.Points( ORIG_Geometry, ORIG_Material) ;
ptORIG.position.x = ORIG.x ;
ptORIG.position.y = ORIG.z ;
ptORIG.position.z = - ORIG.y ;
ptORIG.position.y = ORIG.y ;
ptORIG.position.z = ORIG.z ;
}
else if(fileType == "3MF"){
@@ -579,14 +613,7 @@ function AdaptSeceneToContest() {
// SetOrig(ptORIG);
}
// Frame
AddFrame( ptORIG) ;
// Grid
AddGrid( ptORIG) ;
// render
render() ;
// inputs --------------
setCameraZoom( m_bZoom) ;
@@ -604,8 +631,16 @@ function AdaptSeceneToContest() {
// }) ;
// document.getElementById( 'ck_dir').addEventListener( 'change', function( e) {
// ManageSceneElements( e) ;
// })
// })
// Frame
AddFrame( ptORIG) ;
// Grid
AddGrid( ptORIG) ;
// render
render() ;
}
function render() {
renderer.render( scene, activeCamera) ;
@@ -633,59 +668,59 @@ function setCameraRot( bRot) {
controls.enableRotate = bRot ;
}
function SetCameraPos( nPos, ptORIG) {
var nTraslFactor = 10 ;
var x ; var y ; var z ;
y = GLBox3D.max.y ; // fixed
switch( nPos) {
case 0 :
x = GLBox3D.min.x ;
z = 0.5 * ( GLBox3D.min.z + GLBox3D.max.z) ;
break ;
case 1 :
x = GLBox3D.min.x ;
z = GLBox3D.max.z ;
break ;
case 2 :
x = 0.5 * ( GLBox3D.min.x + GLBox3D.max.x) ;
z = GLBox3D.max.z ;
break ;
case 3 :
x = GLBox3D.max.x ;
z = GLBox3D.max.z ;
break ;
case 4 :
x = GLBox3D.max.x ;
z = 0.5 * ( GLBox3D.min.z + GLBox3D.max.z) ;
break ;
case 5 :
x = GLBox3D.max.x ;
z = GLBox3D.min.z ;
break ;
case 6 :
x = 0.5 * ( GLBox3D.min.x + GLBox3D.max.x) ;
z = GLBox3D.min.z ;
break ;
case 7 :
x = GLBox3D.min.x ;
z = GLBox3D.min.z ;
break ;
case 8 : // optimal
x = ( ORIG.x + 1.0 * Math.abs( GLBox3D.max.x - GLBox3D.min.x)) ;
z = ( ORIG.z + 1.0 * Math.abs( GLBox3D.max.z - GLBox3D.min.z)) ;
break ;
default :
return ;
}
var vtPos = new THREE.Vector3( x, y, z) ;
const vtCenter = new THREE.Vector3( 0, 0, 0) ; GLBox3D.getCenter( vtCenter) ;
var vtDirOut = new THREE.Vector3( vtPos.x - vtCenter.x, vtPos.y - vtCenter.y, vtPos.z - vtCenter.z) ;
vtDirOut.x *= nTraslFactor ; vtDirOut.y *= nTraslFactor ; vtDirOut.z *= nTraslFactor ;
activeCamera.position.set( x + vtDirOut.x, y + vtDirOut.y, z + vtDirOut.z) ;
controls.target.set( vtCenter.x, vtCenter.y, vtCenter.z) ;
controls.minZoom = 0.25 ;
controls.maxDistance = 1.1 * activeCamera.position.distanceTo( controls.target) ;
controls.update() ;
var nTraslFactor = 20 ;
var x ; var y ; var z ;
y = GLBox3D.max.y ; // fixed
switch( nPos) {
case 0 :
x = GLBox3D.min.x ;
z = 0.5 * ( GLBox3D.min.z + GLBox3D.max.z) ;
break ;
case 1 :
x = GLBox3D.min.x ;
z = GLBox3D.max.z ;
break ;
case 2 :
x = 0.5 * ( GLBox3D.min.x + GLBox3D.max.x) ;
z = GLBox3D.max.z ;
break ;
case 3 :
x = GLBox3D.max.x ;
z = GLBox3D.max.z ;
break ;
case 4 :
x = GLBox3D.max.x ;
z = 0.5 * ( GLBox3D.min.z + GLBox3D.max.z) ;
break ;
case 5 :
x = GLBox3D.max.x ;
z = GLBox3D.min.z ;
break ;
case 6 :
x = 0.5 * ( GLBox3D.min.x + GLBox3D.max.x) ;
z = GLBox3D.min.z ;
break ;
case 7 :
x = GLBox3D.min.x ;
z = GLBox3D.min.z ;
break ;
case 8 : // optimal
x = ( ORIG.x + 1.0 * Math.abs( GLBox3D.max.x - GLBox3D.min.x)) ;
z = ( ORIG.z + 1.0 * Math.abs( GLBox3D.max.z - GLBox3D.min.z)) ;
break ;
default :
return ;
}
var vtPos = new THREE.Vector3( x, y, z) ;
const vtCenter = new THREE.Vector3( 0, 0, 0) ; GLBox3D.getCenter( vtCenter) ;
var vtDirOut = new THREE.Vector3( vtPos.x - vtCenter.x, vtPos.y - vtCenter.y, vtPos.z - vtCenter.z) ;
vtDirOut.x *= nTraslFactor ; vtDirOut.y *= nTraslFactor ; vtDirOut.z *= nTraslFactor ;
activeCamera.position.set( x + vtDirOut.x, y + vtDirOut.y, z + vtDirOut.z) ;
controls.target.set( vtCenter.x, vtCenter.y, vtCenter.z) ;
controls.minZoom = 0.25 ;
controls.maxDistance = 1.1 * activeCamera.position.distanceTo( controls.target) ;
controls.update() ;
}
function setFog() {
//var dNear = Math.abs( Math.max( 0.5 * activeCamera.position.distanceTo( controls.target), Math.max( Math.max( GLBox3D.max.x, GLBox3D.max.y), GLBox3D.max.z))) ;
@@ -694,116 +729,118 @@ function setFog() {
}
function toggleCamera( perspectiveCamera, orthographicCamera) {
var cameraPosition = activeCamera.position.clone() ;
var cameraMatrix = activeCamera.matrix.clone() ;
var ControlsTarget = controls.target.clone() ;
var ControlsPosition = controls.position0.clone() ;
var cameraMatrix = activeCamera.matrix.clone() ;
var ControlsTarget = controls.target.clone() ;
var ControlsPosition = controls.position0.clone() ;
if ( m_bIsOrtographic == true) {
m_bIsOrtographic = false ;
activeCamera = perspectiveCamera ;
activeCamera.position.copy( cameraPosition) ;
activeCamera.matrix.copy( cameraMatrix) ;
} else {
m_bIsOrtographic = true ;
activeCamera = orthographicCamera ;
activeCamera.position.copy( cameraPosition) ;
activeCamera.matrix.copy( cameraMatrix) ;
}
activeCamera.updateProjectionMatrix() ;
controls.object = activeCamera ;
controls.target = ControlsTarget ;
controls.position0 = ControlsPosition ;
controls.update() ;
render() ;
if ( m_bIsOrtographic == true) {
m_bIsOrtographic = false ;
activeCamera = perspectiveCamera ;
activeCamera.position.copy( cameraPosition) ;
activeCamera.matrix.copy( cameraMatrix) ;
} else {
m_bIsOrtographic = true ;
activeCamera = orthographicCamera ;
activeCamera.position.copy( cameraPosition) ;
activeCamera.matrix.copy( cameraMatrix) ;
}
activeCamera.updateProjectionMatrix() ;
controls.object = activeCamera ;
controls.target = ControlsTarget ;
controls.position0 = ControlsPosition ;
controls.update() ;
render() ;
}
function AddFrame( ptORIG) {
if ( ! m_bShowFrame)
return ;
return ;
if ( m_bFrameType == 0) {
const axesHelper = new THREE.AxesHelper( m_lFrameLen) ;
axesHelper.rotation.set( -Math.PI/2, 0 , 0) ;
axesHelper.position.set( ptORIG.position.x, ptORIG.position.y, ptORIG.position.z) ;
scene.add( axesHelper ) ;
}
else if ( m_bFrameType == 1) {
var vtDirs = [ new THREE.Vector3( 1, 0, 0), new THREE.Vector3( 0, 0, -1), new THREE.Vector3( 0, 1, 0)] ;
var vtCols = [ new THREE.Color( 0xff0000), new THREE.Color( 0x00ff00), new THREE.Color( 0x0000ff)] ;
var vtORIG = new THREE.Vector3( ptORIG.position.x, ptORIG.position.y, ptORIG.position.z) ;
for ( var i = 0 ; i < 3 ; ++ i) {
const arrowHelper = new THREE.ArrowHelper( vtDirs[i], vtORIG, m_lFrameLen, vtCols[i]) ;
scene.add( arrowHelper ) ;
}
}
else if ( m_bFrameType == 2) {
var vtCols = [ new THREE.Color( 0x0000ff), new THREE.Color( 0xff0000), new THREE.Color( 0x00ff00)] ;
for ( var i = 0 ; i < 3 ; ++ i) {
// cylinder ----
const fr_geometry_cyl = new THREE.CylinderGeometry( m_dFrameFat, m_dFrameFat, 0.8 * m_lFrameLen, 32) ;
const fr_material = new THREE.MeshBasicMaterial({color: vtCols[i]}) ;
const fr_cylinder = new THREE.Mesh( fr_geometry_cyl, fr_material) ;
fr_cylinder.position.set( ptORIG.position.x, ptORIG.position.y, ptORIG.position.z) ;
if ( i == 1)
fr_cylinder.rotation.set( 0, 0, -Math.PI/2) ;
if ( i == 2)
fr_cylinder.rotation.set( -Math.PI/2, 0, 0) ;
scene.add( fr_cylinder) ;
fr_cylinder.translateY( 0.4 * m_lFrameLen) ;
// pyramid
const fr_geometry_cone = new THREE.CylinderGeometry( 0, 2 * m_dFrameFat, 0.2 * m_lFrameLen, 32) ;
const fr_cone = new THREE.Mesh( fr_geometry_cone, fr_material) ;
fr_cone.position.set( ptORIG.position.x, ptORIG.position.y, ptORIG.position.z) ;
if ( i == 1)
fr_cone.rotation.set( 0, 0, -Math.PI/2) ;
if ( i == 2)
fr_cone.rotation.set( -Math.PI/2, 0, 0) ;
scene.add( fr_cone) ;
fr_cone.translateY( 0.9 * m_lFrameLen) ;
}
// Origin
const fr_orig_geometry = new THREE.SphereGeometry( 2 * m_dFrameFat, 32, 16) ;
const fr_orig_material = new THREE.MeshBasicMaterial( {color: 0x00000}) ;
const fr_sphere = new THREE.Mesh( fr_orig_geometry, fr_orig_material) ;
fr_sphere.position.set( ptORIG.position.x, ptORIG.position.y, ptORIG.position.z) ;
scene.add( fr_sphere) ;
}
else
console.log( 'Frame type not defined') ;
if ( m_bFrameType == 0) {
const axesHelper = new THREE.AxesHelper( m_lFrameLen) ;
axesHelper.rotation.set( -Math.PI/2, 0 , 0) ;
axesHelper.position.set( ptORIG.position.x, ptORIG.position.y, ptORIG.position.z) ;
scene.add( axesHelper ) ;
}
else if ( m_bFrameType == 1) {
var vtDirs = [ new THREE.Vector3( 1, 0, 0), new THREE.Vector3( 0, 0, -1), new THREE.Vector3( 0, 1, 0)] ;
var vtCols = [ new THREE.Color( 0xff0000), new THREE.Color( 0x00ff00), new THREE.Color( 0x0000ff)] ;
var vtORIG = new THREE.Vector3( ptORIG.position.x, ptORIG.position.y, ptORIG.position.z) ;
for ( var i = 0 ; i < 3 ; ++ i) {
const arrowHelper = new THREE.ArrowHelper( vtDirs[i], vtORIG, m_lFrameLen, vtCols[i]) ;
scene.add( arrowHelper ) ;
}
}
else if ( m_bFrameType == 2) {
var vtCols = [ new THREE.Color( 0x0000ff), new THREE.Color( 0xff0000), new THREE.Color( 0x00ff00)] ;
for ( var i = 0 ; i < 3 ; ++ i) {
// cylinder ----
const fr_geometry_cyl = new THREE.CylinderGeometry( m_dFrameFat, m_dFrameFat, 0.8 * m_lFrameLen, 32) ;
const fr_material = new THREE.MeshBasicMaterial({color: vtCols[i]}) ;
const fr_cylinder = new THREE.Mesh( fr_geometry_cyl, fr_material) ;
fr_cylinder.position.set( ptORIG.position.x, ptORIG.position.y, ptORIG.position.z) ;
if ( i == 1)
fr_cylinder.rotation.set( 0, 0, -Math.PI/2) ;
if ( i == 2)
fr_cylinder.rotation.set( -Math.PI/2, 0, 0) ;
scene.add( fr_cylinder) ;
fr_cylinder.translateY( 0.4 * m_lFrameLen) ;
// pyramid
const fr_geometry_cone = new THREE.CylinderGeometry( 0, 2 * m_dFrameFat, 0.2 * m_lFrameLen, 32) ;
const fr_cone = new THREE.Mesh( fr_geometry_cone, fr_material) ;
fr_cone.position.set( ptORIG.position.x, ptORIG.position.y, ptORIG.position.z) ;
if ( i == 1)
fr_cone.rotation.set( 0, 0, -Math.PI/2) ;
if ( i == 2)
fr_cone.rotation.set( -Math.PI/2, 0, 0) ;
scene.add( fr_cone) ;
fr_cone.translateY( 0.9 * m_lFrameLen) ;
}
// Origin
const fr_orig_geometry = new THREE.SphereGeometry( 2 * m_dFrameFat, 32, 16) ;
const fr_orig_material = new THREE.MeshBasicMaterial( {color: 0x00000}) ;
const fr_sphere = new THREE.Mesh( fr_orig_geometry, fr_orig_material) ;
fr_sphere.position.set( ptORIG.position.x, ptORIG.position.y, ptORIG.position.z) ;
scene.add( fr_sphere) ;
}
else
console.log( 'Frame type not defined') ;
}
function AddGrid( ptORIG) {
if ( ! m_bShowGrid)
return ;
const size = 1000000 ;
const divisions = 10000 ;
if ( m_bInfGrid) {
const gridHelper = new THREE.GridHelper( size, divisions) ;
gridHelper.position.set( ptORIG.position.x, ptORIG.position.y, ptORIG.position.z) ;
gridHelper.material.transparent = true ;
gridHelper.material.opacity = 0.25 ;
setFog() ;
scene.add( gridHelper) ;
}
else {
var dDimX = Math.abs( GLBox3D.max.x - GLBox3D.min.x) ;
var dDimY = Math.abs( GLBox3D.max.y - GLBox3D.min.y) ;
var dDimZ = Math.abs( GLBox3D.max.z - GLBox3D.min.z) ;
var dDim = 1.5 * Math.sqrt( dDimX * dDimX + dDimY * dDimY + dDimZ * dDimZ) ;
const gridHelper = new THREE.GridHelper( dDim, Math.ceil( 0.1 * dDim)) ;
gridHelper.position.set( ptORIG.position.x + 0.5 * dDim , ptORIG.position.y, ptORIG.position.z - 0.5 * dDim) ;
gridHelper.material.transparent = true ;
gridHelper.material.opacity = 0.45 ;
scene.add( gridHelper) ;
}
return ;
const size = 1000000 ;
const divisions = 10000 ;
if ( m_bInfGrid) {
const gridHelper = new THREE.GridHelper( size, divisions) ;
gridHelper.position.set( ptORIG.position.x, ptORIG.position.y, ptORIG.position.z) ;
gridHelper.material.transparent = true ;
gridHelper.material.opacity = 0.25 ;
setFog() ;
scene.add( gridHelper) ;
}
else {
var dDimX = Math.abs( GLBox3D.max.x - GLBox3D.min.x) ;
var dDimY = Math.abs( GLBox3D.max.y - GLBox3D.min.y) ;
var dDimZ = Math.abs( GLBox3D.max.z - GLBox3D.min.z) ;
var dDim = 1.5 * Math.sqrt( dDimX * dDimX + dDimY * dDimY + dDimZ * dDimZ) ;
const gridHelper = new THREE.GridHelper( dDim, Math.ceil( 0.1 * dDim)) ;
gridHelper.position.set( ptORIG.position.x + 0.5 * dDim , ptORIG.position.y, ptORIG.position.z - 0.5 * dDim) ;
gridHelper.material.transparent = true ;
gridHelper.material.opacity = 0.45 ;
scene.add( gridHelper) ;
}
}
function ShowPrintDir(showDir){
console.log(showDir);
m_bShowDir = showDir;
for ( var i = 0 ; i < layer_list.length ; ++ i) {
if ( layer_list[i].isArrow) {
if ( layer_list[i].nLayer >= nLayerStart &&
layer_list[i].nLayer <= nLayerEnd)
if ( layer_list[i].nLayer >= m_nStartL &&
layer_list[i].nLayer <= m_nEndL)
layer_list[i].visible = showDir ;
else
layer_list[i].visible = false ;