Example #1
0
	void CoreLayer2D_Imp::DrawLineAdditionally(Vector2DF point1, Vector2DF point2, float thickness, Color color, AlphaBlendMode alphaBlend, int32_t priority)
	{
		Vector2DF vector = point2 - point1;

		auto binorm = vector;
		{
			auto deg = binorm.GetDegree();
			deg += 90;
			binorm.SetDegree(deg);
			binorm.Normalize();
		}

		auto halfThickness = thickness / 2;
		
		std::array<Vector2DF, 4> pos = { point1 + binorm*halfThickness, point2 + binorm*halfThickness, point2 - binorm*halfThickness, point1 - binorm*halfThickness };
		std::array<Color, 4> col = { color, color, color, color };
		std::array<Vector2DF, 4> uv = { Vector2DF(0, 0), Vector2DF(0, 0), Vector2DF(0, 0), Vector2DF(0, 0) };

		Sprite sprite;

		sprite.pos = pos;
		sprite.col = col;
		sprite.uv = uv;
		sprite.Texture_ = nullptr;
		sprite.AlphaBlend_ = alphaBlend;
		sprite.Priority = priority;

		sprites.push_back(sprite);
	}
	void CoreCircleCollider_Imp::Update() {

		if (ownerObject == nullptr) {
			return;
		}

		auto matrix = ownerObject->GetAbsoluteMatrixToTransform();

		auto vc = Vector3DF(center.X, center.Y, 1);
		auto res1 = matrix * vc;
		auto tcenter = Vector2DF(res1.X, res1.Y);

		auto vr = Vector3DF(center.X + radius, center.Y, 1);
		auto res2 = matrix * vr;
		auto tradpos = Vector2DF(res2.X, res2.Y);
		auto tradius = (tcenter - tradpos).GetLength();

		aabb.X = tcenter.X - tradius;
		aabb.Y = tcenter.Y - tradius;
		aabb.Width = aabb.Height = tradius * 2;

		b2circleShape.m_p = b2Vec2(tcenter.X, tcenter.Y);
		b2circleShape.m_radius = tradius;

		coreCollision2DManager->NotifyLastTransformed(this);
	}
	Vector2DF MaterialPropertyBlock_Imp::GetVector2DF(const achar* name)
	{
		if (values.count(name) > 0)
		{
			if (values[name].ValueType != ShaderVariableType::Vector2DF) return Vector2DF(0.0f, 0.0f);
			return Vector2DF(values[name].Data.Float4[0], values[name].Data.Float4[1]);
		}
		return Vector2DF(0.0f, 0.0f);
	}
	void CorePolygonShape_Imp::DivideToTriangles()
	{

		float maxLeft = FLT_MAX;
		float maxRight = -FLT_MAX;
		float maxHigh = FLT_MAX;
		float maxLow = -FLT_MAX;

		for (auto vertex : vertexes)
		{
			maxLeft = Min(maxLeft, vertex.X);
			maxRight = Max(maxRight, vertex.X);
			maxHigh = Min(maxHigh, vertex.Y);
			maxLow = Max(maxLow, vertex.Y);
		}

		std::vector<p2t::Point*> points;

		for (auto vertex : vertexes)
		{
			p2t::Point* point = new p2t::Point(vertex.X, vertex.Y);
			points.push_back(point);
		}

		p2t::CDT* cdt = new p2t::CDT(points);
		cdt->Triangulate();

		auto outTriangles = cdt->GetTriangles();

		for (auto tri : outTriangles)
		{
			CoreTriangleShape* triangle = new CoreTriangleShape_Imp();
			for (int i = 0; i < 3; ++i)
			{
				auto p = tri->GetPoint(i);
				triangle->SetPointByIndex(Vector2DF(p->x, p->y), i);

				float uvX = (p->x - maxLeft) / (maxRight - maxLeft);
				float uvY = (p->y - maxHigh) / (maxLow - maxHigh);
				triangle->SetUVByIndex(Vector2DF(uvX, uvY), i);
			}
			triangles.push_back(triangle);
		}


		delete cdt;

		for (auto point : points)
		{
			delete point;
		}
	}
	World::World(int resolution, RectF worldRange) :
		resolution(resolution),
		initialResolution(resolution),
		worldRange(worldRange),
		nextFirstSortedKey(0),
		nextSecondSortedKey(0),
		outOfRangeObjectsCount(0),
		upperLeft_(Vector2DF(0, 0)),
		lowerRight_(Vector2DF(0, 0)),
		minRadius_(FLT_MAX)
	{
		initQuadtree();
		//initQuadtreeGrids(resolution, worldRange);
	}
	CoreCircleCollider_Imp::CoreCircleCollider_Imp() :
		center(Vector2DF(0, 0)),
		radius(0),
		isVisible(false)
	{

	}
	void CoreRectangleCollider_Imp::Update() {

		if (ownerObject == nullptr) {
			return;
		}

		auto vertexes = area.GetVertexes();

		std::vector<b2Vec2> polyPoints;
		culling2d::Vector2DF minPos = culling2d::Vector2DF(FLT_MAX, FLT_MAX);
		culling2d::Vector2DF maxPos = culling2d::Vector2DF(FLT_MIN, FLT_MIN);

		auto matrix = ownerObject->GetAbsoluteMatrixToTransform();
		for (auto vertex : vertexes)
		{

			auto vc = Vector3DF(vertex.X, vertex.Y, 1);
			auto res1 = matrix * vc;
			auto pos = Vector2DF(res1.X, res1.Y);

			polyPoints.push_back(b2Vec2(pos.X, pos.Y));
			minPos.X = Min(minPos.X, pos.X);
			minPos.Y = Min(minPos.Y, pos.Y);
			maxPos.X = Max(maxPos.X, pos.X);
			maxPos.Y = Max(maxPos.Y, pos.Y);
		}
		aabb.X = minPos.X;
		aabb.Y = minPos.Y;
		aabb.Width = maxPos.X - minPos.X;
		aabb.Height = maxPos.Y - minPos.Y;

		b2polygonShape.Set(polyPoints.data(), polyPoints.size());

		coreCollision2DManager->NotifyLastTransformed(this);
	}
	CoreCircleShape_Imp::CoreCircleShape_Imp()
		:position(Vector2DF(0,0))
		, innerDiameter(0)
		, outerDiameter(0)
		, numberOfCorners(96)
		, angle(0)
	{

	}
	void CoreGeometryObject2D_Imp::Draw(Renderer2D* renderer)
	{
		if (!GetAbsoluteBeingDrawn() || !GetIsAlive() || m_shape == nullptr)
		{
			return;
		}

		auto shape_Imp = CoreShape2DToImp(m_shape);

		auto& triangles = shape_Imp->GetDividedTriangles();

		if (triangles.size() == 0)
		{
			auto layer = GetLayer();
			auto g = (Graphics_Imp*)layer->GetGraphicsImp();
			g->GetLog()->Write("無効な形状を描画しました。正しく表示されない場合があります。", LogLevel::Warning);
		}

		for (auto triangle : triangles)
		{
			std::array<Vector2DF, 4> position;
			std::array<Vector2DF, 4> uvs;

			for (int i = 0; i < 3; ++i)
			{
				position[i] = triangle->GetPointByIndex(i);
				uvs[i] = triangle->GetUVByIndex(i);
			}
			position[3] = position[2];
			uvs[3] = uvs[2];

			auto matrix = GetAbsoluteMatrixToTransform();
			for (auto& pos : position)
			{
				pos -= centerPosition;
				auto v3 = Vector3DF(pos.X, pos.Y, 1);
				auto result = matrix * v3;
				pos = Vector2DF(result.X, result.Y);
			}

			Color color[4];
			auto col = GetAbsoluteColor();
			color[0] = col;
			color[1] = col;
			color[2] = col;
			color[3] = col;

			renderer->AddSprite(
				position.data(),
				color,
				uvs.data(),
				(m_shape->GetShapeType() == ShapeType::LineShape) ? nullptr : m_texture,
				alphaBlendMode,
				GetAbsoluteDrawingPriority(),
				m_textureFilterType);
		}
	}
	CoreGeometryObject2D_Imp::CoreGeometryObject2D_Imp(Graphics_Imp* graphics)
		: CoreDrawnObject2D_Imp(graphics)
		, m_shape(nullptr)
		, alphaBlendMode(AlphaBlendMode::Blend)
		, centerPosition(Vector2DF())
		, m_textureFilterType(TextureFilterType::Nearest)
		, m_texture(nullptr)
	{

	}
	void CorePolygonShape_Imp::CalculateBoundingCircle()
	{
		float maxLeft = FLT_MAX;
		float maxRight = -FLT_MAX;
		float maxHigh = FLT_MAX;
		float maxLow = -FLT_MAX;

		for (auto vertex : vertexes)
		{
			maxLeft = Min(maxLeft, vertex.X);
			maxRight = Max(maxRight, vertex.X);
			maxHigh = Min(maxHigh, vertex.Y);
			maxLow = Max(maxLow, vertex.Y);
		}

		auto center = (Vector2DF(maxLeft, maxHigh) + Vector2DF(maxRight, maxLow)) / 2.0f;
		auto radius = (Vector2DF(maxLeft, maxHigh) - center).GetLength();

		boundingCircle = culling2d::Circle(culling2d::Vector2DF(center.X, center.Y), radius);
	}
	void CoreCircleCollider_Imp::DrawVisibleCollisionsAdditionally(CoreLayer2D* layer) {
		layer->DrawCircleAdditionally(
			Vector2DF(b2circleShape.m_p.x, b2circleShape.m_p.y), 
			b2circleShape.m_radius * 2, 
			0, 
			asd::Color(255, 0, 0, 100), 
			100, 
			0, 
			nullptr, 
			asd::AlphaBlendMode::Blend,
			10000);
	}
	void ProfilerViewer_Imp::DrawProfiledInfoSection(int top)
	{
		DrawTextSprite(
			Vector2DF(COLUMN_HEADER_OFFSET, top),
			SECTION_HEADER_COLOR,
			ToAString("Profiled Information"));

		DrawTextSprite(
			Vector2DF(COLUMN1_OFFSET, top + ROW_HEIGHT),
			HEADER_COLOR,
			ToAString("ID"));

		DrawTextSprite(
			Vector2DF(COLUMN2_OFFSET, top + ROW_HEIGHT),
			HEADER_COLOR,
			ToAString("Time(ns)"));

		DrawTextSprite(
			Vector2DF(COLUMN3_OFFSET, top + ROW_HEIGHT),
			HEADER_COLOR,
			ToAString("Processor"));

		int bodyTop = top + ROW_HEIGHT * 2;
		int index = 0;
		for (auto& profile : m_profiler->GetProfiles())
		{
			auto perf = profile->GetLastLog();
			if (perf == nullptr)
			{
				continue;
			}

			int time = perf->GetEndTime() - perf->GetStartTime();

			DrawTextSprite(
				Vector2DF(COLUMN1_OFFSET, bodyTop + index * ROW_HEIGHT),
				CONTENT_COLOR,
				ToAString(to_string(profile->GetID()).c_str()));

			DrawTextSprite(
				Vector2DF(COLUMN2_OFFSET, bodyTop + index * ROW_HEIGHT),
				CONTENT_COLOR,
				ToAString(to_string(time).c_str()));

			DrawTextSprite(
				Vector2DF(COLUMN3_OFFSET, bodyTop + index * ROW_HEIGHT),
				CONTENT_COLOR,
				ToAString(to_string(perf->GetProcessorNumber()).c_str()));

			++index;
		}

		DrawSprite(RectF(0, top, m_windowSize.X, (index + 2)*ROW_HEIGHT + SECTION_SPAN), Color(0, 64, 64, 128), 0);
	}
	int ProfilerViewer_Imp::DrawGlobalInfoSection()
	{
		DrawSprite(RectF(0, 0, m_windowSize.X, SECTION_SPAN * 2 + ROW_HEIGHT * 2), Color(64, 64, 0, 128), 0);

		DrawTextSprite(
			Vector2DF(COLUMN_HEADER_OFFSET, SECTION_SPAN),
			SECTION_HEADER_COLOR,
			ToAString("Global Information"));

		auto fpsStr = to_string((int)m_core->GetCurrentFPS()) + "fps";
		DrawTextSprite(
			Vector2DF(COLUMN1_OFFSET, SECTION_SPAN + ROW_HEIGHT),
			CONTENT_COLOR,
			ToAString(fpsStr.c_str()));

		auto drawCallStr = to_string(m_graphics->GetDrawCallCount()) + "DrawCall";
		DrawTextSprite(
			Vector2DF(DRAWCALL_OFFSET, SECTION_SPAN + ROW_HEIGHT),
			CONTENT_COLOR,
			ToAString(drawCallStr.c_str()));

		return SECTION_SPAN * 2 + ROW_HEIGHT * 2;
	}
Example #15
0
	void CoreLayer2D_Imp::DrawAdditionalObjects()
	{

#if __CULLING_2D__
		//一時的にエフェクトは無条件に描画(本来ここではない)
		for (auto& x : m_objects)
		{
			if (x->GetIsAlive() && x->GetObjectType() == Object2DType::Effect)
			{
				x->Draw(m_renderer);
			}
		}
#endif

		for (auto& sprite : sprites)
		{
			m_renderer->AddSprite(
				sprite.pos.data(),
				sprite.col.data(),
				sprite.uv.data(),
				sprite.Texture_.get(),
				sprite.AlphaBlend_,
				sprite.Priority);
		}

		for (auto& text : texts)
		{
			Matrix33 matP;
			Matrix33 mat;
			mat.SetTranslation(text.Position_.X, text.Position_.Y);

			m_renderer->AddText(
				matP,
				mat,
				Vector2DF(),
				false,
				false,
				text.Color_,
				text.Font_.get(),
				text.Text_.c_str(),
				text.WritingDirection_,
				text.AlphaBlend_,
				text.Priority_,
				0,
				0);
		}
	}
	int ProfilerViewer_Imp::DrawLayerInfoSection(int top)
	{
		DrawTextSprite(
			Vector2DF(COLUMN_HEADER_OFFSET, top),
			SECTION_HEADER_COLOR,
			ToAString("Layer Information"));

		DrawTextSprite(
			Vector2DF(COLUMN1_OFFSET, top + ROW_HEIGHT),
			HEADER_COLOR,
			ToAString("Name"));

		DrawTextSprite(
			Vector2DF(COLUMN2_OFFSET + LAYER_NAME_OFFSET, top + ROW_HEIGHT),
			HEADER_COLOR,
			ToAString("Time(ns)"));

		DrawTextSprite(
			Vector2DF(COLUMN3_OFFSET + LAYER_NAME_OFFSET, top + ROW_HEIGHT),
			HEADER_COLOR,
			ToAString("Objects"));

		auto bodyTop = top + ROW_HEIGHT * 2;
		int index = 0;
		for (auto& profile : m_layerProfiler->GetProfiles())
		{
			DrawTextSprite(
				Vector2DF(COLUMN1_OFFSET, bodyTop + index * ROW_HEIGHT),
				CONTENT_COLOR,
				profile->GetName());

			DrawTextSprite(
				Vector2DF(COLUMN2_OFFSET + LAYER_NAME_OFFSET, bodyTop + index * ROW_HEIGHT),
				CONTENT_COLOR,
				ToAString(to_string(profile->GetTime()).c_str()));

			DrawTextSprite(
				Vector2DF(COLUMN3_OFFSET + LAYER_NAME_OFFSET, bodyTop + index * ROW_HEIGHT),
				CONTENT_COLOR,
				ToAString(to_string(profile->GetObjectCount()).c_str()));

			++index;
		}

		DrawSprite(RectF(0, top, m_windowSize.X, (index + 2)*ROW_HEIGHT + SECTION_SPAN), Color(64, 0, 64, 128), 0);

		return top + (index + 2) * ROW_HEIGHT + SECTION_SPAN;
	}
	void ProfilerViewer_Imp::DrawTextSprite(Vector2DF position, Color color, astring text)
	{
		m_renderer->AddText(
			Matrix33().SetIdentity(),
			Matrix33().SetTranslation(position.X, position.Y),
			Vector2DF(),
			false,
			false,
			color,
			m_font.get(),
			text.c_str(),
			WritingDirection::Horizontal,
			AlphaBlendMode::Blend,
			1,
			0,
			0,
			false);
	}
	void CoreGeometryObject2D_Imp::CalculateBoundingCircle()
	{
		auto shape_Imp = CoreShape2DToImp(m_shape);
		if (shape_Imp == nullptr)
		{
			m_boundingCircle = culling2d::Circle(culling2d::Vector2DF(), 0);
			return;
		}

		m_boundingCircle = shape_Imp->GetBoundingCircle();

		std::array<Vector2DF, 4> position;

		auto p1 = m_boundingCircle.Position - culling2d::Vector2DF(m_boundingCircle.Radius, 0);
		position[0] = Vector2DF(p1.X, p1.Y);

		auto p2 = m_boundingCircle.Position + culling2d::Vector2DF(m_boundingCircle.Radius, 0);
		position[1] = Vector2DF(p2.X, p2.Y);

		auto p3 = m_boundingCircle.Position - culling2d::Vector2DF(0, m_boundingCircle.Radius);
		position[2] = Vector2DF(p3.X, p3.Y);

		auto p4 = m_boundingCircle.Position + culling2d::Vector2DF(0, m_boundingCircle.Radius);
		position[3] = Vector2DF(p4.X, p4.Y);

		auto parentMatrix = GetParentsMatrix();
		auto matrix = GetMatrixToTransform();

		Vector2DF sum = Vector2DF();
		for (auto& pos : position)
		{
			pos -= centerPosition;
			auto v3 = Vector3DF(pos.X, pos.Y, 1);
			auto result = parentMatrix * matrix * v3;
			pos = Vector2DF(result.X, result.Y);
			sum += pos;
		}

		auto c = sum / 4.0f;
		auto r = 0.0f;

		for (auto& pos : position)
		{
			r = Max(r, (pos - c).GetLength());
		}

		m_boundingCircle = culling2d::Circle(culling2d::Vector2DF(c.X, c.Y), r);
	}
	void World::Update()
	{
		for (auto improperObject : improperObjects)
		{
			auto grid = mapObjectToGrid[improperObject];
			grid->RemoveObject(improperObject);
			auto newGrid = AddObjectInternal(improperObject);
			assert(newGrid != nullptr);
			mapObjectToGrid[improperObject] = newGrid;
		}

		improperObjects.clear();

		if (outOfRangeObjectsCount >= CHANGE_OBJECT_NUM)
		{
			Vector2DF center = (upperLeft_ + lowerRight_) / 2;
			float diam = Max(lowerRight_.X - upperLeft_.X, lowerRight_.Y - upperLeft_.Y) + ALLOWANCE * 2;
			Vector2DF upper = center - Vector2DF(diam, diam) / 2;

			ResetWorld(initialResolution, RectF(upper.X, upper.Y, diam, diam));
		}
	}
Example #20
0
	void TransitionFade::OnUpdate()
	{
		float cp = 0.0f;
		float cn = 0.0f;

		if (time < fadeoutDuration)
		{
			cp = 1.0f - time / fadeoutDuration;
		}
		else if (time <= fadeoutDuration + fadeinDuration)
		{
			if (!IsSceneChanged())
			{
				ChangeScene();
			}

			cn = (time - fadeoutDuration) / fadeinDuration;
		}
		else
		{
			if (!IsSceneChanged())
			{
				ChangeScene();
			}

			if (!GetIsFinished())
			{
				Finish();
			}

			cn = 1.0f;
		}

		uint8_t cp_ = Clamp(cp * 255, 255, 0);
		uint8_t cn_ = Clamp(cn * 255, 255, 0);

		DrawRectangleWithPreviousScene(
			Vector2DF(0.0, 0.0),
			Vector2DF(1.0, 0.0),
			Vector2DF(1.0, 1.0),
			Vector2DF(0.0, 1.0),
			Color(255, 255, 255, cp_),
			Color(255, 255, 255, cp_),
			Color(255, 255, 255, cp_),
			Color(255, 255, 255, cp_),
			Vector2DF(0.0, 0.0),
			Vector2DF(1.0, 0.0),
			Vector2DF(1.0, 1.0),
			Vector2DF(0.0, 1.0));

		DrawRectangleWithNextScene(
			Vector2DF(0.0, 0.0),
			Vector2DF(1.0, 0.0),
			Vector2DF(1.0, 1.0),
			Vector2DF(0.0, 1.0),
			Color(255, 255, 255, cn_),
			Color(255, 255, 255, cn_),
			Color(255, 255, 255, cn_),
			Color(255, 255, 255, cn_),
			Vector2DF(0.0, 0.0),
			Vector2DF(1.0, 0.0),
			Vector2DF(1.0, 1.0),
			Vector2DF(0.0, 1.0));

		time += Engine::GetDeltaTime();
	}
	//----------------------------------------------------------------------------------
	//
	//----------------------------------------------------------------------------------
	Vector2DF Vector2DF::operator/(float right)
	{
		return Vector2DF(X / right, Y / right);
	}
	//----------------------------------------------------------------------------------
	//
	//----------------------------------------------------------------------------------
	Vector2DF Vector2DF::operator*(float right)
	{
		return Vector2DF(X * right, Y * right);
	}
	//----------------------------------------------------------------------------------
	//
	//----------------------------------------------------------------------------------
	Vector2DF Vector2DF::operator/(const Vector2DF& right)
	{
		return Vector2DF(X / right.X, Y / right.Y);
	}
	//----------------------------------------------------------------------------------
	//
	//----------------------------------------------------------------------------------
	Vector2DF Vector2DF::operator*(const Vector2DF& right)
	{
		return Vector2DF(X * right.X, Y * right.Y);
	}
	//----------------------------------------------------------------------------------
	//
	//----------------------------------------------------------------------------------
	Vector2DF Vector2DF::operator-(const Vector2DF& right)
	{
		return Vector2DF(X - right.X, Y - right.Y);
	}
	//----------------------------------------------------------------------------------
	//
	//----------------------------------------------------------------------------------
	Vector2DF Vector2DF::operator+(const Vector2DF& right)
	{
		return Vector2DF(X + right.X, Y + right.Y);
	}
	//----------------------------------------------------------------------------------
	//
	//----------------------------------------------------------------------------------
	Vector2DF Vector2DF::operator-()
	{
		return Vector2DF(-X, -Y);
	}
	void CoreRectangleCollider_Imp::DrawVisibleCollisionsAdditionally(CoreLayer2D* layer) {
		auto vertices = b2polygonShape.m_vertices;
		layer->DrawTriangleAdditionally(
			Vector2DF(vertices[0].x, vertices[0].y),
			Vector2DF(vertices[1].x, vertices[1].y),
			Vector2DF(vertices[2].x, vertices[2].y),
			Color(255, 0, 0, 100),
			Vector2DF(),
			Vector2DF(),
			Vector2DF(),
			nullptr,
			AlphaBlendMode::Blend,
			10000);

		layer->DrawTriangleAdditionally(
			Vector2DF(vertices[2].x, vertices[2].y),
			Vector2DF(vertices[3].x, vertices[3].y),
			Vector2DF(vertices[0].x, vertices[0].y),
			Color(255, 0, 0, 100),
			Vector2DF(),
			Vector2DF(),
			Vector2DF(),
			nullptr,
			AlphaBlendMode::Blend,
			10000);
	}