VaporwaveSunShaderMaterialComponent.re.ts 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. import * as RE from 'rogue-engine'
  2. import * as THREE from 'three'
  3. export default class VaporwaveSunShaderMaterialComponent extends RE.Component {
  4. elapsed = 0
  5. uniforms : any = {}
  6. vertexShader = `#define GLSLIFY 1
  7. varying vec3 v_position;
  8. varying vec3 v_normal;
  9. varying vec2 v_Uv;
  10. void main() {
  11. // Save the varyings
  12. v_position = position;
  13. v_normal = normalize(normalMatrix * normal);
  14. v_Uv = uv;
  15. // Vertex shader output
  16. gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);
  17. }
  18. `
  19. fragmentShader = `#define GLSLIFY 1
  20. uniform float u_time;
  21. varying vec3 v_position;
  22. varying vec3 v_normal;
  23. varying vec2 v_Uv;
  24. void main() {
  25. float invertedUv = 1.0 - v_Uv.y;
  26. float yPos = 64.0 * (invertedUv * invertedUv * invertedUv);
  27. float delta = 2.0 * u_time;
  28. // Don't paint the pixels between the stripes
  29. if (cos(yPos + delta) > 0.0 && v_Uv.y < 0.6) {
  30. discard;
  31. }
  32. float lerpValue = v_Uv.y / 1.0;
  33. vec3 colorA = vec3(1., 1., 0);
  34. vec3 colorB = vec3(1.0, 0.0, 1.0);
  35. gl_FragColor = vec4(mix(colorB, colorA, lerpValue), 1.0);
  36. }
  37. `
  38. awake() {
  39. this.initMaterial()
  40. }
  41. update() {
  42. this.elapsed += RE.Runtime.deltaTime
  43. this.uniforms.u_time.value = this.elapsed;
  44. }
  45. start() {
  46. this.initMaterial()
  47. }
  48. initMaterial() {
  49. this.elapsed = 0
  50. this.uniforms = {
  51. u_time : {
  52. type : "f",
  53. value : 0.0
  54. }
  55. };
  56. const material = (this.object3d as any).material as THREE.ShaderMaterial
  57. material.uniforms = this.uniforms
  58. material.vertexShader = this.vertexShader
  59. material.fragmentShader = this.fragmentShader
  60. material.extensions.derivatives = true
  61. material.side = THREE.FrontSide
  62. material.transparent = true
  63. material.needsUpdate = true
  64. material.uniformsNeedUpdate = true
  65. }
  66. }
  67. RE.registerComponent(VaporwaveSunShaderMaterialComponent);