1. Project requirementsThe project needs to have a function: when a light spot passes through a path, all the positions on the path will light up. 2. Document contentThe function is similar to the eraser function of this great god: https://forum.cocos.org/t/2-0-8/74246 However, the path that the project's requirements will take has fuzzy edges around it—it gets darker from the center to the edge. So for shader examples borrowed from the masters on the Internet, similar to the following examples: I made some changes on the shoulders of the great god to meet the needs of the project. 3. Project ExamplesHere's an example from my own test project: (Please ignore the poor image quality, I'm too lazy to install screen recording software) 4. Project codeSliderPointLight.ts const { ccclass, property } = cc._decorator; @ccclass export default class Follow_spot extends cc.Component { @property(cc.Node) bg: cc.Node = null; material: cc.Material = null; center: number[] = [0.5, 0.5]; testArr: number[] = []; onLoad() { this.material = this.bg.getComponent(cc.Sprite).getMaterial(0); this.material.setProperty('wh_ratio', this.bg.width / this.bg.height); this.material.setProperty('center', this.center); //The most important sentence in js is this one, where the parameter is the array length * the dimension of the vector in the array this.material.setProperty('colorArr', new Float32Array(400)); //When setting here, you need to expand the vector components in the array into a one-dimensional array this.material.setProperty('colorArr', []); this.bg.on(cc.Node.EventType.TOUCH_MOVE, this.touchMoveEvent, this); } touchMoveEvent(evt: cc.Event.EventTouch) { this.center[0] = evt.getLocation().x / this.bg.width; this.center[1] = 1 - evt.getLocation().y / this.bg.height; console.log(this.center); this.material.setProperty('center', this.center); if (this.testArr.length >= 400) { this.testArr.shift(); this.testArr.shift(); } this.testArr.push(this.center[0]); this.testArr.push(this.center[1]); //The most important sentence in js is this one, where the parameter is the array length * the dimension of the vector in the array this.material.setProperty('colorArr', new Float32Array(this.testArr)); //When setting here, you need to expand the vector components in the array into a one-dimensional array this.material.setProperty('colorArr', this.testArr); } } SliderPointLight.effect CCEeffect % { Techniques: -passes: -vert: vs frag: fs blendState: targets: -blend: true rasterizerState: cullMode: none properties: texture: value: white } wh_ratio: value: 1.78, editor: tooltip: "Aspect Ratio" } } blur: value: 0.35, editor: tooltip: "Aperture Blur Level" } } radius: value: 0.5, editor: tooltip: "Aperture Radius" } } center: value: [0.5, 0.5], editor: tooltip: "Aperture start point" } } colorArr: { value: [0.5, 0.5, 0.5, 0.5] } } % CCProgram vs % precision highp float; #include <cc-global> #include <cc-local> in vec3 a_position; in vec4 a_color; out vec4 v_color; #if USE_TEXTURE in vec2 a_uv0; out vec2 v_uv0; #endif void main() { vec4 pos = vec4(a_position, 1); #if CC_USE_MODEL pos = cc_matViewProj * cc_matWorld * pos; #else pos = cc_matViewProj * pos; #endif #if USE_TEXTURE v_uv0 = a_uv0; #endif v_color = a_color; gl_Position = pos; } } % CCProgram fs % { precision highp float; #include <alpha-test> in vec4 v_color; #if USE_TEXTURE in vec2 v_uv0; uniform sampler2D texture; #endif uniform ARGS float radius; float blur; vec2 center; float wh_ratio; }; //effect definition uniform Metaball { vec4 colorArr[100]; }; void main() { vec4 o = vec4(1, 1, 1, 0); o *= texture(texture, v_uv0); o *= v_color; float circle = radius * radius; for (int i = 0; i < 100; i++) { float colorX = colorArr[i].x; float colorY = colorArr[i].y; float rx = colorX * wh_ratio; float ry = colorY; float dis = (v_uv0.x * wh_ratio - rx) * (v_uv0.x * wh_ratio - rx) + (v_uv0.y - ry) * (v_uv0.y - ry); oa = smoothstep(circle, circle - blur, dis) + oa; } gl_FragColor = o; } }% The above is the details of how CocosCreator can display the texture at the position you swipe. For more information about CocosCreator, please pay attention to other related articles on 123WORDPRESS.COM! You may also be interested in:
|
<<: How to uninstall MySQL 5.7.19 under Linux
>>: Docker installs the official Redis image and enables password authentication
Table of contents Set is a special collection who...
Table of contents 0. The kernel tree that comes w...
1. Download the 64-bit zip file from the official...
CSS attribute selectors are amazing. They can hel...
Mysql8.0.12 decompression version installation me...
Source code (some classes deleted): Copy code The ...
How to solve VMware workstation virtual machine c...
All the orchestration files and configuration fil...
When vue2 converts timestamps, it generally uses ...
In order to extend the disk life for storing audi...
Uninstall the installed version on Ubuntu: sudo a...
MySQL 5.7.27 detailed download, installation and ...
Use the FRAME property to control the style type ...
This article shares the specific code of jQuery t...
The following error message appears when installi...