Skip to content

Commit

Permalink
test: Add custom material examples and configure 2D sample content.
Browse files Browse the repository at this point in the history
测试示例: 增加自定义材质示例,配置2D示例内容.
  • Loading branch information
INEEDSSD committed Dec 11, 2024
1 parent f104239 commit c5c61ce
Show file tree
Hide file tree
Showing 5 changed files with 963 additions and 881 deletions.
61 changes: 61 additions & 0 deletions src/samples/2d/Material2DDemo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { Laya } from "Laya"
import { Sprite } from "laya/display/Sprite";
import { Stage } from "laya/display/Stage";
import { Stat } from "laya/utils/Stat";
import { Main } from "../Main";
import { Scene } from "laya/display/Scene";
import { Material } from "laya/resource/Material";
import { Graphics } from "laya/display/Graphics";
import { Shader3D } from "laya/RenderEngine/RenderShader/Shader3D";
import { ShaderDataType } from "laya/RenderDriver/DriverDesign/RenderDevice/ShaderData";
import { Color } from "laya/maths/Color";

export class Material2DDemo {
Main: typeof Main = null;
scene: Scene;

constructor(mainClass: typeof Main) {
this.Main = mainClass;
Laya.init(0, 0).then(() => {
Laya.stage.scaleMode = Stage.SCALE_FULL;
Laya.stage.screenMode = Stage.SCREEN_NONE;
Stat.show();
Laya.stage.bgColor = "#232628";
//提前加载CustomShader1与其对应的material
let res: string[] = ["res/2DRender/custom2DShader_1.shader", "res/apes/monkey3.png"];
this.scene = new Scene();
this.Main.box2D.addChild(this.scene);
Laya.loader.load(res).then(() => {
this.set2DCustomMaterial();
});
});
}

set2DCustomMaterial(): void {
// 自定义shader与全局2D uniform变量使用
let customShaderSp = new Sprite();
customShaderSp.loadImage("res/apes/monkey3.png");
this.scene.addChild(customShaderSp);
this.loadCustom2DShader(customShaderSp);

// 自定义2d材质使用
Laya.loader.load("res/2DRender/customMaterial_1.lmat").then((mat: Material) => {
let customMaterialSp = new Sprite();
customMaterialSp.pos(100, 0);
this.scene.addChild(customMaterialSp);
customMaterialSp.loadImage("res/apes/monkey3.png");
customMaterialSp.graphics.material = mat;
});
}

loadCustom2DShader(sp: Sprite): void {
Laya.loader.load("res/2DRender/custom2DShader_0.shader").then(() => {
let mat = new Material();
mat.setShaderName("custom2DShader_0");
// 设置2D全局uniform变量
Graphics.add2DGlobalUniformData(Shader3D.propertyNameToID("u_GlobalColor"), "u_GlobalColor", ShaderDataType.Color);
(this.scene as Scene).sceneShaderData.setColor(Shader3D.propertyNameToID("u_GlobalColor"), new Color(0.0, 1.0, 0.0, 1.0));
sp.graphics.material = mat;
});
}
}
2 changes: 2 additions & 0 deletions src/samples/2d/Physics_Bridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { CircleCollider } from "laya/physics/Collider2D/CircleCollider";
import { PolygonCollider } from "laya/physics/Collider2D/PolygonCollider";
import { Vector2 } from "laya/maths/Vector2";
import { Physics2D } from "laya/physics/Physics2D";
import { Physics2DOption } from "laya/physics/Physics2DOption";

export class Physics_Bridge {
Main: typeof Main = null;
Expand Down Expand Up @@ -127,6 +128,7 @@ export class Physics_Bridge {
tempVec.y = targetY - circleCollider.y;
Vector2.normalize(tempVec, tempVec);
Vector2.scale(tempVec, 25, tempVec);
Vector2.scale(tempVec, Physics2DOption.pixelRatio, tempVec);
circleBody.linearVelocity = tempVec.toArray();
Laya.timer.frameOnce(120, this, function () {
newBall.destroy();
Expand Down
3 changes: 2 additions & 1 deletion src/samples/2d/Physics_Strandbeests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { Vector2 } from "laya/maths/Vector2";
import { CheckBox } from "laya/ui/CheckBox";
import { Handler } from "laya/utils/Handler";
import { Utils } from "laya/utils/Utils";
import { Physics2DOption } from "laya/physics/Physics2DOption";

const dampingRatio: number = 0.5;
const frequencyHz: number = 10.0;
Expand Down Expand Up @@ -224,7 +225,6 @@ export class Physics_Strandbeests {

// 单击产生新的小球刚体
Laya.stage.on(Event.CLICK, this, () => {

let tempVec = this.TempVec;
let newBall = new Sprite();
newBall.pos(Laya.stage.mouseX, Laya.stage.mouseY);
Expand All @@ -236,6 +236,7 @@ export class Physics_Strandbeests {
tempVec.y = this.chassis.y - newBall.y;
Vector2.normalize(tempVec, tempVec);
Vector2.scale(tempVec, 50, tempVec);
Vector2.scale(tempVec, Physics2DOption.pixelRatio, tempVec);
circleBody.linearVelocity = tempVec.toArray();
Laya.timer.frameOnce(120, this, function () {
newBall.destroy();
Expand Down
Loading

0 comments on commit c5c61ce

Please sign in to comment.