Explorar el Código

Gizmo icon rendering works

Marko Pintera hace 11 años
padre
commit
9f993f134b

+ 3 - 3
BansheeEditor/Include/BsGizmoManager.h

@@ -175,9 +175,9 @@ namespace BansheeEngine
 		struct IconMaterialData
 		struct IconMaterialData
 		{
 		{
 			MaterialProxyPtr proxy;
 			MaterialProxyPtr proxy;
-			GpuParamsPtr mFragParams;
-			GpuParamMat4 mViewProj;
-			GpuParamTexture mTexture;
+			GpuParamsPtr mFragParams[2];
+			GpuParamMat4 mViewProj[2];
+			GpuParamTexture mTexture[2];
 		};
 		};
 
 
 		struct PickingMaterialData
 		struct PickingMaterialData

+ 2 - 0
BansheeEditor/Include/BsProjectLibrary.h

@@ -65,6 +65,8 @@ namespace BansheeEngine
 		void moveEntry(const Path& oldPath, const Path& newPath);
 		void moveEntry(const Path& oldPath, const Path& newPath);
 		void deleteEntry(const Path& path);
 		void deleteEntry(const Path& path);
 
 
+		const Path& getResourcesFolder() const { return mResourcesFolder; }
+
 		Event<void(const Path&)> onEntryRemoved;
 		Event<void(const Path&)> onEntryRemoved;
 		Event<void(const Path&)> onEntryAdded;
 		Event<void(const Path&)> onEntryAdded;
 	private:
 	private:

+ 2 - 2
BansheeEditor/Source/BsBuiltinEditorResources.cpp

@@ -1430,13 +1430,13 @@ namespace BansheeEngine
 
 
 		DEPTH_STENCIL_STATE_DESC depthStencilState0Desc;
 		DEPTH_STENCIL_STATE_DESC depthStencilState0Desc;
 		depthStencilState0Desc.depthWriteEnable = false;
 		depthStencilState0Desc.depthWriteEnable = false;
-		depthStencilState0Desc.backStencilComparisonFunc = CMPF_LESS_EQUAL;
+		depthStencilState0Desc.depthComparisonFunc = CMPF_LESS_EQUAL;
 
 
 		HDepthStencilState depthStencilState0 = DepthStencilState::create(depthStencilState0Desc);
 		HDepthStencilState depthStencilState0 = DepthStencilState::create(depthStencilState0Desc);
 
 
 		DEPTH_STENCIL_STATE_DESC depthStencilState1Desc;
 		DEPTH_STENCIL_STATE_DESC depthStencilState1Desc;
 		depthStencilState1Desc.depthWriteEnable = false;
 		depthStencilState1Desc.depthWriteEnable = false;
-		depthStencilState1Desc.backStencilComparisonFunc = CMPF_GREATER;
+		depthStencilState1Desc.depthComparisonFunc = CMPF_GREATER;
 
 
 		HDepthStencilState depthStencilState1 = DepthStencilState::create(depthStencilState1Desc);
 		HDepthStencilState depthStencilState1 = DepthStencilState::create(depthStencilState1Desc);
 
 

+ 26 - 18
BansheeEditor/Source/BsGizmoManager.cpp

@@ -456,7 +456,7 @@ namespace BansheeEngine
 		}
 		}
 
 
 		iconRenderData = bs_shared_ptr<IconRenderDataVec>();
 		iconRenderData = bs_shared_ptr<IconRenderDataVec>();
-		UINT32 lastTextureIdx = 0;
+		UINT32 lastTextureIdx = std::numeric_limits<UINT32>::max();
 		HTexture curTexture;
 		HTexture curTexture;
 
 
 		// Note: This assumes the meshes will be rendered using the same camera
 		// Note: This assumes the meshes will be rendered using the same camera
@@ -466,7 +466,9 @@ namespace BansheeEngine
 			SortedIconData& sortedIconData = mSortedIconData[i];
 			SortedIconData& sortedIconData = mSortedIconData[i];
 			const IconData& curIconData = iconData[sortedIconData.iconIdx];
 			const IconData& curIconData = iconData[sortedIconData.iconIdx];
 
 
-			if (curTexture != curIconData.texture)
+			HTexture atlasTexture = curIconData.texture->getTexture();
+
+			if (curTexture != atlasTexture)
 			{
 			{
 				UINT32 numIconsPerTexture = i - lastTextureIdx;
 				UINT32 numIconsPerTexture = i - lastTextureIdx;
 				if (numIconsPerTexture > 0)
 				if (numIconsPerTexture > 0)
@@ -474,11 +476,11 @@ namespace BansheeEngine
 					iconRenderData->push_back(IconRenderData());
 					iconRenderData->push_back(IconRenderData());
 					IconRenderData& renderData = iconRenderData->back();
 					IconRenderData& renderData = iconRenderData->back();
 					renderData.count = numIconsPerTexture;
 					renderData.count = numIconsPerTexture;
-					renderData.texture = curTexture;
+					renderData.texture = atlasTexture;
 				}
 				}
 
 
 				lastTextureIdx = i;
 				lastTextureIdx = i;
-				curTexture = curIconData.texture;
+				curTexture = atlasTexture;
 			}
 			}
 
 
 			UINT32 iconWidth = curIconData.texture->getWidth();
 			UINT32 iconWidth = curIconData.texture->getWidth();
@@ -486,11 +488,9 @@ namespace BansheeEngine
 
 
 			limitIconSize(iconWidth, iconHeight);
 			limitIconSize(iconWidth, iconHeight);
 
 
-			Color normalColor, fadedColor;
-			calculateIconColors(curIconData.color, *camera.get(), iconHeight, curIconData.fixedScale, normalColor, fadedColor);
-
-			Vector3 position((float)sortedIconData.screenPosition.x, (float)sortedIconData.screenPosition.y, sortedIconData.distance);
-			// TODO - Does the depth need to be corrected since it was taken from a projective camera (probably)?
+			Vector3 position((float)sortedIconData.screenPosition.x, (float)sortedIconData.screenPosition.y, -sortedIconData.distance);
+			Vector3 projPosition = camera->projectPoint(position);
+			position.z = -projPosition.z;
 
 
 			float halfWidth = iconWidth * 0.5f;
 			float halfWidth = iconWidth * 0.5f;
 			float halfHeight = iconHeight * 0.5f;
 			float halfHeight = iconHeight * 0.5f;
@@ -499,14 +499,17 @@ namespace BansheeEngine
 			{
 			{
 				float iconScale = 1.0f;
 				float iconScale = 1.0f;
 				if (camera->getProjectionType() == PT_ORTHOGRAPHIC)
 				if (camera->getProjectionType() == PT_ORTHOGRAPHIC)
-					iconScale = cameraScale;
+					iconScale = cameraScale * ICON_TEXEL_WORLD_SIZE;
 				else
 				else
-					iconScale = cameraScale / sortedIconData.distance;
+					iconScale = (cameraScale * ICON_TEXEL_WORLD_SIZE) / sortedIconData.distance;
 
 
 				halfWidth *= iconScale;
 				halfWidth *= iconScale;
 				halfHeight *= iconScale;
 				halfHeight *= iconScale;
 			}
 			}
 
 
+			Color normalColor, fadedColor;
+			calculateIconColors(curIconData.color, *camera.get(), (UINT32)(halfHeight * 2.0f), curIconData.fixedScale, normalColor, fadedColor);
+
 			Vector3 positions[4];
 			Vector3 positions[4];
 			positions[0] = position + Vector3(-halfWidth, -halfHeight, 0.0f);
 			positions[0] = position + Vector3(-halfWidth, -halfHeight, 0.0f);
 			positions[1] = position + Vector3(halfWidth, -halfHeight, 0.0f);
 			positions[1] = position + Vector3(halfWidth, -halfHeight, 0.0f);
@@ -614,13 +617,17 @@ namespace BansheeEngine
 
 
 		{
 		{
 			MaterialProxyPtr proxy = mIconMaterial.proxy;
 			MaterialProxyPtr proxy = mIconMaterial.proxy;
-			GpuParamsPtr vertParams = proxy->params[proxy->passes[0].vertexProgParamsIdx];
+			GpuParamsPtr vertParams0 = proxy->params[proxy->passes[0].vertexProgParamsIdx];
+			GpuParamsPtr vertParams1 = proxy->params[proxy->passes[1].vertexProgParamsIdx];
 
 
-			vertParams->getParam("matViewProj", mIconMaterial.mViewProj);
+			vertParams0->getParam("matViewProj", mIconMaterial.mViewProj[0]);
+			vertParams1->getParam("matViewProj", mIconMaterial.mViewProj[1]);
 
 
-			mIconMaterial.mFragParams = proxy->params[proxy->passes[0].fragmentProgParamsIdx];
+			mIconMaterial.mFragParams[0] = proxy->params[proxy->passes[0].fragmentProgParamsIdx];
+			mIconMaterial.mFragParams[1] = proxy->params[proxy->passes[1].fragmentProgParamsIdx];
 
 
-			mIconMaterial.mFragParams->getTextureParam("mainTexture", mIconMaterial.mTexture);
+			mIconMaterial.mFragParams[0]->getTextureParam("mainTexture", mIconMaterial.mTexture[0]);
+			mIconMaterial.mFragParams[1]->getTextureParam("mainTexture", mIconMaterial.mTexture[1]);
 		}
 		}
 
 
 		{
 		{
@@ -744,7 +751,8 @@ namespace BansheeEngine
 		float far = rs.getMaximumDepthInputValue();
 		float far = rs.getMaximumDepthInputValue();
 
 
 		projMat.makeProjectionOrtho(left, right, top, bottom, near, far);
 		projMat.makeProjectionOrtho(left, right, top, bottom, near, far);
-		mIconMaterial.mViewProj.set(projMat);
+		mIconMaterial.mViewProj[0].set(projMat);
+		mIconMaterial.mViewProj[1].set(projMat);
 
 
 		for (UINT32 passIdx = 0; passIdx < 2; passIdx++)
 		for (UINT32 passIdx = 0; passIdx < 2; passIdx++)
 		{
 		{
@@ -753,8 +761,8 @@ namespace BansheeEngine
 			UINT32 curIndexOffset = 0;
 			UINT32 curIndexOffset = 0;
 			for (auto curRenderData : *renderData)
 			for (auto curRenderData : *renderData)
 			{
 			{
-				mIconMaterial.mTexture.set(curRenderData.texture);
-				rs.bindGpuParams(GPT_FRAGMENT_PROGRAM, mIconMaterial.mFragParams);
+				mIconMaterial.mTexture[passIdx].set(curRenderData.texture);
+				rs.bindGpuParams(GPT_FRAGMENT_PROGRAM, mIconMaterial.mFragParams[passIdx]);
 
 
 				rs.drawIndexed(curIndexOffset, curRenderData.count * 6, 0, curRenderData.count * 4);
 				rs.drawIndexed(curIndexOffset, curRenderData.count * 6, 0, curRenderData.count * 4);
 				curIndexOffset += curRenderData.count * 6;
 				curIndexOffset += curRenderData.count * 6;

+ 10 - 5
BansheeEngine/Include/BsCamera.h

@@ -364,6 +364,16 @@ namespace BansheeEngine
 		 */
 		 */
 		Ray screenPointToRay(const Vector2I& screenPoint) const;
 		Ray screenPointToRay(const Vector2I& screenPoint) const;
 
 
+		/**
+		 * @brief	Projects a point from view to clip space.
+		 */
+		Vector3 projectPoint(const Vector3& point) const;
+
+		/**
+		 * @brief	Un-projects a point in clip space to view space.
+		 */
+		Vector3 unprojectPoint(const Vector3& point) const;
+
         static const float INFINITE_FAR_PLANE_ADJUST; /**< Small constant used to reduce far plane projection to avoid inaccuracies. */
         static const float INFINITE_FAR_PLANE_ADJUST; /**< Small constant used to reduce far plane projection to avoid inaccuracies. */
 
 
 		/************************************************************************/
 		/************************************************************************/
@@ -407,11 +417,6 @@ namespace BansheeEngine
 		 */
 		 */
 		virtual void calcProjectionParameters(float& left, float& right, float& bottom, float& top) const;
 		virtual void calcProjectionParameters(float& left, float& right, float& bottom, float& top) const;
 
 
-		/**
-		 * @brief	Un-projects a point in clip space to world space.
-		 */
-		Vector3 unprojectPoint(const Vector3& point) const;
-
 		/**
 		/**
 		 * @brief	Recalculate frustum if dirty.
 		 * @brief	Recalculate frustum if dirty.
 		 */
 		 */

+ 23 - 13
BansheeEngine/Source/BsCamera.cpp

@@ -540,19 +540,7 @@ namespace BansheeEngine
 
 
 	Vector2 Camera::viewToClipPoint(const Vector3& viewPoint) const
 	Vector2 Camera::viewToClipPoint(const Vector3& viewPoint) const
 	{
 	{
-		Vector4 projPoint = getProjectionMatrix().multiply(Vector4(viewPoint.x, viewPoint.y, viewPoint.z, 1.0f));
-
-		if (projPoint.w > 1e-7f)
-		{
-			float invW = 1.0f / projPoint.w;
-			projPoint.x *= invW;
-			projPoint.y *= invW;
-		}
-		else
-		{
-			projPoint.x = 0.0f;
-			projPoint.y = 0.0f;
-		}
+		Vector3 projPoint = projectPoint(viewPoint);
 
 
 		return Vector2(projPoint.x, projPoint.y);
 		return Vector2(projPoint.x, projPoint.y);
 	}
 	}
@@ -587,6 +575,28 @@ namespace BansheeEngine
 		return Ray(near, Vector3::normalize(far - near));
 		return Ray(near, Vector3::normalize(far - near));
 	}
 	}
 
 
+	Vector3 Camera::projectPoint(const Vector3& point) const
+	{
+		Vector4 projPoint4(point.x, point.y, point.z, 1.0f);
+		projPoint4 = getProjectionMatrix().multiply(projPoint4);
+
+		if (projPoint4.w > 1e-7f)
+		{
+			float invW = 1.0f / projPoint4.w;
+			projPoint4.x *= invW;
+			projPoint4.y *= invW;
+			projPoint4.z *= invW;
+		}
+		else
+		{
+			projPoint4.x = 0.0f;
+			projPoint4.y = 0.0f;
+			projPoint4.z = 0.0f;
+		}
+
+		return Vector3(projPoint4.x, projPoint4.y, projPoint4.z);
+	}
+
 	Vector3 Camera::unprojectPoint(const Vector3& point) const
 	Vector3 Camera::unprojectPoint(const Vector3& point) const
 	{
 	{
 		Vector4 dir4(point.x, point.y, 0.95f, 1.0f); // 0.95f arbitrary
 		Vector4 dir4(point.x, point.y, 0.95f, 1.0f); // 0.95f arbitrary

+ 10 - 0
MBansheeEditor/DbgGizmo.cs

@@ -9,9 +9,17 @@ namespace BansheeEditor
 {
 {
     class DbgGizmo
     class DbgGizmo
     {
     {
+        private static SpriteTexture iconTexture;
+
         [DrawGizmo(DrawGizmoFlags.NotSelected | DrawGizmoFlags.Pickable)]
         [DrawGizmo(DrawGizmoFlags.NotSelected | DrawGizmoFlags.Pickable)]
         private static void DrawDbgComponentGizmo(DbgGizmoComponent target)
         private static void DrawDbgComponentGizmo(DbgGizmoComponent target)
         {
         {
+            if (iconTexture == null)
+            {
+                Texture2D iconTex = ProjectLibrary.Load<Texture2D>("debugIcon.psd");
+                iconTexture = new SpriteTexture(iconTex);
+            }
+
             Gizmos.DrawCube(target.sceneObject.position, new Vector3(1, 1, 1));
             Gizmos.DrawCube(target.sceneObject.position, new Vector3(1, 1, 1));
             Gizmos.DrawSphere(target.sceneObject.position + 2 * Vector3.xAxis, 1.0f);
             Gizmos.DrawSphere(target.sceneObject.position + 2 * Vector3.xAxis, 1.0f);
             Gizmos.DrawWireCube(target.sceneObject.position + 4 * Vector3.xAxis, new Vector3(1, 1, 1));
             Gizmos.DrawWireCube(target.sceneObject.position + 4 * Vector3.xAxis, new Vector3(1, 1, 1));
@@ -19,6 +27,8 @@ namespace BansheeEditor
             Gizmos.DrawLine(target.sceneObject.position + 7.5f * Vector3.xAxis,
             Gizmos.DrawLine(target.sceneObject.position + 7.5f * Vector3.xAxis,
                 target.sceneObject.position + 8.5f * Vector3.xAxis);
                 target.sceneObject.position + 8.5f * Vector3.xAxis);
             Gizmos.DrawFrustum(target.sceneObject.position + 10 * Vector3.xAxis, 1920.0f / 1080.0f, 90, 1.0f, 1000.0f);
             Gizmos.DrawFrustum(target.sceneObject.position + 10 * Vector3.xAxis, 1920.0f / 1080.0f, 90, 1.0f, 1000.0f);
+
+            Gizmos.DrawIcon(target.sceneObject.position + new Vector3(0, 10, 0), iconTexture, false);
         }
         }
     }
     }
 }
 }

+ 9 - 3
MBansheeEngine/SpriteTexture.cs

@@ -10,12 +10,18 @@ namespace BansheeEngine
             
             
         }
         }
 
 
-        public SpriteTexture(int dummyParam)
+        public SpriteTexture(Texture2D texture)
         {
         {
-            Internal_CreateInstance(this);
+            Internal_CreateInstance(this, texture, Vector2.zero, Vector2.one);
+        }
+
+        public SpriteTexture(Texture2D texture, Vector2 uvOffset, Vector2 uvScale)
+        {
+            Internal_CreateInstance(this, texture, uvOffset, uvScale);
         }
         }
 
 
         [MethodImpl(MethodImplOptions.InternalCall)]
         [MethodImpl(MethodImplOptions.InternalCall)]
-        private static extern void Internal_CreateInstance(SpriteTexture instance);
+        private static extern void Internal_CreateInstance(SpriteTexture instance, 
+            Texture2D teture, Vector2 offset, Vector2 scale);
     }
     }
 }
 }

+ 4 - 1
SBansheeEditor/Source/BsScriptProjectLibrary.cpp

@@ -38,7 +38,10 @@ namespace BansheeEngine
 	{
 	{
 		Path resourcePath = MonoUtil::monoToWString(path);
 		Path resourcePath = MonoUtil::monoToWString(path);
 
 
-		ProjectLibrary::LibraryEntry* entry = ProjectLibrary::instance().findEntry(resourcePath);
+		Path fullPath = ProjectLibrary::instance().getResourcesFolder();
+		fullPath.append(resourcePath);
+
+		ProjectLibrary::LibraryEntry* entry = ProjectLibrary::instance().findEntry(fullPath);
 
 
 		if (entry == nullptr || entry->type == ProjectLibrary::LibraryEntryType::Directory)
 		if (entry == nullptr || entry->type == ProjectLibrary::LibraryEntryType::Directory)
 			return nullptr;
 			return nullptr;

+ 1 - 1
SBansheeEngine/Include/BsScriptObject.h

@@ -65,7 +65,7 @@ namespace BansheeEngine
 		{
 		{
 			Type* nativeInstance = nullptr;
 			Type* nativeInstance = nullptr;
 
 
-			if(metaData.thisPtrField != nullptr)
+			if (metaData.thisPtrField != nullptr && managedInstance != nullptr)
 				metaData.thisPtrField->getValue(managedInstance, &nativeInstance);
 				metaData.thisPtrField->getValue(managedInstance, &nativeInstance);
 
 
 			return nativeInstance;
 			return nativeInstance;

+ 1 - 1
SBansheeEngine/Include/BsScriptSpriteTexture.h

@@ -19,7 +19,7 @@ namespace BansheeEngine
 	private:
 	private:
 		friend class ScriptResourceManager;
 		friend class ScriptResourceManager;
 
 
-		static void internal_createInstance(MonoObject* instance);
+		static void internal_createInstance(MonoObject* instance, MonoObject* texture, Vector2 offset, Vector2 scale);
 
 
 		ScriptSpriteTexture(MonoObject* instance, const HSpriteTexture& texture);
 		ScriptSpriteTexture(MonoObject* instance, const HSpriteTexture& texture);
 
 

+ 14 - 2
SBansheeEngine/Source/BsScriptSpriteTexture.cpp

@@ -5,6 +5,7 @@
 #include "BsMonoClass.h"
 #include "BsMonoClass.h"
 #include "BsMonoManager.h"
 #include "BsMonoManager.h"
 #include "BsSpriteTexture.h"
 #include "BsSpriteTexture.h"
+#include "BsScriptTexture2D.h"
 
 
 namespace BansheeEngine
 namespace BansheeEngine
 {
 {
@@ -19,9 +20,20 @@ namespace BansheeEngine
 		metaData.scriptClass->addInternalCall("Internal_CreateInstance", &ScriptSpriteTexture::internal_createInstance);
 		metaData.scriptClass->addInternalCall("Internal_CreateInstance", &ScriptSpriteTexture::internal_createInstance);
 	}
 	}
 
 
-	void ScriptSpriteTexture::internal_createInstance(MonoObject* instance)
+	void ScriptSpriteTexture::internal_createInstance(MonoObject* instance, MonoObject* texture, Vector2 offset, Vector2 scale)
 	{
 	{
-		ScriptResourceManager::instance().createScriptSpriteTexture(instance, SpriteTexture::dummy()); // TODO - DUMMY CODE!
+		ScriptTexture2D* scriptTexture = ScriptTexture2D::toNative(texture);
+
+		if (scriptTexture == nullptr)
+		{
+			ScriptResourceManager::instance().createScriptSpriteTexture(instance, SpriteTexture::dummy());
+		}
+		else
+		{
+			HSpriteTexture spriteTexture = SpriteTexture::create(offset, scale, scriptTexture->getNativeHandle());
+
+			ScriptResourceManager::instance().createScriptSpriteTexture(instance, spriteTexture);
+		}
 	}
 	}
 
 
 	void ScriptSpriteTexture::_onManagedInstanceDeleted()
 	void ScriptSpriteTexture::_onManagedInstanceDeleted()

+ 3 - 0
SceneView.txt

@@ -4,6 +4,9 @@
     - Make those two a non-component types. Anywhere they are used in the Renderer they should just be passed as pointers.
     - Make those two a non-component types. Anywhere they are used in the Renderer they should just be passed as pointers.
 	- Then make a Component wrapper around the non-component types, and also a C# wrapper around the same types
 	- Then make a Component wrapper around the non-component types, and also a C# wrapper around the same types
 
 
+RESOURCES CAN BE INCORRECTLY CAST between each other. e.g. HSpriteTexture to HTexture will be cast implicitly.
+Second pass for Gizmo ICon rendering doesn't work - actually it doesn't seem to render at all even if I send normal depth compare function
+
 TESTING:
 TESTING:
  - Test picking on an object with alpha
  - Test picking on an object with alpha
  - Ensure that selecting an item in scene properly marks it in scene view
  - Ensure that selecting an item in scene properly marks it in scene view