Kaynağa Gözat

Change default unitSize2D to 100, Apply model scale in ParticleModel2D.

aster2013 11 yıl önce
ebeveyn
işleme
3101a308f9

+ 4 - 4
Bin/Data/LuaScripts/24_Urho2DSprite.lua

@@ -44,8 +44,8 @@ function CreateScene()
     local camera = cameraNode:CreateComponent("Camera")
     local camera = cameraNode:CreateComponent("Camera")
     camera.orthographic = true
     camera.orthographic = true
 
 
-    local width = graphics.width
-    local height = graphics.height
+    local width = graphics.width / scene_.unitSize2D
+    local height = graphics.height / scene_.unitSize2D
     camera:SetOrthoSize(Vector2(width, height))
     camera:SetOrthoSize(Vector2(width, height))
 
 
     local sprite = cache:GetResource("Sprite2D", "Urho2D/Aster.png")
     local sprite = cache:GetResource("Sprite2D", "Urho2D/Aster.png")
@@ -70,7 +70,7 @@ function CreateScene()
         staticSprite.sprite = sprite
         staticSprite.sprite = sprite
 
 
         -- Set move speed
         -- Set move speed
-        spriteNode.moveSpeed = Vector3(Random(-200.0, 200.0), Random(-200.0, 200.0), 0.0)
+        spriteNode.moveSpeed = Vector3(Random(-2.0, 2.0), Random(-2.0, 2.0), 0.0)
         -- Set rotate speed
         -- Set rotate speed
         spriteNode.rotateSpeed = Random(-90.0, 90.0)
         spriteNode.rotateSpeed = Random(-90.0, 90.0)
 
 
@@ -140,7 +140,7 @@ function MoveCamera(timeStep)
     end
     end
 
 
     -- Movement speed as world units per second
     -- Movement speed as world units per second
-    local MOVE_SPEED = 400.0
+    local MOVE_SPEED = 4.0
 
 
     -- Read WASD keys and move the camera scene node to the corresponding direction if they are pressed
     -- Read WASD keys and move the camera scene node to the corresponding direction if they are pressed
     -- Use the TranslateRelative() function to move relative to the node's orientation. Alternatively we could
     -- Use the TranslateRelative() function to move relative to the node's orientation. Alternatively we could

+ 3 - 2
Bin/Data/LuaScripts/25_Urho2DParticle.lua

@@ -45,7 +45,7 @@ function CreateScene()
     cameraNode.position = Vector3(0.0, 0.0, -10.0)
     cameraNode.position = Vector3(0.0, 0.0, -10.0)
     local camera = cameraNode:CreateComponent("Camera")
     local camera = cameraNode:CreateComponent("Camera")
     camera.orthographic = true
     camera.orthographic = true
-    camera:SetOrthoSize(Vector2(graphics.width, graphics.height))
+    camera:SetOrthoSize(Vector2(graphics.width, graphics.height) / scene_.unitSize2D)
 
 
     local particleModel = cache:GetResource("ParticleModel2D", "Urho2D/LavaFlow.plist")
     local particleModel = cache:GetResource("ParticleModel2D", "Urho2D/LavaFlow.plist")
     if particleModel == nil then
     if particleModel == nil then
@@ -85,6 +85,7 @@ function HandleMouseMove(eventType, eventData)
     if particleNode ~= nil then
     if particleNode ~= nil then
         local x = eventData:GetInt("x")
         local x = eventData:GetInt("x")
         local y = eventData:GetInt("y")
         local y = eventData:GetInt("y")
-        particleNode.position = Vector3(x - graphics.width * 0.5, -y + graphics.height * 0.5, 0.0)
+        local camera = cameraNode:GetComponent("Camera")
+        particleNode.position = camera:ScreenToWorldPoint(Vector3(x / graphics.width, y / graphics.height, 10.0))
     end
     end
 end
 end

+ 6 - 6
Bin/Data/Scripts/24_Urho2DSprite.as

@@ -47,8 +47,8 @@ void CreateScene()
     Camera@ camera = cameraNode.CreateComponent("Camera");
     Camera@ camera = cameraNode.CreateComponent("Camera");
     camera.orthographic = true;
     camera.orthographic = true;
 
 
-    uint width = graphics.width;
-    uint height = graphics.height;
+    uint width = graphics.width / scene_.unitSize2D;
+    uint height = graphics.height / scene_.unitSize2D;
     camera.SetOrthoSize(Vector2(width, height));
     camera.SetOrthoSize(Vector2(width, height));
 
 
     Sprite2D@ sprite = cache.GetResource("Sprite2D", "Urho2D/Aster.png");
     Sprite2D@ sprite = cache.GetResource("Sprite2D", "Urho2D/Aster.png");
@@ -77,7 +77,7 @@ void CreateScene()
         // Set sprite
         // Set sprite
         staticSprite.sprite = sprite;
         staticSprite.sprite = sprite;
         
         
-        spriteNode.vars["MoveSpeed"] = Vector3(Random(-200.0f, 200.0f), Random(-200.0f, 200.0f), 0.0f);
+        spriteNode.vars["MoveSpeed"] = Vector3(Random(-2.0f, 2.0f), Random(-2.0f, 2.0f), 0.0f);
         spriteNode.vars["RotateSpeed"] = Random(-90.0f, 90.0f);
         spriteNode.vars["RotateSpeed"] = Random(-90.0f, 90.0f);
 
 
         spriteNodes.Push(spriteNode);
         spriteNodes.Push(spriteNode);
@@ -125,7 +125,7 @@ void MoveCamera(float timeStep)
         return;
         return;
 
 
     // Movement speed as world units per second
     // Movement speed as world units per second
-    const float MOVE_SPEED = 400.0f;
+    const float MOVE_SPEED = 4.0f;
     
     
     // Read WASD keys and move the camera scene node to the corresponding direction if they are pressed
     // Read WASD keys and move the camera scene node to the corresponding direction if they are pressed
     // Use the TranslateRelative() function to move relative to the node's orientation. Alternatively we could
     // Use the TranslateRelative() function to move relative to the node's orientation. Alternatively we could
@@ -166,8 +166,8 @@ void HandleUpdate(StringHash eventType, VariantMap& eventData)
     // Move the camera, scale movement with time step
     // Move the camera, scale movement with time step
     MoveCamera(timeStep);
     MoveCamera(timeStep);
 
 
-    float halfWidth = graphics.width * 0.5f;
-    float halfHeight = graphics.height * 0.5f;
+    float halfWidth = graphics.width * 0.5f / scene_.unitSize2D;
+    float halfHeight = graphics.height * 0.5f / scene_.unitSize2D;
 
 
     // Go through all sprites
     // Go through all sprites
     for (uint i = 0; i < spriteNodes.length; ++i)
     for (uint i = 0; i < spriteNodes.length; ++i)

+ 5 - 4
Bin/Data/Scripts/25_Urho2DParticle.as

@@ -49,7 +49,7 @@ void CreateScene()
 
 
     Camera@ camera = cameraNode.CreateComponent("Camera");
     Camera@ camera = cameraNode.CreateComponent("Camera");
     camera.orthographic = true;
     camera.orthographic = true;
-    camera.SetOrthoSize(Vector2(graphics.width, graphics.height));
+    camera.SetOrthoSize(Vector2(graphics.width, graphics.height) / scene_.unitSize2D);
 
 
     ParticleModel2D@ particleModel = cache.GetResource("ParticleModel2D", "Urho2D/LavaFlow.plist");
     ParticleModel2D@ particleModel = cache.GetResource("ParticleModel2D", "Urho2D/LavaFlow.plist");
     if (particleModel is null)
     if (particleModel is null)
@@ -91,9 +91,10 @@ void HandleMouseMove(StringHash eventType, VariantMap& eventData)
 {
 {
     if (particleNode !is null)
     if (particleNode !is null)
     {
     {
-        int x = eventData["x"].GetInt();
-        int y = eventData["y"].GetInt();
-        particleNode.position = Vector3(x - graphics.width * 0.5f, -y + graphics.height * 0.5f, 0.0);
+        float x = eventData["x"].GetInt();
+        float y = eventData["y"].GetInt();
+        Camera@ camera = cameraNode.GetComponent("Camera");
+        particleNode.position = camera.ScreenToWorldPoint(Vector3(x / graphics.width, y / graphics.height, 10.0f));
     }
     }
 }
 }
 
 

+ 1 - 1
Source/Engine/Scene/Scene.cpp

@@ -61,7 +61,7 @@ Scene::Scene(Context* context) :
     checksum_(0),
     checksum_(0),
     timeScale_(1.0f),
     timeScale_(1.0f),
     elapsedTime_(0),
     elapsedTime_(0),
-    unitSize2D_(1.0f),
+    unitSize2D_(100.0f),
     smoothingConstant_(DEFAULT_SMOOTHING_CONSTANT),
     smoothingConstant_(DEFAULT_SMOOTHING_CONSTANT),
     snapThreshold_(DEFAULT_SNAP_THRESHOLD),
     snapThreshold_(DEFAULT_SNAP_THRESHOLD),
     updateEnabled_(true),
     updateEnabled_(true),

+ 20 - 17
Source/Engine/Urho2D/ParticleModel2D.cpp

@@ -155,37 +155,40 @@ bool ParticleModel2D::Load(Deserializer& source)
         }
         }
     }
     }
 
 
+    // Apply model scale
+    const float modelScale = 0.01f;
+
     duration_ = keyValueMapping["duration"].GetFloat();
     duration_ = keyValueMapping["duration"].GetFloat();
     emitterType_ = (EmitterType2D)(int)keyValueMapping["emitterType"].GetFloat();
     emitterType_ = (EmitterType2D)(int)keyValueMapping["emitterType"].GetFloat();
 
 
-    sourcePositionVariance_.x_ = keyValueMapping["sourcePositionVariancex"].GetFloat();
-    sourcePositionVariance_.y_ = keyValueMapping["sourcePositionVariancey"].GetFloat();    
+    sourcePositionVariance_.x_ = keyValueMapping["sourcePositionVariancex"].GetFloat() * modelScale;
+    sourcePositionVariance_.y_ = keyValueMapping["sourcePositionVariancey"].GetFloat() * modelScale;
 
 
     maxParticles_ = (unsigned)keyValueMapping["maxParticles"].GetFloat();
     maxParticles_ = (unsigned)keyValueMapping["maxParticles"].GetFloat();
     particleLifeSpan_ = keyValueMapping["particleLifespan"].GetFloat();
     particleLifeSpan_ = keyValueMapping["particleLifespan"].GetFloat();
 
 
     particleLifeSpanVariance_ = keyValueMapping["particleLifespanVariance"].GetFloat();
     particleLifeSpanVariance_ = keyValueMapping["particleLifespanVariance"].GetFloat();
-    startParticleSize_ = keyValueMapping["startParticleSize"].GetFloat();
-    startParticleSizeVariance_ = keyValueMapping["startParticleSizeVariance"].GetFloat();
-    endParticleSize_ = keyValueMapping["finishParticleSize"].GetFloat();
-    endParticleSizeVariance_ = keyValueMapping["finishParticleSizeVariance"].GetFloat();
+    startParticleSize_ = keyValueMapping["startParticleSize"].GetFloat() * modelScale;
+    startParticleSizeVariance_ = keyValueMapping["startParticleSizeVariance"].GetFloat() * modelScale;
+    endParticleSize_ = keyValueMapping["finishParticleSize"].GetFloat() * modelScale;
+    endParticleSizeVariance_ = keyValueMapping["finishParticleSizeVariance"].GetFloat() * modelScale;
     emitAngle_ = keyValueMapping["angle"].GetFloat();
     emitAngle_ = keyValueMapping["angle"].GetFloat();
     emitAngleVariance_ = keyValueMapping["angleVariance"].GetFloat();
     emitAngleVariance_ = keyValueMapping["angleVariance"].GetFloat();
 
 
-    speed_ = keyValueMapping["speed"].GetFloat();
-    speedVariance_ = keyValueMapping["speedVariance"].GetFloat();
+    speed_ = keyValueMapping["speed"].GetFloat() * modelScale;
+    speedVariance_ = keyValueMapping["speedVariance"].GetFloat() * modelScale;
 
 
-    gravity_.x_ = keyValueMapping["gravityx"].GetFloat();
-    gravity_.y_ = keyValueMapping["gravityy"].GetFloat();
+    gravity_.x_ = keyValueMapping["gravityx"].GetFloat() * modelScale;
+    gravity_.y_ = keyValueMapping["gravityy"].GetFloat() * modelScale;
 
 
-    radialAcceleration_ = keyValueMapping["radialAcceleration"].GetFloat();
-    radialAccelerationVariance_ = keyValueMapping["radialAccelVariance"].GetFloat();
-    tangentialAcceleration_ = keyValueMapping["tangentialAcceleration"].GetFloat();
-    tangentialAccelerationVariance_ = keyValueMapping["tangentialAccelVariance"].GetFloat();
+    radialAcceleration_ = keyValueMapping["radialAcceleration"].GetFloat() * modelScale;
+    radialAccelerationVariance_ = keyValueMapping["radialAccelVariance"].GetFloat() * modelScale;
+    tangentialAcceleration_ = keyValueMapping["tangentialAcceleration"].GetFloat() * modelScale;
+    tangentialAccelerationVariance_ = keyValueMapping["tangentialAccelVariance"].GetFloat() * modelScale;
 
 
-    maxRadius_ = keyValueMapping["maxRadius"].GetFloat();
-    maxRadiusVariance_ = keyValueMapping["maxRadiusVariance"].GetFloat();
-    minRadius_ = keyValueMapping["minRadius"].GetFloat();
+    maxRadius_ = keyValueMapping["maxRadius"].GetFloat() * modelScale;
+    maxRadiusVariance_ = keyValueMapping["maxRadiusVariance"].GetFloat() * modelScale;
+    minRadius_ = keyValueMapping["minRadius"].GetFloat() * modelScale;
     rotatePerSecond_ = keyValueMapping["rotatePerSecond"].GetFloat();
     rotatePerSecond_ = keyValueMapping["rotatePerSecond"].GetFloat();
     rotatePerSecondVariance_ = keyValueMapping["rotatePerSecondVariance"].GetFloat();
     rotatePerSecondVariance_ = keyValueMapping["rotatePerSecondVariance"].GetFloat();
 
 

+ 3 - 3
Source/Samples/24_Urho2DSprite/Urho2DSprite.cpp

@@ -84,8 +84,8 @@ void Urho2DSprite::CreateScene()
     camera->SetOrthographic(true);
     camera->SetOrthographic(true);
 
 
     Graphics* graphics = GetSubsystem<Graphics>();
     Graphics* graphics = GetSubsystem<Graphics>();
-    float width = (float)graphics->GetWidth();
-    float height = (float)graphics->GetHeight();
+    float width = (float)graphics->GetWidth() / scene_->GetUnitSize2D();
+    float height = (float)graphics->GetHeight() / scene_->GetUnitSize2D();
     camera->SetOrthoSize(Vector2(width, height));
     camera->SetOrthoSize(Vector2(width, height));
 
 
     ResourceCache* cache = GetSubsystem<ResourceCache>();
     ResourceCache* cache = GetSubsystem<ResourceCache>();
@@ -168,7 +168,7 @@ void Urho2DSprite::MoveCamera(float timeStep)
     Input* input = GetSubsystem<Input>();
     Input* input = GetSubsystem<Input>();
 
 
     // Movement speed as world units per second
     // Movement speed as world units per second
-    const float MOVE_SPEED = 400.0f;
+    const float MOVE_SPEED = 4.0f;
 
 
     // Read WASD keys and move the camera scene node to the corresponding direction if they are pressed
     // Read WASD keys and move the camera scene node to the corresponding direction if they are pressed
     // Use the TranslateRelative() function to move relative to the node's orientation. Alternatively we could
     // Use the TranslateRelative() function to move relative to the node's orientation. Alternatively we could

+ 3 - 2
Source/Samples/25_Urho2DParticle/Urho2DParticle.cpp

@@ -82,7 +82,7 @@ void Urho2DParticle::CreateScene()
     camera->SetOrthographic(true);
     camera->SetOrthographic(true);
 
 
     Graphics* graphics = GetSubsystem<Graphics>();
     Graphics* graphics = GetSubsystem<Graphics>();
-    camera->SetOrthoSize(Vector2((float)graphics->GetWidth(), (float)graphics->GetHeight()));
+    camera->SetOrthoSize(Vector2((float)graphics->GetWidth(), (float)graphics->GetHeight()) / scene_->GetUnitSize2D());
 
 
     ResourceCache* cache = GetSubsystem<ResourceCache>();
     ResourceCache* cache = GetSubsystem<ResourceCache>();
     ParticleModel2D* particleModel = cache->GetResource<ParticleModel2D>("Urho2D/LavaFlow.plist");
     ParticleModel2D* particleModel = cache->GetResource<ParticleModel2D>("Urho2D/LavaFlow.plist");
@@ -132,6 +132,7 @@ void Urho2DParticle::HandleMouseMove(StringHash eventType, VariantMap& eventData
         float x = (float)eventData[P_X].GetInt();
         float x = (float)eventData[P_X].GetInt();
         float y = (float)eventData[P_Y].GetInt();
         float y = (float)eventData[P_Y].GetInt();
         Graphics* graphics = GetSubsystem<Graphics>();
         Graphics* graphics = GetSubsystem<Graphics>();
-        particleNode_->SetPosition(Vector3(x - graphics->GetWidth() * 0.5f, -y + graphics->GetHeight() * 0.5f, 0.0));
+        Camera* camera = cameraNode_->GetComponent<Camera>();
+        particleNode_->SetPosition(camera->ScreenToWorldPoint(Vector3(x / graphics->GetWidth(), y / graphics->GetHeight(), 10.0f)));
     }
     }
 }
 }