GradientShaderMaterialComponent.re.ts 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. import * as RE from 'rogue-engine';
  2. import * as THREE from 'three';
  3. export default class GradientShaderMaterialComponent extends RE.Component {
  4. fragmentShader = `uniform vec3 colorA;
  5. uniform vec3 colorB;
  6. uniform float meshHeight;
  7. varying vec3 vUv;
  8. void main() {
  9. float lerpValue = vUv.y / meshHeight;
  10. gl_FragColor = vec4(mix(colorA, colorB, lerpValue), 1.0);
  11. }`
  12. vertexShader = `varying vec3 vBC;
  13. varying vec3 vUv;
  14. void main() {
  15. vUv = position;
  16. vec4 modelViewPosition = modelViewMatrix * vec4(position, 1.0);
  17. gl_Position = projectionMatrix * modelViewPosition;
  18. }`
  19. shaderMaterial
  20. elapsed: number = 0
  21. awake() {
  22. this.shaderMaterial = new THREE.ShaderMaterial({
  23. extensions: {
  24. // needed for anti-alias smoothstep, aastep()
  25. derivatives: true
  26. },
  27. transparent: false,
  28. side: THREE.FrontSide,
  29. uniforms: {
  30. colorA: { type: 'vec3', value: new THREE.Color(0x0c1328) },
  31. colorB: { type: 'vec3', value: new THREE.Color(0x071647) },
  32. meshHeight: { type: 'float', value: 25.0}
  33. } as any,
  34. fragmentShader: this.fragmentShader,
  35. vertexShader: this.vertexShader,
  36. });
  37. }
  38. start() {
  39. if((this.object3d as any).material instanceof THREE.ShaderMaterial) {
  40. console.log("It's a shader")
  41. } else {
  42. console.log("no shader here")
  43. }
  44. (this.object3d as any).material = this.shaderMaterial
  45. }
  46. update() {
  47. }
  48. }
  49. RE.registerComponent(GradientShaderMaterialComponent);