2
0

LightTest.as 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. #include "Scripts/Utilities/Network.as"
  2. Scene@ testScene;
  3. Camera@ camera;
  4. Node@ cameraNode;
  5. float yaw = 0.0;
  6. float pitch = 0.0;
  7. int modelIndex = 0;
  8. int maxLights = 100;
  9. int maxObjects = 100;
  10. int numLights = 0;
  11. int numObjects = 1;
  12. Array<String> modelNames = {"Models/Box.mdl", "Models/Mushroom.mdl", "Models/Jack.mdl"};
  13. Array<String> materialNames = {"Materials/Stone.xml", "Materials/Mushroom.xml", "Materials/Jack.xml"};
  14. Array<Vector3> modelScales = {Vector3(0.6, 0.6, 0.6), Vector3(1, 1, 1), Vector3(1, 1, 1)};
  15. Array<Light@> lights;
  16. Array<StaticModel@> objects;
  17. void Start()
  18. {
  19. if (!engine.headless)
  20. {
  21. InitConsole();
  22. InitUI();
  23. }
  24. else
  25. OpenConsoleWindow();
  26. InitScene();
  27. SubscribeToEvent("Update", "HandleUpdate");
  28. SubscribeToEvent("KeyDown", "HandleKeyDown");
  29. SubscribeToEvent("MouseMove", "HandleMouseMove");
  30. SubscribeToEvent("MouseButtonDown", "HandleMouseButtonDown");
  31. SubscribeToEvent("MouseButtonUp", "HandleMouseButtonUp");
  32. }
  33. void InitConsole()
  34. {
  35. XMLFile@ uiStyle = cache.GetResource("XMLFile", "UI/DefaultStyle.xml");
  36. engine.CreateDebugHud();
  37. debugHud.style = uiStyle;
  38. debugHud.mode = DEBUGHUD_SHOW_ALL;
  39. engine.CreateConsole();
  40. console.style = uiStyle;
  41. }
  42. void InitUI()
  43. {
  44. XMLFile@ uiStyle = cache.GetResource("XMLFile", "UI/DefaultStyle.xml");
  45. Cursor@ newCursor = Cursor("Cursor");
  46. newCursor.style = uiStyle;
  47. newCursor.position = IntVector2(graphics.width / 2, graphics.height / 2);
  48. ui.cursor = newCursor;
  49. }
  50. void InitScene()
  51. {
  52. testScene = Scene("TestScene");
  53. testScene.CreateComponent("Octree");
  54. testScene.CreateComponent("DebugRenderer");
  55. Node@ zoneNode = testScene.CreateChild("Zone");
  56. Zone@ zone = zoneNode.CreateComponent("Zone");
  57. zone.ambientColor = Color(0.1, 0.1, 0.1);
  58. zone.fogColor = Color(0.0, 0.0, 0.0);
  59. zone.fogStart = 100.0;
  60. zone.fogEnd = 300.0;
  61. zone.boundingBox = BoundingBox(-1000.0, 1000.0);
  62. for (int i = 0; i < maxObjects; ++i)
  63. {
  64. Node@ objectNode = testScene.CreateChild("Object");
  65. if (i >= 1)
  66. objectNode.position = GetRandomPosition();
  67. StaticModel@ object = objectNode.CreateComponent("StaticModel");
  68. object.visible = false;
  69. objects.Push(object);
  70. }
  71. for (int i = 0; i < maxLights; ++i)
  72. {
  73. Node@ lightNode = testScene.CreateChild("Light");
  74. Light@ light = lightNode.CreateComponent("Light");
  75. lightNode.position = GetRandomPosition();
  76. Color color((RandomInt() & 1) * 0.5 + 0.5, (RandomInt() & 1) * 0.5 + 0.5, (RandomInt() & 1) * 0.5 + 0.5);
  77. if (color.r == 0.5 && color.g == 0.5 && color.b == 0.5)
  78. color = Color(1, 1, 1);
  79. light.visible = false;
  80. light.range = 2.0;
  81. light.color = color;
  82. light.specularIntensity = 1.0;
  83. lights.Push(light);
  84. }
  85. LoadNewModel();
  86. EnableLights();
  87. EnableObjects();
  88. // Enable access to this script file & scene from the console
  89. script.defaultScene = testScene;
  90. script.defaultScriptFile = scriptFile;
  91. // Create the camera outside the scene so it is unaffected by scene load/save
  92. cameraNode = Node();
  93. camera = cameraNode.CreateComponent("Camera");
  94. cameraNode.position = Vector3(0, 0, -10);
  95. if (!engine.headless)
  96. {
  97. renderer.viewports[0] = Viewport(testScene, camera);
  98. // Add FXAA effect to the renderpath. Clone the default renderpath so that we don't affect it
  99. RenderPath@ newRenderPath = renderer.viewports[0].renderPath.Clone();
  100. newRenderPath.Append(cache.GetResource("XMLFile", "PostProcess/EdgeFilter.xml"));
  101. newRenderPath.SetActive("Bloom", false);
  102. newRenderPath.SetActive("EdgeFilter", false);
  103. renderer.viewports[0].renderPath = newRenderPath;
  104. audio.listener = cameraNode.CreateComponent("SoundListener");
  105. }
  106. }
  107. void LoadNewModel()
  108. {
  109. for (uint i = 0; i < objects.length; ++i)
  110. {
  111. objects[i].model = cache.GetResource("Model", modelNames[modelIndex]);
  112. objects[i].material = cache.GetResource("Material", materialNames[modelIndex]);
  113. objects[i].node.scale = modelScales[modelIndex];
  114. }
  115. }
  116. void EnableLights()
  117. {
  118. for (uint i = 0; i < lights.length; ++i)
  119. lights[i].visible = i < numLights;
  120. }
  121. void EnableObjects()
  122. {
  123. for (uint i = 0; i < objects.length; ++i)
  124. objects[i].visible = i < numObjects;
  125. }
  126. void RandomizePositions()
  127. {
  128. for (uint i = 0; i < objects.length; ++i)
  129. objects[i].node.position = GetRandomPosition();
  130. for (uint i = 0; i < lights.length; ++i)
  131. lights[i].node.position = GetRandomPosition();
  132. }
  133. void ToggleVertexLighting()
  134. {
  135. for (uint i = 0; i < lights.length; ++i)
  136. lights[i].perVertex = !lights[i].perVertex;
  137. }
  138. Vector3 GetRandomPosition()
  139. {
  140. return Vector3(Random(4.0) - 2.0, Random(3.0) - 1.5, Random(4.0) - 2.0);
  141. }
  142. void HandleUpdate(StringHash eventType, VariantMap& eventData)
  143. {
  144. float timeStep = eventData["TimeStep"].GetFloat();
  145. if (ui.focusElement is null)
  146. {
  147. float speedMultiplier = 1.0;
  148. if (input.keyDown[KEY_LSHIFT])
  149. speedMultiplier = 5.0;
  150. if (input.keyDown[KEY_LCTRL])
  151. speedMultiplier = 0.1;
  152. if (input.keyDown['W'])
  153. cameraNode.TranslateRelative(Vector3(0, 0, 10) * timeStep * speedMultiplier);
  154. if (input.keyDown['S'])
  155. cameraNode.TranslateRelative(Vector3(0, 0, -10) * timeStep * speedMultiplier);
  156. if (input.keyDown['A'])
  157. cameraNode.TranslateRelative(Vector3(-10, 0, 0) * timeStep * speedMultiplier);
  158. if (input.keyDown['D'])
  159. cameraNode.TranslateRelative(Vector3(10, 0, 0) * timeStep * speedMultiplier);
  160. if (input.keyPress['1'])
  161. {
  162. int quality = renderer.textureQuality;
  163. ++quality;
  164. if (quality > 2)
  165. quality = 0;
  166. renderer.textureQuality = quality;
  167. }
  168. if (input.keyPress['2'])
  169. {
  170. int quality = renderer.materialQuality;
  171. ++quality;
  172. if (quality > 2)
  173. quality = 0;
  174. renderer.materialQuality = quality;
  175. }
  176. if (input.keyPress['3'])
  177. renderer.specularLighting = !renderer.specularLighting;
  178. if (input.keyPress['4'])
  179. renderer.drawShadows = !renderer.drawShadows;
  180. if (input.keyPress['5'])
  181. {
  182. int size = renderer.shadowMapSize;
  183. size *= 2;
  184. if (size > 2048)
  185. size = 512;
  186. renderer.shadowMapSize = size;
  187. }
  188. if (input.keyPress['6'])
  189. renderer.shadowQuality = renderer.shadowQuality + 1;
  190. if (input.keyPress['7'])
  191. {
  192. bool occlusion = renderer.maxOccluderTriangles > 0;
  193. occlusion = !occlusion;
  194. renderer.maxOccluderTriangles = occlusion ? 5000 : 0;
  195. }
  196. if (input.keyPress['8'])
  197. renderer.dynamicInstancing = !renderer.dynamicInstancing;
  198. if (input.keyPress['O'])
  199. camera.orthographic = !camera.orthographic;
  200. if (input.keyPress['F'])
  201. renderer.viewports[0].renderPath.ToggleActive("EdgeFilter");
  202. if (input.keyPress['T'])
  203. debugHud.Toggle(DEBUGHUD_SHOW_PROFILER);
  204. if (input.keyPress['Z'])
  205. {
  206. --modelIndex;
  207. if (modelIndex < 0)
  208. modelIndex = 0;
  209. LoadNewModel();
  210. }
  211. if (input.keyPress['X'])
  212. {
  213. ++modelIndex;
  214. if (modelIndex > modelNames.length - 1)
  215. modelIndex = modelNames.length - 1;
  216. LoadNewModel();
  217. }
  218. if (input.keyPress[KEY_RIGHT])
  219. {
  220. ++numObjects;
  221. if (numObjects > maxObjects)
  222. numObjects = maxObjects;
  223. EnableObjects();
  224. }
  225. if (input.keyPress[KEY_LEFT])
  226. {
  227. --numObjects;
  228. if (numObjects < 0)
  229. numObjects = 0;
  230. EnableObjects();
  231. }
  232. if (input.keyPress[KEY_UP])
  233. {
  234. ++numLights;
  235. if (numLights > maxLights)
  236. numLights = maxLights;
  237. EnableLights();
  238. }
  239. if (input.keyPress[KEY_DOWN])
  240. {
  241. --numLights;
  242. if (numLights < 0)
  243. numLights = 0;
  244. EnableLights();
  245. }
  246. if (input.keyPress[KEY_PAGEUP])
  247. {
  248. numLights += 10;
  249. if (numLights > maxLights)
  250. numLights = maxLights;
  251. EnableLights();
  252. }
  253. if (input.keyPress[KEY_PAGEDOWN])
  254. {
  255. numLights -= 10;
  256. if (numLights < 0)
  257. numLights = 0;
  258. EnableLights();
  259. }
  260. if (input.keyPress['R'])
  261. RandomizePositions();
  262. if (input.keyPress['V'])
  263. ToggleVertexLighting();
  264. }
  265. if (input.keyPress[KEY_ESC])
  266. {
  267. if (ui.focusElement is null)
  268. engine.Exit();
  269. else
  270. console.visible = false;
  271. }
  272. }
  273. void HandleKeyDown(StringHash eventType, VariantMap& eventData)
  274. {
  275. // Check for toggling the console
  276. if (eventData["Key"].GetInt() == KEY_F1)
  277. console.Toggle();
  278. }
  279. void HandleMouseMove(StringHash eventType, VariantMap& eventData)
  280. {
  281. if (eventData["Buttons"].GetInt() & MOUSEB_RIGHT != 0)
  282. {
  283. int mousedx = eventData["DX"].GetInt();
  284. int mousedy = eventData["DY"].GetInt();
  285. yaw += mousedx / 10.0f;
  286. pitch += mousedy / 10.0f;
  287. if (pitch < -90.0f)
  288. pitch = -90.0f;
  289. if (pitch > 90.0f)
  290. pitch = 90.0f;
  291. cameraNode.rotation = Quaternion(pitch, yaw, 0);
  292. }
  293. }
  294. void HandleMouseButtonDown(StringHash eventType, VariantMap& eventData)
  295. {
  296. int button = eventData["Button"].GetInt();
  297. if (button == MOUSEB_RIGHT)
  298. ui.cursor.visible = false;
  299. }
  300. void HandleMouseButtonUp(StringHash eventType, VariantMap& eventData)
  301. {
  302. if (eventData["Button"].GetInt() == MOUSEB_RIGHT)
  303. ui.cursor.visible = true;
  304. }