pmndrs / lamina

🍰 An extensible, layer based shader material for ThreeJS
MIT License
1.02k stars 42 forks source link

Problems using the lamina noise functions in custom layer #46

Closed apsamuel closed 2 years ago

apsamuel commented 2 years ago

I tried to move some of the Noise.ts into my custom layer as I'm working on a shader that will likely use noise and needed a starting point. The uniform values are set to the values in the Noise.ts, however, I keep getting the error about csm_DiffuseColor = lamina_finalColor;. Any insight into what may be going wrong would be much appreciated. Stuck =).

    uniform vec3 u_color;
    uniform vec3 u_colorA;
    uniform vec3 u_colorB;
    uniform vec3 u_colorC;
    uniform vec3 u_colorD;
    uniform vec3 u_offset;
    uniform float u_scale;
    uniform float u_alpha;
    uniform float u_time;
    varying vec3 v_Position;
    varying vec2 v_Uv;

    vec4 main() {
      // Local variables must be prefixed by "f_"
      float f_n = lamina_noise_perlin((v_Position + u_offset) * u_scale);
      float f_step1 = 0.;
      float f_step2 = 0.2;
      float f_step3 = 0.6;
      float f_step4 = 1.;

      vec3 f_color = mix(u_colorA, u_colorB, smoothstep(f_step1, f_step2, f_n));
      f_color = mix(f_color, u_colorC, smoothstep(f_step2, f_step3, f_n));
      f_color = mix(f_color, u_colorD, smoothstep(f_step3, f_step4, f_n));

      vec4 f_color_test = vec4(vec3(v_Uv, 0.), u_time);
      // return f_color_test;
      return vec4(f_color, u_alpha);

    }

The Error

Program Info Log: Fragment shader is not compiled.

FRAGMENT

ERROR: 0:832: ';' : syntax error

  827: 
  828: 
  829: 
  830:     lamina_finalColor = lamina_blend_alpha(lamina_finalColor, 
  831: 
> 832:           csm_DiffuseColor = lamina_finalColor;
  833:          
  834:         
  835:           
  836: #if 0 > 0
  837:  vec4 plane;
  838:  

related deps:

    "@react-three/cannon": "^6.4.0",
    "@react-three/drei": "^9.25.3",
    "@react-three/fiber": "^8.6.2",
    "https": "^1.0.0",
    "lamina": "^1.1.23",
    "leva": "^0.9.31",
    "react": "^18.2.0",
    "react-dom": "^18.2.0",
    "react-scripts": "^5.0.1",
    "three": "^0.144.0",
    "three-stdlib": "^2.15.0"
apsamuel commented 2 years ago

I think I traced down the issue; there was a comment between the last two lines of the fragment shader that was being compiled into the final string; removing that comment resolved the syntax error.

image