shields-ui-controller.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. import {THREE} from './three-defs.js';
  2. import {entity} from './entity.js';
  3. export const shields_ui_controller = (() => {
  4. const _VS = `
  5. varying vec3 vWorldPosition;
  6. varying vec2 vUV;
  7. void main() {
  8. vec4 worldPosition = modelMatrix * vec4( position, 1.0 );
  9. vWorldPosition = worldPosition.xyz;
  10. vUV = uv;
  11. gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
  12. }`;
  13. const _FS = `
  14. uniform float time;
  15. uniform float shields;
  16. uniform vec3 colour;
  17. varying vec3 vWorldPosition;
  18. varying vec2 vUV;
  19. float sdf_Box(vec2 coords, vec2 bounds) {
  20. vec2 dist = abs(coords) - bounds;
  21. return length(max(dist, 0.0)) + min(max(dist.x, dist.y), 0.0);
  22. }
  23. float smootherstep(float a, float b, float x) {
  24. x = clamp((x - a) / (b - a), 0.0, 1.0);
  25. return x * x * x * (x * ( x * 6.0 - 15.0) + 10.0);
  26. }
  27. // The MIT License
  28. // Copyright © 2013 Inigo Quilez
  29. // https://www.youtube.com/c/InigoQuilez
  30. // https://iquilezles.org/
  31. // Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  32. // https://www.shadertoy.com/view/4sfGzS
  33. float hash(vec3 p) // replace this by something better
  34. {
  35. p = fract( p*0.3183099+.1 );
  36. p *= 17.0;
  37. return fract( p.x*p.y*p.z*(p.x+p.y+p.z) );
  38. }
  39. float noise( in vec3 x )
  40. {
  41. vec3 i = floor(x);
  42. vec3 f = fract(x);
  43. f = f*f*(3.0-2.0*f);
  44. return mix(mix(mix( hash(i+vec3(0,0,0)),
  45. hash(i+vec3(1,0,0)),f.x),
  46. mix( hash(i+vec3(0,1,0)),
  47. hash(i+vec3(1,1,0)),f.x),f.y),
  48. mix(mix( hash(i+vec3(0,0,1)),
  49. hash(i+vec3(1,0,1)),f.x),
  50. mix( hash(i+vec3(0,1,1)),
  51. hash(i+vec3(1,1,1)),f.x),f.y),f.z);
  52. }
  53. const mat3 _M = mat3( 0.00, 0.80, 0.60,
  54. -0.80, 0.36, -0.48,
  55. -0.60, -0.48, 0.64 );
  56. float FBM(vec3 p, int octaves) {
  57. float total = 0.0;
  58. float amplitude = 0.5;
  59. vec3 q = p;
  60. for (int i = 0; i < octaves; ++i) {
  61. total += noise(q) * amplitude;
  62. amplitude *= 0.5;
  63. q = _M * q * 2.0;
  64. }
  65. total = 1.0 - abs(total * 2.0 - 1.0);
  66. total = abs(total * 2.0 - 1.0);
  67. total = abs(total * 2.0 - 1.0);
  68. total = abs(total * 2.0 - 1.0);
  69. // total = total * 0.5 + 0.5;
  70. // total = pow(total, 1.0);
  71. return total;
  72. }
  73. void main() {
  74. vec2 boxCoords = fract(vUV * vec2(8.0, 1.0)) - 0.5;
  75. float d = sdf_Box(boxCoords, vec2(0.3, 0.4)) - 0.1;
  76. float a = smoothstep(0.0, -0.05, d) * step(round(vUV.x * 8.0 + 0.5)/8.0, shields);
  77. float c = mix(0.9, 1.0, clamp(smoothstep(-0.05, -0.2, d), 0.0, 1.0));
  78. vec3 col = colour;
  79. col *= 1.0 - exp(-16.0*abs(d));
  80. col *= vec3(pow(16.0*vUV.x*(1.0-vUV.x)*vUV.y*(1.0-vUV.y), 0.1));
  81. col *= vec3(FBM(vec3(vUV * vec2(8.0, 1.0), 0.5*time), 6) * 0.2 + 0.8);
  82. col *= c;
  83. gl_FragColor = vec4(col, a);
  84. }`;
  85. const _C1 = new THREE.Color(0xfa5959);
  86. const _C2 = new THREE.Color(0x2ce93c);
  87. class ShieldsUIController extends entity.Component {
  88. constructor(params) {
  89. super();
  90. this.params_ = params;
  91. this.timeElapsed_ = 0.0;
  92. this.colourTarget_ = _C2.clone();
  93. }
  94. Destroy() {
  95. this.sprite_.traverse(c => {
  96. if (c.material) {
  97. let materials = c.material;
  98. if (!(c.material instanceof Array)) {
  99. materials = [c.material];
  100. }
  101. for (let m of materials) {
  102. m.dispose();
  103. }
  104. }
  105. if (c.geometry) {
  106. c.geometry.dispose();
  107. }
  108. });
  109. if (this.sprite_.parent) {
  110. this.sprite_.parent.remove(this.sprite_);
  111. }
  112. }
  113. InitEntity() {
  114. this.CreateSprite_();
  115. }
  116. InitComponent() {
  117. this.RegisterHandler_('player.hit', (m) => { this.UpdateShieldColour_(); } );
  118. }
  119. UpdateShieldColour_() {
  120. const t = this.Parent.Attributes.shields / this.Parent.Attributes.maxShields;
  121. this.colourTarget_ = _C1.clone().lerpHSL(_C2, t);
  122. }
  123. OnDeath_() {
  124. this.Destroy();
  125. }
  126. CreateSprite_() {
  127. const mat = new THREE.ShaderMaterial({
  128. uniforms: {
  129. time: {value: 0.0},
  130. shields: {value: 0.0},
  131. colour: {value: _C2.clone()}
  132. },
  133. vertexShader: _VS,
  134. fragmentShader: _FS,
  135. side: THREE.FrontSide,
  136. transparent: true,
  137. });
  138. this.sprite_ = new THREE.Sprite(mat);
  139. this.sprite_.scale.set(32, 4, 1)
  140. this.sprite_.position.set(0, 5, 0);
  141. const threejs = this.FindEntity('threejs').GetComponent('ThreeJSController');
  142. threejs.uiScene_.add(this.sprite_);
  143. }
  144. Update(timeElapsed) {
  145. const threejs = this.FindEntity('threejs').GetComponent('ThreeJSController');
  146. const camera = threejs.camera_;
  147. this.timeElapsed_ += timeElapsed;
  148. const ndc = new THREE.Vector3(0, 1.5, -10);
  149. this.UpdateShieldColour_();
  150. const t = 1.0 - Math.pow(0.05, timeElapsed);
  151. this.sprite_.material.uniforms.colour.value.lerpHSL(this.colourTarget_, t);
  152. this.sprite_.material.uniforms.time.value = this.timeElapsed_;
  153. this.sprite_.material.uniforms.shields.value = this.Parent.Attributes.shields / this.Parent.Attributes.maxShields;
  154. this.sprite_.scale.set(0.8, 0.1 * camera.aspect, 1);
  155. this.sprite_.position.copy(ndc);
  156. }
  157. };
  158. return {
  159. ShieldsUIController: ShieldsUIController,
  160. };
  161. })();