查看: 5416|回复: 8

[原创发布] 抛砖引玉 mv中使用shader

[复制链接]
梦石
0
星屑
3703
在线时间
3081 小时
注册时间
2011-11-17
回帖
943
发表于 2017-10-4 11:54:00 | 显示全部楼层 |阅读模式

加入我们,或者,欢迎回来。

您需要 登录 才可以下载或查看,没有账号?注册会员

×
虽然我已经不用rm了 为了庆祝论坛复活 国庆期间看了下mv 于是就有了这东西 111.gif

  1. function Sprite_Shader() {
  2.     this.initialize.apply(this, arguments);
  3. }
  4. Sprite_Shader.defultfrag = " \
  5.     varying vec2 vTextureCoord; \
  6.     uniform sampler2D uSampler; \
  7.     void main(void) \
  8.     { \
  9.         gl_FragColor = texture2D(uSampler, vTextureCoord); \
  10.     } \
  11. ";


  12. Sprite_Shader.defultvert= " \
  13.     attribute vec2 aVertexPosition; \
  14.     attribute vec2 aTextureCoord; \
  15.     uniform mat3 projectionMatrix; \
  16.     varying vec2 vTextureCoord; \
  17.     void main(void) \
  18.     { \
  19.         gl_Position = vec4((projectionMatrix * vec3(aVertexPosition, 1.0)).xy, 0.0, 1.0); \
  20.         vTextureCoord = aTextureCoord; \
  21.     } \
  22.     ";

  23. Sprite_Shader.prototype = Object.create(Sprite.prototype);
  24. Sprite_Shader.prototype.constructor = Sprite_Shader;

  25. Sprite_Shader.prototype.initialize = function(bitmap) {
  26.     Sprite.prototype.initialize.call(this);
  27.     this.bitmap = bitmap;
  28.     this.mode = -1;   
  29. };


  30. Sprite_Shader.prototype.gray = function() {
  31.     this.mode= 1;
  32.     var frag = " \
  33.         varying vec2 vTextureCoord; \
  34.         uniform sampler2D uSampler; \
  35.         void main(void) \
  36.         { \
  37.             gl_FragColor = texture2D(uSampler, vTextureCoord); \
  38.             float  gray = (gl_FragColor.r + gl_FragColor.g + gl_FragColor.b)/3.0; \
  39.             gl_FragColor.r = gray; \
  40.             gl_FragColor.g = gray; \
  41.             gl_FragColor.b = gray; \
  42.         } \
  43.     ";
  44.     var simpleShader = new PIXI.AbstractFilter(Sprite_Shader.defultvert,frag);
  45.     this._filters = [simpleShader];
  46. };



  47. Sprite_Shader.prototype.shadow = function() {
  48.     this.mode= 2;
  49.     var uniform = {};

  50.     var frag = " \
  51.         varying vec2 vTextureCoord; \
  52.         uniform sampler2D uSampler; \
  53.         const vec2 step = vec2(0.015, 0.002); \
  54.         void main(void) \
  55.         { \
  56.             vec3 color = texture2D(uSampler, vTextureCoord).rgb; \
  57.             float olda =  texture2D(uSampler, vTextureCoord).a; \
  58.             float weight = 10.0; \
  59.             float assess = texture2D(uSampler, vTextureCoord).a * weight; \
  60.             float alpha = assess; \
  61.             alpha += texture2D( uSampler, vTextureCoord.xy + vec2( -3.0*step.x, -3.0*step.y ) ).a; \
  62.             alpha /= (2.0+assess);  \
  63.             gl_FragColor = vec4(color, alpha); \
  64.         } \
  65.     ";
  66.     var simpleShader = new PIXI.AbstractFilter(Sprite_Shader.defultvert,frag,uniform);
  67.     this._filters = [simpleShader];
  68. };



  69. Sprite_Shader.prototype.twist = function() {
  70.     this.mode= 3;
  71.     this.uniform = {};
  72.     this.uniform.iGlobalTime = {
  73.         type:"f",
  74.         value:0
  75.     }

  76.     frag = " \
  77.         varying vec2 vTextureCoord; \
  78.         uniform sampler2D uSampler; \
  79.         uniform float iGlobalTime; \
  80.         void main(void) \
  81.         { \
  82.             vec2 uv = vTextureCoord.xy; \
  83.             float w = (0.5 - (uv.x)); \
  84.             float h = 0.5 - uv.y; \
  85.             float distanceFromCenter = sqrt(w * w + h * h); \
  86.             float sinArg = distanceFromCenter * 10.0 - iGlobalTime * 10.0; \
  87.             float slope = cos(sinArg) ; \
  88.             vec4 color = texture2D(uSampler, uv + normalize(vec2(w, h)) * slope * 0.05); \
  89.             gl_FragColor = color; \
  90.         } \
  91.     ";
  92.     var simpleShader = new PIXI.AbstractFilter(Sprite_Shader.defultvert,frag,this.uniform);
  93.     this._filters = [simpleShader];
  94. };

  95. Sprite_Shader.prototype.magic = function(mode) {
  96.     this.mode= 4;
  97.     this.uniform = {};
  98.     this.uniform._Angle = {
  99.         type:"f",
  100.         value:0
  101.     };
  102.       this.uniform._Radius = {
  103.         type:"f",
  104.         value:0
  105.     };
  106.       this.uniform._mode = {
  107.         type:"f",
  108.         value:mode
  109.     };

  110.     frag = " \
  111.         varying vec2 vTextureCoord; \
  112.         uniform sampler2D uSampler; \
  113.         uniform float _Angle;  \
  114.         uniform float _Radius;  \
  115.         uniform float _mode;  \
  116.         void main(void) \
  117.         { \
  118.             vec2 uv = vTextureCoord; \
  119.             uv -= vec2(0.25, 0.5); \
  120.             float dist = length(uv); \
  121.             float percent = (_Radius - dist) / _Radius; \
  122.             if (percent < 1.0 && percent >= 0.0) \
  123.             {  \
  124.                 float theta = percent * percent * _Angle * 3.0; \
  125.                 float s = sin(theta); \
  126.                 float c = cos(theta); \
  127.                 uv = vec2(uv.x*c - uv.y*s*_mode , uv.x*s + uv.y*c); \
  128.             } \
  129.             uv += vec2(0.25, 0.5); \
  130.             gl_FragColor = texture2D(uSampler, uv); \
  131.         } \
  132.     ";
  133.     var simpleShader = new PIXI.AbstractFilter(Sprite_Shader.defultvert,frag,this.uniform);
  134.     this._filters = [simpleShader];
  135. };


  136. Sprite_Shader.prototype.update = function() {
  137.     Sprite.prototype.update.call(this);
  138.     if(this.mode == 3){
  139.         this._filters[0].uniforms.iGlobalTime += 0.01;
  140.     }
  141.     else if(this.mode == 4){
  142.         this._filters[0].uniforms._Angle += 0.01;
  143.         this._filters[0].uniforms._Radius += 0.001;
  144.         if (this._filters[0].uniforms._Radius >= 0.6)
  145.         {
  146.             this._filters[0].uniforms._Angle = 0;
  147.             this._filters[0].uniforms._Radius = 0.1;
  148.         }
  149.     }
  150. };
复制代码




至于用法 我随便找了个地方addchild
var test1 = new Sprite_Shader(ImageManager.loadPicture("Actor1_3"));
    test1.magic(-1);
    this.addChild(test1);

    var test2 = new Sprite_Shader(ImageManager.loadPicture("Actor1_3"));
    test2.magic(1);
    test2.x = 280;
    test2.y = 0;
    this.addChild(test2);

    var test3 = new Sprite_Shader(ImageManager.loadPicture("Actor1_3"));
    test3.twist();
    test3.x = 560;
    test3.y = 0;
    this.addChild(test3);


    var test4 = new Sprite_Shader(ImageManager.loadPicture("Actor1_3"));
    test4.shadow();
    test4.x = 0;
    test4.y = 300;
    this.addChild(test4);

    var test5 = new Sprite_Shader(ImageManager.loadPicture("Actor1_3"));
    test5.gray();
    test5.x = 560;
    test5.y = 300;
    this.addChild(test5);

评分

参与人数 4星屑 +200 梦石 +1 赞 +3 收起 理由
saicatedoan + 1 精品文章
石林尘影 + 1 认可答案
余烬之中 + 200 + 1 原创发布
fux2 + 1 精品文章

查看全部评分

老黄鸡

梦石
4
星屑
46133
在线时间
7978 小时
注册时间
2009-7-6
回帖
13342

MZ评测员RM创作大赛01组委会开拓者贵宾

发表于 2017-10-4 17:56:06 | 显示全部楼层
果然有人把pixi里的这玩意拿出来玩

点评

这东西 迟早得被挖掘,而且和美术资源一样 就算自己数学差脑补不出效果, 网上有大把的 cocos unity 现成的shader 拿来参考修改  发表于 2017-10-4 19:29
RGDirect - DirectX驱动的RGSS,点我了解.
(排满,暂停)RM全系列成套系统定制请联系QQ1213237796
不接受对其他插件维护的委托
回复

使用道具 举报

名侦探小柯

梦石
10
星屑
4425
在线时间
3741 小时
注册时间
2006-9-6
回帖
36596

极短27获奖MZ评测员开拓者贵宾第3届短篇游戏大赛主流游戏组亚军第5届短篇游戏比赛亚军

发表于 2017-10-4 21:33:54 | 显示全部楼层
变化范围不能超过原图尺寸是不是有点遗憾了

点评

解决方案有很多,懒得解决的话增加图片透明区域即可,图形学这个深坑我也只是抛砖的菜鸟  发表于 2017-10-4 23:31
不定期回归 ~ 游戏开发之旅
———————————————————————————————
回复

使用道具 举报

梦石
0
星屑
15309
在线时间
720 小时
注册时间
2011-7-16
回帖
1373

开拓者

发表于 2017-10-6 19:35:22 | 显示全部楼层
强,只能膜拜,已经挖不动了

点评

照葫芦画瓢就行了  发表于 2017-10-7 13:12
RMMV网络插件,开源免费,内含服务器端,无需强制登录,云数据,弹幕,云存档,排名,兑换码,版本检测,可自由上架下架删除。q群399090587
免打包运行MV游戏,云游戏,安卓App雷神游戏厅,在线玩游戏,上传下载游戏
开源游戏:重装机兵之重装归来【RMMV制作】全球首款按照美剧分季分集的方式发布的游戏
体素画 -- MV画3D像素图的画板
RMMV显示3D模型和场景的插件
RMMV显示spine骨骼动画的插件
RMMV秘密通道插件
突破敌群数量上限8个的插件
在rmmv中显示gif动态图片的插件
一款可以在mv游戏界面的任意位置显示任意文字的插件
RMMV Toast 插件 带物品得失提示,可以设置开启关闭 兼容yep itemcore
制作一个改名卡道具插件、调整标题页面菜单的插件、在标题页面之前显示大段文字的插件、标题页面显示版本号的插件
物品得失自动提示自动上色自动换行插件
我的Q群 663889472
另外,我的插件、范例、游戏都在这里
回复

使用道具 举报

梦石
0
星屑
69
在线时间
0 小时
注册时间
2018-11-30
回帖
32
发表于 2018-11-30 05:08:20 | 显示全部楼层
感谢楼主分享
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册会员

本版积分规则

Powered by Discuz! X5.0 © 2001-2026 Discuz! Team.

在本版发帖返回顶部