source: js/standard_expdef_demo/js/world/core.js @ 880

Last change on this file since 880 was 880, checked in by Maciej Komosinski, 5 years ago

A simple javascript demo/animation of the basic behavior of "standard.expdef" experiment definition in Framsticks

File size: 3.9 KB
Line 
1/**
2 * @file World Object
3 * @author Patryk Gliszczynski
4 * @version 1.0
5 */
6
7class World {
8  /**
9  * Class used for storing all the objects visualized in the animation.
10  */
11
12  constructor(camera, scene) {
13    this.camera = camera;
14    this.scene = scene;
15    this.ground = this.Ground();
16    this.lights = this.Lighting();
17    this.sky = this.Sky();
18    this.sun = this.Sun();
19    this.table = this.Table();
20  }
21
22  Ground() {
23    let texture = new THREE.TextureLoader().load(Config.Ground.TEXTURE);
24    texture.wrapS = texture.wrapT = THREE.RepeatWrapping;
25    texture.repeat.set(Config.Ground.REPEAT, Config.Ground.REPEAT);
26    texture.anisotropy = Config.Ground.ANISOTROPY;
27
28    let geometry = new THREE.PlaneBufferGeometry(Config.Ground.WIDTH, Config.Ground.WIDTH);
29    let material = new THREE.MeshLambertMaterial({map: texture});
30    let ground = new THREE.Mesh(geometry, material);
31    ground.position.y = Config.Ground.Y_POS;
32    ground.rotation.x = Config.Ground.X_ROT;
33    ground.receiveShadow = Config.Ground.RECEIVE_SHADOW;
34    this.scene.add(ground)
35    return ground
36  }
37
38  Table() {
39    let table = new Table(Config.Simulation.GENOTYPES);
40    this.scene.add(table.board);
41    this.scene.add(table.genotypeHeader.mesh);
42    this.scene.add(table.genotypeHeader.textMesh);
43    this.scene.add(table.fitnessHeader.mesh);
44    this.scene.add(table.fitnessHeader.textMesh);
45    for (let i = 0; i < table.genotypes.length; i++) {
46      this.scene.add(table.genotypePlanks[i].mesh);
47      this.scene.add(table.genotypePlanks[i].textMesh);
48      this.scene.add(table.fitnessPlanks[i].mesh);
49      this.scene.add(table.fitnessPlanks[i].textMesh);
50    }
51    return table
52  }
53
54  Lighting() {
55    let lights = [new THREE.AmbientLight(Config.Light.Ambient.COLOR)];
56    let light = new THREE.DirectionalLight(Config.Light.Directional.COLOR,
57                                           Config.Light.Directional.INTENSITY);
58    light.position.set(Config.Light.Directional.X_POS,
59                       Config.Light.Directional.Y_POS,
60                       Config.Light.Directional.Z_POS);
61    light.castShadow = Config.Light.Directional.CAST_SHADOW;
62    light.shadow.mapSize.width = Config.Light.Directional.Shadow.MapSize.WIDTH;
63    light.shadow.mapSize.height = Config.Light.Directional.Shadow.MapSize.HEIGHT;
64    light.shadow.camera.left = Config.Light.Directional.Shadow.Camera.LEFT;
65    light.shadow.camera.right = Config.Light.Directional.Shadow.Camera.RIGHT;
66    light.shadow.camera.top = Config.Light.Directional.Shadow.Camera.TOP;
67    light.shadow.camera.bottom = Config.Light.Directional.Shadow.Camera.BOTTOM;
68    light.shadow.camera.far = Config.Light.Directional.Shadow.Camera.FAR;
69    lights.push(light);
70
71    for (let i = 0; i < lights.length; i++) {
72      this.scene.add(lights[i]);
73    }
74    return lights;
75  }
76
77  Sky() {
78    let sky = new THREE.Sky()
79    sky.scale.setScalar(Config.Sky.SCALE);
80    sky.material.uniforms.turbidity.value = Config.Sky.TURBIDITY;
81    sky.material.uniforms.rayleigh.value = Config.Sky.RAYLEIGH;
82    sky.material.uniforms.luminance.value = Config.Sky.LUMINANCE;
83    sky.material.uniforms.mieCoefficient.value = Config.Sky.MIE_COEFFICIENT;
84    sky.material.uniforms.mieDirectionalG.value = Config.Sky.MIE_DIRECTIONAL_G;
85    this.scene.add(sky);
86    return sky;
87  }
88
89  Sun() {
90    let sun = new THREE.Mesh(
91      new THREE.SphereBufferGeometry(Config.Sun.RADIUS, Config.Sun.WIDTH_SEGMENTS, Config.Sun.HEIGHT_SEGMENTS),
92      new THREE.MeshBasicMaterial({color: Config.Sun.COLOR})
93    );
94    sun.position.y = Config.Sun.Y_POS;
95
96    let theta = Math.PI * (Config.Sun.INCLINATION - 0.5);
97    let phi = 2 * Math.PI * (Config.Sun.AZIMUTH - 0.5);
98    sun.position.x = 400000 * Math.cos(phi);
99    sun.position.y = 400000 * Math.sin(phi) * Math.sin(theta);
100    sun.position.z = 400000 * Math.sin(phi) * Math.cos(theta);
101    this.sky.material.uniforms.sunPosition.value.copy(sun.position);
102    this.scene.add(sun);
103    return sun;
104  }
105}
Note: See TracBrowser for help on using the repository browser.