0

I am trying to generate random colors in my fragment shader, but I'm running into compiler errors. Here is my code:

    <!--here is where the color is set-->
    <script id="fragment-shader" type="x-shader/x-fragment">

    precision mediump float;

    void
    main()
    {
        float red = Math.random();
        float green = 0.5;
        float blue = 0.7;
        gl_FragColor = vec4( red, green, blue, 1.0 );
    }
    </script><!--here is where the color is set-->

Here is the error I am getting:

    Fragment shader failed to compile.  The error log is:<pre>ERROR: 0:8: 'Math' : undeclared identifier 
    ERROR: 0:8: '' : methods are not supported 
    ERROR: 0:8: 'random' : no matching overloaded function found 
    </pre>

I am new to HMTL, WebGL, and Javascript and I'm not sure what is breaking and why. Why am I unable to use Math.random?

hededo
  • 371
  • 2
  • 16
  • Duplicate: http://stackoverflow.com/questions/10803176/three-js-webgl-glsl-utilizing-random-math – asimes Sep 16 '14 at 06:35

1 Answers1

1

Math is a JavaScript object, shaders are written in GLSL.

To generate random numbers in GLSL see the comment by @asimes.

LJᛃ
  • 7,655
  • 2
  • 24
  • 35