一、需求灵感来源
gamemcu的小车扫光

二、自己实现
指导大佬:傲慢なる木竜王「温馨」
指导思想:Babylon的话可以用材质插件注入多个材质。然后用世界位置做这个效果
材质插件:材质插件可以在已有材质上,增加自定义效果,就能轻松实现全局扫光,且不影响现有物体材质。材质插件 |Babylon.js 文档
https://playground.babylonjs.com/#RNO05A#6

var createScene = function () {
// This creates a basic Babylon Scene object (non-mesh)
var scene = new BABYLON.Scene(engine);
var camera = new BABYLON.ArcRotateCamera("camera", 0, 1, 10, BABYLON.Vector3.Zero(), scene);
camera.attachControl(canvas, true);
// This creates a light, aiming 0,1,0 - to the sky (non-mesh)
var light = new BABYLON.HemisphericLight("light", new BABYLON.Vector3(0, 1, 0), scene);
// Default intensity is 1. Let's dim the light a small amount
light.intensity = 0.7;
const createMeshWithMaterial = (name, position) => {
const mesh = BABYLON.MeshBuilder.CreateBox(name, {}, scene);
mesh.position = position;
const material = new BABYLON.StandardMaterial(`${name}Material`, scene);
material.diffuseColor = new BABYLON.Color3(Math.random(), Math.random(), Math.random());
mesh.material = material;
return mesh;
};
var sphere1 = BABYLON.MeshBuilder.CreateSphere("sphere", {diameter: 1}, scene);
sphere1.position= new BABYLON.Vector3(3.3, 0, 0);
var sphere2 = BABYLON.MeshBuilder.CreateSphere("sphere", {diameter: 1}, scene);
sphere2.position= new BABYLON.Vector3(-1.1, 0, 0);
const meshes = [
createMeshWithMaterial("box1", new BABYLON.Vector3(0, 0, 0)),
createMeshWithMaterial("box2", new BABYLON.Vector3(1.1, 0, 0)),
createMeshWithMaterial("box2", new BABYLON.Vector3(2.2, 0, 0)),sphere1,sphere2
];
// 创建 ShaderMaterial
const shaderMaterial = new BABYLON.ShaderMaterial("shader", scene, {
vertexSource: `
precision highp float;
attribute vec3 position;
attribute vec2 uv;
uniform mat4 worldViewProjection;
uniform mat4 world;
varying vec3 vPosition;
varying vec2 vUv;
varying vec4 vWorldPosition;
void main(void) {
vUv = uv;
vPosition = position;
vWorldPosition = world * vec4(position, 1.0);
gl_Position = worldViewProjection * vec4(position, 1.0);
}
`,
fragmentSource: `
precision highp float;
varying vec3 vPosition;
varying vec2 vUv;
uniform float time;
varying vec4 vWorldPosition;
void main(void) {
float intensity = abs(sin(time + vWorldPosition.x));
gl_FragColor = vec4(0, intensity, 1, 1.0);
}
`
}, {
attributes: ["position"],
uniforms: ["worldViewProjection", "time","world"]
});
shaderMaterial.onBind = (mesh) => {
shaderMaterial.setFloat("time", performance.now() / 1000);
};
meshes.forEach(mesh => {
mesh.material = shaderMaterial;
});
return scene;
};