书接上回,这里讨论MeshAction网格行为!!!
一、搭建基础场景
let box:AbstractMesh;
let cube:AbstractMesh;
let sphere:AbstractMesh;
let cylinder:AbstractMesh;
let mat:PBRMaterial;
// 创建天空盒
const createSkyBox = (scene:Scene):void=>{
const envTex = CubeTexture.CreateFromPrefilteredData('./env/environment.env',scene)
scene.environmentTexture = envTex;
scene.createDefaultSkybox(envTex,true,1000,0.2,true)
scene.environmentIntensity = 1.5
}
// 创建网格
const creatMeshes=(scene:Scene):void=>{
mat = new PBRMaterial('pbr')
mat.albedoColor = new Color3(1,0,0);
mat.roughness =1
cube = MeshBuilder.CreateBox('cube',{size:2})
cube.material = mat
cube.position = new Vector3(-4,0,0)
sphere = MeshBuilder.CreateSphere('sphere',{diameter:2})
sphere.material = mat
sphere.position = new Vector3(0,0,0)
cylinder = MeshBuilder.CreateCylinder('cylinder',{diameter:2})
cylinder.material = mat
cylinder.position = new Vector3(4,0,0)
// 创建行为
// createActions(scene)
}
// 创建相机
new FreeCamera("camera",new Vector3(0,0,-8));
二、创建网格行为
const createActions=(scene:Scene):void=>{
cube.actionManager = new ActionManager()
/**
* 点击之后放大
*/
cube.actionManager.registerAction(
new SetValueAction(
ActionManager.OnPickDownTrigger,
cube,
"scaling",
new Vector3(1.2,1.2,1.2)
))?.then(new SetValueAction(
ActionManager.NothingTrigger,
cube,
"scaling",
new Vector3(1,1,1)
))
/**
* 点击切换材质反照率颜色
* 3秒内切成换绿色
*
* 再次点击
* 1秒内切换成蓝色
*/
sphere.actionManager = new ActionManager()
sphere.actionManager.registerAction(
new InterpolateValueAction(
ActionManager.OnPickDownTrigger,
mat,
"albedoColor",
new Color3(0,1,0),
2000
)
)?.then(
new InterpolateValueAction(
ActionManager.OnPickDownTrigger,
mat,
"albedoColor",
new Color3(0,0,1),
1000
))
/**
* 圆柱体绕X轴旋转
*/
scene.actionManager = new ActionManager()
scene.actionManager.registerAction(
new IncrementValueAction(
ActionManager.OnEveryFrameTrigger,
cylinder,
"rotation.x",
0.01,
)
)
}