Restore collision layers in local route planner
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
import { EditorCollisionLayer } from "./EditorCollisionLayer";
|
||||
import type { SimulationViewMode } from "./PlayCanvasRuntime";
|
||||
import { waypointHit } from "../../core/simulation/waypointHit";
|
||||
import { PlannerMarkers } from "./PlannerMarkers";
|
||||
import { QUALITY_PROFILES } from "../../core/simulation/gaussianQuality";
|
||||
@@ -22,6 +24,7 @@ export class AiEditorRuntime {
|
||||
private preferences = DEFAULT_PLANNER_PREFERENCES;
|
||||
private selected: string|null = null;
|
||||
private terrain = new EditorTerrain();
|
||||
private collisionLayer: EditorCollisionLayer | null = null;
|
||||
private resize: ResizeObserver;
|
||||
private abort = new AbortController();
|
||||
private disposed = false;
|
||||
@@ -103,6 +106,15 @@ export class AiEditorRuntime {
|
||||
this.controls.sceneCenter = this.terrain.bounds.getCenter(this.controls.sceneCenter);
|
||||
this.setClip(Infinity); this.dirty = true;
|
||||
}
|
||||
setViewMode(mode: SimulationViewMode) {
|
||||
if (mode !== "visual" && !this.collisionLayer) {
|
||||
this.collisionLayer = new EditorCollisionLayer(this.app, this.camera, this.terrain.renderData());
|
||||
this.collisionLayer.setClip(this.clip);
|
||||
}
|
||||
this.scan.enabled = mode !== "collision";
|
||||
this.collisionLayer?.setMode(mode);
|
||||
this.dirty = true;
|
||||
}
|
||||
setRoute(points:number[][]) {this.route=points;this.dirty=true;}
|
||||
top() {this.controls.top((this.route.length?this.route:this.points.map(p=>p.position)).map(toEditor));this.syncCamera();this.dirty=true;}
|
||||
setPoints(points: AiWaypoint[]) { this.points = points; this.annotations.set(points,this.selected,this.preferences);this.dirty = true; }
|
||||
@@ -120,6 +132,7 @@ export class AiEditorRuntime {
|
||||
}
|
||||
setClip(height: number) {
|
||||
this.clip = height;
|
||||
this.collisionLayer?.setClip(height);
|
||||
if (this.scan.gsplat) {
|
||||
this.scan.gsplat.setParameter("editorClip", Number.isFinite(height) ? height : 1e9);
|
||||
this.scan.gsplat.workBufferUpdate = WORKBUFFER_UPDATE_ONCE;
|
||||
@@ -175,6 +188,7 @@ export class AiEditorRuntime {
|
||||
this.controls.dispose();
|
||||
this.abort.abort(); this.resize.disconnect(); this.terrain.dispose();
|
||||
this.annotations.dispose();
|
||||
this.collisionLayer?.dispose();
|
||||
this.asset?.unload(); this.app.destroy(); this.canvas.width=1; this.canvas.height=1;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import type { SimulationViewMode } from "./PlayCanvasRuntime";
|
||||
import type { PreparedRoute } from "../../core/simulation/routePreparation";
|
||||
import type { ScreenPoint } from "../../core/simulation/aiPolygon";
|
||||
import { editWaypoints, waypointEditing, waypointShortcut } from "../../core/simulation/waypointEditing";
|
||||
@@ -30,6 +31,7 @@ export function AiLocalPlanner({ world, initialPoints, onDraft, actionsHost, hea
|
||||
const {points,selected,sequence} = editing;
|
||||
const press=useRef<{id:number;x:number;y:number}|null>(null);
|
||||
const [routeProjection,setRouteProjection]=useState<ScreenPoint[]>([]);
|
||||
const [layerMode,setLayerMode]=useState<SimulationViewMode>("visual");
|
||||
const [viewMode,setViewMode]=useState<"3d"|"top">("3d");
|
||||
const [markers, setMarkers] = useState<EditorMarker[]>([]);
|
||||
const [mode,setMode] = useState<"view" | "add">("view");
|
||||
@@ -40,7 +42,7 @@ export function AiLocalPlanner({ world, initialPoints, onDraft, actionsHost, hea
|
||||
useEffect(() => {
|
||||
if (cache?.status !== "ready" || !canvas.current) return;
|
||||
let active = true;
|
||||
setReady(false); setError(null); setClip(Infinity);
|
||||
setReady(false); setError(null); setClip(Infinity); setLayerMode("visual");
|
||||
let view: AiEditorRuntime;
|
||||
try { view = new AiEditorRuntime(canvas.current,cache,setMarkers,setRouteProjection); }
|
||||
catch (e) { setError((e as Error).message); return; }
|
||||
@@ -97,6 +99,10 @@ export function AiLocalPlanner({ world, initialPoints, onDraft, actionsHost, hea
|
||||
{points.some(p=>p.id===selected)?<IconButton label="Удалить точку" onClick={()=>{
|
||||
edit({type:"delete"});canvas.current?.focus({preventScroll:true});
|
||||
}}><Icon name="trash"/></IconButton>:null}
|
||||
<SegmentedControl label="Слои сцены" value={layerMode}
|
||||
items={[{value:"visual",label:"Gaussian"},{value:"collision",label:"Коллизии"},{value:"combined",label:"Вместе"}]}
|
||||
onChange={v=>{try {runtime.current?.setViewMode(v);setLayerMode(v);setError(null);}
|
||||
catch (e) {setError(e instanceof Error?e.message:"Не удалось показать слой коллизий.");}}}/>
|
||||
{preparedRoute?<SegmentedControl label="Ракурс маршрута" value={viewMode}
|
||||
items={[{value:"3d",label:"3D"},{value:"top",label:"Вид сверху"}]}
|
||||
onChange={v=>{setViewMode(v);if(v==="top")runtime.current?.top();else runtime.current?.home();}}/>:null}
|
||||
|
||||
@@ -0,0 +1,76 @@
|
||||
import {
|
||||
Application, BLEND_NONE, BLEND_NORMAL, CULLFACE_NONE, Entity, Layer, LAYERID_WORLD, Mesh, MeshInstance,
|
||||
SEMANTIC_POSITION, ShaderMaterial,
|
||||
} from "playcanvas";
|
||||
import type { SimulationViewMode } from "./PlayCanvasRuntime";
|
||||
|
||||
export interface CollisionRenderData {
|
||||
positions: Float32Array;
|
||||
indices: Uint16Array | Uint32Array;
|
||||
}
|
||||
|
||||
/** Displays the exact calibrated picking mesh; visibility never changes its geometry or BVH. */
|
||||
export class EditorCollisionLayer {
|
||||
private entity: Entity;
|
||||
private material: ShaderMaterial;
|
||||
private layer = new Layer({ name: "Planning collision surface", clearDepthBuffer: true });
|
||||
constructor(private app: Application, private camera: Entity, geometry: CollisionRenderData[]) {
|
||||
// Unified splats occupy the world pass; the mesh overlay must follow that pass.
|
||||
app.scene.layers.insert(this.layer, app.scene.layers.getTransparentIndex(app.scene.layers.getLayerById(LAYERID_WORLD)!) + 1);
|
||||
camera.camera!.layers = [...camera.camera!.layers, this.layer.id];
|
||||
this.entity = new Entity("Planning collision layer", app);
|
||||
this.material = new ShaderMaterial({
|
||||
uniqueName: "planning-collision-surface",
|
||||
attributes: { vertex_position: SEMANTIC_POSITION },
|
||||
vertexGLSL: `
|
||||
attribute vec3 vertex_position;
|
||||
uniform mat4 matrix_model;
|
||||
uniform mat4 matrix_viewProjection;
|
||||
varying vec3 surfacePosition;
|
||||
void main(void) {
|
||||
vec4 world = matrix_model * vec4(vertex_position, 1.0);
|
||||
surfacePosition = world.xyz;
|
||||
gl_Position = matrix_viewProjection * world;
|
||||
}`,
|
||||
fragmentGLSL: `
|
||||
varying vec3 surfacePosition;
|
||||
uniform float surfaceClip;
|
||||
uniform float surfaceOpacity;
|
||||
void main(void) {
|
||||
if (surfacePosition.y > surfaceClip) discard;
|
||||
vec3 normal = normalize(cross(dFdx(surfacePosition), dFdy(surfacePosition)));
|
||||
float shade = 0.42 + 0.58 * abs(dot(normal, normalize(vec3(0.3, 1.0, 0.4))));
|
||||
gl_FragColor = vec4(vec3(0.2, 0.95, 0.42) * shade, surfaceOpacity);
|
||||
}`,
|
||||
});
|
||||
this.material.cull = CULLFACE_NONE;
|
||||
const instances = geometry.map(({ positions, indices }) => {
|
||||
const mesh = new Mesh(app.graphicsDevice);
|
||||
mesh.setPositions(positions);
|
||||
mesh.setIndices(indices);
|
||||
mesh.update();
|
||||
return new MeshInstance(mesh, this.material, this.entity);
|
||||
});
|
||||
this.entity.addComponent("render", { meshInstances: instances, layers: [this.layer.id], castShadows: false, receiveShadows: false });
|
||||
app.root.addChild(this.entity);
|
||||
this.setClip(Infinity);
|
||||
this.setMode("visual");
|
||||
}
|
||||
setMode(mode: SimulationViewMode) {
|
||||
this.entity.enabled = mode !== "visual";
|
||||
const combined = mode === "combined";
|
||||
this.material.setParameter("surfaceOpacity", combined ? 0.34 : 1);
|
||||
this.material.blendType = combined ? BLEND_NORMAL : BLEND_NONE;
|
||||
this.material.depthWrite = true;
|
||||
this.material.update();
|
||||
}
|
||||
setClip(height: number) {
|
||||
this.material.setParameter("surfaceClip", Number.isFinite(height) ? height : 1e9);
|
||||
}
|
||||
dispose() {
|
||||
this.camera.camera!.layers = this.camera.camera!.layers.filter(id => id !== this.layer.id);
|
||||
this.app.scene.layers.remove(this.layer);
|
||||
this.entity.destroy();
|
||||
this.material.destroy();
|
||||
}
|
||||
}
|
||||
@@ -1,3 +1,4 @@
|
||||
import type { CollisionRenderData } from "./EditorCollisionLayer";
|
||||
import { Box3, BufferAttribute, BufferGeometry, DoubleSide, Mesh, Matrix4, Ray, Vector3 } from "three";
|
||||
import { DRACOLoader } from "three/addons/loaders/DRACOLoader.js";
|
||||
import { GLTFLoader } from "three/addons/loaders/GLTFLoader.js";
|
||||
@@ -47,6 +48,12 @@ export class EditorTerrain {
|
||||
}
|
||||
this.builder?.dispose(); this.builder = null;
|
||||
}
|
||||
renderData(): CollisionRenderData[] {
|
||||
return this.geometries.map((geometry) => ({
|
||||
positions: geometry.getAttribute("position").array as Float32Array,
|
||||
indices: geometry.index!.array as Uint16Array | Uint32Array,
|
||||
}));
|
||||
}
|
||||
pick(origin: number[], direction: number[], clip = Infinity) {
|
||||
const ray = new Ray(new Vector3(...origin), new Vector3(...direction).normalize());
|
||||
return this.trees.flatMap((tree) => tree.raycast(ray, DoubleSide))
|
||||
|
||||
Reference in New Issue
Block a user