Last active
September 3, 2019 16:52
-
-
Save lydonchandra/89fe01870fbc6b6ad1e4b8ffe677737f to your computer and use it in GitHub Desktop.
rayTraceTest.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var cesiumViewer = window.viewer.state ? window.viewer.state.viewer: window.viewer | |
var camera = cesiumViewer.scene.camera | |
var scene = cesiumViewer.scene | |
//var scratchCart3 = new Cesium.Cartesian3() | |
var radius = 5 | |
cesiumViewer.entities.add( | |
{ | |
//id: "centerPixelBoundingSphere_"+Math.random(), | |
position: camera.position, | |
ellipsoid: { | |
radii: new Cesium.Cartesian3(radius, radius, radius), | |
fill: true, | |
outline: true, | |
outlineColor: Cesium.Color.RED.withAlpha( 0.5 ), | |
material: Cesium.Color.RED.withAlpha(0.5) | |
} | |
} ) | |
var point1 = Cesium.Cartesian3.subtract( camera.position, new Cesium.Cartesian3( 20, 20, 20), new Cesium.Cartesian3() ) | |
var point1a = Cesium.Cartesian3.subtract( camera.position, new Cesium.Cartesian3( 10, 10, 10), new Cesium.Cartesian3() ) | |
var vPoint1 = Cesium.Cartesian3.subtract( point1a, point1, new Cesium.Cartesian3() ) | |
cesiumViewer.entities.add({ | |
polyline: { | |
positions : [point1, point1a], | |
width : 2, | |
color: Cesium.Color.BLUE, | |
material: Cesium.Color.BLUE.withAlpha(0.5) | |
} | |
}); | |
scene.requestRender() | |
var point2 = Cesium.Cartesian3.subtract( camera.position, new Cesium.Cartesian3( 20, 20, 20), new Cesium.Cartesian3() ) | |
var point2a = Cesium.Cartesian3.subtract( camera.position, new Cesium.Cartesian3( 15, 1, 15), new Cesium.Cartesian3() ) | |
var vPoint2 = Cesium.Cartesian3.subtract( point2a, point2, new Cesium.Cartesian3() ) | |
cesiumViewer.entities.add({ | |
polyline: { | |
positions : [point2, point2a], | |
width : 2, | |
color: Cesium.Color.GREEN, | |
material: Cesium.Color.GREEN.withAlpha(0.5) | |
} | |
}); | |
var point3 = Cesium.Cartesian3.subtract( camera.position, new Cesium.Cartesian3( 20, 20, 20), new Cesium.Cartesian3() ) | |
var point3a = Cesium.Cartesian3.subtract( camera.position, new Cesium.Cartesian3( 20, 12, 12), new Cesium.Cartesian3() ) | |
var vPoint3 = Cesium.Cartesian3.subtract( point3a, point3, new Cesium.Cartesian3() ) | |
cesiumViewer.entities.add({ | |
polyline: { | |
positions : [point3, point3a], | |
width : 2, | |
color: Cesium.Color.YELLOW, | |
material: Cesium.Color.YELLOW.withAlpha(0.5) | |
} | |
}); | |
var point4 = Cesium.Cartesian3.subtract( camera.position, new Cesium.Cartesian3( 20, 20, 20), new Cesium.Cartesian3() ) | |
var point4a = Cesium.Cartesian3.subtract( camera.position, new Cesium.Cartesian3( 11, 9, 11), new Cesium.Cartesian3() ) | |
var vPoint4 = Cesium.Cartesian3.subtract( point4a, point4, new Cesium.Cartesian3() ) | |
cesiumViewer.entities.add({ | |
polyline: { | |
positions : [point4, point4a], | |
width : 2, | |
color: Cesium.Color.RED, | |
material: Cesium.Color.RED.withAlpha(0.5) | |
} | |
}); | |
scene.requestRender() | |
console.log( "blue line: is intersect: ",isIntersect(camera.position, 10, point1, vPoint1) ) | |
console.log( "green line: is intersect: ",isIntersect(camera.position, 10, point2, vPoint2) ) | |
console.log( "yellow line: is intersect: ",isIntersect(camera.position, 10, point3, vPoint3) ) | |
console.log( "red line: is intersect: ",isIntersect(camera.position, 10, point4, vPoint4) ) | |
function isIntersect( centerPosition, radius, pointPosition, pointDirection ) { | |
var vCenter2Point = Cesium.Cartesian3.subtract( pointPosition, centerPosition, new Cesium.Cartesian3() ) | |
var mCenter2Point = Cesium.Cartesian3.magnitude( vCenter2Point ) | |
if( mCenter2Point < radius ) { | |
return true | |
} | |
else { | |
dotProduct = Cesium.Cartesian3.dot( vCenter2Point, pointDirection ) | |
if( dotProduct <= 0 ) { | |
var vCenter2PointProjected = Cesium.Cartesian3.projectVector( vCenter2Point, pointDirection, new Cesium.Cartesian3() ) | |
var v = Cesium.Cartesian3.subtract( vCenter2Point, vCenter2PointProjected, new Cesium.Cartesian3() ) | |
if( Cesium.Cartesian3.magnitude( v ) <= radius ) { | |
return true | |
} | |
else { | |
return false | |
} | |
} | |
else { | |
return false | |
} | |
} | |
//input | |
// vector A | |
// vector d | |
// vector C | |
// float r | |
// //end of input | |
// vector collisionPoint | |
// vector v | |
// vector CA = A-C | |
// float rSquared = r*r | |
// float vSquared | |
// if(dotProduct(CA,CA) <= rSquared){ | |
// collisionPoint = A | |
// } | |
// else if(dotProduct(CA,d) <= 0){ | |
// v = CA - projection(CA,d) | |
// vSquared = dotProduct(v,v) | |
// if(vSquared <= rSquared){ | |
// collisionPoint = C + v - multiply(normalize(d),squareRoot(rSquared-vSquared)) | |
// } | |
// else{ | |
// collisionPoint = none | |
// } | |
// } | |
// else{ | |
// collisionPoint = none | |
// } | |
} |
Author
lydonchandra
commented
Sep 3, 2019
red and blue lines intersects red sphere
yellow and green lines do not
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment