You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

144 lines
5.4 KiB
JavaScript

/* Copyright (C) 2024 Sebastián Santisi <ssantisi@fi.uba.ar>, CSC-CONICET */
import * as THREE from 'three';
import { STLLoader } from 'https://cdn.jsdelivr.net/npm/three@0.136.0/examples/jsm/loaders/STLLoader.js';
import { Spring } from './spring.js';
class WindMill extends THREE.Group {
static geometryBody = null;
static geometryBlades = null;
static geometryExclamation = null;
static {
const loader = new STLLoader();
loader.load('assets/aspas_126m_85.stl', function(geometry) {
WindMill.geometryBlades = geometry;
});
loader.load('assets/nacele_126m_90.stl', function(geometry) {
//loader.load('assets/cuerpo_126m_50.stl', function(geometry) {
WindMill.geometryBody = geometry;
});
loader.load('assets/exclamacion_50.stl', function(geometry) {
WindMill.geometryExclamation = geometry;
var colors = new three.BufferAttribute(new Float32Array(geometry.attributes.position.array.length), 3);
for(let i = 0; i < colors.count; i++)
if(Math.abs(geometry.attributes.position.getZ(i)) < 7.4)
colors.setXYZ(i, 0xfa / 0xff, 0xd5 / 0xff, 0x0a / 0xff);
WindMill.geometryExclamation.setAttribute('color', colors);
});
}
static isReady() {
return WindMill.geometryBody != null && WindMill.geometryBlades != null && WindMill.geometryExclamation != null;
}
constructor() {
super()
this.isLow = false;
this.material = new THREE.MeshStandardMaterial({color: 0xffffff, roughness: 0.15});
this.materialCircle = new THREE.MeshStandardMaterial({color: 0xaa0000});
//this.materialCircle.transparent = true;
//this.materialCircle.opacity = 0.5;
this.meshBlades = new THREE.Mesh(WindMill.geometryBlades, this.material);
this.meshBlades.castShadow = true;
this.meshBlades.receiveShadow = true;
this.meshBlades.scale.set(0.0075, 0.0075, 0.0075);
this.meshBlades.position.z = 90/126;
this.meshBody = new THREE.Mesh(WindMill.geometryBody, this.material);
this.meshBody.castShadow = true;
this.meshBody.receiveShadow = true;
this.meshBody.scale.set(0.0075, 0.0075, 0.0075);
this.meshBody.position.z = 90/126;
var geometry = new THREE.CylinderGeometry(0.02, 0.025, 90/126, 15);
this.meshPole = new THREE.Mesh(geometry, this.material);
this.meshPole.castShadow = true;
this.meshPole.receiveShadow = true;
this.meshPole.rotateX(Math.PI/2);
this.meshPole.position.z = 90/126/2;
this.meshPole.position.x = 0.02;
geometry = new THREE.CircleGeometry(0.5, 20);
this.meshCircle = new THREE.Mesh(geometry, this.materialCircle);
this.meshCircle.receiveShadow = true;
this.meshCircle.position.z = 0.001;
this.meshCircle.visible = false;
this.add(this.meshBlades);
this.add(this.meshBody);
this.add(this.meshPole);
this.add(this.meshCircle);
this.spring1 = new Spring(0.5, 2, 20, 6, 1, new THREE.MeshBasicMaterial({color: 0xff0000}));
this.spring1.position.z = 90/126;
this.spring1.rotation.z = -Math.PI/2;
this.spring1.rotation.x = -Math.PI;
this.spring1.update();
this.add(this.spring1);
this.spring2 = new Spring(0.5, 2, 20, 6, 1, new THREE.MeshBasicMaterial({color: 0xff0000}));
this.spring2.position.z = 90/126;
this.spring2.rotation.z = -Math.PI/2;
this.spring2.rotation.x = -Math.PI * 1 / 3;
this.spring2.update();
this.add(this.spring2);
this.spring3 = new Spring(0.5, 2, 20, 6, 1, new THREE.MeshBasicMaterial({color: 0xff0000}));
this.spring3.position.z = 90/126;
this.spring3.rotation.z = -Math.PI/2;
this.spring3.rotation.x = Math.PI * 1 / 3;
this.spring3.update();
this.add(this.spring3);
this.spring1.visible = this.spring2.visible = this.spring3.visible = false;
var material = new THREE.MeshStandardMaterial({color: 0xfad50a, roughness: 0.15, vertexColors: true});
this.meshExclamation = new THREE.Mesh(WindMill.geometryExclamation, material);
this.meshExclamation.castShadow = true;
//this.meshExclamation.receiveShadow = true;
this.meshExclamation.scale.setScalar(0.002);
this.meshExclamation.rotateX(Math.PI / 2);
this.meshExclamation.position.z = (90 + 126 / 2 + 1) / 126;
this.add(this.meshExclamation);
this.meshExclamation.visible = false;
this.selected = false;
}
animate(step) {
var inc = (this.isLow ? 0.3 : 3) * step;
this.meshBlades.rotation.x += inc;
this.spring1.rotation.x += inc;
this.spring2.rotation.x += inc;
this.spring3.rotation.x += inc;
this.meshExclamation.rotation.y += 4 * step;
}
select(selected) {
if(selected == this.selected) return;
this.selected = selected;
this.meshCircle.visible = selected;
if(selected)
this.material.color.set(0xff0000);
else
this.material.color.set(0xffffff);
}
lowPower(isLow) {
if(this.isLow == isLow) return;
this.isLow = isLow;
this.meshExclamation.visible = isLow;
}
show_springs(value) {
this.spring1.visible = this.spring2.visible = this.spring3.visible = value;
}
}
export { WindMill };