Advertisement
KpoKec

animMaterial

Sep 1st, 2020
1,820
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*jshint esversion: 6, asi: true, laxbreak: true*/
  2. var MaterialAnim = pc.createScript('animMaterial');
  3. MaterialAnim.attributes.add('localEvents', { type: 'boolean', default: false });
  4. MaterialAnim.attributes.add('eventShow', {type: 'string'});
  5. MaterialAnim.attributes.add('eventEndShow', {type: 'string'});
  6. MaterialAnim.attributes.add('eventHide', {type: 'string'});
  7. MaterialAnim.attributes.add('eventEndHide', {type: 'string'});
  8. MaterialAnim.attributes.add('eventSetDefault', { type: 'string' });
  9. MaterialAnim.attributes.add('useColor', { type: 'boolean', default: true });
  10. MaterialAnim.attributes.add('targetColor', {type: 'rgb'});
  11. MaterialAnim.attributes.add('useAlpha', { type: 'boolean', default: true });
  12. MaterialAnim.attributes.add('targetAlpha', {type: 'number', min: 0, max: 1});
  13. MaterialAnim.attributes.add('timeIn', {type: 'number', default: 1.0});
  14. MaterialAnim.attributes.add('typeIn', {
  15.     type: 'number',
  16.     enum: [
  17.         { 'Linear': 1 },
  18.         { 'ExponentialIn': 2 },
  19.         { 'ExponentialOut': 3 },
  20.         { 'ExponentialInOut': 4 },
  21.         { 'ElasticIn': 5 },
  22.         { 'ElasticOut': 6 },
  23.         { 'ElasticInOut': 7 },
  24.         { 'BounceIn': 8 },
  25.         { 'BounceOut': 9 },
  26.         { 'BounceInOut': 10 },
  27.         { 'SineIn': 11 },
  28.         { 'SineOut': 12 },
  29.         { 'SineInOut': 13 }
  30.     ],
  31.     default: 1
  32. });
  33. MaterialAnim.attributes.add('timeOut', {type: 'number', default: 1.0});
  34. MaterialAnim.attributes.add('typeOut', {
  35.     type: 'number',
  36.     enum: [
  37.         { 'Linear': 1 },
  38.         { 'ExponentialIn': 2 },
  39.         { 'ExponentialOut': 3 },
  40.         { 'ExponentialInOut': 4 },
  41.         { 'ElasticIn': 5 },
  42.         { 'ElasticOut': 6 },
  43.         { 'ElasticInOut': 7 },
  44.         { 'BounceIn': 8 },
  45.         { 'BounceOut': 9 },
  46.         { 'BounceInOut': 10 },
  47.         { 'SineIn': 11 },
  48.         { 'SineOut': 12 },
  49.         { 'SineInOut': 13 }
  50.     ],
  51.     default: 1
  52. });
  53. MaterialAnim.attributes.add('soundShow', {type: 'string'});
  54. MaterialAnim.attributes.add('soundHide', {type: 'string'});
  55.  
  56. MaterialAnim.prototype.initialize = function() {
  57.     this.target = -1;
  58.     if (this.entity.model)
  59.         this.target = 0;
  60.     else if (this.entity.element.material)
  61.         this.target = 1;
  62.  
  63.     if (this.target === 0) {
  64.         this.material = this.entity.model.model.meshInstances[0].material;
  65.     } else if (this.target === 1) {
  66.         this.material = this.entity.element.material;
  67.     }
  68.    
  69.     this.audio = this.app.root.findByTag('sound')[0];
  70.     this.color = this.material.diffuse.clone();
  71.     this.col = new pc.Color();
  72.     this.aa = {value: 0};
  73.     this.alpha = { value: this.material.opacity };
  74.  
  75.     if (this.localEvents) {
  76.         this.entity.on(this.eventShow,function() { this.animateShow(); }.bind(this));
  77.         this.entity.on(this.eventHide,function() { this.animateHide(); }.bind(this));
  78.         this.entity.on(this.eventSetDefault, this.setDefault, this);
  79.     }
  80.     else {
  81.         this.app.on(this.eventShow,function() {
  82.             if (!this.entity.enabled) return;
  83.             //console.log("[AnimMaterial]", this.entity.name, this.eventShow);
  84.             this.animateShow();
  85.         }.bind(this));
  86.         this.app.on(this.eventHide,function() {
  87.             if (!this.entity.enabled) return;
  88.             //console.log("[AnimMaterial]", this.entity.name, this.eventHide);
  89.             this.animateHide();
  90.         }.bind(this));
  91.         this.app.on(this.eventSetDefault, this.setDefault, this);
  92.     }
  93.    
  94.     this.entity.on('reset', this.reset, this);
  95.     this.entity.on('default', this.setDefault, this);
  96. };
  97.  
  98.  
  99. MaterialAnim.prototype.postInitialize = function() {
  100.     let material = null
  101.     if (this.target === 0) {
  102.         material = this.entity.model.model.meshInstances[0].material.clone();
  103.         this.entity.model.model.meshInstances[0].material = material;
  104.     } else if (this.target === 1) {
  105.         material = this.entity.element.material.clone();
  106.         this.entity.element.material = material;
  107.     }
  108.     this.reset();
  109.     if (this.entity.script.holo2)
  110.         this.entity.script.holo2.addCustomMaterial(material);
  111. };
  112.  
  113.  
  114. MaterialAnim.prototype.reset = function() {
  115.     if (this.target === 0) {
  116.         this.material = this.entity.model.model.meshInstances[0].material;
  117.     } else if (this.target === 1) {
  118.         this.material = this.entity.element.material;
  119.     }
  120.     this.color = this.material.diffuse.clone();
  121.     this.alpha = { value: this.material.opacity };
  122. };
  123.  
  124. MaterialAnim.prototype.setDefault = function() {
  125.     if (this.tween1) this.tween1.stop();
  126.     if (this.tween2) this.tween2.stop();
  127.    
  128.     if (this.useColor)
  129.         this.material.diffuse = this.color;
  130.     if (this.useAlpha)
  131.         this.material.opacity = this.alpha.value;
  132.     this.material.update();
  133. };
  134.  
  135.  
  136. MaterialAnim.prototype.animateShow = function() {
  137.     if (this.tween1) this.tween1.stop();
  138.     if (this.tween2) this.tween2.stop();
  139.  
  140.     this.col.copy(this.material.diffuse);
  141.     this.aa = { value: this.material.opacity };
  142.  
  143.     //console.log("show", this.aa.value, "->", this.targetAlpha);
  144.    
  145.     if (this.useColor) {
  146.         this.tween1 = this.entity
  147.             .tween(this.col)
  148.             .to(this.targetColor, this.timeIn, this.getCurve(this.typeIn))
  149.             .on('update', function() {
  150.                 this.material.diffuse = new pc.Color(this.col.r, this.col.g, this.col.b);
  151.                 if (!this.useAlpha)
  152.                     this.material.update();
  153.             }.bind(this), this)
  154.             .loop(false)
  155.             .yoyo(false);
  156.     this.tween1.start();
  157.     }
  158.  
  159.     if (this.useAlpha) {
  160.         this.tween2 = this.entity
  161.             .tween(this.aa)
  162.             .to({ value: this.targetAlpha }, this.timeIn, this.getCurve(this.typeIn))
  163.             .on('update', function() {
  164.                 this.material.opacity = this.aa.value;
  165.                 this.material.update();
  166.             }.bind(this), this)
  167.             .loop(false)
  168.             .yoyo(false);
  169.     this.tween2.start();
  170.     }
  171.    
  172.     if (this.eventEndShow && this.eventEndShow.length > 0) {
  173.         if (this.useColor) {
  174.             if (this.localEvents)
  175.                 this.tween1.on('complete', function() { this.entity.fire(this.eventEndShow); }.bind(this), this);
  176.             else
  177.                 this.tween1.on('complete', function() { this.app.fire(this.eventEndShow); }.bind(this), this);
  178.         }
  179.         else if (this.useAlpha) {
  180.             if (this.localEvents)
  181.                 this.tween2.on('complete', function() { this.entity.fire(this.eventEndShow); }.bind(this), this);
  182.             else
  183.                 this.tween2.on('complete', function() { this.app.fire(this.eventEndShow); }.bind(this), this);
  184.         }
  185.     }
  186.  
  187.     if (this.soundShow && this.soundShow.length > 0)
  188.         this.audio.sound.play(this.soundShow);
  189. };
  190.  
  191. MaterialAnim.prototype.animateHide = function() {
  192.     if (this.tween1) this.tween1.stop();
  193.     if (this.tween2) this.tween2.stop();
  194.  
  195.     this.col.copy(this.material.diffuse);
  196.     this.aa = { value: this.material.opacity };
  197.    
  198.     //console.log("hide", this.aa.value, "->", this.alpha.value);
  199.    
  200.     if (this.useColor) {
  201.         this.tween1 = this.entity
  202.             .tween(this.col)
  203.             .to(this.color, this.timeOut, this.getCurve(this.typeOut))
  204.             .on('update', function() {
  205.                 this.material.diffuse = new pc.Color(this.col.r, this.col.g, this.col.b);
  206.                 if (!this.useAlpha)
  207.                     this.material.update();
  208.             }.bind(this), this)
  209.             .loop(false)
  210.             .yoyo(false);
  211.     this.tween1.start();
  212.     }
  213.    
  214.     if (this.useAlpha) {
  215.         this.tween2 = this.entity
  216.             .tween(this.aa)
  217.             .to(this.alpha, this.timeOut, this.getCurve(this.typeIn))
  218.             .on('update', function() {
  219.                 this.material.opacity = this.aa.value;
  220.                 this.material.update();
  221.             }.bind(this), this)
  222.             .loop(false)
  223.             .yoyo(false);
  224.     this.tween2.start();
  225.     }
  226.  
  227.     if (this.eventEndHide && this.eventEndHide !== "") {
  228.         if (this.useColor) {
  229.             if (this.localEvents)
  230.                 this.tween1.once('complete', function() { this.entity.fire(this.eventEndHide); }.bind(this), this);
  231.             else
  232.                 this.tween1.once('complete', function() { this.app.fire(this.eventEndHide); }.bind(this), this);
  233.         } else if (this.useAlpha) {
  234.             if (this.localEvents)
  235.                 this.tween2.once('complete', function() { this.entity.fire(this.eventEndHide); }.bind(this), this);
  236.             else
  237.                 this.tween2.once('complete', function() { this.app.fire(this.eventEndHide); }.bind(this), this);
  238.         }
  239.     }
  240.  
  241.     if (this.soundHide && this.soundHide.length > 0)
  242.         this.audio.sound.play(this.soundHide);
  243.  
  244. };
  245.  
  246.  
  247. MaterialAnim.prototype.getCurve = function(type) {
  248.     if (type === 1) return pc.Linear;
  249.     if (type === 2) return pc.ExponentialIn;
  250.     if (type === 3) return pc.ExponentialOut;
  251.     if (type === 4) return pc.ExponentialInOut;
  252.     if (type === 5) return pc.ElasticIn;
  253.     if (type === 6) return pc.ElasticOut;
  254.     if (type === 7) return pc.ElasticInOut;
  255.     if (type === 8) return pc.BounceIn;
  256.     if (type === 9) return pc.BounceOut;
  257.     if (type === 10) return pc.BounceInOut;
  258.     if (type === 11) return pc.SineIn;
  259.     if (type === 12) return pc.SineOut;
  260.     if (type === 13) return pc.SineInOut;
  261. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement