Spaces:
Running
Running
File size: 4,767 Bytes
a296c4d |
1 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 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 |
import type { FileData } from "@gradio/client";
import BABYLON from "babylonjs";
const light_types = {
DirectionalLight: BABYLON.DirectionalLight,
PointLight: BABYLON.PointLight,
HemiLight: BABYLON.HemisphericLight,
};
const create_camera = (
scene: BABYLON.Scene,
camera_position: [number | null, number | null, number | null],
zoom_speed: number,
pan_speed: number
): void => {
scene.createDefaultCamera(true, true, true);
var helperCamera = scene.activeCamera! as BABYLON.ArcRotateCamera;
if (camera_position[0] !== null) {
helperCamera.alpha = BABYLON.Tools.ToRadians(camera_position[0]);
}
if (camera_position[1] !== null) {
helperCamera.beta = BABYLON.Tools.ToRadians(camera_position[1]);
}
if (camera_position[2] !== null) {
helperCamera.radius = camera_position[2];
}
helperCamera.lowerRadiusLimit = 0.1;
const updateCameraSensibility = (): void => {
helperCamera.wheelPrecision = 250 / (helperCamera.radius * zoom_speed);
helperCamera.panningSensibility = (10000 * pan_speed) / helperCamera.radius;
};
updateCameraSensibility();
helperCamera.attachControl(true);
helperCamera.onAfterCheckInputsObservable.add(updateCameraSensibility);
};
export const add_new_model = (
canvas: HTMLCanvasElement,
scene: BABYLON.Scene,
engine: BABYLON.Engine,
value: FileData | null,
clear_color: [number, number, number, number],
camera_position: [number | null, number | null, number | null],
zoom_speed: number,
pan_speed: number
): BABYLON.Scene => {
if (scene && !scene.isDisposed && engine) {
scene.dispose();
engine.dispose();
}
engine = new BABYLON.Engine(canvas, true);
scene = new BABYLON.Scene(engine);
scene.createDefaultCameraOrLight();
scene.clearColor = scene.clearColor = new BABYLON.Color4(...clear_color);
engine.runRenderLoop(() => {
scene.render();
});
window.addEventListener("resize", () => {
engine.resize();
});
if (!value) return scene;
let url: string;
url = value.url!;
BABYLON.SceneLoader.ShowLoadingScreen = false;
BABYLON.SceneLoader.Append(
url,
"",
scene,
() => create_camera(scene, camera_position, zoom_speed, pan_speed),
undefined,
undefined,
"." + value.path.split(".")[1]
);
return scene;
};
export const change_camera_position = (
scene: BABYLON.Scene,
camera_position: [number | null, number | null, number | null],
zoom_speed: number
): void => {
var helperCamera = scene.activeCamera! as BABYLON.ArcRotateCamera;
if (camera_position[0] !== null) {
helperCamera.alpha = BABYLON.Tools.ToRadians(camera_position[0]);
}
if (camera_position[1] !== null) {
helperCamera.beta = BABYLON.Tools.ToRadians(camera_position[1]);
}
if (camera_position[2] !== null) {
helperCamera.radius = camera_position[2];
}
helperCamera.wheelPrecision = 2500 / zoom_speed;
};
export const change_light = (
scene: BABYLON.Scene,
light_type: string,
light_position: [number, number, number],
index: number
): void => {
var light = scene.lights[index];
light.dispose();
switch (light_type) {
case "DirectionalLight":
light = new BABYLON.DirectionalLight(
light_type,
new BABYLON.Vector3(...light_position),
scene
);
break;
case "PointLight":
light = new BABYLON.PointLight(
light_type,
new BABYLON.Vector3(...light_position),
scene
);
break;
case "HemiLight":
light = new BABYLON.HemisphericLight(
light_type,
new BABYLON.Vector3(...light_position),
scene
);
break;
}
scene.addLight(light);
};
export const add_new_light = (
scene: BABYLON.Scene,
light_position: [number, number, number]
): void => {
const newLight = new BABYLON.HemisphericLight(
"HemiLight",
new BABYLON.Vector3(...light_position),
scene
);
scene.addLight(newLight);
};
export const change_show_axes = (
scene: BABYLON.Scene,
show_axes: boolean
): void => {
if (show_axes) {
const axes = new BABYLON.Debug.AxesViewer(scene, 5);
}
};
export const zoom = (
inOrOut: "in" | "out",
scene: BABYLON.Scene,
zoom_speed: number
): void => {
var helperCamera = scene.activeCamera! as BABYLON.ArcRotateCamera;
helperCamera.wheelPrecision = 2500 / zoom_speed;
if (inOrOut === "in") {
helperCamera.radius = helperCamera.radius / 2;
} else {
helperCamera.radius = helperCamera.radius * 2;
}
};
export const reset_camera_position = (
scene: BABYLON.Scene,
camera_position: [number | null, number | null, number | null],
zoom_speed: number,
pan_speed: number
): void => {
scene.removeCamera(scene.activeCamera!);
create_camera(scene, camera_position, zoom_speed, pan_speed);
};
|