ShaderUtil.js 1.1 KB

1234567891011121314151617181920212223242526
  1. var ShaderUtils = {
  2. shaderPrograms: {},
  3. setShader: function(sprite, shaderName) {
  4. var glProgram = this.shaderPrograms[shaderName];
  5. if (!glProgram) {
  6. glProgram = new cc.GLProgram();
  7. var vert = require(cc.js.formatStr("%s.vert", shaderName));
  8. var frag = require(cc.js.formatStr("%s.frag", shaderName));
  9. glProgram.initWithString(vert, frag);
  10. if (!cc.sys.isNative) {
  11. glProgram.initWithVertexShaderByteArray(vert, frag);
  12. glProgram.addAttribute(cc.macro.ATTRIBUTE_NAME_POSITION, cc.macro.VERTEX_ATTRIB_POSITION);
  13. glProgram.addAttribute(cc.macro.ATTRIBUTE_NAME_COLOR, cc.macro.VERTEX_ATTRIB_COLOR);
  14. glProgram.addAttribute(cc.macro.ATTRIBUTE_NAME_TEX_COORD, cc.macro.VERTEX_ATTRIB_TEX_COORDS);
  15. }
  16. glProgram.link();
  17. glProgram.updateUniforms();
  18. this.shaderPrograms[shaderName] = glProgram;
  19. }
  20. sprite._sgNode.setShaderProgram(glProgram);
  21. return glProgram;
  22. },
  23. };
  24. module.exports = ShaderUtils;