source: js/standard_expdef_demo/js/world/object/table.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: 6.5 KB
Line 
1/**
2 * @file Table Object
3 * @author Patryk Gliszczynski
4 * @version 1.0
5 */
6
7class Table {
8  /**
9   * Class reposnsible for the visualization of the genotype and fitness table.
10   */
11
12  constructor(genotypes) {
13    this.genotypes = genotypes
14
15    this.board = this.Board();
16    this.genotypePlanks = this.GenotypePlanks();
17    this.fitnessPlanks = this.FitnessPlanks();
18    this.genotypeHeader = this.GenotypeHeader();
19    this.fitnessHeader = this.FitnessHeader();
20  }
21
22  Board() {
23    let texture = new THREE.TextureLoader().load(Config.Table.Board.TEXTURE);
24    texture.wrapS = texture.wrapT = THREE.RepeatWrapping;
25    texture.repeat.set(Config.Table.Board.REPEAT, Config.Table.Board.REPEAT);
26    texture.anisotropy = Config.Table.Board.ANISOTROPY;
27
28    let geometry = new THREE.BoxGeometry(Config.Table.Board.WIDTH,
29                                         (Config.Table.GenotypePlank.HEIGHT + Config.Table.Board.SPACING) * (this.genotypes.length + 2),
30                                         Config.Table.Board.THICKNESS);
31
32    let material = new THREE.MeshLambertMaterial({map: texture});
33    material.opacity = 0;
34    material.blending = THREE.NoBlending;
35
36    let board = new THREE.Mesh(geometry, material);
37    board.position.set(Config.Table.Board.X_POS,
38                       (Config.Table.GenotypePlank.HEIGHT + Config.Table.Board.SPACING) * (this.genotypes.length + 1) / 2,
39                       Config.Table.Board.Z_POS)
40    board.castShadow = true;
41    board.receiveShadow = true;
42    return board
43  }
44
45  GenotypePlanks() {
46    let planks = []
47    for (let i = 0; i < this.genotypes.length; i++) {
48      planks.push(new GenotypePlank(this.genotypes[i], i));
49    }
50    return planks
51  }
52
53  FitnessPlanks() {
54    let planks = []
55    for (let i = 0; i < this.genotypes.length; i++) {
56      planks.push(new FitnessPlank((Math.random() * 100).toFixed(2), i))
57    }
58    return planks;
59  }
60
61  GenotypeHeader() {
62    return new GenotypePlank("Genotype", this.genotypes.length, true)
63  }
64
65  FitnessHeader() {
66    return new FitnessPlank("Fitness", this.genotypes.length)
67  }
68}
69
70
71class GenotypePlank {
72
73  constructor(genotype, position, bold) {
74    this.genotype = genotype;
75    this.position = {
76      x: Config.Table.GenotypePlank.X_POS,
77      y: 50 + (Config.Table.GenotypePlank.HEIGHT + Config.Table.Board.SPACING) * position,
78      z: Config.Table.GenotypePlank.Z_POS
79    };
80
81    let texture = new THREE.TextureLoader().load(Config.Table.GenotypePlank.TEXTURE);
82    texture.wrapS = texture.wrapT = THREE.RepeatWrapping;
83    texture.repeat.set(Config.Table.GenotypePlank.REPEAT, Config.Table.GenotypePlank.REPEAT);
84    texture.anisotropy = Config.Table.GenotypePlank.ANISOTROPY;
85
86    let geometry = new THREE.BoxGeometry(Config.Table.GenotypePlank.WIDTH,
87                                         Config.Table.GenotypePlank.HEIGHT,
88                                         Config.Table.GenotypePlank.THICKNESS);
89    let material = new THREE.MeshLambertMaterial({map: texture});
90    material.blending  = THREE.NoBlending;
91    this.mesh = new THREE.Mesh(geometry, material);
92    this.mesh.position.set(this.position.x, this.position.y, this.position.z);
93    this.mesh.castShadow = Config.Table.GenotypePlank.CAST_SHADOW;
94    this.mesh.receiveShadow = Config.Table.GenotypePlank.RECEIVE_SHADOW;
95
96    this.textMesh = Text.getGenotypeMesh(this.genotype.replace("/*4*/ ", ""));
97    if (bold == true) {
98      this.textMesh = Text.getFitnessMesh(this.genotype.replace("/*4*/ ", ""));
99    }
100    this.textMesh.position.set(this.position.x, this.position.y, this.position.z + 1);
101  }
102
103  move(position) {
104    let tweenPlank = new TWEEN.Tween(this.mesh.position).to(position, Config.Table.GenotypePlank.Tween.Move.SPEED);
105    tweenPlank.easing(Config.Table.GenotypePlank.Tween.Move.EASING);
106    tweenPlank.start();
107
108    let tweenText = new TWEEN.Tween(this.textMesh.position).to(position, Config.Table.GenotypePlank.Tween.Move.SPEED);
109    tweenText.easing(Config.Table.GenotypePlank.Tween.Move.EASING);
110    tweenText.start();
111  }
112
113  rotate(angle) {
114    let tweenPlank = new TWEEN.Tween(this.mesh.rotation).to(angle, Config.Table.GenotypePlank.Tween.Rotate.SPEED);
115    tweenPlank.easing(Config.Table.GenotypePlank.Tween.Rotate.EASING);
116    tweenPlank.start();
117
118    let tweenText = new TWEEN.Tween(this.textMesh.rotation).to(angle, Config.Table.GenotypePlank.Tween.Rotate.SPEED);
119    tweenText.easing(Config.Table.GenotypePlank.Tween.Rotate.EASING);
120    tweenText.start();
121  }
122}
123
124
125class FitnessPlank {
126
127  constructor(fitness, position) {
128    this.fitness = fitness;
129    this.position = {
130      x: Config.Table.FitnessPlank.X_POS,
131      y: 50 + (Config.Table.FitnessPlank.HEIGHT + Config.Table.Board.SPACING) * position,
132      z: Config.Table.FitnessPlank.Z_POS
133    };
134
135    let texture = new THREE.TextureLoader().load(Config.Table.FitnessPlank.TEXTURE);
136    texture.wrapS = texture.wrapT = THREE.RepeatWrapping;
137    texture.repeat.set(Config.Table.FitnessPlank.REPEAT, Config.Table.FitnessPlank.REPEAT);
138    texture.anisotropy = Config.Table.FitnessPlank.ANISOTROPY;
139
140    let geometry = new THREE.BoxGeometry(Config.Table.FitnessPlank.WIDTH,
141                                         Config.Table.FitnessPlank.HEIGHT,
142                                         Config.Table.FitnessPlank.THICKNESS);
143    let material = new THREE.MeshLambertMaterial({map: texture});
144    material.blending  = THREE.NoBlending;
145    this.mesh = new THREE.Mesh(geometry, material);
146    this.mesh.position.set(this.position.x, this.position.y, this.position.z);
147    this.mesh.castShadow = Config.Table.FitnessPlank.CAST_SHADOW;
148    this.mesh.receiveShadow = Config.Table.FitnessPlank.RECEIVE_SHADOW;
149
150    this.textMesh = Text.getFitnessMesh(this.fitness);
151    this.textMesh.position.set(this.position.x, this.position.y, this.position.z + 1);
152  }
153
154  move(position) {
155    let tweenPlank = new TWEEN.Tween(this.mesh.position).to(position, Config.Table.FitnessPlank.Tween.Move.SPEED);
156    tweenPlank.easing(Config.Table.FitnessPlank.Tween.Move.EASING);
157    tweenPlank.start();
158
159    let tweenText = new TWEEN.Tween(this.textMesh.position).to(position, Config.Table.FitnessPlank.Tween.Move.SPEED);
160    tweenText.easing(Config.Table.FitnessPlank.Tween.Move.EASING);
161    tweenText.start();
162  }
163
164  rotate(angle) {
165    let tweenPlank = new TWEEN.Tween(this.mesh.rotation).to(angle, Config.Table.FitnessPlank.Tween.Rotate.SPEED);
166    tweenPlank.easing(Config.Table.FitnessPlank.Tween.Rotate.EASING);
167    tweenPlank.start();
168
169    let tweenText = new TWEEN.Tween(this.textMesh.rotation).to(angle, Config.Table.FitnessPlank.Tween.Rotate.SPEED);
170    tweenText.easing(Config.Table.FitnessPlank.Tween.Rotate.EASING);
171    tweenText.start();
172  }
173}
Note: See TracBrowser for help on using the repository browser.