Browse Source

Models imported as Node "prefabs" with materials mapped

Josh Engebretson 10 years ago
parent
commit
f060eefedf

+ 1 - 0
Data/AtomicEditor/Resources/EditorData/AtomicEditor/typescript/AtomicWork.d.ts

@@ -55,6 +55,7 @@ declare module Atomic {
     export function getInput(): Input;
     export function getGraphics(): Graphics;
     export function getFileSystem(): FileSystem;
+    export function getResourceCache(): ResourceCache;
 
     export function getParentPath(path: string): string;
     export function addTrailingSlash(path: string): string;

+ 19 - 1
Data/AtomicEditor/Resources/EditorData/AtomicEditor/typescript/ui/inspector/MaterialInspector.ts

@@ -22,7 +22,25 @@ class MaterialInspector extends ScriptWidget {
 
     //this.material = mat;
 
-    this.nameTextField.text = asset.name;
+    var cache = Atomic.getResourceCache();
+
+    var material = <Atomic.Material> cache.getResource("Material", asset.path);
+
+    if (!material)
+      return;
+
+    // TODO: support quality level and lod for techniques  
+    var technique = material.getTechnique(0);
+
+    this.nameTextField.text = technique.name;
+
+    var params = material.getShaderParameters();
+
+    for (var i in params) {
+
+      print(params[i].name, " : ", params[i].value, " : ", params[i].type);
+
+    }
 
   }
 

+ 6 - 16
Source/AtomicEditorWork/Editors/SceneEditor3D/SceneView3D.cpp

@@ -23,6 +23,7 @@
 
 #include <Atomic/IO/FileSystem.h>
 #include <Atomic/Resource/ResourceCache.h>
+#include <Atomic/Resource/XMLFile.h>
 #include <Atomic/Physics/PhysicsWorld.h>
 #include <Atomic/UI/UI.h>
 #include <Atomic/UI/UIEvents.h>
@@ -371,25 +372,14 @@ void SceneView3D::HandleDragEnded(StringHash eventType, VariantMap& eventData)
 
         ResourceCache* cache = GetSubsystem<ResourceCache>();
 
-        Model* model = cache->GetResource<Model>(asset->GetGUID());
+        const String& importer = asset->GetImporterName();
 
-        Node* node = scene_->CreateChild(asset->GetName());
-        StaticModel* mc = node->CreateComponent<StaticModel>();
-        mc->SetModel(model);
-
-        Material* material1 = cache->GetResource<Material>("Materials/AlphaMaterial.xml");
-        Material* material2 = cache->GetResource<Material>("Materials/BedMaterial.xml");
-
-        if (mc->GetNumGeometries() > 1)
+        if (importer == "ModelImporter")
         {
-            mc->SetMaterial(0, material1);
-            mc->SetMaterial(1, material2);
+            Node* node = scene_->CreateChild(asset->GetName());
+            XMLFile* xml = cache->GetResource<XMLFile>(asset->GetGUID());
+            node->LoadXML(xml->GetRoot());
         }
-        else
-        {
-            mc->SetMaterial(0, material2);
-        }
-
 
         LOGINFOF("Dropped %s : %s on SceneView3D", asset->GetPath().CString(), asset->GetGUID().CString());
     }

+ 18 - 5
Source/AtomicJS/Javascript/JSAPI.cpp

@@ -209,6 +209,7 @@ void js_push_variant(duk_context *ctx, const Variant& v)
     Object* object;
     Vector2& vector2 = (Vector2&) Vector2::ZERO;
     Vector3& vector3 = (Vector3&) Vector3::ZERO;
+    Vector4& vector4 = (Vector4&) Vector3::ZERO;
     void* uniqueClassID = NULL;
     const char* package = NULL;
 
@@ -272,19 +273,31 @@ void js_push_variant(duk_context *ctx, const Variant& v)
         vector2 = v.GetVector2();
         duk_push_array(ctx);
         duk_push_number(ctx, vector2.x_);
-        duk_put_prop_index(ctx, -1, 0);
+        duk_put_prop_index(ctx, -2, 0);
         duk_push_number(ctx, vector2.y_);
-        duk_put_prop_index(ctx, -1, 1);
+        duk_put_prop_index(ctx, -2, 1);
         break;
     case VAR_VECTOR3:
         vector3 = v.GetVector3();
         duk_push_array(ctx);
         duk_push_number(ctx, vector3.x_);
-        duk_put_prop_index(ctx, -1, 0);
+        duk_put_prop_index(ctx, -2, 0);
         duk_push_number(ctx, vector3.y_);
-        duk_put_prop_index(ctx, -1, 1);
+        duk_put_prop_index(ctx, -2, 1);
         duk_push_number(ctx, vector3.z_);
-        duk_put_prop_index(ctx, -1, 1);
+        duk_put_prop_index(ctx, -2, 2);
+        break;
+    case VAR_VECTOR4:
+        vector4 = v.GetVector4();
+        duk_push_array(ctx);
+        duk_push_number(ctx, vector4.x_);
+        duk_put_prop_index(ctx, -2, 0);
+        duk_push_number(ctx, vector4.y_);
+        duk_put_prop_index(ctx, -2, 1);
+        duk_push_number(ctx, vector4.z_);
+        duk_put_prop_index(ctx, -2, 2);
+        duk_push_number(ctx, vector4.w_);
+        duk_put_prop_index(ctx, -2, 3);
         break;
 
     default:

+ 45 - 0
Source/AtomicJS/Javascript/JSGraphics.cpp

@@ -4,6 +4,8 @@
 
 #include "JSGraphics.h"
 #include "JSVM.h"
+
+#include <Atomic/Graphics/Material.h>
 #include <Atomic/Graphics/Light.h>
 
 namespace Atomic
@@ -50,6 +52,43 @@ static int Light_SetShadowBias(duk_context* ctx)
     return 0;
 }
 
+// Material
+
+static int Material_GetShaderParamaters(duk_context* ctx)
+{
+    duk_push_this(ctx);
+    Material* material = js_to_class_instance<Material>(ctx, -1, 0);
+
+    const HashMap<StringHash, MaterialShaderParameter>& params =  material->GetShaderParameters();
+
+    duk_push_array(ctx);
+
+    unsigned j = 0;
+    for (HashMap<StringHash, MaterialShaderParameter>::ConstIterator i = params.Begin(); i != params.End(); ++i)
+    {
+        duk_push_object(ctx);
+
+        duk_push_string(ctx, i->second_.name_.CString());
+        duk_put_prop_string(ctx, -2, "name");
+
+        js_push_variant(ctx, i->second_.value_);
+        duk_put_prop_string(ctx, -2, "value");
+
+        duk_push_string(ctx, i->second_.value_.ToString().CString());
+        duk_put_prop_string(ctx, -2, "valueString");
+
+        duk_push_string(ctx, i->second_.value_.GetTypeName().CString());
+        duk_put_prop_string(ctx, -2, "typeName");
+
+        duk_push_number(ctx, (double) i->second_.value_.GetType());
+        duk_put_prop_string(ctx, -2, "type");
+
+        duk_put_prop_index(ctx, -2, j++);
+    }
+
+    return 1;
+}
+
 
 void jsapi_init_graphics(JSVM* vm)
 {
@@ -61,6 +100,12 @@ void jsapi_init_graphics(JSVM* vm)
     duk_push_c_function(ctx, Light_SetShadowBias, 2);
     duk_put_prop_string(ctx, -2, "setShadowBias");
     duk_pop(ctx);
+
+    js_class_get_prototype(ctx, "Atomic", "Material");
+    duk_push_c_function(ctx, Material_GetShaderParamaters, 0);
+    duk_put_prop_string(ctx, -2, "getShaderParameters");
+    duk_pop(ctx);
+
 }
 
 }

+ 54 - 3
Source/ToolCore/Import/OpenAssetImporter.cpp

@@ -29,7 +29,9 @@
 #include <Atomic/IO/Log.h>
 #include <Atomic/IO/File.h>
 #include <Atomic/IO/FileSystem.h>
+
 #include <Atomic/Resource/XMLFile.h>
+#include <Atomic/Resource/ResourceCache.h>
 
 #include <Atomic/Atomic3D/AnimatedModel.h>
 #include <Atomic/Atomic3D/Animation.h>
@@ -37,6 +39,7 @@
 #include <Atomic/Graphics/Geometry.h>
 #include <Atomic/Graphics/IndexBuffer.h>
 #include <Atomic/Graphics/VertexBuffer.h>
+#include <Atomic/Graphics/Material.h>
 
 #include "OpenAssetImporter.h"
 
@@ -124,7 +127,7 @@ void OpenAssetImporter::ExportModel(const String& outName, bool animationOnly)
 
     OutModel model;
     model.rootNode_ = rootNode_;
-    model.outName_ = outName;
+    model.outName_ = outName + ".mdl";
 
     CollectMeshes(scene_, model, model.rootNode_);
     CollectBones(model, animationOnly);
@@ -145,6 +148,51 @@ void OpenAssetImporter::ExportModel(const String& outName, bool animationOnly)
         HashSet<String> usedTextures;
         ExportMaterials(usedTextures);
     }
+
+    ResourceCache* cache = GetSubsystem<ResourceCache>();
+
+    Model* mdl = cache->GetResource<Model>( model.outName_);
+
+    // Use a dummy model here?  It kind of depends on how resource layout is
+    // designed, importing, existing models, etc
+/*
+    // Create a dummy model so that the reference can be stored
+
+    String modelName = (useSubdirs_ ? "Models/" : "") + GetFileNameAndExtension(model.outName_);
+
+    if (!cache->Exists(modelName))
+    {
+        Model* dummyModel = new Model(context_);
+        dummyModel->SetName(modelName);
+        dummyModel->SetNumGeometries(model.meshes_.Size());
+        cache->AddManualResource(dummyModel);
+    }
+*/
+
+    SharedPtr<Node> node(new Node(context_));
+    node->SetName("Model");
+    StaticModel* staticModel = node->CreateComponent<StaticModel>();
+    staticModel->SetModel(mdl);
+
+    if (!noMaterials_)
+    {
+        // Set materials if they are known
+        for (unsigned j = 0; j < model.meshes_.Size(); ++j)
+        {
+            String matName = GetMeshMaterialName(model.meshes_[j]);
+
+            String materialName = sourceAssetPath_ + matName;
+
+            staticModel->SetMaterial(j, cache->GetResource<Material>(materialName));
+        }
+
+    }
+
+    File outFile(context_);
+    if (!outFile.Open(outName, FILE_WRITE))
+        ErrorExit("Could not open output file " + outName);
+
+    node->SaveXML(outFile);
 }
 void OpenAssetImporter::BuildAndSaveModel(OutModel& model)
 {
@@ -941,6 +989,9 @@ void OpenAssetImporter::BuildAndSaveMaterial(aiMaterial* material, HashSet<Strin
         specularTexName = GetFileNameAndExtension(FromAIString(stringVal));
     if (material->Get(AI_MATKEY_TEXTURE(aiTextureType_EMISSIVE, 0), stringVal) == AI_SUCCESS)
         emissiveTexName = GetFileNameAndExtension(FromAIString(stringVal));
+
+    diffuseTexName.Replace(".tif", ".png");
+
     if (!noMaterialDiffuseColor_)
     {
         if (material->Get(AI_MATKEY_COLOR_DIFFUSE, colorVal) == AI_SUCCESS)
@@ -950,8 +1001,8 @@ void OpenAssetImporter::BuildAndSaveMaterial(aiMaterial* material, HashSet<Strin
         specularColor = Color(colorVal.r, colorVal.g, colorVal.b);
     if (!emissiveAO_)
     {
-        if (material->Get(AI_MATKEY_COLOR_EMISSIVE, colorVal) == AI_SUCCESS)
-            emissiveColor = Color(colorVal.r, colorVal.g, colorVal.b);
+        // if (material->Get(AI_MATKEY_COLOR_EMISSIVE, colorVal) == AI_SUCCESS)
+            // emissiveColor = Color(colorVal.r, colorVal.g, colorVal.b);
     }
     if (material->Get(AI_MATKEY_OPACITY, floatVal) == AI_SUCCESS)
     {