defaults.ts 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. import { v4 as uuidv4 } from "uuid";
  2. import type {
  3. SceneObject,
  4. Scene,
  5. SceneSettings,
  6. EnvironmentSettings,
  7. ObjectType,
  8. } from "./types";
  9. export function createDefaultGrass(): SceneObject {
  10. return {
  11. id: uuidv4(),
  12. name: "Grass",
  13. description: "Large grass plane",
  14. type: "plane",
  15. position: [0, -0.1, 0],
  16. rotation: [-Math.PI / 2, 0, 0],
  17. scale: [5000, 5000, 1],
  18. material: {
  19. color: "#4CAF50",
  20. metalness: 0.0,
  21. roughness: 0.9,
  22. emissive: "#000000",
  23. emissiveIntensity: 0,
  24. },
  25. physics: {
  26. mass: 0,
  27. isStatic: true,
  28. restitution: 0.2,
  29. friction: 0.8,
  30. colliderType: "cuboid",
  31. },
  32. visible: true,
  33. };
  34. }
  35. export function createDefaultAmbientLight(): SceneObject {
  36. return {
  37. id: uuidv4(),
  38. name: "Ambient Light",
  39. description: "Default ambient light",
  40. type: "light",
  41. position: [0, 3, 0],
  42. rotation: [0, 0, 0],
  43. scale: [1, 1, 1],
  44. material: {
  45. color: "#ffffff",
  46. metalness: 0,
  47. roughness: 1,
  48. emissive: "#000000",
  49. emissiveIntensity: 0,
  50. },
  51. physics: {
  52. mass: 1,
  53. isStatic: false,
  54. restitution: 0.2,
  55. friction: 0.5,
  56. colliderType: "none",
  57. },
  58. visible: true,
  59. intensity: 0.5,
  60. lightType: "ambient",
  61. };
  62. }
  63. export function createDefaultDirectionalLight(): SceneObject {
  64. return {
  65. id: uuidv4(),
  66. name: "Directional Light",
  67. description: "Default directional light",
  68. type: "light",
  69. position: [10, 10, 10],
  70. rotation: [0, 0, 0],
  71. scale: [1, 1, 1],
  72. material: {
  73. color: "#ffffff",
  74. metalness: 0,
  75. roughness: 1,
  76. emissive: "#000000",
  77. emissiveIntensity: 0,
  78. },
  79. physics: {
  80. mass: 1,
  81. isStatic: false,
  82. restitution: 0.2,
  83. friction: 0.5,
  84. colliderType: "none",
  85. },
  86. visible: true,
  87. intensity: 1,
  88. lightType: "directional",
  89. };
  90. }
  91. export function createDefaultPlayer(): SceneObject {
  92. return {
  93. id: "player-" + uuidv4(),
  94. name: "Player",
  95. description: "Default player",
  96. type: "player",
  97. position: [0, 0, 0],
  98. rotation: [0, 0, 0],
  99. scale: [1, 1, 1],
  100. material: {
  101. color: "#3498db",
  102. metalness: 0.1,
  103. roughness: 0.5,
  104. emissive: "#000000",
  105. emissiveIntensity: 0,
  106. },
  107. physics: {
  108. mass: 1,
  109. isStatic: false,
  110. restitution: 0.2,
  111. friction: 0.5,
  112. colliderType: "none",
  113. },
  114. visible: true,
  115. isPlayer: true,
  116. };
  117. }
  118. export function createDefaultSceneSettings(): SceneSettings {
  119. return {
  120. grid: {
  121. visible: true,
  122. size: 1,
  123. divisions: 100,
  124. color: "#444444",
  125. secondaryColor: "#888888",
  126. fadeDistance: 100,
  127. fadeStrength: 1,
  128. },
  129. fog: {
  130. enabled: false,
  131. color: "#cccccc",
  132. near: 1,
  133. far: 50,
  134. },
  135. };
  136. }
  137. export function createDefaultEnvironmentSettings(): EnvironmentSettings {
  138. return {
  139. preset: "city",
  140. customHdri: null,
  141. intensity: 1,
  142. blur: 0.5,
  143. };
  144. }
  145. export function createDefaultScene(name = "Scene 1"): Scene {
  146. return {
  147. id: uuidv4(),
  148. name,
  149. description: "",
  150. isDefault: true,
  151. objects: [
  152. createDefaultGrass(),
  153. createDefaultAmbientLight(),
  154. createDefaultDirectionalLight(),
  155. createDefaultPlayer(),
  156. ],
  157. sceneSettings: createDefaultSceneSettings(),
  158. environmentSettings: createDefaultEnvironmentSettings(),
  159. };
  160. }
  161. export function createDefaultObject(
  162. type: ObjectType,
  163. id: string,
  164. objectCount: number,
  165. ): SceneObject {
  166. const obj: SceneObject = {
  167. id,
  168. name: `${type.charAt(0).toUpperCase() + type.slice(1)} ${objectCount + 1}`,
  169. description: "",
  170. type,
  171. position: [
  172. 0,
  173. type === "box" ||
  174. type === "cylinder" ||
  175. type === "cone" ||
  176. type === "torus"
  177. ? 0.5
  178. : 0,
  179. 0,
  180. ],
  181. rotation: [0, 0, 0],
  182. scale: [1, 1, 1],
  183. material: {
  184. color:
  185. type === "light"
  186. ? "#ffffff"
  187. : type === "player"
  188. ? "#3498db"
  189. : `#${Math.floor(Math.random() * 16777215)
  190. .toString(16)
  191. .padStart(6, "0")}`,
  192. metalness: 0.1,
  193. roughness: 0.5,
  194. emissive: "#000000",
  195. emissiveIntensity: 0,
  196. },
  197. physics: {
  198. mass: 1,
  199. isStatic: false,
  200. restitution: 0.2,
  201. friction: 0.5,
  202. colliderType:
  203. type === "sphere" ? "ball" : type === "capsule" ? "capsule" : "cuboid",
  204. },
  205. damage: {
  206. amount: 0,
  207. enabled: false,
  208. },
  209. visible: true,
  210. };
  211. if (
  212. type === "capsule" ||
  213. type === "extrude" ||
  214. type === "tetrahedron" ||
  215. type === "octahedron" ||
  216. type === "dodecahedron" ||
  217. type === "icosahedron" ||
  218. type === "knot" ||
  219. type === "tube" ||
  220. type === "shape" ||
  221. type === "mesh"
  222. ) {
  223. obj.position = [0, 0.5, 0];
  224. }
  225. if (type === "light") {
  226. obj.intensity = 1;
  227. obj.distance = 0;
  228. obj.decay = 2;
  229. obj.lightType = "point";
  230. obj.physics.colliderType = "none";
  231. }
  232. if (type === "plane") {
  233. obj.rotation = [-Math.PI / 2, 0, 0];
  234. }
  235. if (type === "player") {
  236. obj.name = "Player";
  237. obj.position = [0, 0, 0];
  238. obj.physics.colliderType = "none";
  239. obj.isPlayer = true;
  240. }
  241. if (type === "image" || type === "video") {
  242. obj.physics.colliderType = "none";
  243. obj.physics.isStatic = true;
  244. }
  245. if (type === "shape") {
  246. obj.shapeData = {
  247. points: [
  248. [-0.5, -0.5],
  249. [0.5, -0.5],
  250. [0.5, 0.5],
  251. [-0.5, 0.5],
  252. ],
  253. holes: [],
  254. };
  255. }
  256. if (type === "mesh") {
  257. obj.meshData = {
  258. vertices: [
  259. -0.5, -0.5, 0.5, 0.5, -0.5, 0.5, 0.5, 0.5, 0.5, -0.5, 0.5, 0.5, -0.5,
  260. -0.5, -0.5, -0.5, 0.5, -0.5, 0.5, 0.5, -0.5, 0.5, -0.5, -0.5, -0.5, 0.5,
  261. -0.5, -0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, -0.5, -0.5, -0.5, -0.5,
  262. 0.5, -0.5, -0.5, 0.5, -0.5, 0.5, -0.5, -0.5, 0.5, 0.5, -0.5, -0.5, 0.5,
  263. 0.5, -0.5, 0.5, 0.5, 0.5, 0.5, -0.5, 0.5, -0.5, -0.5, -0.5, -0.5, -0.5,
  264. 0.5, -0.5, 0.5, 0.5, -0.5, 0.5, -0.5,
  265. ],
  266. indices: [
  267. 0, 1, 2, 0, 2, 3, 4, 5, 6, 4, 6, 7, 8, 9, 10, 8, 10, 11, 12, 13, 14, 12,
  268. 14, 15, 16, 17, 18, 16, 18, 19, 20, 21, 22, 20, 22, 23,
  269. ],
  270. };
  271. }
  272. return obj;
  273. }