Git Product home page Git Product logo

creator_2_0_material_demo's Introduction

creator_2_0_material_demo

简介

使用Creator2.0的材质系统实现几个Shader,效果如下:

  • 正常效果(Sprite自带): 效果

  • 灰度效果(Sprite自带): 效果

  • 高亮叠加效果 效果

  • 雨水效果 效果

实现概述

修改Sprite对材质的默认支持

Sprite默认提供了两种材质效果,就是上面的正常效果,和灰度效果,个人觉得实现得有点局限性:比如我想实现一种效果(如上面的高亮),只能通过外部强制指定材质来实现,如果Sprite换了另一张纹理,或是Sprite同时有一个Animation组件用于播放序列帧,那么Sprite内部会强制切换回正常效果。

所以,我Hook了Sprite的实现,增加自定义材质的逻辑,代码在: SpriteHook

自定义材质类

继承自引擎的Material类,实现了一个CustomMaterial,这个材质类可以实现各种不同的效果。

Demo代码片段

高亮效果

{
    var name = 'overlay';
    var mat = this.spImage.getMaterial(name);
    if (!mat) {
        var CustomMaterial = require("CustomMaterial");
        mat = new CustomMaterial(name);
        this.spImage.setMaterial(name, mat);	
    }
    this.spImage.node.color = new cc.Color().fromHEX("#FBC00C")
    this.spImage.activateMaterial(name);
}

雨效果

// 雨珠效果
this.resetImage();

this._start = Date.now();
var name = 'rainheart';
var mat = this.spImage.getMaterial(name);
if (!mat) {
    var CustomMaterial = require("CustomMaterial");
    mat = new CustomMaterial(name, 
        [
            { name: 'texSize', type: renderer.PARAM_FLOAT2 },
            { name: 'iResolution', type: renderer.PARAM_FLOAT3 },
            { name: 'iTime', type: renderer.PARAM_FLOAT },
        ],
        [
            { name: 'HAS_HEART', value: false },
            { name: 'USE_POST_PROCESSING', value: true }
        ]);
    this.spImage.setMaterial(name, mat);
}
this.spImage.activateMaterial(name);
mat.texture.update({flipY: true});
var iResolution = new cc.Vec3(this.spImage.node.width, this.spImage.node.height, 0);
var texSize = new cc.Vec2(this.spImage.node.width, this.spImage.node.height);
mat.setParamValue("iResolution", iResolution);
mat.setParamValue("texSize", texSize);

增加更多的Shader效果

Shaders目录中只有两个,有兴趣的欢迎增加更多的效果:)

creator_2_0_material_demo's People

Contributors

colinsusie avatar whwitpohe avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.