# 渲染对象热力分析
通过 “渲染对象热力分析器”(RenderableThermalAnalyzer)将某些属性插值分析然后将分析结果通过渲染组件展示。
# 创建分析器
import { ColorGradient, Entity, IDWInterpolator3D, Model, RenderableThermalAnalyzer, defined } from '@tx3d/core';
// 加载模型
engine.loadGLTF( './assets/models/水泵房/水泵房.glb' ).then( ( entity ) => {
// 查找所有Model类型渲染组件
const models = [];
Entity.traverse( entity, ( e ) => {
const model = e.getComponent( Model );
if ( defined( model ) ) {
models.push( model );
}
} );
// 创建距离反比插值器
const interpolator = new IDWInterpolator3D();
// 创建分析渐变色
const gradient = new ColorGradient( [
{ value: 0.0, color: '#000066' },
{ value: 0.1, color: 'blue' },
{ value: 0.2, color: 'cyan' },
{ value: 0.3, color: 'lime' },
{ value: 0.4, color: 'yellow' },
{ value: 0.5, color: 'orange' },
{ value: 0.6, color: 'red' },
{ value: 0.7, color: 'maroon' },
{ value: 0.8, color: '#660066' },
{ value: 0.9, color: '#990099' },
{ value: 1.0, color: '#ff66ff' }
] );
// 创建渲染对象热力分析器
const analyzer = new RenderableThermalAnalyzer( engine, { interpolator, gradient } );
} );
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
提示
- 渲染对象热力分析器初始化参数,详见ThermalAnalyzerParameters;
- 构建渲染对象热力分析器时可以通过设置插值器实现不同的插值方案,当前程序提供距离反比(IDWInterpolator3D)和克里金 (KrigingInterpolator3D)两种类型的插值器,如果不设置默认使用距离反比插值器,二次开发时也可以根据实际需求自定义插值器。
# 执行分析
通过渲染对象热力分析器(RenderableThermalAnalyzer)的execute接口执行分析。
import { ColorGradient, Entity, IDWInterpolator3D, Model, RenderableThermalAnalyzer, defined } from '@tx3d/core';
// 加载模型
engine.loadGLTF( './assets/models/水泵房/水泵房.glb' ).then( ( entity ) => {
// 查找所有Model类型渲染组件
const models = [];
Entity.traverse( entity, ( e ) => {
const model = e.getComponent( Model );
if ( defined( model ) ) {
models.push( model );
}
} );
// 创建距离反比插值器
const interpolator = new IDWInterpolator3D();
// 创建分析渐变色
const gradient = new ColorGradient( [
{ value: 0.0, color: '#000066' },
{ value: 0.1, color: 'blue' },
{ value: 0.2, color: 'cyan' },
{ value: 0.3, color: 'lime' },
{ value: 0.4, color: 'yellow' },
{ value: 0.5, color: 'orange' },
{ value: 0.6, color: 'red' },
{ value: 0.7, color: 'maroon' },
{ value: 0.8, color: '#660066' },
{ value: 0.9, color: '#990099' },
{ value: 1.0, color: '#ff66ff' }
] );
// 创建渲染对象热力分析器
const analyzer = new RenderableThermalAnalyzer( engine, { interpolator, gradient } );
// 构建分析参数
// 根据模型外包盒范围随机生成特征点
const bounds = entity.transform.bounds;
const featurePoints = [];
for ( let i = 0; i < 50; i++ ) {
const x = MathUtils.randFloat( bounds.min.x, bounds.max.x );
const y = MathUtils.randFloat( bounds.min.y, bounds.max.y );
const z = MathUtils.randFloat( bounds.min.z, bounds.max.z );
const v = MathUtils.randFloat( 0.0, 1.0 );
featurePoints.push( {
point: [ x, y, z ],
value: v
} );
};
// 等值线参数
const isoline = {
values: [
{ value: 0.1, color: 'orange' },
{ value: 0.3, color: 'maroon' },
{ value: 0.5, color: 'blue' },
{ value: 0.7, color: 'cyan' },
{ value: 0.9, color: 'lime' }
],
thickness: 2.0
}
// 分析完成回调
const onCompleted = ( result ) => {
// 解构分析结果
const {
points, // 分析采样结果(坐标 + 属性值)
bounds, // 分析采样边界
stepSize // 真实分析采样步长
} = result;
// TODO:完成回调
};
// 分析失败回调
const onFailed = ( error ) => {
console.log( error );
}
// 执行热力分析
analyzer.execute( {
renderable: models,
featurePoints,
isoline,
onCompleted,
onFailed
} );
} );
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
提示
如果上次分析未完成,再次调用 execute 会通过 onFailed 返回错误信息。
注意
特征点的属性值需要归一化到[ 0~1 ]区间;
# 结束分析
通过渲染对象热力分析器(RenderableThermalAnalyzer)的stop接口终止正在进行的分析或撤销已完成的分析。
// 终止正在进行的分析或撤销已完成的分析
analyzer.stop();
2
提示
如果上次分析未完成,调用 stop 会终止正在进行的分析,并通过 onFailed 返回分析被终止的错误信息。
# 过滤分析结果
通过渲染对象热力分析器(RenderableThermalAnalyzer)的filtering接口过滤分析。
// 根据模型外包盒范围随机生成分析过滤特征点
const bounds = entity.transform.bounds;
const filterFeaturePoints = [];
for ( let i = 0; i < 50; i++ ) {
const x = MathUtils.randFloat( bounds.min.x, bounds.max.x );
const y = MathUtils.randFloat( bounds.min.y, bounds.max.y );
const z = MathUtils.randFloat( bounds.min.z, bounds.max.z );
const v = MathUtils.randFloat( 0.0, 1.0 );
featurePoints.push( {
point: [ x, y, z ],
value: v
} );
};
// 过滤完成回调
const onCompleted = ( points ) => {
// TODO:过滤完成回调
}
// 过滤失败回调
const onFailed = ( error ) => {
console.log( error );
}
// 过滤分析
analyzer.filtering( { featurePoints, onCompleted, onFailed } );
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# 结束过滤
通过渲染对象热力分析器(RenderableThermalAnalyzer)的stopFiltering接口终止正在进行的分析或撤销已完成的过滤。
// 终止正在进行的分析或撤销已完成的过滤
analyzer.stopFiltering();
2
提示
如果上次过滤未完成,调用 stopFiltering 会终止正在进行的过滤,并通过 onFailed 返回过滤被终止的错误信息。
# 设置分析渐变颜色
通过渲染对象热力分析器(RenderableThermalAnalyzer)的setGradient接口设置分析渐变颜色。
import { Color } from 'three';
import { ColorGradient } from '@tx3d/core';
// 生成随机颜色
const colorValues = [];
for ( let i = 0; i <= 10; i++ ) {
const hue = Math.floor( Math.random() * 360 ); // 0-359° 广色域
colorValues.push( { value: i * 0.1, color: new Color( `hsl(${hue}, 80%, 70%)` ) } );
}
// 设置分析渐变颜色
analyzer.setGradient( new ColorGradient( colorValues ) );
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# 设置分析阈值
通过渲染对象热力分析器(RenderableThermalAnalyzer)的setThreshold接口设置分析阈值。
// 设置分析阈值
analyzer.setThreshold( 0.25, 0.75 );
2
:::warnig 注意 分析阈值最小&最大值需要在[ 0~1 ]区间内。 :::
# 设置过滤阈值
通过渲染对象热力分析器(RenderableThermalAnalyzer)的setFilterThreshold接口设置过滤阈值。
// 设置过滤阈值
analyzer.setFilterThreshold( 0.15, 0.5 );
2
:::warnig 注意 过滤阈值最小&最大值需要在[ 0~1 ]区间内。 :::
# 销毁分析器
通过渲染对象热力分析器(RenderableThermalAnalyzer)的dispose接口销毁分析器。
// 销毁分析器
analyzer.dispose();
analyzer = null;
2
3