-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
91 lines (77 loc) · 2.39 KB
/
Copy pathmain.js
File metadata and controls
91 lines (77 loc) · 2.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import { ShadingScene, colormaps } from "@openpv/simshady"
import * as THREE from "three"
import { MapControls } from "three/addons/controls/MapControls.js"
import { STLLoader } from "three/examples/jsm/loaders/STLLoader.js"
async function main() {
console.log("Main script started")
const geometry = await loadSTLFile("building.stl")
console.log("geometry:")
console.log(geometry)
const scene = new ShadingScene()
scene.addSimulationGeometry(geometry)
await scene.addSolarIrradianceFromURL(
"https://api.openpv.de/skymaps/irradiance_50.0_8.0_2018_yearly.json",
)
const c0 = [0, 0, 0.2]
const c1 = [1, 0.2, 0.1]
const c2 = [1, 1, 0.1]
scene.addColorMap(
colormaps.interpolateThreeColors({ c0: c0, c1: c1, c2: c2 }),
)
let mesh = await scene.calculate({
// .21 is the efficiency of a solar panel
// .78 is the coverage factor of panels on a roof
solarToElectricityConversionEfficiency: 0.21 * 0.78,
})
showThreeJS(mesh)
}
main()
async function loadSTLFile(url) {
const loader = new STLLoader()
const response = await fetch(url)
console.log(response)
const arrayBuffer = await response.arrayBuffer()
return loader.parse(arrayBuffer)
}
function showThreeJS(mesh) {
console.log("Called showThreeJS")
console.log(mesh)
const scene = new THREE.Scene()
const renderer = new THREE.WebGLRenderer()
const camera = new THREE.PerspectiveCamera(
45,
window.innerWidth / window.innerHeight,
0.1,
1000,
)
camera.up = new THREE.Vector3(0, 0, 1)
const controls = new MapControls(camera, renderer.domElement)
camera.position.set(0, -5, 5) // Adjust the camera position as needed
camera.up.set(0, 0, 1)
controls.mouseButtons = {
LEFT: THREE.MOUSE.PAN,
MIDDLE: THREE.MOUSE.DOLLY,
RIGHT: THREE.MOUSE.ROTATE,
}
controls.screenSpacePanning = false
controls.maxPolarAngle = Math.PI / 2
scene.add(new THREE.AmbientLight(0xffffff, 1))
renderer.setSize(window.innerWidth, window.innerHeight)
document.body.appendChild(renderer.domElement)
window.addEventListener(
"resize",
function () {
renderer.setSize(window.innerWidth, window.innerHeight)
camera.aspect = window.innerWidth / window.innerHeight
camera.updateProjectionMatrix()
},
false,
)
scene.add(mesh)
function animate() {
requestAnimationFrame(animate)
controls.update()
renderer.render(scene, camera)
}
animate()
}