登峰造极境

  • WIN
    • CSharp
    • JAVA
    • OAM
    • DirectX
    • Emgucv
  • UNIX
    • FFmpeg
    • QT
    • Python
    • Opencv
    • Openwrt
    • Twisted
    • Design Patterns
    • Mysql
    • Mycat
    • MariaDB
    • Make
    • OAM
    • Supervisor
    • Nginx
    • KVM
    • Docker
    • OpenStack
  • WEB
    • ASP
    • Node.js
    • PHP
    • Directadmin
    • Openssl
    • Regex
  • APP
    • Android
  • AI
    • Algorithm
    • Deep Learning
    • Machine Learning
  • IOT
    • Device
    • MSP430
  • DIY
    • Algorithm
    • Design Patterns
    • MATH
    • X98 AIR 3G
    • Tucao
    • fun
  • LIFE
    • 美食
    • 关于我
  • LINKS
  • ME
Claves
长风破浪会有时,直挂云帆济沧海
  1. 首页
  2. Programming
  3. babylon.js
  4. 正文

Babylonjs 如何实现全场景跨Mesh全局扫光

2025-06-20

一、需求灵感来源

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;
};
标签: 暂无
最后更新:2025-06-24

代号山岳

知之为知之 不知为不知

点赞
< 上一篇

COPYRIGHT © 2099 登峰造极境. ALL RIGHTS RESERVED.

Theme Kratos Made By Seaton Jiang

蜀ICP备14031139号-5

川公网安备51012202000587号