How to display texture at the position of swipe in CocosCreator

How to display texture at the position of swipe in CocosCreator

1. Project requirements

The 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 content

The 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 Examples

Here'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 code

SliderPointLight.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:
  • Detailed explanation of cocoscreater prefab
  • How to use resident nodes for layer management in CocosCreator
  • How to use CocosCreator for sound processing in game development
  • CocosCreator ScrollView optimization series: frame loading
  • Detailed explanation of CocosCreator project structure mechanism
  • How to use CocosCreator object pool
  • Organize the common knowledge points of CocosCreator
  • Comprehensive explanation of CocosCreator hot update
  • CocosCreator classic entry project flappybird
  • CocosCreator Universal Framework Design Network
  • How to use CocosCreator to create a shooting game
  • How to use a game controller in CocosCreator

<<:  How to uninstall MySQL 5.7.19 under Linux

>>:  Docker installs the official Redis image and enables password authentication

Recommend

Font Treasure House 50 exquisite free English font resources Part 1

Designers have their own font library, which allo...

40 fonts recommended for famous website logos

Do you know what fonts are used in the logo desig...

100 ways to change the color of an image using CSS (worth collecting)

Preface “When it comes to image processing, we of...

Writing methods that should be prohibited in native JS

Table of contents Block-level functions Directly ...

Analysis of MySQL latency issues and data flushing strategy process

Table of contents 1. MySQL replication process 2....

MySQL query redundant indexes and unused index operations

MySQL 5.7 and above versions provide direct query...

Solution to no Chinese input method in Ubuntu

There is no solution for Chinese input method und...

How to install and configure Redis in CentOS7

Introduction There is no need to introduce Redis ...

Three solutions for sub-functions accessing external variables in JavaScript

Preface When we write web pages, we will definite...

Detailed explanation of MySQL transactions and MySQL logs

Transactional Characteristics 1. Atomicity: After...

Analysis of different MySQL table sorting rules error

The following error is reported when MySQL joins ...

Use PS to create an xhtml+css website homepage in two minutes

There are too many articles about xhtml+css websi...

React configuration px conversion rem method

Install related dependencies npm i lib-flexible -...

MYSQL slow query and log example explanation

1. Introduction By enabling the slow query log, M...

Linux installation MySQL tutorial (binary distribution)

This tutorial shares the detailed steps of instal...