Go Cocos2dx-js using Shader on a Sprite

Source: Internet
Author: User

For example, the following is a simple color change program, blue to Red, JS code:

  1. var url = "res/sb.jpg";
  2. var sp = new CC. Sprite (URL);
  3. Sp.setanchorpoint (0,0);
  4. This.addchild (SP);
  5. This.shader = new CC. Glprogram ("Res/shaders/example_flower.vsh", "Res/shaders/example_flower.fsh");
  6. This.shader.retain ();
  7. This.shader.link ();
  8. this.shader.updateUniforms ();
  9. var program = This.shader.getProgram ();
  10. Sp.shaderprogram = This.shader;

VertexShader

  1. Attribute Vec4 a_position;
  2. Attribute VEC2 A_texcoord;
  3. Attribute Vec4 A_color;
  4. #ifdef gl_es
  5. Varying lowp vec4 v_fragmentcolor;
  6. Varying mediump vec2 V_texcoord;
  7. #Else
  8. Varying VEC4 v_fragmentcolor;
  9. Varying VEC2 V_texcoord;
  10. #endif
  11. void main()
  12. {
  13. gl_position = Cc_pmatrix * a_position;
  14. V_fragmentcolor = A_color;
  15. V_texcoord = A_texcoord;
  16. }



Fragmentshader

    1. #ifdef gl_es
    2. Precision lowp float;
    3. #endif
    4. Varying VEC4 v_fragmentcolor;
    5. Varying VEC2 V_texcoord;
    6. void main()
    7. {
    8. Gl_fragcolor = V_fragmentcolor * TEXTURE2D (CC_TEXTURE0, V_texcoord);
    9. float r = gl_fragcolor[0];
    10. float g = gl_fragcolor[1];
    11. float B = gl_fragcolor[2];
    12. float a = gl_fragcolor[3];
    13. if (b > R && B > G && B > (r*0.5 + g*0.5) *1.5) {
    14. gl_fragcolor[0] = b;
    15. gl_fragcolor[2] = R;
    16. }
    17. } Original Address: 50542860

(GO) cocos2dx-js use Shader on Sprite

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.