Jelajahi Sumber

Added material support to ScreenMesh, ignoreParentMatrix now set to false for collisionOnly entities in 2D physics module

Ivan Safrin 12 tahun lalu
induk
melakukan
bbd27232a4

+ 28 - 0
Core/Contents/Include/PolyScreenMesh.h

@@ -24,6 +24,7 @@ THE SOFTWARE.
 #include "PolyGlobals.h"
 #include "PolyScreenEntity.h"
 #include "PolyMesh.h"
+#include "PolyMaterial.h"
 
 namespace Polycode {
 
@@ -95,6 +96,31 @@ namespace Polycode {
 			*/						
 			void setTexture(Texture *texture);
 			
+			/**
+			* Set material from existing Material instance.
+			* @param material Material to apply.
+			*/												
+			void setMaterial(Material *material);
+
+			/**
+			* Set material by name. You can create materials in material files and name them there, then use this to set a material by name to a scene mesh.
+			* @param materialName Name of material to apply.
+			*/									
+			void setMaterialByName(const String& materialName);
+			
+			/**
+			* Clears the currently applied material
+			*/
+			void clearMaterial();
+			
+			/**
+			* Returns the material applied.
+			*/							
+			Material *getMaterial();			
+			
+			
+			ShaderBinding *getLocalShaderOptions();
+						
 			/**
 			* If this is set to true, the lines in wireframe meshes will be anti-aliased if the support is available in the renderer.
 			*/			
@@ -114,6 +140,8 @@ namespace Polycode {
 		
 		protected:
 		
+			Material *material;
+			ShaderBinding *localShaderOptions;			
 			Mesh *mesh;
 			Texture *texture;
 	};

+ 2 - 0
Core/Contents/Source/PolyCoreServices.cpp

@@ -219,11 +219,13 @@ void CoreServices::Render() {
 		renderer->clearScreen();					
 
 	if(drawScreensFirst) {
+		renderer->clearLights();	
 		screenManager->Render();
 		renderer->setPerspectiveMode();
 		sceneManager->Render();	
 	} else {
 		sceneManager->Render();
+		renderer->clearLights();		
 		screenManager->Render();	
 	}
 }

+ 54 - 6
Core/Contents/Source/PolyScreenMesh.cpp

@@ -23,12 +23,13 @@
 #include "PolyScreenMesh.h"
 #include "PolyCoreServices.h"
 #include "PolyMaterialManager.h"
+#include "PolyResourceManager.h"
 #include "PolyMesh.h"
 #include "PolyRenderer.h"
 
 using namespace Polycode;
 
-ScreenMesh::ScreenMesh(Mesh *mesh) : ScreenEntity(), texture(NULL) {
+ScreenMesh::ScreenMesh(Mesh *mesh) : ScreenEntity(), texture(NULL), material(NULL) {
 	this->mesh = mesh;
 	lineSmooth = false;
 	lineWidth = 1.0;
@@ -36,14 +37,14 @@ ScreenMesh::ScreenMesh(Mesh *mesh) : ScreenEntity(), texture(NULL) {
 	updateHitBox();
 }
 
-ScreenMesh::ScreenMesh(const String& fileName) : ScreenEntity(), texture(NULL) {
+ScreenMesh::ScreenMesh(const String& fileName) : ScreenEntity(), texture(NULL), material(NULL) {
 	mesh = new Mesh(fileName);
 	lineSmooth = false;
 	lineWidth = 1.0;
 	
 }
 
-ScreenMesh::ScreenMesh(int meshType) : ScreenEntity(), texture(NULL) {
+ScreenMesh::ScreenMesh(int meshType) : ScreenEntity(), texture(NULL), material(NULL){
 	mesh = new Mesh(meshType);
 	lineSmooth = false;
 	lineWidth = 1.0;
@@ -77,11 +78,53 @@ void ScreenMesh::setTexture(Texture *texture) {
 }
 
 void ScreenMesh::loadTexture(const String& fileName) {
-	texture = CoreServices::getInstance()->getMaterialManager()->createTextureFromFile(fileName, false, true);
+	texture = CoreServices::getInstance()->getMaterialManager()->createTextureFromFile(fileName, false, false);
 }
 
 void ScreenMesh::loadTexture(Image *image) {
-	texture = CoreServices::getInstance()->getMaterialManager()->createTextureFromImage(image, false, true);
+	texture = CoreServices::getInstance()->getMaterialManager()->createTextureFromImage(image, false, false);
+}
+
+void ScreenMesh::clearMaterial() {
+	if(localShaderOptions)
+		delete localShaderOptions;
+	localShaderOptions = NULL;
+	this->material = NULL;
+}
+
+void ScreenMesh::setMaterial(Material *material) {
+
+	if(this->material)
+		clearMaterial();
+	
+	if(!material)
+		return;
+		
+	if(material->getNumShaders() == 0)
+			return;
+		
+	this->material = material;
+	localShaderOptions = material->getShader(0)->createBinding();
+	if(texture) {
+		localShaderOptions->clearTexture("diffuse");
+		localShaderOptions->addTexture("diffuse", texture);
+	}
+	
+}
+
+Material *ScreenMesh::getMaterial() {
+	return material;
+}
+
+ShaderBinding *ScreenMesh::getLocalShaderOptions() {
+	return localShaderOptions;
+}
+
+void ScreenMesh::setMaterialByName(const String& materialName) {
+	Material *material =  (Material*)CoreServices::getInstance()->getResourceManager()->getResource(Resource::RESOURCE_MATERIAL, materialName);
+	if(!material)
+		return;
+	setMaterial(material);
 }
 
 void ScreenMesh::Render() {	
@@ -90,7 +133,12 @@ void ScreenMesh::Render() {
 	renderer->setLineSize(lineWidth);
 	renderer->setLineSmooth(lineSmooth);
 	
-	renderer->setTexture(texture);
+	if(material) {
+		renderer->applyMaterial(material, localShaderOptions,0);
+	} else {
+		renderer->setTexture(texture);
+	}
+	
 	if(mesh->useVertexColors) {
 		renderer->pushDataArrayForMesh(mesh, RenderDataArray::COLOR_DATA_ARRAY);
 	}

+ 3 - 1
IDE/Contents/Source/PolycodeScreenEditor.cpp

@@ -87,7 +87,7 @@ EntityTreeView::EntityTreeView(Entity *rootEntity) : UIElement() {
 	addChild(label);
 	label->setPosition(10, 3);
 	
-	treeContainer = new UITreeContainer("Images/entity_icon.png", L"Layers", 200, 555);
+	treeContainer = new UITreeContainer("Images/entity_icon.png", L"Root", 200, 555);
 	treeContainer->getRootNode()->toggleCollapsed();
 	treeContainer->getRootNode()->addEventListener(this, UITreeEvent::SELECTED_EVENT);
 	treeContainer->addEventListener(this, InputEvent::EVENT_MOUSEDOWN);
@@ -1523,6 +1523,8 @@ void PolycodeScreenEditorMain::selectEntity(ScreenEntity *entity) {
 	if(entity->getEntityProp("editor_type") != "root") {
 		entitySheet->entity = entity;
 		entityPropSheet->entity = entity;
+	} else {
+		entitySheet->entity = entity;	
 	}
 
 	if(dynamic_cast<ScreenParticleEmitter*>(entity)) {

+ 3 - 2
Modules/Contents/2DPhysics/Source/PolyPhysicsScreen.cpp

@@ -337,7 +337,7 @@ void PhysicsScreen::setVelocityY(ScreenEntity *ent, Number fy) {
 PhysicsScreenEntity *PhysicsScreen::addCollisionChild(ScreenEntity *newEntity, int entType, int groupIndex, bool sensorOnly) {
 	PhysicsScreenEntity *ret;
 	ret = addPhysicsChild(newEntity, entType, false, 0,0,0, sensorOnly, false, groupIndex);
-    
+	newEntity->ignoreParentMatrix = false;    
 	ret->collisionOnly = true; 
 	return ret;
 }
@@ -345,7 +345,8 @@ PhysicsScreenEntity *PhysicsScreen::addCollisionChild(ScreenEntity *newEntity, i
 PhysicsScreenEntity *PhysicsScreen::trackCollisionChild(ScreenEntity *newEntity, int entType, int groupIndex) {
 	PhysicsScreenEntity *ret;
 	ret = trackPhysicsChild(newEntity, entType, false, 0,0.0,0, true, false, groupIndex);
-	ret->collisionOnly = true; 
+	ret->collisionOnly = true;
+	newEntity->ignoreParentMatrix = false;
 	return ret;
 }