import Effect, {fract} from './effect';

export default class HueDisplaceEffect extends Effect {
  insertIntoVertexShader(vertexShader, state) {
    if (state.hueDisplaceDistance !== 0) {
      vertexShader.uniforms += `
        uniform float hueDisplaceDistance;
        uniform float hueDisplaceTime;
        uniform float hueDisplaceDirectionOffset;
        uniform float hueDisplaceScaleByValue;
      `;
      vertexShader.mainBody += `
        {
          float angle = hsv[0] + hueDisplaceDirectionOffset;
          float offset = (-cos(hueDisplaceTime) + 1.) / 2.;
          position.xy += offset * getDirectionVector(angle) * hueDisplaceDistance * (1. - hueDisplaceScaleByValue * (1. - hsv[2]));
        }
      `;
    }
  }

  insertUniforms(uniforms) {
    uniforms.hueDisplaceDistance = (ctx, props) => {
      return props.state.hueDisplaceDistance;
    };
    uniforms.hueDisplaceTime = (ctx, props) => {
      return fract(ctx.time / props.state.hueDisplacePeriod) * 2 * Math.PI;
    };
    uniforms.hueDisplaceDirectionOffset = (ctx, props) => {
      let result = props.state.hueDisplaceRotate * fract(ctx.time / props.state.hueDisplacePeriod) * 2 * Math.PI;
      if (props.state.hueDisplaceRandomDirectionOffset) {
        if (props.hueDisplaceRandomDirectionOffsetValue === undefined
          || Math.floor(props.renderer.oldTime / props.state.hueDisplacePeriod)
          !== Math.floor(props.renderer.currentTime / props.state.hueDisplacePeriod)
        ) {
          props.hueDisplaceRandomDirectionOffsetValue = Math.random() * 2 * Math.PI;
        }
        result += props.hueDisplaceRandomDirectionOffsetValue;
      }

      return result;
    };
    uniforms.hueDisplaceScaleByValue = (ctx, props) => {
      return props.state.hueDisplaceScaleByValue;
    };
  }
}

@@ -1223,24 +1223,103 @@
 onOffset) %7B%0A
+        // TODO It's really ugly that this effect writes into the global state%0A
         if (
@@ -1320,24 +1320,30 @@
   if (props.
+state.
 hueDisplaceR
@@ -1412,25 +1412,16 @@
 r(props.
-renderer.
 oldTime 
@@ -1488,17 +1488,8 @@
 ops.
-renderer.
 curr
@@ -1553,24 +1553,30 @@
       props.
+state.
 hueDisplaceR
@@ -1666,16 +1666,22 @@
 = props.
+state.
 hueDispl

Fix HueDisplace effect