import * as THREE from 'three'; import { OrbitControls } from 'three-orbitcontrols-ts';
private renderer: THREE.WebGLRenderer; private camera: THREE.OrthographicCamera; public scene: THREE.Scene; public s = 200; public controls: OrbitControls; public satellite: THREE.Points;
@ViewChild('canvas') private canvasRef: ElementRef;
constructor() { this.render = this.render.bind(this); }
private get canvas(): HTMLCanvasElement { return this.canvasRef.nativeElement; }
private getVCenter(v1, v2) { let v = v1.add(v2); return v.divideScalar(2); }
private getLenVcetor(v1, v2, len) { let v1v2Len = v1.distanceTo(v2); return v1.lerp(v2, len / v1v2Len); }
lglt2xyz(lng, lat, radius) { const theta = (90 + lng) * (Math.PI / 180) const phi = (90 - lat) * (Math.PI / 180) return (new THREE.Vector3()).setFromSpherical(new THREE.Spherical(radius, phi, theta)) }
private addLines(v0, v3) { var angle = (v0.angleTo(v3) * 1.8) / Math.PI / 0.1; var aLen = angle * 12, hLen = angle * angle * 12; var p0 = new THREE.Vector3(0, 0, 0); var vtop = new THREE.Vector3(0, 0, 0); var rayLine = new THREE.Ray(p0, this.getVCenter(v0.clone(), v3.clone())); rayLine.at(hLen / rayLine.at(1, rayLine.origin).length(), vtop); var v1 = this.getLenVcetor(v0.clone(), vtop, aLen); var v2 = this.getLenVcetor(v3.clone(), vtop, aLen); var curve = new THREE.CubicBezierCurve3(v0, v1, v2, v3); var gLine = new THREE.BufferGeometry(); var gPoints = curve.getPoints(60); gLine.setFromPoints(gPoints) var mLine = new THREE.LineBasicMaterial({ color: 'violet', }); return { curve: curve, lineMesh: new THREE.Line(gLine, mLine) }; }
private createScene() { this.scene = new THREE.Scene(); var geometry = new THREE.SphereGeometry(130,40,40); var p1 = new THREE.Vector3(200,0,0); const points = [p1]; var satelliteGeometry = new THREE.BufferGeometry(); satelliteGeometry.setFromPoints(points); var loader = new THREE.TextureLoader(); loader.load('../../assets/images/earth3.jpg', (texture)=>{ var material = new THREE.MeshLambertMaterial({ map:texture, }) var mesh = new THREE.Mesh(geometry,material) this.scene.add(mesh) }); loader.load('../../assets/images/moon.png',(texture)=>{ var satelliteMaterial = new THREE.PointsMaterial({ map:texture, transparent:true, size:20, depthWrite:false }) this.satellite = new THREE.Points(satelliteGeometry,satelliteMaterial) this.scene.add(this.satellite) this.render(); }) var lGroup = new THREE.Group(); lGroup.add(this.addLines(this.lglt2xyz(116.20,39.56,130),this.lglt2xyz(-74.70,40.43,130)).lineMesh) this.scene.add(lGroup) }
private createLight() { var light = new THREE.AmbientLight(0xffffff) this.scene.add(light); }
private createCamera() { var k = window.innerWidth/window.innerHeight; this.camera = new THREE.OrthographicCamera(-this.s*k,this.s*k,this.s,-this.s,1,10000); this.camera.position.set(5,-20,200); this.camera.lookAt(this.scene.position); }
private renderSetting() { this.renderer = new THREE.WebGLRenderer({ canvas: this.canvas, antialias: true }); this.renderer.setSize(window.innerWidth, window.innerHeight); this.renderer.setClearColor(0xb9d3ff, 1); }
public render() { this.renderer.render(this.scene, this.camera); this.satellite.rotateY(0.01); requestAnimationFrame(this.render); }
public addControls() { this.controls = new OrbitControls(this.camera,this.renderer.domElement); }
@HostListener('window:resize', ['$event']) public onResize(event: Event) { this.canvas.style.width = "100%"; this.canvas.style.height = "100%"; this.renderer.setSize(this.canvas.offsetWidth, this.canvas.offsetHeight); this.camera.updateProjectionMatrix(); } ngOnInit() { this.createScene(); this.createLight(); this.createCamera(); this.renderSetting(); this.addControls(); }
|