Compare commits
23
Commits
2b53ccea2b
...
Alpha0.2.1
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
70ff9fa0ad | ||
|
|
4eb6637062 | ||
|
|
8670b577f9 | ||
|
|
4768667803 | ||
|
|
571e80d542 | ||
|
|
da493bc6ae | ||
|
|
904de02140 | ||
|
|
4ade5d4bc3 | ||
|
|
e814f5e061 | ||
|
|
dfb3905c9b | ||
|
|
974cd2951d | ||
|
|
f660aefa4c | ||
|
|
c6b0f84cef | ||
|
|
456c21f6c2 | ||
|
|
436ba7b3d9 | ||
|
|
dcd2d10147 | ||
|
|
658765141c | ||
|
|
1825f3fc14 | ||
|
|
904dee9e86 | ||
|
|
73b68e1eac | ||
|
|
6c23ce62ff | ||
|
|
e11518ce8a | ||
|
|
c86ff9ef1a |
@@ -0,0 +1,304 @@
|
||||
const PATH = require("path");
|
||||
const FS = require("fs");
|
||||
const MINIMIST = require("minimist");
|
||||
const READLINE = require("readline-sync");
|
||||
|
||||
const ARGS = MINIMIST(process.argv.slice(2), {
|
||||
alias: {i: ["input", "save"], o: ["output", "obj"]}
|
||||
});
|
||||
|
||||
// 获取路径
|
||||
const inputFilePath = PATH.resolve(ARGS.i ?? "./save.ltss");
|
||||
const outputFilePath = PATH.resolve(ARGS.o ?? "./output.obj");
|
||||
|
||||
// 读取文件
|
||||
const fileString = FS.readFileSync(inputFilePath);
|
||||
console.log("文件读取成功...\r\n");
|
||||
|
||||
/**
|
||||
* 引入所需类型
|
||||
* @typedef {import("../source/Model/Archive").IArchiveObject} IArchiveObject
|
||||
* @typedef {import("../source/Model/Clip").IArchiveClip} IArchiveClip
|
||||
*/
|
||||
|
||||
/**
|
||||
* 解析文件
|
||||
* @type {IArchiveObject}
|
||||
*/
|
||||
const archive = JSON.parse(fileString);
|
||||
console.log("文件解析成功...\r\n");
|
||||
|
||||
// 打印全部的剪辑列表
|
||||
if (archive.clipPool.length > 0) {
|
||||
console.log("这个存档中存在以下剪辑:");
|
||||
}
|
||||
archive.clipPool.map((item, index) => {
|
||||
console.log(" \033[44;30m" + (index + 1) + "\033[40;32m " + item.name + " [" + item.id + "]\033[0m");
|
||||
});
|
||||
|
||||
/**
|
||||
* 选择剪辑
|
||||
* @type {IArchiveClip}
|
||||
*/
|
||||
let clip;
|
||||
if (archive.clipPool.length <= 0) {
|
||||
console.log("存档中没有剪辑, 退出程序...\r\n");
|
||||
process.exit();
|
||||
} else if (archive.clipPool.length === 1) {
|
||||
console.log("\r\n存档中只有一个剪辑, 自动选择...\r\n");
|
||||
clip = archive.clipPool[0];
|
||||
} else {
|
||||
console.log("\r\n请选择一个剪辑: ");
|
||||
let userInput = READLINE.question();
|
||||
for (let i = 0; i < archive.clipPool.length; i++) {
|
||||
if ((i + 1) == userInput) {
|
||||
clip = archive.clipPool[i];
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 选择提示
|
||||
if (clip) {
|
||||
console.log("已选择剪辑: " + clip.name + "\r\n");
|
||||
} else {
|
||||
console.log("没有选择任何剪辑, 退出程序...\r\n");
|
||||
process.exit();
|
||||
}
|
||||
|
||||
// 解压缩文件
|
||||
console.log("正在还原压缩剪辑记录...\r\n");
|
||||
const frames = clip.frames;
|
||||
|
||||
/**
|
||||
* @type {Map<string, {name: string, type: string, select?: boolean}}
|
||||
*/
|
||||
const objectMapper = new Map();
|
||||
|
||||
const LastFrameData = "@";
|
||||
|
||||
/**
|
||||
* @type {IArchiveClip["frames"]}
|
||||
*/
|
||||
const F = [];
|
||||
frames.forEach((frame) => {
|
||||
/**
|
||||
* @type {IArchiveClip["frames"][number]["commands"]}
|
||||
*/
|
||||
const FCS = [];
|
||||
frame.commands.forEach((command) => {
|
||||
|
||||
// 压缩指令
|
||||
const FC = {
|
||||
id: command.id,
|
||||
type: command.type
|
||||
};
|
||||
|
||||
/**
|
||||
* 上一帧
|
||||
* @type {IArchiveClip["frames"][number]}
|
||||
*/
|
||||
const lastFrame = F[F.length - 1];
|
||||
|
||||
/**
|
||||
* 上一帧指令
|
||||
* @type {IArchiveClip["frames"][number]["commands"][number]}
|
||||
*/
|
||||
const lastCommand = lastFrame?.commands.filter((c) => {
|
||||
if (c.type === command.type && c.id === command.id) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
})[0];
|
||||
|
||||
// 记录
|
||||
FC.name = (LastFrameData === command.name) ? lastCommand?.name : command.name;
|
||||
|
||||
FC.data = (LastFrameData === command.data) ? lastCommand?.data : command.data;
|
||||
|
||||
FC.mapId = (LastFrameData === command.mapId) ? lastCommand?.mapId : command.mapId;
|
||||
|
||||
FC.position = (LastFrameData === command.position) ? lastCommand?.position : command.position;
|
||||
|
||||
FC.radius = (LastFrameData === command.radius) ? lastCommand?.radius : command.radius;
|
||||
|
||||
// 获取 Mapper
|
||||
const mapper = objectMapper.get(FC.id);
|
||||
if (mapper) {
|
||||
mapper.type = FC.type ?? mapper.type;
|
||||
mapper.name = FC.name ?? mapper.name;
|
||||
} else {
|
||||
objectMapper.set(FC.id, {
|
||||
type: FC.type,
|
||||
name: FC.name
|
||||
});
|
||||
}
|
||||
|
||||
FCS.push(FC);
|
||||
});
|
||||
|
||||
F.push({
|
||||
duration: frame.duration,
|
||||
process: frame.process,
|
||||
commands: FCS
|
||||
});
|
||||
});
|
||||
|
||||
console.log("剪辑记录还原成功...\r\n");
|
||||
console.log("剪辑共 " + F.length + " 帧, 对象 " + objectMapper.size + " 个\r\n");
|
||||
if (objectMapper.size) {
|
||||
console.log("剪辑记录中存在以下对象:");
|
||||
} else {
|
||||
console.log("剪辑记录中没有任何对象,退出程序...");
|
||||
process.exit();
|
||||
}
|
||||
let objectMapperForEachIndex = 1;
|
||||
objectMapper.forEach((item, key) => {
|
||||
console.log(" \033[44;30m" + (objectMapperForEachIndex ++) + "\033[40;32m " + item.type + " " + item.name + " [" + key + "]\033[0m");
|
||||
});
|
||||
|
||||
/**
|
||||
* @type {number[]}
|
||||
*/
|
||||
const pointMapper = [];
|
||||
|
||||
/**
|
||||
* @param {number} x
|
||||
* @param {number} y
|
||||
* @param {number} z
|
||||
* @returns {number}
|
||||
*/
|
||||
function getPointID(x, y, z) {
|
||||
let search = -1;
|
||||
let pointMapperLength = (pointMapper.length / 3);
|
||||
// for (let i = 0; i < pointMapperLength; i++) {
|
||||
// if (
|
||||
// pointMapper[i * 3 + 0] === x &&
|
||||
// pointMapper[i * 3 + 1] === y &&
|
||||
// pointMapper[i * 3 + 2] === z
|
||||
// ) {
|
||||
// search = i;
|
||||
// }
|
||||
// }
|
||||
if (search >= 0) {
|
||||
return search;
|
||||
} else {
|
||||
pointMapper.push(x);
|
||||
pointMapper.push(y);
|
||||
pointMapper.push(z);
|
||||
return pointMapperLength + 1;
|
||||
}
|
||||
}
|
||||
|
||||
let frameId = 0;
|
||||
/**
|
||||
* @type {Map<string, {id: number, start: number, last: number, name: string, point: number[]}[]>}
|
||||
*/
|
||||
const objectLineMapper = new Map();
|
||||
/**
|
||||
* @param {string} obj
|
||||
* @param {number} id
|
||||
* @param {number} point
|
||||
*/
|
||||
function recordPoint(obj, id, point) {
|
||||
let searchObj = objectLineMapper.get(obj);
|
||||
if (searchObj) {
|
||||
|
||||
/**
|
||||
* @type {{id: number, start: number, last: number, point: number[]}}
|
||||
*/
|
||||
let search;
|
||||
for (let i = 0; i < searchObj.length; i++) {
|
||||
if (searchObj[i].id === id && searchObj[i].last === (frameId - 1)) {
|
||||
search = searchObj[i];
|
||||
}
|
||||
}
|
||||
|
||||
if (search) {
|
||||
search.point.push(point);
|
||||
search.last = frameId;
|
||||
} else {
|
||||
searchObj.push({
|
||||
id: id,
|
||||
start: frameId,
|
||||
last: frameId,
|
||||
point: [point]
|
||||
});
|
||||
}
|
||||
} else {
|
||||
objectLineMapper.set(obj, [{
|
||||
id: id,
|
||||
start: frameId,
|
||||
last: frameId,
|
||||
point: [point]
|
||||
}]);
|
||||
}
|
||||
}
|
||||
|
||||
console.log("\r\n正在收集多边形数据...\r\n");
|
||||
for (frameId = 0; frameId < F.length; frameId ++) {
|
||||
F[frameId].commands.forEach((command) => {
|
||||
|
||||
if (command.type === "points" && command.mapId && command.data) {
|
||||
command.mapId.forEach((pid, index) => {
|
||||
|
||||
const x = command.data[index * 3 + 0];
|
||||
const y = command.data[index * 3 + 1]
|
||||
const z = command.data[index * 3 + 2]
|
||||
|
||||
if (
|
||||
x !== undefined &&
|
||||
y !== undefined &&
|
||||
z !== undefined
|
||||
) {
|
||||
recordPoint(command.id, pid, getPointID(x, y, z));
|
||||
}
|
||||
})
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
let pointCount = (pointMapper.length / 3);
|
||||
|
||||
console.log("收集点数据 " + pointCount + "个\r\n");
|
||||
console.log("收集样条:");
|
||||
let objectLineMapperIndexPrint = 1;
|
||||
objectLineMapper.forEach((item, key) => {
|
||||
let iName = objectMapper.get(key).name;
|
||||
console.log(" \033[44;30m" + (objectLineMapperIndexPrint ++) + "\033[40;32m " + item.length + " " + iName + " [" + key + "]\033[0m");
|
||||
});
|
||||
|
||||
console.log("\r\n正在生成 .obj 文件...\r\n");
|
||||
|
||||
let fileStr = ""; let fileStrVec = "";
|
||||
|
||||
objectLineMapper.forEach((item, key) => {
|
||||
|
||||
for (let i = 0; i < item.length; i++) {
|
||||
fileStr += "\r\n";
|
||||
fileStr += ("o " + objectMapper.get(key).name + " " + item[i].id + "\r\n");
|
||||
fileStr += "usemtl default\r\n";
|
||||
fileStr += "l ";
|
||||
fileStr += getPointID(
|
||||
item[i].start / (F.length - 1),
|
||||
item[i].last / (F.length - 1),
|
||||
(item[i].last - item[i].start) / (F.length - 1)
|
||||
) + " ";
|
||||
fileStr += item[i].point.join(" ");
|
||||
fileStr += "\r\n";
|
||||
}
|
||||
|
||||
fileStr += "\r\n";
|
||||
});
|
||||
|
||||
pointCount = (pointMapper.length / 3);
|
||||
for (let i = 0; i < pointCount; i++) {
|
||||
fileStrVec += ("v " + pointMapper[i * 3 + 0] + " " + pointMapper[i * 3 + 1] + " " + pointMapper[i * 3 + 2] + "\r\n");
|
||||
}
|
||||
|
||||
const file = "# Create with Living Together (Parse from .ltss file)\r\n\r\n" + fileStrVec + fileStr;
|
||||
|
||||
console.log("正在生成保存文件...\r\n");
|
||||
FS.writeFileSync(outputFilePath, file);
|
||||
console.log("成功\r\n");
|
||||
Generated
+14
@@ -19,6 +19,7 @@
|
||||
"react-dnd": "^16.0.1",
|
||||
"react-dnd-html5-backend": "^16.0.1",
|
||||
"react-dom": "^17.0.2",
|
||||
"readline-sync": "^1.4.10",
|
||||
"uuid": "^8.3.2"
|
||||
},
|
||||
"devDependencies": {
|
||||
@@ -6367,6 +6368,14 @@
|
||||
"node": ">=8.10.0"
|
||||
}
|
||||
},
|
||||
"node_modules/readline-sync": {
|
||||
"version": "1.4.10",
|
||||
"resolved": "https://registry.npmmirror.com/readline-sync/-/readline-sync-1.4.10.tgz",
|
||||
"integrity": "sha512-gNva8/6UAe8QYepIQH/jQ2qn91Qj0B9sYjMBBs3QOB8F2CXcKgLxQaJRP76sWVRQt+QU+8fAkCbCvjjMFu7Ycw==",
|
||||
"engines": {
|
||||
"node": ">= 0.8.0"
|
||||
}
|
||||
},
|
||||
"node_modules/rechoir": {
|
||||
"version": "0.7.1",
|
||||
"resolved": "https://registry.npmjs.org/rechoir/-/rechoir-0.7.1.tgz",
|
||||
@@ -13309,6 +13318,11 @@
|
||||
"picomatch": "^2.2.1"
|
||||
}
|
||||
},
|
||||
"readline-sync": {
|
||||
"version": "1.4.10",
|
||||
"resolved": "https://registry.npmmirror.com/readline-sync/-/readline-sync-1.4.10.tgz",
|
||||
"integrity": "sha512-gNva8/6UAe8QYepIQH/jQ2qn91Qj0B9sYjMBBs3QOB8F2CXcKgLxQaJRP76sWVRQt+QU+8fAkCbCvjjMFu7Ycw=="
|
||||
},
|
||||
"rechoir": {
|
||||
"version": "0.7.1",
|
||||
"resolved": "https://registry.npmjs.org/rechoir/-/rechoir-0.7.1.tgz",
|
||||
|
||||
@@ -79,6 +79,7 @@
|
||||
"react-dnd": "^16.0.1",
|
||||
"react-dnd-html5-backend": "^16.0.1",
|
||||
"react-dom": "^17.0.2",
|
||||
"readline-sync": "^1.4.10",
|
||||
"uuid": "^8.3.2"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,86 @@
|
||||
import { Behavior } from "@Model/Behavior";
|
||||
import { Group } from "@Model/Group";
|
||||
import { Individual } from "@Model/Individual";
|
||||
import { Model } from "@Model/Model";
|
||||
|
||||
type IAvoidanceBehaviorParameter = {
|
||||
avoid: "CLG",
|
||||
strength: "number",
|
||||
range: "number"
|
||||
}
|
||||
|
||||
type IAvoidanceBehaviorEvent = {}
|
||||
|
||||
class Avoidance extends Behavior<IAvoidanceBehaviorParameter, IAvoidanceBehaviorEvent> {
|
||||
|
||||
public override behaviorId: string = "Avoidance";
|
||||
|
||||
public override behaviorName: string = "$Title";
|
||||
|
||||
public override iconName: string = "FastMode";
|
||||
|
||||
public override describe: string = "$Intro";
|
||||
|
||||
public override category: string = "$Interactive";
|
||||
|
||||
public override parameterOption = {
|
||||
avoid: { name: "$Avoid", type: "CLG" },
|
||||
strength: { type: "number", name: "$Strength", defaultValue: 1, numberMin: 0, numberStep: .1 },
|
||||
range: { type: "number", name: "$Range", defaultValue: 4, numberMin: 0, numberStep: .1 }
|
||||
};
|
||||
|
||||
public effect = (individual: Individual, group: Group, model: Model, t: number): void => {
|
||||
|
||||
let currentDistant: number = Infinity;
|
||||
let avoidTarget = undefined as Individual | undefined;
|
||||
|
||||
for (let i = 0; i < this.parameter.avoid.objects.length; i++) {
|
||||
const targetGroup = this.parameter.avoid.objects[i];
|
||||
|
||||
targetGroup.individuals.forEach((targetIndividual) => {
|
||||
|
||||
// 排除自己
|
||||
if (targetIndividual === individual) return;
|
||||
let dis = targetIndividual.distanceTo(individual);
|
||||
|
||||
if (dis < currentDistant && dis <= this.parameter.range) {
|
||||
avoidTarget = targetIndividual;
|
||||
currentDistant = dis;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
if (avoidTarget && currentDistant !== Infinity) {
|
||||
individual.applyForce(
|
||||
(individual.position[0] - avoidTarget.position[0]) * this.parameter.strength / currentDistant,
|
||||
(individual.position[1] - avoidTarget.position[1]) * this.parameter.strength / currentDistant,
|
||||
(individual.position[2] - avoidTarget.position[2]) * this.parameter.strength / currentDistant
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
public override terms: Record<string, Record<string, string>> = {
|
||||
"$Title": {
|
||||
"ZH_CN": "躲避",
|
||||
"EN_US": "Avoidance"
|
||||
},
|
||||
"$Intro": {
|
||||
"ZH_CN": "远离视野范围内最近的躲避目标",
|
||||
"EN_US": "Stay away from the nearest evasive target in the field of vision"
|
||||
},
|
||||
"$Avoid": {
|
||||
"ZH_CN": "躲避对象",
|
||||
"EN_US": "Avoid object"
|
||||
},
|
||||
"$Strength": {
|
||||
"ZH_CN": "躲避强度",
|
||||
"EN_US": "Avoidance intensity"
|
||||
},
|
||||
"$Range": {
|
||||
"ZH_CN": "视野范围",
|
||||
"EN_US": "Field of vision"
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
export { Avoidance };
|
||||
@@ -7,6 +7,12 @@ import { Tracking } from "@Behavior/Tracking";
|
||||
import { ContactAttacking } from "@Behavior/ContactAttacking";
|
||||
import { ContactAssimilate } from "@Behavior/ContactAssimilate";
|
||||
import { DelayAssimilate } from "@Behavior/DelayAssimilate";
|
||||
import { Avoidance } from "@Behavior/Avoidance";
|
||||
import { DirectionCluster } from "@Behavior/DirectionCluster";
|
||||
import { CentralCluster } from "@Behavior/CentralCluster";
|
||||
import { Manufacture } from "@Behavior/Manufacture";
|
||||
import { Wastage } from "@Behavior/Wastage";
|
||||
import { SampleTracking } from "@Behavior/SampleTracking";
|
||||
|
||||
const AllBehaviors: IAnyBehaviorRecorder[] = [
|
||||
new BehaviorRecorder(Template),
|
||||
@@ -17,6 +23,12 @@ const AllBehaviors: IAnyBehaviorRecorder[] = [
|
||||
new BehaviorRecorder(ContactAttacking),
|
||||
new BehaviorRecorder(ContactAssimilate),
|
||||
new BehaviorRecorder(DelayAssimilate),
|
||||
new BehaviorRecorder(Avoidance),
|
||||
new BehaviorRecorder(DirectionCluster),
|
||||
new BehaviorRecorder(CentralCluster),
|
||||
new BehaviorRecorder(Manufacture),
|
||||
new BehaviorRecorder(Wastage),
|
||||
new BehaviorRecorder(SampleTracking),
|
||||
]
|
||||
|
||||
/**
|
||||
|
||||
@@ -48,11 +48,17 @@ class BoundaryConstraint extends Behavior<IBoundaryConstraintBehaviorParameter,
|
||||
|
||||
if (ox || oy || oz) {
|
||||
|
||||
let currentFLen = individual.vectorLength(rx, ry, rz);
|
||||
const backFocus: number[] = [0, 0, 0];
|
||||
|
||||
if (ox) backFocus[0] = rx - rx * rangeList[i].radius[0] / Math.abs(rx);
|
||||
if (oy) backFocus[1] = ry - ry * rangeList[i].radius[1] / Math.abs(ry);
|
||||
if (oz) backFocus[2] = rz - rz * rangeList[i].radius[2] / Math.abs(rz);
|
||||
|
||||
let currentFLen = individual.vectorLength(backFocus);
|
||||
if (currentFLen < fLen) {
|
||||
fx = rx;
|
||||
fy = ry;
|
||||
fz = rz;
|
||||
fx = backFocus[0];
|
||||
fy = backFocus[1];
|
||||
fz = backFocus[2];
|
||||
fLen = currentFLen;
|
||||
}
|
||||
|
||||
|
||||
+133
-6
@@ -7,7 +7,9 @@ type IBrownianBehaviorParameter = {
|
||||
maxFrequency: "number",
|
||||
minFrequency: "number",
|
||||
maxStrength: "number",
|
||||
minStrength: "number"
|
||||
minStrength: "number",
|
||||
dirLimit: "boolean",
|
||||
angle: "number"
|
||||
}
|
||||
|
||||
type IBrownianBehaviorEvent = {}
|
||||
@@ -28,9 +30,88 @@ class Brownian extends Behavior<IBrownianBehaviorParameter, IBrownianBehaviorEve
|
||||
maxFrequency: { type: "number", name: "$Max.Frequency", defaultValue: 5, numberStep: .1, numberMin: 0 },
|
||||
minFrequency: { type: "number", name: "$Min.Frequency", defaultValue: 0, numberStep: .1, numberMin: 0 },
|
||||
maxStrength: { type: "number", name: "$Max.Strength", defaultValue: 10, numberStep: .01, numberMin: 0 },
|
||||
minStrength: { type: "number", name: "$Min.Strength", defaultValue: 0, numberStep: .01, numberMin: 0 }
|
||||
minStrength: { type: "number", name: "$Min.Strength", defaultValue: 0, numberStep: .01, numberMin: 0 },
|
||||
dirLimit: { type: "boolean", name: "$Direction.Limit", defaultValue: false },
|
||||
angle: {
|
||||
type: "number", name: "$Angle", defaultValue: 180, numberStep: 5,
|
||||
numberMin: 0, numberMax: 360, condition: { key: "dirLimit", value: true }
|
||||
}
|
||||
};
|
||||
|
||||
private randomFocus360(): number[] {
|
||||
let randomVec = [
|
||||
Math.random() * 2 - 1,
|
||||
Math.random() * 2 - 1,
|
||||
Math.random() * 2 - 1
|
||||
];
|
||||
|
||||
let randomVecLen = (randomVec[0] ** 2 + randomVec[1] ** 2 + randomVec[2] ** 2) ** 0.5;
|
||||
return [randomVec[0] / randomVecLen, randomVec[1] / randomVecLen, randomVec[2] / randomVecLen];
|
||||
}
|
||||
|
||||
private rotateWithVec(vec: number[], r: number[], ang: number) {
|
||||
|
||||
const cos = Math.cos(ang); const sin = Math.sin(ang);
|
||||
const a1 = r[0] ?? 0; const a2 = r[1] ?? 0; const a3 = r[2] ?? 0;
|
||||
|
||||
return [
|
||||
(cos + (1 - cos) * a1 * a1) * (vec[0] ?? 0) +
|
||||
((1 - cos) * a1 * a2 - sin * a3) * (vec[1] ?? 0) +
|
||||
((1 - cos) * a1 * a3 + sin * a2) * (vec[2] ?? 0),
|
||||
|
||||
((1 - cos) * a1 * a2 + sin * a3) * (vec[0] ?? 0) +
|
||||
(cos + (1 - cos) * a2 * a2) * (vec[1] ?? 0) +
|
||||
((1 - cos) * a2 * a3 - sin * a1) * (vec[2] ?? 0),
|
||||
|
||||
((1 - cos) * a1 * a3 - sin * a2) * (vec[0] ?? 0) +
|
||||
((1 - cos) * a2 * a3 + sin * a1) * (vec[1] ?? 0) +
|
||||
(cos + (1 - cos) * a3 * a3) * (vec[2] ?? 0)
|
||||
]
|
||||
}
|
||||
private angle2Vector(v1: number[], v2: number[]): number {
|
||||
return Math.acos(
|
||||
(v1[0] * v2[0] + v1[1] * v2[1] + v1[2] * v2[2]) /
|
||||
(
|
||||
(v1[0] ** 2 + v1[1] ** 2 + v1[2] ** 2) ** 0.5 *
|
||||
(v2[0] ** 2 + v2[1] ** 2 + v2[2] ** 2) ** 0.5
|
||||
)
|
||||
) * 180 / Math.PI;
|
||||
}
|
||||
|
||||
private randomFocusRange(dir: number[], angle: number): number[] {
|
||||
|
||||
// 计算 X-Z 投影
|
||||
let pxz = [dir[0] ?? 0, 0, dir[2] ?? 0];
|
||||
|
||||
// 通过叉乘计算垂直向量
|
||||
let dxz: number[];
|
||||
|
||||
// 如果投影向量没有长度,使用单位向量
|
||||
if (pxz[0] ** 2 + pxz[2] ** 2 === 0) {
|
||||
dxz = [0, 0, 1];
|
||||
}
|
||||
|
||||
// 通过叉乘计算垂直轴线
|
||||
else {
|
||||
dxz = [
|
||||
dir[1] * pxz[2] - pxz[1] * dir[2],
|
||||
dir[2] * pxz[0] - pxz[2] * dir[0],
|
||||
dir[0] * pxz[1] - pxz[0] * dir[1]
|
||||
];
|
||||
|
||||
let lenDxz = (dxz[0] ** 2 + dxz[1] ** 2 + dxz[2] ** 2) ** 0.5;
|
||||
dxz = [dxz[0] / lenDxz, dxz[1] / lenDxz, dxz[2] / lenDxz];
|
||||
}
|
||||
|
||||
// 航偏角 360 随机旋转
|
||||
let randomH = this.rotateWithVec(dxz, dir, Math.random() * Math.PI * 2);
|
||||
|
||||
// 俯仰角 180 * R 随机旋转
|
||||
let randomP = this.rotateWithVec(dir, randomH, (Math.random() - 0.5) * 2 * angle * Math.PI / 180);
|
||||
|
||||
return randomP;
|
||||
}
|
||||
|
||||
public effect = (individual: Individual, group: Group, model: Model, t: number): void => {
|
||||
|
||||
const {maxFrequency, minFrequency, maxStrength, minStrength} = this.parameter;
|
||||
@@ -41,11 +122,49 @@ class Brownian extends Behavior<IBrownianBehaviorParameter, IBrownianBehaviorEve
|
||||
|
||||
currentTime += t;
|
||||
if (currentTime > nextTime) {
|
||||
|
||||
let randomDir: number[];
|
||||
|
||||
// 开启角度限制
|
||||
if (this.parameter.dirLimit) {
|
||||
|
||||
// 计算当前速度大小
|
||||
const vLen = individual.vectorLength(individual.velocity);
|
||||
|
||||
// 随机旋转算法
|
||||
if (vLen > 0) {
|
||||
randomDir = this.randomFocusRange(
|
||||
[
|
||||
individual.velocity[0] / vLen,
|
||||
individual.velocity[1] / vLen,
|
||||
individual.velocity[2] / vLen
|
||||
],
|
||||
this.parameter.angle / 2
|
||||
);
|
||||
|
||||
if (isNaN(randomDir[0]) || isNaN(randomDir[1]) || isNaN(randomDir[2])) {
|
||||
randomDir = this.randomFocus360()
|
||||
}
|
||||
}
|
||||
|
||||
else {
|
||||
randomDir = this.randomFocus360()
|
||||
}
|
||||
}
|
||||
|
||||
// 随机生成算法
|
||||
else {
|
||||
randomDir = this.randomFocus360()
|
||||
}
|
||||
|
||||
const randomLength = minStrength + Math.random() * (maxStrength - minStrength);
|
||||
|
||||
individual.applyForce(
|
||||
minStrength + (Math.random() * 2 - 1) * (maxStrength - minStrength),
|
||||
minStrength + (Math.random() * 2 - 1) * (maxStrength - minStrength),
|
||||
minStrength + (Math.random() * 2 - 1) * (maxStrength - minStrength)
|
||||
randomDir[0] * randomLength,
|
||||
randomDir[1] * randomLength,
|
||||
randomDir[2] * randomLength
|
||||
);
|
||||
|
||||
nextTime = minFrequency + Math.random() * (maxFrequency - minFrequency);
|
||||
currentTime = 0;
|
||||
}
|
||||
@@ -78,7 +197,15 @@ class Brownian extends Behavior<IBrownianBehaviorParameter, IBrownianBehaviorEve
|
||||
"$Min.Strength": {
|
||||
"ZH_CN": "最小强度",
|
||||
"EN_US": "Minimum strength"
|
||||
}
|
||||
},
|
||||
"$Direction.Limit": {
|
||||
"ZH_CN": "开启角度限制",
|
||||
"EN_US": "Enable limit angle"
|
||||
},
|
||||
"$Angle": {
|
||||
"ZH_CN": "限制立体角 (deg)",
|
||||
"EN_US": "Restricted solid angle (deg)"
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,96 @@
|
||||
import { Behavior } from "@Model/Behavior";
|
||||
import { Group } from "@Model/Group";
|
||||
import { Individual } from "@Model/Individual";
|
||||
import { Model } from "@Model/Model";
|
||||
|
||||
type ICentralClusterBehaviorParameter = {
|
||||
cluster: "CLG",
|
||||
strength: "number",
|
||||
range: "number"
|
||||
}
|
||||
|
||||
type ICentralClusterBehaviorEvent = {}
|
||||
|
||||
class CentralCluster extends Behavior<ICentralClusterBehaviorParameter, ICentralClusterBehaviorEvent> {
|
||||
|
||||
public override behaviorId: string = "CentralCluster";
|
||||
|
||||
public override behaviorName: string = "$Title";
|
||||
|
||||
public override iconName: string = "ZoomToFit";
|
||||
|
||||
public override describe: string = "$Intro";
|
||||
|
||||
public override category: string = "$Interactive";
|
||||
|
||||
public override parameterOption = {
|
||||
cluster: { name: "$Cluster", type: "CLG" },
|
||||
strength: { type: "number", name: "$Strength", defaultValue: 1, numberMin: 0, numberStep: .1 },
|
||||
range: { type: "number", name: "$Range", defaultValue: 4, numberMin: 0, numberStep: .1 }
|
||||
};
|
||||
|
||||
public effect = (individual: Individual, group: Group, model: Model, t: number): void => {
|
||||
|
||||
let findCount = 0;
|
||||
let centerPos: number[] = [0, 0, 0];
|
||||
|
||||
for (let i = 0; i < this.parameter.cluster.objects.length; i++) {
|
||||
const targetGroup = this.parameter.cluster.objects[i];
|
||||
|
||||
targetGroup.individuals.forEach((targetIndividual) => {
|
||||
|
||||
// 排除自己
|
||||
if (targetIndividual === individual) return;
|
||||
let dis = targetIndividual.distanceTo(individual);
|
||||
|
||||
if (dis <= this.parameter.range) {
|
||||
centerPos[0] += targetIndividual.position[0];
|
||||
centerPos[1] += targetIndividual.position[1];
|
||||
centerPos[2] += targetIndividual.position[2];
|
||||
findCount ++;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
if (findCount > 0) {
|
||||
|
||||
let dirX = centerPos[0] / findCount - individual.position[0];
|
||||
let dirY = centerPos[1] / findCount - individual.position[1];
|
||||
let dirZ = centerPos[2] / findCount - individual.position[2];
|
||||
let length = individual.vectorLength(dirX, dirY, dirZ);
|
||||
|
||||
if (length > 0) {
|
||||
individual.applyForce(
|
||||
dirX * this.parameter.strength / length,
|
||||
dirY * this.parameter.strength / length,
|
||||
dirZ * this.parameter.strength / length
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public override terms: Record<string, Record<string, string>> = {
|
||||
"$Title": {
|
||||
"ZH_CN": "中心结群",
|
||||
"EN_US": "Central cluster"
|
||||
},
|
||||
"$Intro": {
|
||||
"ZH_CN": "个体将按照视野范围内目标方向结群对象个体的几何中心移动",
|
||||
"EN_US": "The individual will move according to the geometric center of the grouped object individual in the target direction within the field of view"
|
||||
},
|
||||
"$Cluster": {
|
||||
"ZH_CN": "中心结群对象",
|
||||
"EN_US": "Central clustering object"
|
||||
},
|
||||
"$Strength": {
|
||||
"ZH_CN": "结群强度",
|
||||
"EN_US": "Clustering strength"
|
||||
},
|
||||
"$Range": {
|
||||
"ZH_CN": "视野范围",
|
||||
"EN_US": "Field of vision"
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
export { CentralCluster };
|
||||
@@ -6,6 +6,7 @@ import { Model } from "@Model/Model";
|
||||
type IContactAssimilateBehaviorParameter = {
|
||||
target: "CLG",
|
||||
assimilate: "CG",
|
||||
self: "CG",
|
||||
success: "number",
|
||||
range: "number"
|
||||
}
|
||||
@@ -27,15 +28,13 @@ class ContactAssimilate extends Behavior<IContactAssimilateBehaviorParameter, IC
|
||||
public override parameterOption = {
|
||||
target: { type: "CLG", name: "$Target" },
|
||||
assimilate: { type: "CG", name: "$Assimilate" },
|
||||
self: { type: "CG", name: "$Self" },
|
||||
range: { type: "number", name: "$Range", defaultValue: .05, numberMin: 0, numberStep: .01 },
|
||||
success: { type: "number", name: "$Success", defaultValue: 90, numberMin: 0, numberMax: 100, numberStep: 5 }
|
||||
};
|
||||
|
||||
public effect = (individual: Individual, group: Group, model: Model, t: number): void => {
|
||||
|
||||
let assimilateGroup = this.parameter.assimilate.objects;
|
||||
if (!assimilateGroup) return;
|
||||
|
||||
for (let i = 0; i < this.parameter.target.objects.length; i++) {
|
||||
const targetGroup = this.parameter.target.objects[i];
|
||||
|
||||
@@ -50,7 +49,18 @@ class ContactAssimilate extends Behavior<IContactAssimilateBehaviorParameter, IC
|
||||
|
||||
// 成功判定
|
||||
if (Math.random() * 100 < this.parameter.success) {
|
||||
targetIndividual.transfer(assimilateGroup!);
|
||||
|
||||
// 同化目标
|
||||
let assimilateGroup = this.parameter.assimilate.objects;
|
||||
if (assimilateGroup) {
|
||||
targetIndividual.transfer(assimilateGroup);
|
||||
}
|
||||
|
||||
// 同化目标
|
||||
let selfGroup = this.parameter.self.objects;
|
||||
if (selfGroup) {
|
||||
individual.transfer(selfGroup);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
@@ -70,6 +80,10 @@ class ContactAssimilate extends Behavior<IContactAssimilateBehaviorParameter, IC
|
||||
"ZH_CN": "到哪个群...",
|
||||
"EN_US": "To group..."
|
||||
},
|
||||
"$Self": {
|
||||
"ZH_CN": "自身同化到...",
|
||||
"EN_US": "Self assimilate to..."
|
||||
},
|
||||
"$Range": {
|
||||
"ZH_CN": "同化范围 (m)",
|
||||
"EN_US": "Assimilate range (m)"
|
||||
|
||||
@@ -6,7 +6,8 @@ import { Model } from "@Model/Model";
|
||||
type IContactAttackingBehaviorParameter = {
|
||||
target: "CLG",
|
||||
success: "number",
|
||||
range: "number"
|
||||
range: "number",
|
||||
assimilate: "CG",
|
||||
}
|
||||
|
||||
type IContactAttackingBehaviorEvent = {}
|
||||
@@ -26,7 +27,8 @@ class ContactAttacking extends Behavior<IContactAttackingBehaviorParameter, ICon
|
||||
public override parameterOption = {
|
||||
target: { type: "CLG", name: "$Target" },
|
||||
range: { type: "number", name: "$Range", defaultValue: .05, numberMin: 0, numberStep: .01 },
|
||||
success: { type: "number", name: "$Success", defaultValue: 90, numberMin: 0, numberMax: 100, numberStep: 5 }
|
||||
success: { type: "number", name: "$Success", defaultValue: 90, numberMin: 0, numberMax: 100, numberStep: 5 },
|
||||
assimilate: { type: "CG", name: "$Assimilate"}
|
||||
};
|
||||
|
||||
public effect = (individual: Individual, group: Group, model: Model, t: number): void => {
|
||||
@@ -46,6 +48,10 @@ class ContactAttacking extends Behavior<IContactAttackingBehaviorParameter, ICon
|
||||
// 成功判定
|
||||
if (Math.random() * 100 < this.parameter.success) {
|
||||
targetIndividual.die();
|
||||
|
||||
if (this.parameter.assimilate?.objects) {
|
||||
individual.transfer(this.parameter.assimilate.objects);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
@@ -72,6 +78,10 @@ class ContactAttacking extends Behavior<IContactAttackingBehaviorParameter, ICon
|
||||
"$Intro": {
|
||||
"ZH_CN": "攻击进入共进范围的目标群个体",
|
||||
"EN_US": "Attack the target group and individual entering the range"
|
||||
},
|
||||
"$Assimilate": {
|
||||
"ZH_CN": "同化",
|
||||
"EN_US": "Assimilate"
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@@ -0,0 +1,93 @@
|
||||
import { Behavior } from "@Model/Behavior";
|
||||
import { Group } from "@Model/Group";
|
||||
import { Individual } from "@Model/Individual";
|
||||
import { Model } from "@Model/Model";
|
||||
|
||||
type IDirectionClusterBehaviorParameter = {
|
||||
cluster: "CLG",
|
||||
strength: "number",
|
||||
range: "number"
|
||||
}
|
||||
|
||||
type IDirectionClusterBehaviorEvent = {}
|
||||
|
||||
class DirectionCluster extends Behavior<IDirectionClusterBehaviorParameter, IDirectionClusterBehaviorEvent> {
|
||||
|
||||
public override behaviorId: string = "DirectionCluster";
|
||||
|
||||
public override behaviorName: string = "$Title";
|
||||
|
||||
public override iconName: string = "RawSource";
|
||||
|
||||
public override describe: string = "$Intro";
|
||||
|
||||
public override category: string = "$Interactive";
|
||||
|
||||
public override parameterOption = {
|
||||
cluster: { name: "$Cluster", type: "CLG" },
|
||||
strength: { type: "number", name: "$Strength", defaultValue: 1, numberMin: 0, numberStep: .1 },
|
||||
range: { type: "number", name: "$Range", defaultValue: 4, numberMin: 0, numberStep: .1 }
|
||||
};
|
||||
|
||||
public effect = (individual: Individual, group: Group, model: Model, t: number): void => {
|
||||
|
||||
let findCount = 0;
|
||||
let centerDir: number[] = [0, 0, 0];
|
||||
|
||||
for (let i = 0; i < this.parameter.cluster.objects.length; i++) {
|
||||
const targetGroup = this.parameter.cluster.objects[i];
|
||||
|
||||
targetGroup.individuals.forEach((targetIndividual) => {
|
||||
|
||||
// 排除自己
|
||||
if (targetIndividual === individual) return;
|
||||
let dis = targetIndividual.distanceTo(individual);
|
||||
|
||||
if (dis <= this.parameter.range) {
|
||||
centerDir[0] += targetIndividual.velocity[0];
|
||||
centerDir[1] += targetIndividual.velocity[1];
|
||||
centerDir[2] += targetIndividual.velocity[2];
|
||||
findCount ++;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
if (findCount > 0) {
|
||||
|
||||
let length = individual.vectorLength(centerDir);
|
||||
|
||||
if (length) {
|
||||
individual.applyForce(
|
||||
centerDir[0] * this.parameter.strength / length,
|
||||
centerDir[1] * this.parameter.strength / length,
|
||||
centerDir[2] * this.parameter.strength / length
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public override terms: Record<string, Record<string, string>> = {
|
||||
"$Title": {
|
||||
"ZH_CN": "方向结群",
|
||||
"EN_US": "Directional clustering"
|
||||
},
|
||||
"$Intro": {
|
||||
"ZH_CN": "个体将按照视野范围内目标方向结群对象个体的平均移动方向移动",
|
||||
"EN_US": "Individuals will move according to the average moving direction of the grouped object individuals in the target direction within the field of vision"
|
||||
},
|
||||
"$Cluster": {
|
||||
"ZH_CN": "方向结群对象",
|
||||
"EN_US": "Directional clustering object"
|
||||
},
|
||||
"$Strength": {
|
||||
"ZH_CN": "结群强度",
|
||||
"EN_US": "Clustering strength"
|
||||
},
|
||||
"$Range": {
|
||||
"ZH_CN": "视野范围",
|
||||
"EN_US": "Field of vision"
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
export { DirectionCluster };
|
||||
@@ -0,0 +1,83 @@
|
||||
import { Behavior } from "@Model/Behavior";
|
||||
import { Group } from "@Model/Group";
|
||||
import { Individual } from "@Model/Individual";
|
||||
import { Model } from "@Model/Model";
|
||||
|
||||
type IManufactureBehaviorParameter = {
|
||||
maxFrequency: "number",
|
||||
minFrequency: "number",
|
||||
genTarget: "CG",
|
||||
}
|
||||
|
||||
type IManufactureBehaviorEvent = {}
|
||||
|
||||
class Manufacture extends Behavior<IManufactureBehaviorParameter, IManufactureBehaviorEvent> {
|
||||
|
||||
public override behaviorId: string = "Manufacture";
|
||||
|
||||
public override behaviorName: string = "$Title";
|
||||
|
||||
public override iconName: string = "ProductionFloorManagement";
|
||||
|
||||
public override describe: string = "$Intro";
|
||||
|
||||
public override category: string = "$Initiative";
|
||||
|
||||
public override parameterOption = {
|
||||
genTarget: { type: "CG", name: "$Gen.Target" },
|
||||
maxFrequency: { type: "number", name: "$Max.Frequency", defaultValue: 5, numberStep: .1, numberMin: 0 },
|
||||
minFrequency: { type: "number", name: "$Min.Frequency", defaultValue: 0, numberStep: .1, numberMin: 0 }
|
||||
};
|
||||
|
||||
public effect = (individual: Individual, group: Group, model: Model, t: number): void => {
|
||||
|
||||
const {genTarget, maxFrequency, minFrequency} = this.parameter;
|
||||
|
||||
if (genTarget.objects) {
|
||||
|
||||
let nextTime = individual.getData("Manufacture.nextTime") ??
|
||||
minFrequency + Math.random() * (maxFrequency - minFrequency);
|
||||
let currentTime = individual.getData("Manufacture.currentTime") ?? 0;
|
||||
|
||||
if (currentTime > nextTime) {
|
||||
|
||||
// 生成个体
|
||||
let newIndividual = genTarget.objects.new(1);
|
||||
newIndividual.position = individual.position.concat([]);
|
||||
|
||||
nextTime = minFrequency + Math.random() * (maxFrequency - minFrequency);
|
||||
currentTime = 0;
|
||||
}
|
||||
|
||||
currentTime += t;
|
||||
|
||||
individual.setData("Manufacture.nextTime", nextTime);
|
||||
individual.setData("Manufacture.currentTime", currentTime);
|
||||
}
|
||||
}
|
||||
|
||||
public override terms: Record<string, Record<string, string>> = {
|
||||
"$Title": {
|
||||
"ZH_CN": "生产",
|
||||
"EN_US": "Manufacture"
|
||||
},
|
||||
"$Intro": {
|
||||
"ZH_CN": "在指定的群创造新的个体",
|
||||
"EN_US": "Create new individuals in a given group"
|
||||
},
|
||||
"$Gen.Target": {
|
||||
"ZH_CN": "目标群",
|
||||
"EN_US": "Target group"
|
||||
},
|
||||
"$Max.Frequency": {
|
||||
"ZH_CN": "最大频率",
|
||||
"EN_US": "Maximum frequency"
|
||||
},
|
||||
"$Min.Frequency": {
|
||||
"ZH_CN": "最小频率",
|
||||
"EN_US": "Minimum frequency"
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
export { Manufacture };
|
||||
@@ -31,11 +31,11 @@ class PhysicsDynamics extends Behavior<IPhysicsDynamicsBehaviorParameter, IPhysi
|
||||
limit: { name: "$Limit", type: "boolean", defaultValue: true },
|
||||
maxAcceleration: {
|
||||
name: "$Max.Acceleration", type: "number", defaultValue: 6.25,
|
||||
numberStep: .1, numberMin: 0, condition: { key: "limit", value: true }
|
||||
numberStep: .1, numberMin: 0.0001, condition: { key: "limit", value: true }
|
||||
},
|
||||
maxVelocity: {
|
||||
name: "$Max.Velocity", type: "number", defaultValue: 12.5,
|
||||
numberStep: .1, numberMin: 0, condition: { key: "limit", value: true }
|
||||
numberStep: .1, numberMin: 0.0001, condition: { key: "limit", value: true }
|
||||
},
|
||||
};
|
||||
|
||||
@@ -64,7 +64,7 @@ class PhysicsDynamics extends Behavior<IPhysicsDynamicsBehaviorParameter, IPhysi
|
||||
// 加速度约束
|
||||
if (this.parameter.limit) {
|
||||
const lengthA = individual.vectorLength(individual.acceleration);
|
||||
if (lengthA > this.parameter.maxAcceleration) {
|
||||
if (lengthA > this.parameter.maxAcceleration && lengthA) {
|
||||
individual.acceleration[0] = individual.acceleration[0] * this.parameter.maxAcceleration / lengthA;
|
||||
individual.acceleration[1] = individual.acceleration[1] * this.parameter.maxAcceleration / lengthA;
|
||||
individual.acceleration[2] = individual.acceleration[2] * this.parameter.maxAcceleration / lengthA;
|
||||
@@ -79,7 +79,7 @@ class PhysicsDynamics extends Behavior<IPhysicsDynamicsBehaviorParameter, IPhysi
|
||||
// 速度约束
|
||||
if (this.parameter.limit) {
|
||||
const lengthV = individual.vectorLength(individual.velocity);
|
||||
if (lengthV > this.parameter.maxVelocity) {
|
||||
if (lengthV > this.parameter.maxVelocity && lengthV) {
|
||||
individual.velocity[0] = individual.velocity[0] * this.parameter.maxVelocity / lengthV;
|
||||
individual.velocity[1] = individual.velocity[1] * this.parameter.maxVelocity / lengthV;
|
||||
individual.velocity[2] = individual.velocity[2] * this.parameter.maxVelocity / lengthV;
|
||||
|
||||
@@ -0,0 +1,160 @@
|
||||
import { Behavior } from "@Model/Behavior";
|
||||
import { Group } from "@Model/Group";
|
||||
import { Individual } from "@Model/Individual";
|
||||
import { Model } from "@Model/Model";
|
||||
|
||||
type ISampleTrackingBehaviorParameter = {
|
||||
target: "CLG",
|
||||
key: "string",
|
||||
strength: "number",
|
||||
range: "number",
|
||||
angle: "number",
|
||||
accuracy: "number"
|
||||
}
|
||||
|
||||
type ISampleTrackingBehaviorEvent = {}
|
||||
|
||||
class SampleTracking extends Behavior<ISampleTrackingBehaviorParameter, ISampleTrackingBehaviorEvent> {
|
||||
|
||||
public override behaviorId: string = "SampleTracking";
|
||||
|
||||
public override behaviorName: string = "$Title";
|
||||
|
||||
public override iconName: string = "Video360Generic";
|
||||
|
||||
public override describe: string = "$Intro";
|
||||
|
||||
public override category: string = "$Initiative";
|
||||
|
||||
public override parameterOption = {
|
||||
target: { type: "CLG", name: "$Target" },
|
||||
key: { type: "string", name: "$Key"},
|
||||
range: { type: "number", name: "$Range", defaultValue: 4, numberMin: 0, numberStep: .1 },
|
||||
angle: { type: "number", name: "$Angle", defaultValue: 180, numberMin: 0, numberMax: 360, numberStep: 5 },
|
||||
strength: { type: "number", name: "$Strength", defaultValue: 1, numberMin: 0, numberStep: .1 },
|
||||
accuracy: { type: "number", name: "$Accuracy", defaultValue: 5, numberMin: 0, numberMax: 180, numberStep: 1 }
|
||||
};
|
||||
|
||||
private angle2Vector(v1: number[], v2: number[]): number {
|
||||
return Math.acos(
|
||||
(v1[0] * v2[0] + v1[1] * v2[1] + v1[2] * v2[2]) /
|
||||
(
|
||||
(v1[0] ** 2 + v1[1] ** 2 + v1[2] ** 2) ** 0.5 *
|
||||
(v2[0] ** 2 + v2[1] ** 2 + v2[2] ** 2) ** 0.5
|
||||
)
|
||||
) * 180 / Math.PI;
|
||||
}
|
||||
|
||||
public effect = (individual: Individual, group: Group, model: Model, t: number): void => {
|
||||
|
||||
const dirArr: number[][] = []; const valArr: number[] = [];
|
||||
|
||||
for (let i = 0; i < this.parameter.target.objects.length; i++) {
|
||||
const targetGroup = this.parameter.target.objects[i];
|
||||
|
||||
targetGroup.individuals.forEach((targetIndividual) => {
|
||||
|
||||
// 计算距离
|
||||
let dis = targetIndividual.distanceTo(individual);
|
||||
if (dis > this.parameter.range) return;
|
||||
|
||||
// 计算方向
|
||||
let targetDir = [
|
||||
targetIndividual.position[0] - individual.position[0],
|
||||
targetIndividual.position[1] - individual.position[1],
|
||||
targetIndividual.position[2] - individual.position[2]
|
||||
];
|
||||
|
||||
// 计算视线角度
|
||||
let angle = this.angle2Vector(individual.velocity, targetDir);
|
||||
|
||||
// 在可视角度内
|
||||
if (angle < (this.parameter.angle ?? 360) / 2) {
|
||||
|
||||
// 采样
|
||||
let isFindNest = false;
|
||||
for (let i = 0; i < valArr.length; i++) {
|
||||
|
||||
// 计算采样角度
|
||||
let sampleAngle = this.angle2Vector(dirArr[i], targetDir);
|
||||
|
||||
// 小于采样精度,合并
|
||||
if (sampleAngle < this.parameter.accuracy ?? 5) {
|
||||
dirArr[i][0] += targetDir[0];
|
||||
dirArr[i][1] += targetDir[1];
|
||||
dirArr[i][2] += targetDir[2];
|
||||
valArr[i] += targetIndividual.getData(this.parameter.key) ?? 0;
|
||||
isFindNest = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (!isFindNest) {
|
||||
|
||||
// 保存
|
||||
dirArr.push(targetDir);
|
||||
valArr.push(targetIndividual.getData(this.parameter.key) ?? 0);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// 计算最大方向
|
||||
let maxVal = -1; let maxDir: number[] | undefined;
|
||||
for (let i = 0; i < valArr.length; i++) {
|
||||
if (valArr[i] > maxVal) {
|
||||
maxVal = valArr[i];
|
||||
maxDir = dirArr[i];
|
||||
}
|
||||
}
|
||||
|
||||
if (maxDir) {
|
||||
const dir = individual.vectorNormalize(maxDir);
|
||||
individual.applyForce(
|
||||
dir[0] * this.parameter.strength,
|
||||
dir[1] * this.parameter.strength,
|
||||
dir[2] * this.parameter.strength
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
public override terms: Record<string, Record<string, string>> = {
|
||||
"$Title": {
|
||||
"ZH_CN": "采样追踪",
|
||||
"EN_US": "Sample tracking"
|
||||
},
|
||||
"$Target": {
|
||||
"ZH_CN": "追踪目标",
|
||||
"EN_US": "Tracking target"
|
||||
},
|
||||
"$Key": {
|
||||
"ZH_CN": "计算键值",
|
||||
"EN_US": "Calculate key value"
|
||||
},
|
||||
"$Accuracy": {
|
||||
"ZH_CN": "采样精度",
|
||||
"EN_US": "Sampling accuracy"
|
||||
},
|
||||
"$Range": {
|
||||
"ZH_CN": "追踪范围 (m)",
|
||||
"EN_US": "Tracking range (m)"
|
||||
},
|
||||
"$Strength": {
|
||||
"ZH_CN": "追踪强度系数",
|
||||
"EN_US": "Tracking intensity coefficient"
|
||||
},
|
||||
"$Intro": {
|
||||
"ZH_CN": "个体将主动向目标个体较多的方向发起追踪",
|
||||
"EN_US": "Individuals will actively initiate tracking in the direction of more target individuals"
|
||||
},
|
||||
"$Interactive": {
|
||||
"ZH_CN": "交互",
|
||||
"EN_US": "Interactive"
|
||||
},
|
||||
"$Angle": {
|
||||
"ZH_CN": "可视角度",
|
||||
"EN_US": "Viewing angle"
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
export { SampleTracking };
|
||||
@@ -7,6 +7,7 @@ type ITrackingBehaviorParameter = {
|
||||
target: "CLG",
|
||||
strength: "number",
|
||||
range: "number",
|
||||
angle: "number",
|
||||
lock: "boolean"
|
||||
}
|
||||
|
||||
@@ -28,12 +29,23 @@ class Tracking extends Behavior<ITrackingBehaviorParameter, ITrackingBehaviorEve
|
||||
target: { type: "CLG", name: "$Target" },
|
||||
lock: { type: "boolean", name: "$Lock", defaultValue: false },
|
||||
range: { type: "number", name: "$Range", defaultValue: 4, numberMin: 0, numberStep: .1 },
|
||||
angle: { type: "number", name: "$Angle", defaultValue: 180, numberMin: 0, numberMax: 360, numberStep: 5 },
|
||||
strength: { type: "number", name: "$Strength", defaultValue: 1, numberMin: 0, numberStep: .1 }
|
||||
};
|
||||
|
||||
private target: Individual | undefined = undefined;
|
||||
private currentDistant: number = Infinity;
|
||||
|
||||
private angle2Vector(v1: number[], v2: number[]): number {
|
||||
return Math.acos(
|
||||
(v1[0] * v2[0] + v1[1] * v2[1] + v1[2] * v2[2]) /
|
||||
(
|
||||
(v1[0] ** 2 + v1[1] ** 2 + v1[2] ** 2) ** 0.5 *
|
||||
(v2[0] ** 2 + v2[1] ** 2 + v2[2] ** 2) ** 0.5
|
||||
)
|
||||
) * 180 / Math.PI;
|
||||
}
|
||||
|
||||
private searchTarget(individual: Individual) {
|
||||
|
||||
for (let i = 0; i < this.parameter.target.objects.length; i++) {
|
||||
@@ -46,8 +58,21 @@ class Tracking extends Behavior<ITrackingBehaviorParameter, ITrackingBehaviorEve
|
||||
let dis = targetIndividual.distanceTo(individual);
|
||||
|
||||
if (dis < this.currentDistant && dis <= this.parameter.range) {
|
||||
this.target = targetIndividual;
|
||||
this.currentDistant = dis;
|
||||
|
||||
// 计算目标方位
|
||||
const targetDir = [
|
||||
targetIndividual.position[0] - individual.position[0],
|
||||
targetIndividual.position[1] - individual.position[1],
|
||||
targetIndividual.position[2] - individual.position[2]
|
||||
];
|
||||
|
||||
// 计算角度
|
||||
const angle = this.angle2Vector(individual.velocity, targetDir);
|
||||
|
||||
if (angle < (this.parameter.angle ?? 360) / 2) {
|
||||
this.target = targetIndividual;
|
||||
this.currentDistant = dis;
|
||||
}
|
||||
}
|
||||
|
||||
});
|
||||
@@ -150,7 +175,11 @@ class Tracking extends Behavior<ITrackingBehaviorParameter, ITrackingBehaviorEve
|
||||
"$Interactive": {
|
||||
"ZH_CN": "交互",
|
||||
"EN_US": "Interactive"
|
||||
}
|
||||
},
|
||||
"$Angle": {
|
||||
"ZH_CN": "可视角度",
|
||||
"EN_US": "Viewing angle"
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,100 @@
|
||||
import { Behavior } from "@Model/Behavior";
|
||||
import { Group } from "@Model/Group";
|
||||
import { Individual } from "@Model/Individual";
|
||||
import { Model } from "@Model/Model";
|
||||
|
||||
type IWastageBehaviorParameter = {
|
||||
key: "string",
|
||||
value: "number",
|
||||
speed: "number",
|
||||
threshold: "number",
|
||||
kill: "boolean",
|
||||
assimilate: "CG"
|
||||
}
|
||||
|
||||
type IWastageBehaviorEvent = {}
|
||||
|
||||
class Wastage extends Behavior<IWastageBehaviorParameter, IWastageBehaviorEvent> {
|
||||
|
||||
public override behaviorId: string = "Wastage";
|
||||
|
||||
public override behaviorName: string = "$Title";
|
||||
|
||||
public override iconName: string = "BackgroundColor";
|
||||
|
||||
public override describe: string = "$Intro";
|
||||
|
||||
public override category: string = "$Initiative";
|
||||
|
||||
public override parameterOption = {
|
||||
key: { type: "string", name: "$Key" },
|
||||
value: { type: "number", name: "$Value", defaultValue: 100, numberStep: 1 },
|
||||
speed: { type: "number", name: "$Speed", defaultValue: 1, numberStep: .1 },
|
||||
threshold: { type: "number", name: "$Threshold", defaultValue: 0, numberStep: 1 },
|
||||
kill: { type: "boolean", name: "$Kill", defaultValue: true },
|
||||
assimilate: { type: "CG", name: "$Assimilate", condition: { key: "kill", value: false } }
|
||||
};
|
||||
|
||||
public effect = (individual: Individual, group: Group, model: Model, t: number): void => {
|
||||
const key = this.parameter.key;
|
||||
if (!key) return;
|
||||
|
||||
let currentValue = individual.getData(`Wastage.${key}`) ?? this.parameter.value;
|
||||
currentValue -= this.parameter.speed * t;
|
||||
|
||||
// 超过阈值
|
||||
if (currentValue < this.parameter.threshold) {
|
||||
|
||||
currentValue = undefined;
|
||||
|
||||
// 杀死个体
|
||||
if (this.parameter.kill) {
|
||||
individual.die();
|
||||
}
|
||||
|
||||
// 开启同化
|
||||
else if (this.parameter.assimilate.objects) {
|
||||
individual.transfer(this.parameter.assimilate.objects);
|
||||
}
|
||||
}
|
||||
|
||||
individual.setData(`Wastage.${key}`, currentValue);
|
||||
}
|
||||
|
||||
public override terms: Record<string, Record<string, string>> = {
|
||||
"$Title": {
|
||||
"ZH_CN": "流逝",
|
||||
"EN_US": "Wastage"
|
||||
},
|
||||
"$Intro": {
|
||||
"ZH_CN": "随着时间流逝",
|
||||
"EN_US": "As time goes by"
|
||||
},
|
||||
"$Key": {
|
||||
"ZH_CN": "元数据",
|
||||
"EN_US": "Metadata"
|
||||
},
|
||||
"$Value": {
|
||||
"ZH_CN": "默认数值",
|
||||
"EN_US": "Default value"
|
||||
},
|
||||
"$Speed": {
|
||||
"ZH_CN": "流逝速度 (c/s)",
|
||||
"EN_US": "Passing speed (c/s)"
|
||||
},
|
||||
"$Threshold": {
|
||||
"ZH_CN": "阈值",
|
||||
"EN_US": "Threshold"
|
||||
},
|
||||
"$Kill": {
|
||||
"ZH_CN": "死亡",
|
||||
"EN_US": "Death"
|
||||
},
|
||||
"$Assimilate": {
|
||||
"ZH_CN": "同化",
|
||||
"EN_US": "Assimilate"
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
export { Wastage };
|
||||
@@ -188,11 +188,6 @@ class CommandBar extends Component<IMixinSettingProps & IMixinStatusProps, IComm
|
||||
this.props.status ? this.props.status.newLabel() : undefined;
|
||||
}}
|
||||
/>
|
||||
|
||||
<CommandButton
|
||||
iconName="Camera"
|
||||
i18NKey="Command.Bar.Camera.Info"
|
||||
/>
|
||||
</div>
|
||||
<div>
|
||||
<CommandButton
|
||||
|
||||
@@ -3,4 +3,6 @@
|
||||
div.setting-popup {
|
||||
width: 100%;
|
||||
height: 100%;
|
||||
box-sizing: border-box;
|
||||
padding: 10px;
|
||||
}
|
||||
@@ -2,8 +2,11 @@ import { Component, ReactNode } from "react";
|
||||
import { Popup } from "@Context/Popups";
|
||||
import { Theme } from "@Component/Theme/Theme";
|
||||
import { Localization } from "@Component/Localization/Localization";
|
||||
import { useSettingWithEvent, IMixinSettingProps, Themes } from "@Context/Setting";
|
||||
import { ComboInput } from "@Input/ComboInput/ComboInput";
|
||||
import "./SettingPopup.scss";
|
||||
|
||||
|
||||
interface ISettingPopupProps {
|
||||
|
||||
}
|
||||
@@ -24,10 +27,42 @@ class SettingPopup extends Popup<ISettingPopupProps> {
|
||||
}
|
||||
}
|
||||
|
||||
class SettingPopupComponent extends Component<ISettingPopupProps> {
|
||||
@useSettingWithEvent("themes", "language")
|
||||
class SettingPopupComponent extends Component<ISettingPopupProps & IMixinSettingProps> {
|
||||
|
||||
public render(): ReactNode {
|
||||
return <Theme className="setting-popup"></Theme>
|
||||
return <Theme className="setting-popup">
|
||||
|
||||
<ComboInput
|
||||
keyI18n="Language"
|
||||
allOption={[
|
||||
{ key: "EN_US", i18n: "EN_US" },
|
||||
{ key: "ZH_CN", i18n: "ZH_CN" }
|
||||
]}
|
||||
value={{
|
||||
key: this.props.setting?.language ?? "EN_US",
|
||||
i18n: this.props.setting?.language ?? "EN_US"
|
||||
}}
|
||||
valueChange={(data) => {
|
||||
this.props.setting?.setProps("language", data.key as any);
|
||||
}}
|
||||
/>
|
||||
|
||||
<ComboInput
|
||||
keyI18n="Themes"
|
||||
allOption={[
|
||||
{ key: Themes.dark as any, i18n: "Themes.Dark" },
|
||||
{ key: Themes.light as any, i18n: "Themes.Light" }
|
||||
]}
|
||||
value={{
|
||||
key: this.props.setting?.themes ?? Themes.dark as any,
|
||||
i18n: this.props.setting?.themes === Themes.dark ? "Themes.Dark" : "Themes.Light"
|
||||
}}
|
||||
valueChange={(data) => {
|
||||
this.props.setting?.setProps("themes", parseInt(data.key));
|
||||
}}
|
||||
/>
|
||||
</Theme>
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -174,11 +174,13 @@ class Status extends Emitter<IStatusEvent> {
|
||||
this.emit("objectChange");
|
||||
this.emit("labelChange");
|
||||
this.emit("behaviorChange");
|
||||
this.emit("clipChange");
|
||||
|
||||
// 清除焦点对象
|
||||
this.setBehaviorObject();
|
||||
this.setFocusObject(new Set());
|
||||
this.setLabelObject();
|
||||
this.setClipObject();
|
||||
|
||||
// 映射
|
||||
this.emit("fileLoad");
|
||||
@@ -196,11 +198,13 @@ class Status extends Emitter<IStatusEvent> {
|
||||
this.on("objectChange", handelFileChange);
|
||||
this.on("behaviorChange", handelFileChange);
|
||||
this.on("labelChange", handelFileChange);
|
||||
this.on("clipChange", handelFileChange);
|
||||
this.on("individualChange", handelFileChange);
|
||||
this.on("groupAttrChange", handelFileChange);
|
||||
this.on("rangeAttrChange", handelFileChange);
|
||||
this.on("labelAttrChange", handelFileChange);
|
||||
this.on("behaviorAttrChange", handelFileChange);
|
||||
this.on("clipAttrChange", handelFileChange);
|
||||
this.on("fileChange", () => this.archive.emit("fileChange"));
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,10 @@
|
||||
const EN_US = {
|
||||
"Language": "Language",
|
||||
"EN_US": "English (US)",
|
||||
"ZH_CN": "Chinese (Simplified)",
|
||||
"Themes": "Themes",
|
||||
"Themes.Dark": "Dark",
|
||||
"Themes.Light": "Light",
|
||||
"Header.Bar.Title": "Living Together | Emulator",
|
||||
"Header.Bar.Title.Info": "Group Behavior Research Emulator",
|
||||
"Header.Bar.File.Name.Info": "{file} ({status})",
|
||||
|
||||
@@ -1,6 +1,10 @@
|
||||
const ZH_CN = {
|
||||
"Language": "语言",
|
||||
"EN_US": "英语 (美国)",
|
||||
"ZH_CN": "中文 (简体)",
|
||||
"Themes": "主题",
|
||||
"Themes.Dark": "黑暗",
|
||||
"Themes.Light": "亮色",
|
||||
"Header.Bar.Title": "群生共进 | 仿真器",
|
||||
"Header.Bar.Title.Info": "群体行为研究仿真器",
|
||||
"Header.Bar.File.Name.Info": "{file} ({status})",
|
||||
|
||||
@@ -301,4 +301,4 @@ class Archive extends Emitter<IArchiveEvent> {
|
||||
}
|
||||
}
|
||||
|
||||
export { Archive };
|
||||
export { Archive, IArchiveObject };
|
||||
+47
-6
@@ -7,7 +7,9 @@ import { archiveObject2Parameter, IArchiveParseFn, parameter2ArchiveObject } fro
|
||||
interface IDrawCommand {
|
||||
type: "points" | "cube";
|
||||
id: string;
|
||||
name?: string;
|
||||
data?: Float32Array;
|
||||
mapId?: number[];
|
||||
position?: number[];
|
||||
radius?: number[];
|
||||
parameter?: IAnyObject;
|
||||
@@ -143,7 +145,7 @@ class Clip {
|
||||
return res;
|
||||
}
|
||||
|
||||
public isArrayEqual(a1?: number[], a2?: number[]): boolean {
|
||||
public isArrayEqual(a1?: Array<number | string>, a2?: Array<number | string>): boolean {
|
||||
|
||||
if ((a1 && !a2) || (!a1 && a2)) {
|
||||
return false;
|
||||
@@ -179,6 +181,26 @@ class Clip {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
/**
|
||||
* ID 映射
|
||||
*/
|
||||
private sorterIdMapper: Map<string, number> = new Map();
|
||||
private sorterIdMapperNextId: number = 1;
|
||||
|
||||
/**
|
||||
* 获取映射ID
|
||||
*/
|
||||
private getMapperId = (id: string): number => {
|
||||
let mapperId = this.sorterIdMapper.get(id);
|
||||
if (mapperId === undefined) {
|
||||
mapperId = this.sorterIdMapperNextId ++;
|
||||
this.sorterIdMapper.set(id, mapperId);
|
||||
return mapperId;
|
||||
} else {
|
||||
return mapperId;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 录制一帧
|
||||
*/
|
||||
@@ -196,10 +218,12 @@ class Clip {
|
||||
const lastCommand = this.getCommandFromLastFrame("points", object.id);
|
||||
|
||||
// 记录
|
||||
const dataBuffer = object.exportPositionId(this.getMapperId);
|
||||
const recodeData: IDrawCommand = {
|
||||
type: "points",
|
||||
id: object.id,
|
||||
data: object.exportPositionData()
|
||||
name: object.displayName,
|
||||
data: dataBuffer[0]
|
||||
}
|
||||
|
||||
// 对比校验
|
||||
@@ -209,6 +233,12 @@ class Clip {
|
||||
recodeData.parameter = this.cloneRenderParameter(object.renderParameter);
|
||||
}
|
||||
|
||||
if (this.isArrayEqual(dataBuffer[1], lastCommand?.mapId)) {
|
||||
recodeData.mapId = lastCommand?.mapId;
|
||||
} else {
|
||||
recodeData.mapId = dataBuffer[1];
|
||||
}
|
||||
|
||||
commands.push(recodeData);
|
||||
}
|
||||
|
||||
@@ -221,7 +251,8 @@ class Clip {
|
||||
// 记录
|
||||
const recodeData: IDrawCommand = {
|
||||
type: "cube",
|
||||
id: object.id
|
||||
id: object.id,
|
||||
name: object.displayName
|
||||
}
|
||||
|
||||
// 释放上一帧的内存
|
||||
@@ -260,7 +291,7 @@ class Clip {
|
||||
return frame;
|
||||
}
|
||||
|
||||
public readonly LastFrameData: "@L" = "@L";
|
||||
public readonly LastFrameData: "@" = "@";
|
||||
|
||||
/**
|
||||
* 压缩帧数据
|
||||
@@ -287,9 +318,15 @@ class Clip {
|
||||
undefined;
|
||||
|
||||
// 记录
|
||||
command.name = (lastCommand?.name === commands[j].name) ?
|
||||
this.LastFrameData as any : commands[j].name;
|
||||
|
||||
command.data = (lastCommand?.data === commands[j].data) ?
|
||||
this.LastFrameData as any : Array.from(commands[j].data ?? []);
|
||||
|
||||
command.mapId = (lastCommand?.mapId === commands[j].mapId) ?
|
||||
this.LastFrameData as any : commands[j].mapId;
|
||||
|
||||
command.position = (lastCommand?.position === commands[j].position) ?
|
||||
this.LastFrameData as any : commands[j].position?.concat([]);
|
||||
|
||||
@@ -336,12 +373,16 @@ class Clip {
|
||||
this.getCommandFromLastFrame(command.type, command.id, resFrame[resFrame.length - 1]) :
|
||||
undefined;
|
||||
|
||||
console.log(lastCommand);
|
||||
|
||||
// 记录
|
||||
command.name = (this.LastFrameData as any === commands[j].name) ?
|
||||
lastCommand?.name : commands[j].name;
|
||||
|
||||
command.data = (this.LastFrameData as any === commands[j].data) ?
|
||||
lastCommand?.data : new Float32Array(commands[j].data ?? []);
|
||||
|
||||
command.mapId = (this.LastFrameData as any === commands[j].mapId) ?
|
||||
lastCommand?.mapId : commands[j].mapId;
|
||||
|
||||
command.position = (this.LastFrameData as any === commands[j].position) ?
|
||||
lastCommand?.position : commands[j].position;
|
||||
|
||||
|
||||
@@ -428,6 +428,22 @@ class Group extends CtrlObject<IArchiveGroup> {
|
||||
return dataBuffer;
|
||||
}
|
||||
|
||||
/**
|
||||
* 导出个体id列表
|
||||
*/
|
||||
public exportPositionId(idMapper: (id: string) => number ): [Float32Array, number[]] {
|
||||
let bi = 0; let ii = 0;
|
||||
let dataBuffer = new Float32Array(this.individuals.size * 3);
|
||||
let idBUffer: number[] = new Array(this.individuals.size).fill("");
|
||||
this.individuals.forEach((individual) => {
|
||||
idBUffer[ii ++] = idMapper(individual.id);
|
||||
dataBuffer[bi ++] = individual.position[0];
|
||||
dataBuffer[bi ++] = individual.position[1];
|
||||
dataBuffer[bi ++] = individual.position[2];
|
||||
});
|
||||
return [dataBuffer, idBUffer];
|
||||
}
|
||||
|
||||
public override toArchive(): IArchiveCtrlObject & IArchiveGroup {
|
||||
return {
|
||||
...super.toArchive(),
|
||||
|
||||
Reference in New Issue
Block a user