8625 views|3 replies

37

Posts

0

Resources
The OP
 

Teach you step by step to achieve the spotlight effect [Copy link]

A spotlight is a special point light source that can project light in one direction. Spotlights project light in a cone-like shape, which is consistent with the spotlights we see in reality. The light is emitted from one point and illuminates a conical range in a certain direction. A spotlight is similar to a point light source with a limited angle range. Spotlights can be used to simulate light source effects such as stage, car headlights, flashlights, and table lamps in digital twin visualization scenes. They can be added under objects such as 3D containers and cameras, and will take effect on all corresponding digital twin visualization objects.

In digital twin visualization scenes, spotlights are one of the most commonly used light sources because the project requires it, especially if we want to use shadows. The spotlight in ThingJS can be used to simulate linear lighting effects such as flashlights, car lights, etc., emitting light from a point to a cone range. The official type is spotlight.

If the target object in the digital twin visualization scene is dynamic, the mousemove event is used to realize the movement of the target object. The mousemove event is a real-time response event. When the position of the mouse pointer changes (at least one pixel), the mousemove event is triggered. The sensitivity of the event response mainly refers to the speed of the mouse pointer movement and the speed of the browser tracking update. The official creates a spotlight 5 meters above the digital twin visualization object and allows the object to continuously circulate along the path direction to achieve a "follow the object" spotlight effect. The light hits the moving object, and the illumination range and angle change as the object moves.

However, please note that too many spotlights in the digital twin visualization scene will affect rendering performance.

The specific code is as follows:


var app = new THING.App({
    url: 'https://www.thingjs.com/static/models/storehouse',
});

// 参数
var dataObj = {
    'type': 'SpotLight',
    'lightAngle': 30,
    'intensity': 1,
    'penumbra': 0.5,
    'castShadow': false,
    'position': null,
    'height': 0,
    'color': 0xFFFFFF,
    'distance': null,
    'target': null,
    'helper': true,
    'follow': true,
};
// 叉车
let car1;
let car2;
// 当前灯光
let curLight;
let curLightPosition;
// 创建聚光灯方法
function createSpotLight(position, target) {
    dataObj['lightAngle'] = 30;
    dataObj['intensity'] = 0.5;
    dataObj['penumbra'] = 0.5;
    dataObj['castShadow'] = false;
    dataObj['position'] = position;
    dataObj['distance'] = 25;
    dataObj['color'] = 0xFFFFFF;
    dataObj['helper'] = true;
    dataObj['follow'] = true;
    //创建聚光灯
    var spotLight = app.create(dataObj);
    curLight = spotLight;
    curLightPosition = spotLight.position;
    createSpotLightControlPanel(spotLight);
    curLight.lookAt(car1);
}

/**
 * 灯光控制面板
 */
function createSpotLightControlPanel() {
    var panel = new THING.widget.Panel({
        isDrag: true,
        titleText: "灯光参数调整",
        width: '260px',
        hasTitle: true
    });
    // 设置 panel 位置    
    panel.position = [10, 35];
    panel.addNumberSlider(dataObj, 'lightAngle').caption('灯光角度').step(1).min(0).max(180).isChangeValue(true).on('change', function(value) {
        curLight.lightAngle = value;
    });
    panel.addNumberSlider(dataObj, 'intensity').caption('亮度').step(0.01).min(0).max(1).isChangeValue(true).on('change', function(value) {
        curLight.intensity = value;
    });
    panel.addNumberSlider(dataObj, 'penumbra').caption('半影').step(0.01).min(0).max(1).isChangeValue(true).on('change', function(value) {
        curLight.penumbra = value;
    });
    panel.addNumberSlider(dataObj, 'distance').caption('距离').step(0.1).min(0).max(200).isChangeValue(true).on('change', function(value) {
        curLight.distance = value;
    });
    panel.addNumberSlider(dataObj, 'height').caption('高度').step(0.1).min(0).max(200).isChangeValue(true).on('change', function(value) {
        curLight.position = [curLightPosition[0], curLightPosition[1] + value, curLightPosition[2]];
    });
    panel.addBoolean(dataObj, 'castShadow').caption('影子').on('change', function(value) {
        curLight.castShadow = value;
    });
    panel.addBoolean(dataObj, 'helper').caption('辅助线').on('change', function(value) {
        curLight.helper = value;
    });
    panel.addBoolean(dataObj, 'follow').caption('跟随物体').on('change', function(value) {
        if (value) {
            curLight.lookAt(car1);
        } else {
            curLight.lookAt(null);
        }
    });
    panel.addColor(dataObj, 'color').caption('颜色').on('change', function(value) {
        curLight.lightColor = value;
    });

}

/**
 * 注册鼠标移动事件,检查是否按下'shift'键, 按下设置聚光灯跟随鼠标位置
 */
app.on('mousemove', function(ev) {
    if (!curLight) {
        return;
    }

    if (!ev.shiftKey) {
        return;
    }

    var pickedPosition = ev.pickedPosition;
    if (pickedPosition) {
        curLight.lookAt(pickedPosition);
    }
})

/**
 * 注册场景load事件
 */
app.on('load', function(ev) {
    // createTip();
    // 主灯强度设置为0,突出聚光灯效果
    app.lighting = {
        mainLight: {
            intensity: 0
        }
    };

    // 获取场景内id为'car01' 和 'car02' 的叉车
    car1 = app.query('car01')[0];
    car2 = app.query('car02')[0];

    // 参数1: 在car2上方5米创建一个聚光灯
    // 参数2: 初始target设置为car1的位置
    createSpotLight(THING.Math.addVector(car2.position, [0, 5, 0]), car1.position);

    // 创建一个圆形路径
    var path = [];
    var radius = 6;
    for (var degree = 0; degree <= 360; degree += 10) {
        var x = Math.cos(degree * 2 * Math.PI / 360) * radius;
        var z = Math.sin(degree * 2 * Math.PI / 360) * radius;
        path.push(THING.Math.addVector(car1.position, [x, 0, z]));
    }
    // 让 car1 沿圆形路径运动
    car1.movePath({
        orientToPath: true, // 物体移动时沿向路径方向
        path: path,
        time: 10 * 1000,
        loopType: THING.LoopType.Repeat // 循环类型
    });

    initThingJsTip("左侧面板可对灯光参数进行调整。按住 shift 键,聚光灯可追踪鼠标位置");
    $(".warninfo3").css("left", "55%");
})

———————————————————

Digital twin visualization: https://www.thingjs.com/

This post is from LED Zone

Latest reply

Really good, it's fun to make games~   Details Published on 2021-7-7 17:12
 
 

6593

Posts

0

Resources
2
 

The spotlight effect is very realistic

Need code?

This post is from LED Zone

Comments

It is completely OK to have simple front-end development experience  Details Published on 2021-7-8 15:20
 
 
 

1942

Posts

2

Resources
3
 

Really good, it's fun to make games~

This post is from LED Zone
 
 
 

37

Posts

0

Resources
4
 
Jacktang posted on 2021-7-7 15:21 The spotlight effect is very realistic. Do you need code?

It is completely OK to have simple front-end development experience

This post is from LED Zone
 
 
 

Guess Your Favourite
Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list