| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366 |
- #include "Scripts/Utilities/Network.as"
- Scene@ testScene;
- Camera@ camera;
- Node@ cameraNode;
- float yaw = 0.0;
- float pitch = 0.0;
- int modelIndex = 0;
- int maxLights = 100;
- int maxObjects = 100;
- int numLights = 0;
- int numObjects = 1;
- Array<String> modelNames = {"Models/Box.mdl", "Models/Mushroom.mdl", "Models/Jack.mdl"};
- Array<String> materialNames = {"Materials/Stone.xml", "Materials/Mushroom.xml", "Materials/Jack.xml"};
- Array<Vector3> modelScales = {Vector3(0.6, 0.6, 0.6), Vector3(1, 1, 1), Vector3(1, 1, 1)};
- Array<Light@> lights;
- Array<StaticModel@> objects;
- void Start()
- {
- if (!engine.headless)
- {
- InitConsole();
- InitUI();
- }
- else
- OpenConsoleWindow();
- InitScene();
- SubscribeToEvent("Update", "HandleUpdate");
- SubscribeToEvent("KeyDown", "HandleKeyDown");
- SubscribeToEvent("MouseMove", "HandleMouseMove");
- SubscribeToEvent("MouseButtonDown", "HandleMouseButtonDown");
- SubscribeToEvent("MouseButtonUp", "HandleMouseButtonUp");
- }
- void InitConsole()
- {
- XMLFile@ uiStyle = cache.GetResource("XMLFile", "UI/DefaultStyle.xml");
- engine.CreateDebugHud();
- debugHud.style = uiStyle;
- debugHud.mode = DEBUGHUD_SHOW_ALL;
- engine.CreateConsole();
- console.style = uiStyle;
- }
- void InitUI()
- {
- XMLFile@ uiStyle = cache.GetResource("XMLFile", "UI/DefaultStyle.xml");
- Cursor@ newCursor = Cursor("Cursor");
- newCursor.style = uiStyle;
- newCursor.position = IntVector2(graphics.width / 2, graphics.height / 2);
- ui.cursor = newCursor;
- }
- void InitScene()
- {
- testScene = Scene("TestScene");
- testScene.CreateComponent("Octree");
- testScene.CreateComponent("DebugRenderer");
- Node@ zoneNode = testScene.CreateChild("Zone");
- Zone@ zone = zoneNode.CreateComponent("Zone");
- zone.ambientColor = Color(0.1, 0.1, 0.1);
- zone.fogColor = Color(0.0, 0.0, 0.0);
- zone.fogStart = 100.0;
- zone.fogEnd = 300.0;
- zone.boundingBox = BoundingBox(-1000.0, 1000.0);
- for (int i = 0; i < maxObjects; ++i)
- {
- Node@ objectNode = testScene.CreateChild("Object");
- if (i >= 1)
- objectNode.position = GetRandomPosition();
- StaticModel@ object = objectNode.CreateComponent("StaticModel");
- object.visible = false;
- objects.Push(object);
- }
-
- for (int i = 0; i < maxLights; ++i)
- {
- Node@ lightNode = testScene.CreateChild("Light");
- Light@ light = lightNode.CreateComponent("Light");
- lightNode.position = GetRandomPosition();
- Color color((RandomInt() & 1) * 0.5 + 0.5, (RandomInt() & 1) * 0.5 + 0.5, (RandomInt() & 1) * 0.5 + 0.5);
- if (color.r == 0.5 && color.g == 0.5 && color.b == 0.5)
- color = Color(1, 1, 1);
- light.visible = false;
- light.range = 2.0;
- light.color = color;
- light.specularIntensity = 1.0;
- lights.Push(light);
- }
-
- LoadNewModel();
- EnableLights();
- EnableObjects();
- // Enable access to this script file & scene from the console
- script.defaultScene = testScene;
- script.defaultScriptFile = scriptFile;
- // Create the camera outside the scene so it is unaffected by scene load/save
- cameraNode = Node();
- camera = cameraNode.CreateComponent("Camera");
- cameraNode.position = Vector3(0, 0, -10);
- if (!engine.headless)
- {
- renderer.viewports[0] = Viewport(testScene, camera);
- // Add FXAA effect to the renderpath. Clone the default renderpath so that we don't affect it
- RenderPath@ newRenderPath = renderer.viewports[0].renderPath.Clone();
- newRenderPath.Append(cache.GetResource("XMLFile", "PostProcess/EdgeFilter.xml"));
- newRenderPath.SetActive("Bloom", false);
- newRenderPath.SetActive("EdgeFilter", false);
- renderer.viewports[0].renderPath = newRenderPath;
- audio.listener = cameraNode.CreateComponent("SoundListener");
- }
- }
- void LoadNewModel()
- {
- for (uint i = 0; i < objects.length; ++i)
- {
- objects[i].model = cache.GetResource("Model", modelNames[modelIndex]);
- objects[i].material = cache.GetResource("Material", materialNames[modelIndex]);
- objects[i].node.scale = modelScales[modelIndex];
- }
- }
- void EnableLights()
- {
- for (uint i = 0; i < lights.length; ++i)
- lights[i].visible = i < numLights;
- }
- void EnableObjects()
- {
- for (uint i = 0; i < objects.length; ++i)
- objects[i].visible = i < numObjects;
- }
- void RandomizePositions()
- {
- for (uint i = 0; i < objects.length; ++i)
- objects[i].node.position = GetRandomPosition();
- for (uint i = 0; i < lights.length; ++i)
- lights[i].node.position = GetRandomPosition();
- }
- void ToggleVertexLighting()
- {
- for (uint i = 0; i < lights.length; ++i)
- lights[i].perVertex = !lights[i].perVertex;
- }
- Vector3 GetRandomPosition()
- {
- return Vector3(Random(4.0) - 2.0, Random(3.0) - 1.5, Random(4.0) - 2.0);
- }
- void HandleUpdate(StringHash eventType, VariantMap& eventData)
- {
- float timeStep = eventData["TimeStep"].GetFloat();
- if (ui.focusElement is null)
- {
- float speedMultiplier = 1.0;
- if (input.keyDown[KEY_LSHIFT])
- speedMultiplier = 5.0;
- if (input.keyDown[KEY_LCTRL])
- speedMultiplier = 0.1;
- if (input.keyDown['W'])
- cameraNode.TranslateRelative(Vector3(0, 0, 10) * timeStep * speedMultiplier);
- if (input.keyDown['S'])
- cameraNode.TranslateRelative(Vector3(0, 0, -10) * timeStep * speedMultiplier);
- if (input.keyDown['A'])
- cameraNode.TranslateRelative(Vector3(-10, 0, 0) * timeStep * speedMultiplier);
- if (input.keyDown['D'])
- cameraNode.TranslateRelative(Vector3(10, 0, 0) * timeStep * speedMultiplier);
- if (input.keyPress['1'])
- {
- int quality = renderer.textureQuality;
- ++quality;
- if (quality > 2)
- quality = 0;
- renderer.textureQuality = quality;
- }
- if (input.keyPress['2'])
- {
- int quality = renderer.materialQuality;
- ++quality;
- if (quality > 2)
- quality = 0;
- renderer.materialQuality = quality;
- }
- if (input.keyPress['3'])
- renderer.specularLighting = !renderer.specularLighting;
- if (input.keyPress['4'])
- renderer.drawShadows = !renderer.drawShadows;
- if (input.keyPress['5'])
- {
- int size = renderer.shadowMapSize;
- size *= 2;
- if (size > 2048)
- size = 512;
- renderer.shadowMapSize = size;
- }
- if (input.keyPress['6'])
- renderer.shadowQuality = renderer.shadowQuality + 1;
- if (input.keyPress['7'])
- {
- bool occlusion = renderer.maxOccluderTriangles > 0;
- occlusion = !occlusion;
- renderer.maxOccluderTriangles = occlusion ? 5000 : 0;
- }
- if (input.keyPress['8'])
- renderer.dynamicInstancing = !renderer.dynamicInstancing;
- if (input.keyPress['O'])
- camera.orthographic = !camera.orthographic;
- if (input.keyPress['F'])
- renderer.viewports[0].renderPath.ToggleActive("EdgeFilter");
- if (input.keyPress['T'])
- debugHud.Toggle(DEBUGHUD_SHOW_PROFILER);
- if (input.keyPress['Z'])
- {
- --modelIndex;
- if (modelIndex < 0)
- modelIndex = 0;
- LoadNewModel();
- }
- if (input.keyPress['X'])
- {
- ++modelIndex;
- if (modelIndex > modelNames.length - 1)
- modelIndex = modelNames.length - 1;
- LoadNewModel();
- }
-
- if (input.keyPress[KEY_RIGHT])
- {
- ++numObjects;
- if (numObjects > maxObjects)
- numObjects = maxObjects;
- EnableObjects();
- }
- if (input.keyPress[KEY_LEFT])
- {
- --numObjects;
- if (numObjects < 0)
- numObjects = 0;
- EnableObjects();
- }
- if (input.keyPress[KEY_UP])
- {
- ++numLights;
- if (numLights > maxLights)
- numLights = maxLights;
- EnableLights();
- }
- if (input.keyPress[KEY_DOWN])
- {
- --numLights;
- if (numLights < 0)
- numLights = 0;
- EnableLights();
- }
- if (input.keyPress[KEY_PAGEUP])
- {
- numLights += 10;
- if (numLights > maxLights)
- numLights = maxLights;
- EnableLights();
- }
- if (input.keyPress[KEY_PAGEDOWN])
- {
- numLights -= 10;
- if (numLights < 0)
- numLights = 0;
- EnableLights();
- }
-
- if (input.keyPress['R'])
- RandomizePositions();
- if (input.keyPress['V'])
- ToggleVertexLighting();
- }
- if (input.keyPress[KEY_ESC])
- {
- if (ui.focusElement is null)
- engine.Exit();
- else
- console.visible = false;
- }
- }
- void HandleKeyDown(StringHash eventType, VariantMap& eventData)
- {
- // Check for toggling the console
- if (eventData["Key"].GetInt() == KEY_F1)
- console.Toggle();
- }
- void HandleMouseMove(StringHash eventType, VariantMap& eventData)
- {
- if (eventData["Buttons"].GetInt() & MOUSEB_RIGHT != 0)
- {
- int mousedx = eventData["DX"].GetInt();
- int mousedy = eventData["DY"].GetInt();
- yaw += mousedx / 10.0f;
- pitch += mousedy / 10.0f;
- if (pitch < -90.0f)
- pitch = -90.0f;
- if (pitch > 90.0f)
- pitch = 90.0f;
- cameraNode.rotation = Quaternion(pitch, yaw, 0);
- }
- }
- void HandleMouseButtonDown(StringHash eventType, VariantMap& eventData)
- {
- int button = eventData["Button"].GetInt();
- if (button == MOUSEB_RIGHT)
- ui.cursor.visible = false;
- }
- void HandleMouseButtonUp(StringHash eventType, VariantMap& eventData)
- {
- if (eventData["Button"].GetInt() == MOUSEB_RIGHT)
- ui.cursor.visible = true;
- }
|