[880] | 1 | /** |
---|
| 2 | * @file Framstick Object |
---|
| 3 | * @author Patryk Gliszczynski |
---|
| 4 | * @version 1.0 |
---|
| 5 | */ |
---|
| 6 | |
---|
| 7 | class Framstick { |
---|
| 8 | |
---|
| 9 | constructor(genotype) { |
---|
| 10 | this.genotype = genotype; |
---|
| 11 | this.parts = []; |
---|
| 12 | this.joints = []; |
---|
| 13 | this.meshes = []; |
---|
| 14 | this.partfactory = new PartMeshFactory(Config.Framstick.Geometry.PART); |
---|
| 15 | this.jointfactory = new JointMeshFactory(Config.Framstick.Geometry.JOINT); |
---|
| 16 | |
---|
| 17 | let helper = new THREE.Mesh(); |
---|
| 18 | this.scaleUpSize = Config.Framstick.Tween.Scale.Up.SIZE |
---|
| 19 | this.scaleDownSize = Config.Framstick.Tween.Scale.Down.SIZE |
---|
| 20 | this.material = new THREE.MeshLambertMaterial({color: Config.Framstick.COLOR}); |
---|
| 21 | this.loadFromGenotype(helper); |
---|
| 22 | |
---|
| 23 | let bbox = new THREE.Box3().setFromObject(helper); |
---|
| 24 | bbox.getCenter(helper.position); |
---|
| 25 | helper.position.multiplyScalar(-1); |
---|
| 26 | |
---|
| 27 | this.mesh = new THREE.Group(); |
---|
| 28 | this.mesh.add(helper); |
---|
| 29 | |
---|
| 30 | helper.castShadow = Config.Framstick.CAST_SHADOW; |
---|
| 31 | helper.receiveShadow = Config.Framstick.RECEIVE_SHADOW; |
---|
| 32 | |
---|
| 33 | this.mesh.scale.set(0.01, 0.01, 0.01); |
---|
| 34 | this.mesh.position.y = Config.Framstick.Y_POS; |
---|
| 35 | this.mesh.rotation.x = Config.Framstick.X_ROT; |
---|
| 36 | this.mesh.castShadow = Config.Framstick.CAST_SHADOW; |
---|
| 37 | this.mesh.receiveShadow = Config.Framstick.RECEIVE_SHADOW; |
---|
| 38 | |
---|
| 39 | this.scaleUp(); |
---|
| 40 | this.autoRotate(); |
---|
| 41 | } |
---|
| 42 | |
---|
| 43 | scaleUp() { |
---|
| 44 | let scale = this.scaleUpSize; |
---|
| 45 | let speed = Config.Framstick.Tween.Scale.SPEED; |
---|
| 46 | let tween = new TWEEN.Tween(this.mesh.scale).to({x: scale, y: scale, z: scale}, speed); |
---|
| 47 | tween.easing(Config.Framstick.Tween.Scale.Up.EASING); |
---|
| 48 | tween.onComplete(this.scaleDown.bind(this)); |
---|
| 49 | tween.start(); |
---|
| 50 | } |
---|
| 51 | |
---|
| 52 | scaleDown() { |
---|
| 53 | let scale = this.scaleDownSize; |
---|
| 54 | let speed = Config.Framstick.Tween.Scale.SPEED; |
---|
| 55 | let tween = new TWEEN.Tween(this.mesh.scale).to({x: scale, y: scale, z: scale}, speed); |
---|
| 56 | tween.easing(Config.Framstick.Tween.Scale.Down.EASING); |
---|
| 57 | tween.onComplete(this.scaleUp.bind(this)); |
---|
| 58 | tween.start(); |
---|
| 59 | } |
---|
| 60 | |
---|
| 61 | autoRotate() { |
---|
| 62 | let speed = Config.Framstick.Tween.Rotate.SPEED; |
---|
| 63 | let tween = new TWEEN.Tween(this.mesh.rotation).to({z: this.mesh.rotation.z + Math.PI / 4}, speed); |
---|
| 64 | tween.easing(Config.Framstick.Tween.Rotate.EASING) |
---|
| 65 | tween.onComplete(this.autoRotate.bind(this)); |
---|
| 66 | tween.start(); |
---|
| 67 | } |
---|
| 68 | |
---|
| 69 | /** |
---|
| 70 | * Code below was taken from: http://fugue.synology.me:30000/grzegorzlatosinski/framsticks-js |
---|
| 71 | * @author Grzegorz Latosiński |
---|
| 72 | * @pull_date 22.01.2018 |
---|
| 73 | */ |
---|
| 74 | loadFromGenotype(mesh) { |
---|
| 75 | let model = this.getModelFromGenotype(this.genotype); |
---|
| 76 | |
---|
| 77 | if (typeof model !== 'undefined') { |
---|
| 78 | let partsforjoints = []; |
---|
| 79 | this.meshes = []; |
---|
| 80 | this.parts = []; |
---|
| 81 | this.joints = []; |
---|
| 82 | |
---|
| 83 | for (let i = 0; i < model.getPartCount(); i++) { |
---|
| 84 | let m = this.partfactory.create(model.getPart(i)); |
---|
| 85 | partsforjoints.push(m.userData); |
---|
| 86 | mesh.add(m) |
---|
| 87 | this.parts.push(m); |
---|
| 88 | this.meshes.push(m); |
---|
| 89 | } |
---|
| 90 | |
---|
| 91 | for (let i = 0; i < model.getJointCount(); i++) { |
---|
| 92 | let m = this.jointfactory.create(model.getJoint(i), partsforjoints); |
---|
| 93 | mesh.add(m); |
---|
| 94 | this.joints.push(m); |
---|
| 95 | this.meshes.push(m); |
---|
| 96 | } |
---|
| 97 | Module.destroy(model); |
---|
| 98 | } |
---|
| 99 | } |
---|
| 100 | |
---|
| 101 | getModelFromGenotype(genotype) { |
---|
| 102 | let genetics = new Module.PreconfiguredGenetics(); |
---|
| 103 | let stringObj = new Module.SString(); |
---|
| 104 | stringObj.set(genotype); |
---|
| 105 | let genoObj = new Module.Geno(stringObj); |
---|
| 106 | |
---|
| 107 | if (!genoObj.isValid()) { |
---|
| 108 | Module.destroy(stringObj); |
---|
| 109 | Module.destroy(genoObj); |
---|
| 110 | Module.destroy(genetics); |
---|
| 111 | return; |
---|
| 112 | } |
---|
| 113 | |
---|
| 114 | let model = new Module.Model(genoObj, true); |
---|
| 115 | Module.destroy(stringObj); |
---|
| 116 | Module.destroy(genoObj); |
---|
| 117 | Module.destroy(genetics); |
---|
| 118 | return model; |
---|
| 119 | } |
---|
| 120 | } |
---|