Пример #1
0
 private void AddVec4(string name, GenericMaterial genericMaterial, long paramId, Vector4 defaultValue)
 {
     // Convert parameters into colors for easier visualization.
     if (vec4ByParamId.ContainsKey(paramId))
     {
         var value = vec4ByParamId[paramId];
         genericMaterial.AddVector4(name, value);
     }
     else if (boolByParamId.ContainsKey(paramId))
     {
         var value = boolByParamId[paramId];
         if (value)
         {
             genericMaterial.AddVector4(name, new Vector4(1, 0, 1, 0));
         }
         else
         {
             genericMaterial.AddVector4(name, new Vector4(0, 0, 1, 0));
         }
     }
     else if (floatByParamId.ContainsKey(paramId))
     {
         var value = floatByParamId[paramId];
         genericMaterial.AddVector4(name, new Vector4(value, value, value, 0));
     }
     else
     {
         genericMaterial.AddVector4(name, defaultValue);
     }
 }
Пример #2
0
        public void SetMaterialUniforms(Shader shader, Material previousMaterial)
        {
            // TODO: This code could be moved to the constructor.
            if (genericMaterial == null || shouldUpdateTextures)
            {
                genericMaterial      = CreateGenericMaterial();
                shouldUpdateTextures = true;
            }

            if (uniformBlock == null)
            {
                uniformBlock = new UniformBlock(shader, "MaterialParams")
                {
                    BlockBinding = 1
                };
                SetMaterialParams(uniformBlock);
            }

            if (shouldUpdateUniformBlock)
            {
                SetMaterialParams(uniformBlock);
                shouldUpdateUniformBlock = false;
            }

            // This needs to be updated more than once.
            AddDebugParams(uniformBlock);

            // Update the uniform values.
            genericMaterial.SetShaderUniforms(shader, previousMaterial?.genericMaterial);
            uniformBlock.BindBlock(shader);
        }
Пример #3
0
        private static void MatPropertyShaderUniform(GenericMaterial genericMaterial, NUD.Material mat, string propertyName, float default1,
                                                     float default2, float default3, float default4)
        {
            // Attempt to get the values from the material's properties.
            // Otherwise, use the specified default values.
            float[] values;
            mat.entries.TryGetValue(propertyName, out values);
            if (mat.anims.ContainsKey(propertyName))
            {
                values = mat.anims[propertyName];
            }
            if (values == null)
            {
                values = new float[] { default1, default2, default3, default4 }
            }
            ;

            string uniformName = propertyName.Substring(3); // remove the NU_ from name

            if (values.Length == 4)
            {
                genericMaterial.AddVector4(uniformName, new Vector4(values[0], values[1], values[2], values[3]));
            }
            else
            {
                Debug.WriteLine(uniformName + " invalid parameter count: " + values.Length);
            }
        }
Пример #4
0
        private void AddMaterialParams(GenericMaterial genericMaterial)
        {
            // Set specific parameters and use a default value if not present.
            AddVec4("vec4Param", genericMaterial, RenderSettings.Instance.ParamId, new Vector4(0));

            // Assume no edge lighting if not present.
            AddVec4("paramA6", genericMaterial, 0xA6, new Vector4(0));

            // Some sort of skin subsurface color?
            AddVec4("paramA3", genericMaterial, 0xA3, new Vector4(1));

            // Mario Galaxy rim light?
            AddVec4("paramA0", genericMaterial, 0xA0, new Vector4(1));

            // Diffuse color multiplier?
            AddVec4("paramA5", genericMaterial, 0xA5, new Vector4(1));

            // Sprite sheet UV parameters.
            AddVec4("paramAA", genericMaterial, 0xAA, new Vector4(1));

            // Enables/disables specular occlusion.
            AddBool("paramE9", genericMaterial, 0xE9, true);

            // Controls anisotropic specular.
            AddFloat("paramCA", genericMaterial, 0xCA, 0.0f);

            // Some sort of sprite sheet scale toggle.
            AddBool("paramF1", genericMaterial, 0xF1, true);

            // Alpha offset.
            AddVec4("param98", genericMaterial, 0x98, new Vector4(0, 0, 0, 0));
        }
Пример #5
0
        public static void SetMaterialPropertyUniforms(UniformBlock uniformBlock, Shader shader, Nud.Material mat)
        {
            // Skip expensive buffer updates for redundant value updates.
            if (mat.ShouldUpdateRendering)
            {
                foreach (var property in defaultValueByProperty)
                {
                    MatPropertyShaderUniform(uniformBlock, mat, property.Key, property.Value);
                }
                mat.ShouldUpdateRendering = false;
            }

            // Always bind the uniform block because each polygon has its own uniform block.
            uniformBlock.BindBlock(shader, "MaterialProperties");

            // Create some conditionals rather than using different shaders.
            var genericMaterial = new GenericMaterial();

            HasMatPropertyShaderUniform(genericMaterial, mat, "NU_softLightingParams", "hasSoftLight");
            HasMatPropertyShaderUniform(genericMaterial, mat, "NU_customSoftLightParams", "hasCustomSoftLight");
            HasMatPropertyShaderUniform(genericMaterial, mat, "NU_specularParams", "hasSpecularParams");
            HasMatPropertyShaderUniform(genericMaterial, mat, "NU_dualNormalScrollParams", "hasDualNormal");
            HasMatPropertyShaderUniform(genericMaterial, mat, "NU_normalSamplerAUV", "hasNrmSamplerAUV");
            HasMatPropertyShaderUniform(genericMaterial, mat, "NU_normalSamplerBUV", "hasNrmSamplerBUV");
            HasMatPropertyShaderUniform(genericMaterial, mat, "NU_finalColorGain", "hasFinalColorGain");
            HasMatPropertyShaderUniform(genericMaterial, mat, "NU_effUniverseParam", "hasUniverseParam");

            genericMaterial.SetShaderUniforms(shader);
        }
Пример #6
0
        // just store GenericMaterial object, we can't use it here
        public int BuildMaterial(GenericMaterial m)
        {
            int id = Materials.Count;

            Materials.Add(m);
            return(id);
        }
Пример #7
0
        void customizeMaterial_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            int             index           = (sender as CustomizeMaterial).Index;
            Material        oldMaterial     = (sender as CustomizeMaterial).GetCurrentMaterial();
            GenericMaterial genericMaterial = new GenericMaterial(materials, index, false);

            genericMaterial.StatusUpdated += genericMaterial_StatusUpdated;
            genericMaterial.ShowDialog();
            Material currentMaterial = genericMaterial.GetCurrentMaterial();

            if (GenericCategory.GetMaterialByImagePath(materials, currentMaterial.FullPath) != null)
            {
                int i = GetIndexOfMaterial(oldMaterial);
                if (i == -1)
                {
                    i = currentObjectMaterials.Count;
                    currentObjectMaterials.Add(new WorldObjectMaterial(currentMaterial, currentObject.GetTotalAreaPerTextureByPath(currentMaterial.FullPath)));
                }
                else
                {
                    currentObjectMaterials[i] = new WorldObjectMaterial(currentMaterial, currentObjectMaterials[i].SurfaceNeeded);
                }
            }

            currentObject.SetMaterials(currentObjectMaterials);
            //importedObject.Materials[
            //importedObject.Materials.Add(currentMaterial);
            InitializeMaterials();
        }
Пример #8
0
        public static void SetTextureUniformsNudMatSphere(Shader shader, Nud.Material mat, Dictionary <NudEnums.DummyTexture, Texture> dummyTextures)
        {
            SetHasTextureUniforms(shader, mat);
            SetRenderModeTextureUniforms(shader);

            // The material shader just uses predefined textures from the Resources folder.
            Nud.MatTexture diffuse     = new Nud.MatTexture((int)NudEnums.DummyTexture.DummyRamp);
            Nud.MatTexture cubeMapHigh = new Nud.MatTexture((int)NudEnums.DummyTexture.StageMapHigh);

            SetHasTextureUniforms(shader, mat);
            SetRenderModeTextureUniforms(shader);

            // The type of texture can be partially determined by texture order.
            GenericMaterial textures = new GenericMaterial(nutTextureUnitOffset);

            textures.AddTexture("dif", GetSphereTexture("dif", dummyTextures));
            textures.AddTexture("spheremap", GetSphereTexture("spheremap", dummyTextures));
            textures.AddTexture("dif2", GetSphereTexture("dif2", dummyTextures));
            textures.AddTexture("dif3", GetSphereTexture("dif3", dummyTextures));
            textures.AddTexture("stagecube", GetSphereTexture("stagecube", dummyTextures));
            textures.AddTexture("cube", GetSphereTexture("cube", dummyTextures));
            textures.AddTexture("ao", GetSphereTexture("ao", dummyTextures));
            textures.AddTexture("normalMap", GetSphereTexture("normalMap", dummyTextures));
            textures.AddTexture("ramp", GetSphereTexture("ramp", dummyTextures));
            textures.AddTexture("dummyRamp", GetSphereTexture("dummyRamp", dummyTextures));

            textures.SetShaderUniforms(shader);
        }
Пример #9
0
        public void SetFromMaterial(GenericMaterial Material)
        {
            Bind();

            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (int)Material.SWrap);
            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (int)Material.TWrap);
        }
Пример #10
0
        public ActionResult Create(string materialName, string materialCode, bool requiresSetup, bool isDisabled, string materialDetails, int CategoryId)
        {
            try
            {
                /*We do not check if the material being created is repeated or not. Is there such a need?*/
                GenericMaterial newMaterial = new GenericMaterial
                {
                    Label = materialName,
                    MaterialCode = materialCode,
                    RequiresSetup = requiresSetup,
                    Disabled = isDisabled,
                    Details = materialDetails,
                    CategoryId = CategoryId
                };

                DB.GenericMaterials.Add(newMaterial);
                DB.SaveChanges();
                //TODO Remove all occurrences of this kind of code return => it is of any use to the user
                return new HttpStatusCodeResult(HttpStatusCode.OK);
            }
            catch (Exception ex)
            {
                return new HttpStatusCodeResult(HttpStatusCode.InternalServerError, ex.Message);
            }
        }
Пример #11
0
        public void SetMaterialUniforms(Shader shader, GenericMaterial previousMaterial)
        {
            // TODO: Rework default texture creation.
            if (defaultTextures == null)
            {
                defaultTextures = new Resources.DefaultTextures();
            }

            if (genericMaterial == null)
            {
                genericMaterial = Material.CreateGenericMaterial();
            }
            genericMaterial.SetShaderUniforms(shader, previousMaterial);

            if (uniformBlock == null)
            {
                uniformBlock = new UniformBlock(shader, "MaterialParams")
                {
                    BlockBinding = 1
                };
                Material.AddMaterialParams(uniformBlock);
            }
            // This needs to be updated more than once.
            Material.AddDebugParams(uniformBlock);

            uniformBlock.BindBlock(shader);
        }
Пример #12
0
 private void AddTextures(GenericMaterial genericMaterial)
 {
     AddMaterialTextures(genericMaterial);
     AddImageBasedLightingTextures(genericMaterial);
     AddRenderModeTextures(genericMaterial);
     genericMaterial.AddTexture("colorLut", DefaultTextures.Instance.Value.ColorGradingLut);
 }
Пример #13
0
        void genericMaterial_StatusUpdated(object sender, EventArgs e)
        {
            GenericMaterial g = (sender as GenericMaterial);
            Material        currentMaterial = g.GetCurrentMaterial();

            currentObject.SetTexture(g.Index, currentMaterial.FullPath, openGLControl.OpenGL);
        }
Пример #14
0
        private void AddTextures(GenericMaterial genericMaterial)
        {
            AddMaterialTextures(genericMaterial);

            AddImageBasedLightingTextures(genericMaterial);

            AddRenderModeTextures(genericMaterial);
        }
Пример #15
0
        public void SetMaterialValues(NUD.Material nudMaterial, SFGraphics.GLObjects.Shaders.Shader shader)
        {
            var material = new GenericMaterial();

            NudUniforms.SetMaterialPropertyUniforms(material, nudMaterial);

            material.SetShaderUniforms(shader);
        }
Пример #16
0
        private void AddTextures(GenericMaterial genericMaterial)
        {
            AddMaterialTextures(genericMaterial);

            AddImageBasedLightingTextures(genericMaterial);

            AddRenderModeTextures(genericMaterial);

            genericMaterial.AddTexture("stipplePattern", defaultTextures.stipplePattern);
        }
Пример #17
0
        public GenericMaterial CreateGenericMaterial(Material material)
        {
            // Don't use the default texture unit.
            var genericMaterial = new GenericMaterial(1);

            AddTextures(genericMaterial);
            AddMaterialParams(genericMaterial);

            return(genericMaterial);
        }
Пример #18
0
        private void ReadTextureCodes(DataReader reader, GenericMaterial Mat)
        {
            bool done = false;

            while (!done)
            {
                //Console.WriteLine(reader.pos().ToString("x") + " " + reader.readByte().ToString("x"));
                //reader.Seek(reader.pos() - 1);
                switch (reader.ReadByte())
                {
                case 0xF5:
                    reader.Seek(reader.Position - 1);
                    uint b1 = (uint)reader.ReadInt32();
                    uint b2 = (uint)reader.ReadInt32();

                    int cmT = (int)((b2 >> 18) & 0x3);
                    int cmS = (int)((b2 >> 8) & 0x3);

                    Console.WriteLine(b1.ToString("x") + " " + b2.ToString("x") + " " + cmT + " " + cmS);

                    switch (cmS)
                    {
                    case 0: Mat.TWrap = TextureWrapMode.Repeat; break;

                    case 1: Mat.TWrap = TextureWrapMode.MirroredRepeat; break;

                    case 2: Mat.TWrap = TextureWrapMode.ClampToEdge; break;

                    case 3: Mat.TWrap = TextureWrapMode.ClampToEdgeSgis; break;
                    }
                    switch (cmT)
                    {
                    case 0: Mat.SWrap = TextureWrapMode.Repeat; break;

                    case 1: Mat.SWrap = TextureWrapMode.MirroredRepeat; break;

                    case 2: Mat.SWrap = TextureWrapMode.ClampToEdge; break;

                    case 3: Mat.SWrap = TextureWrapMode.ClampToEdgeSgis; break;
                    }
                    //Mat.T = TextureWrapMode.Repeat;
                    //Mat.S = TextureWrapMode.Repeat;
                    break;

                case 0xDF:
                    done = true;
                    break;

                default:
                    reader.Skip(7);
                    break;
                }
            }
        }
Пример #19
0
        Mesh3d CreateModel(Object3dInfo obj, Vector3 diffuse, Vector3 specular, float roughness)
        {
            var terrain3dInfo   = obj;
            var terrainMaterial = new GenericMaterial();

            terrainMaterial.DiffuseColor  = diffuse;
            terrainMaterial.SpecularColor = specular;
            terrainMaterial.Roughness     = roughness;
            var terrainMesh = Mesh3d.Create(terrain3dInfo, terrainMaterial);

            return(terrainMesh);
        }
Пример #20
0
 private void AddBool(string name, GenericMaterial genericMaterial, long paramId, bool defaultValue)
 {
     if (boolByParamId.ContainsKey(paramId))
     {
         var value = boolByParamId[paramId];
         genericMaterial.AddBoolToInt(name, value);
     }
     else
     {
         genericMaterial.AddBoolToInt(name, defaultValue);
     }
 }
Пример #21
0
 private void AddFloat(string name, GenericMaterial genericMaterial, long paramId, float defaultValue)
 {
     if (floatByParamId.ContainsKey(paramId))
     {
         var value = floatByParamId[paramId];
         genericMaterial.AddFloat(name, value);
     }
     else
     {
         genericMaterial.AddFloat(name, defaultValue);
     }
 }
Пример #22
0
        private static void HasMatPropertyShaderUniform(GenericMaterial genericMaterial, NUD.Material mat, string propertyName, string uniformName)
        {
            bool hasValue = mat.entries.ContainsKey(propertyName) || mat.anims.ContainsKey(propertyName);

            if (hasValue)
            {
                genericMaterial.AddInt(uniformName, 1);
            }
            else
            {
                genericMaterial.AddInt(uniformName, 0);
            }
        }
Пример #23
0
        Mesh3d CreateDiffuseModelFromRaw(string obj, Vector3 color)
        {
            var terrain3dManager = Object3dManager.LoadFromRaw(Media.Get(obj));
            var terrain3dInfo    = new Object3dInfo(terrain3dManager.Vertices);
            var terrainMaterial  = new GenericMaterial();

            terrainMaterial.DiffuseColor  = new Vector3(0);
            terrainMaterial.SpecularColor = new Vector3(1);
            terrainMaterial.Roughness     = 0.5f;
            var terrainMesh = Mesh3d.Create(terrain3dInfo, terrainMaterial);

            return(terrainMesh);
        }
Пример #24
0
        private static void HasMatPropertyShaderUniform(GenericMaterial genericMaterial, Nud.Material mat, string propertyName, string uniformName)
        {
            bool hasValue = mat.HasProperty(propertyName) || mat.HasPropertyAnim(propertyName);

            if (hasValue)
            {
                genericMaterial.AddInt(uniformName, 1);
            }
            else
            {
                genericMaterial.AddInt(uniformName, 0);
            }
        }
Пример #25
0
        Mesh3d CreateWall(Vector2 start, Vector2 end, Quaternion rotation, Vector3 position, Vector3 color)
        {
            var terrain3dManager = Object3dGenerator.CreateGround(start, end, new Vector2(1), Vector3.UnitY);
            var terrain3dInfo    = new Object3dInfo(terrain3dManager.Vertices);
            var terrainMaterial  = new GenericMaterial();

            terrainMaterial.DiffuseColor  = color;
            terrainMaterial.SpecularColor = Vector3.Zero;
            terrainMaterial.Roughness     = 0f;
            var terrainMesh = Mesh3d.Create(terrain3dInfo, terrainMaterial);

            terrainMesh.GetInstance(0).Rotate(rotation);
            return(terrainMesh);
        }
Пример #26
0
        private void AddFloat(GenericMaterial genericMaterial, long paramId, float defaultValue)
        {
            var name = $"param{paramId.ToString("X")}";

            if (floatByParamId.ContainsKey(paramId))
            {
                var value = floatByParamId[paramId];
                genericMaterial.AddFloat(name, value);
            }
            else
            {
                genericMaterial.AddFloat(name, defaultValue);
            }
        }
Пример #27
0
        private void AddBool(GenericMaterial genericMaterial, long paramId, bool defaultValue)
        {
            var name = $"param{paramId.ToString("X")}";

            if (boolByParamId.ContainsKey(paramId))
            {
                var value = boolByParamId[paramId];
                genericMaterial.AddBoolToInt(name, value);
            }
            else
            {
                genericMaterial.AddBoolToInt(name, defaultValue);
            }
        }
Пример #28
0
        private GenericMaterial CreateGenericMaterial()
        {
            // Don't use the default texture unit.
            var genericMaterial = new GenericMaterial(1);

            AddTextures(genericMaterial);

            genericMaterial.AddFloat("depthBias", DepthBias);
            genericMaterial.AddVector3("materialId", MaterialIdColorRgb255 / 255f);

            // TODO: Convert from quaternion values in light.nuanimb.
            AddQuaternion("chrLightDir", genericMaterial, -0.453154f, -0.365998f, -0.211309f, 0.784886f);

            return(genericMaterial);
        }
Пример #29
0
        public GenericMaterial CreateGenericMaterial(Material material)
        {
            // Don't use the default texture unit.
            var genericMaterial = new GenericMaterial(1);

            AddTextures(genericMaterial);
            AddMaterialParams(genericMaterial);

            // HACK: There isn't an easy way to access the current frame.
            genericMaterial.AddFloat("currentFrame", CurrentFrame);

            // TODO: Convert from quaternion values in light.nuanimb.
            AddQuaternion("chrLightDir", genericMaterial, -0.453154f, -0.365998f, -0.211309f, 0.784886f);

            return(genericMaterial);
        }
Пример #30
0
        public static void SetMaterialPropertyUniforms(GenericMaterial genericMaterial, NUD.Material mat)
        {
            foreach (var property in defaultValueByProperty)
            {
                MatPropertyShaderUniform(genericMaterial, mat, property.Key, property.Value);
            }

            // Create some conditionals rather than using different shaders.
            HasMatPropertyShaderUniform(genericMaterial, mat, "NU_softLightingParams", "hasSoftLight");
            HasMatPropertyShaderUniform(genericMaterial, mat, "NU_customSoftLightParams", "hasCustomSoftLight");
            HasMatPropertyShaderUniform(genericMaterial, mat, "NU_specularParams", "hasSpecularParams");
            HasMatPropertyShaderUniform(genericMaterial, mat, "NU_dualNormalScrollParams", "hasDualNormal");
            HasMatPropertyShaderUniform(genericMaterial, mat, "NU_normalSamplerAUV", "hasNrmSamplerAUV");
            HasMatPropertyShaderUniform(genericMaterial, mat, "NU_normalSamplerBUV", "hasNrmSamplerBUV");
            HasMatPropertyShaderUniform(genericMaterial, mat, "NU_finalColorGain", "hasFinalColorGain");
            HasMatPropertyShaderUniform(genericMaterial, mat, "NU_effUniverseParam", "hasUniverseParam");
        }
Пример #31
0
 private void AddMaterialTextures(GenericMaterial genericMaterial)
 {
     genericMaterial.AddTexture("colMap", GetTexture(MatlEnums.ParamId.Texture0), GetSampler(MatlEnums.ParamId.Sampler0));
     genericMaterial.AddTexture("col2Map", GetTexture(MatlEnums.ParamId.Texture1), GetSampler(MatlEnums.ParamId.Sampler1));
     genericMaterial.AddTexture("prmMap", GetTexture(MatlEnums.ParamId.Texture6), GetSampler(MatlEnums.ParamId.Sampler6));
     genericMaterial.AddTexture("norMap", GetTexture(MatlEnums.ParamId.Texture4), GetSampler(MatlEnums.ParamId.Sampler4));
     genericMaterial.AddTexture("inkNorMap", GetTexture(MatlEnums.ParamId.Texture16), GetSampler(MatlEnums.ParamId.Sampler16));
     genericMaterial.AddTexture("emiMap", GetTexture(MatlEnums.ParamId.Texture5), GetSampler(MatlEnums.ParamId.Sampler5));
     genericMaterial.AddTexture("emi2Map", GetTexture(MatlEnums.ParamId.Texture14), GetSampler(MatlEnums.ParamId.Sampler14));
     genericMaterial.AddTexture("bakeLitMap", GetTexture(MatlEnums.ParamId.Texture9), GetSampler(MatlEnums.ParamId.Sampler9));
     genericMaterial.AddTexture("gaoMap", GetTexture(MatlEnums.ParamId.Texture3), GetSampler(MatlEnums.ParamId.Sampler3));
     genericMaterial.AddTexture("projMap", GetTexture(MatlEnums.ParamId.Texture13), GetSampler(MatlEnums.ParamId.Sampler13));
     genericMaterial.AddTexture("difCubeMap", GetTexture(MatlEnums.ParamId.Texture8), GetSampler(MatlEnums.ParamId.Sampler8));
     genericMaterial.AddTexture("difMap", GetTexture(MatlEnums.ParamId.Texture10), GetSampler(MatlEnums.ParamId.Sampler10));
     genericMaterial.AddTexture("dif2Map", GetTexture(MatlEnums.ParamId.Texture11), GetSampler(MatlEnums.ParamId.Sampler11));
     genericMaterial.AddTexture("dif3Map", GetTexture(MatlEnums.ParamId.Texture12), GetSampler(MatlEnums.ParamId.Sampler12));
 }
Пример #32
0
        public static List<InstancedMesh3d> CreateTree(float minNodesAngle, float maxNodesAngle, int maxNodesCountPerLevel, int iterations, int randomSeed, float scaleDescendant, bool addLeaves)
        {
            LeafMaterial = GenericMaterial.FromMedia("leaf_color.png");
            LeafMaterial.SetAlphaMaskFromMedia("leaf_alpha.png");
            NodeMaterial.SetBumpMapFromMedia("treebump.png");
            NodeMaterial.SetNormalMapFromMedia("nmaphires.jpg");
            NodeMaterial.TesselationMultiplier = 10;
            Node.ScaleUV(10);
            List<InstancedMesh3d> elements = new List<InstancedMesh3d>();

            Randomizer = new Random(DateTime.Now.Millisecond);

            var tree = CreateNode(Vector3.Zero, Quaternion.Identity, minNodesAngle, maxNodesAngle, maxNodesCountPerLevel, 1.0f, iterations, scaleDescendant, addLeaves);
            elements.Add(InstancedMesh3d.FromSimilarMesh3dList(tree.Nodes));
            if(addLeaves) elements.Add(InstancedMesh3d.FromSimilarMesh3dList(tree.Leafs));

            return elements;
        }
Пример #33
0
        private void LoadFromString(string[] lines)
        {
            Materials = new List<GenericMaterial>();
            Meshes = new List<Mesh3d>();
            Lights = new List<ILight>();
            Cameras = new List<Camera>();
            var regx = new Regex("(.+?)[ ]+(.+)");
            var currentMaterial = new GenericMaterial(Vector3.One);
            ILight tempLight = null;
            GenericMaterial tempMaterial = null;
            Mesh3d tempMesh = null;
            Camera tempCamera = null;
            Action flush = () =>
            {
                if(tempLight != null)
                {
                    Lights.Add(tempLight);
                    if(OnObjectFinish != null)
                        OnObjectFinish.Invoke(this, tempLight);
                }
                if(tempMaterial != null)
                {
                    Materials.Add(tempMaterial);
                    if(OnObjectFinish != null)
                        OnObjectFinish.Invoke(this, tempMaterial);
                }
                if(tempMesh != null)
                {
                    Meshes.Add(tempMesh);
                    if(OnObjectFinish != null)
                        OnObjectFinish.Invoke(this, tempMesh);
                }
                if(tempCamera != null)
                {
                    Cameras.Add(tempCamera);
                    if(OnObjectFinish != null)
                        OnObjectFinish.Invoke(this, tempCamera);
                }

                tempLight = null;
                tempMaterial = null;
                tempMesh = null;
                tempCamera = null;
            };
            string vertexbuffer = "";
            PhysicalBody physob = null;
            foreach(var l in lines)
            {
                if(l.StartsWith("//") || l.Trim().Length == 0)
                    continue;
                var regout = regx.Match(l);
                if(!regout.Success)
                {
                        throw new ArgumentException("Invalid line in scene string: " + l);
                }
                string command = regout.Groups[1].Value.Trim();
                string data = regout.Groups[2].Value.Trim();
                if(command.Length == 0 || data.Length == 0)
                    throw new ArgumentException("Invalid line in scene string: " + l);

                switch(command)
                {
                    // Mesh3d start
                    case "mesh":
                    {
                        flush();
                        tempMesh = Mesh3d.Empty;
                        tempMesh.AddInstance(new Mesh3dInstance(new TransformationManager(Vector3.Zero), data));
                        //tempMesh.Name = data;
                        break;
                    }

                    case "usematerial":
                    {
                        tempMesh.AddLodLevel(null, Materials.First((a) => a.Name == data), 0, 999999);
                        break;
                    }
                    case "scaleuv":
                    {
                        string[] literals = data.Split(' ');
                        float x, y;
                        if(!float.TryParse(literals[0], System.Globalization.NumberStyles.Float, System.Globalization.CultureInfo.InvariantCulture, out x))
                            throw new ArgumentException("Invalid line in scene string: " + l);
                        if(!float.TryParse(literals[1], System.Globalization.NumberStyles.Float, System.Globalization.CultureInfo.InvariantCulture, out y))
                            throw new ArgumentException("Invalid line in scene string: " + l);
                        tempMesh.GetLodLevel(0).Info3d.Manager.ScaleUV(x, y);
                        var obj = tempMesh.GetLodLevel(0).Info3d.Manager;
                        tempMesh.GetLodLevel(0).Info3d = new Object3dInfo(tempMesh.GetLodLevel(0).Info3d.Manager.Vertices);
                        tempMesh.GetLodLevel(0).Info3d.Manager = obj;
                        break;
                    }
                    case "vbo":
                    {
                        vertexbuffer = data;
                        ApplyVboIbo(tempMesh, vertexbuffer);
                        vertexbuffer = "";
                        break;
                    }

                    case "physics":
                    {
                        var datas = data.Split(' ');
                        if(datas[0] == "convexhull")
                        {
                            if(tempMesh == null)
                                throw new ArgumentException("Invalid line in scene string: " + l);
                            var shape = tempMesh.GetLodLevel(0).Info3d.Manager.GetConvexHull();
                            float mass;
                            if(!float.TryParse(datas[1], System.Globalization.NumberStyles.Float, System.Globalization.CultureInfo.InvariantCulture, out mass))
                                throw new ArgumentException("Invalid line in scene string: " + l);
                            physob = Game.World.Physics.CreateBody(mass, tempMesh.GetInstance(0), shape);
                        }
                        if(datas[0] == "boundingbox")
                        {
                            if(tempMesh == null)
                                throw new ArgumentException("Invalid line in scene string: " + l);
                            var shape = Physics.CreateBoundingBoxShape(tempMesh.GetLodLevel(0).Info3d.Manager);
                            float mass;
                            if(!float.TryParse(datas[1], System.Globalization.NumberStyles.Float, System.Globalization.CultureInfo.InvariantCulture, out mass))
                                throw new ArgumentException("Invalid line in scene string: " + l);
                            physob = Game.World.Physics.CreateBody(mass, tempMesh.GetInstance(0), shape);
                        }
                        if(datas[0] == "accurate")
                        {
                            if(tempMesh == null)
                                throw new ArgumentException("Invalid line in scene string: " + l);
                            var shape = tempMesh.GetLodLevel(0).Info3d.Manager.GetAccurateCollisionShape();
                            float mass;
                            if(!float.TryParse(datas[1], System.Globalization.NumberStyles.Float, System.Globalization.CultureInfo.InvariantCulture, out mass))
                                throw new ArgumentException("Invalid line in scene string: " + l);
                            physob = Game.World.Physics.CreateBody(mass, tempMesh.GetInstance(0), shape);
                        }
                        if(datas[0] == "enable")
                        {
                            if(physob == null)
                                throw new ArgumentException("Invalid line in scene string: " + l);
                            physob.Enable();
                        }
                        break;
                    }

                    case "translate":
                    {
                        string[] literals = data.Split(' ');
                        if(literals.Length != 3)
                            throw new ArgumentException("Invalid line in scene string: " + l);
                        float x, y, z;
                        if(!float.TryParse(literals[0], System.Globalization.NumberStyles.Float, System.Globalization.CultureInfo.InvariantCulture, out x))
                            throw new ArgumentException("Invalid line in scene string: " + l);
                        if(!float.TryParse(literals[1], System.Globalization.NumberStyles.Float, System.Globalization.CultureInfo.InvariantCulture, out y))
                            throw new ArgumentException("Invalid line in scene string: " + l);
                        if(!float.TryParse(literals[2], System.Globalization.NumberStyles.Float, System.Globalization.CultureInfo.InvariantCulture, out z))
                            throw new ArgumentException("Invalid line in scene string: " + l);
                        if(tempMesh != null)
                        {
                            tempMesh.GetInstance(0).Transformation.Translate(x, y, z);
                        }
                        if(tempCamera != null)
                        {
                            tempCamera.Transformation.Translate(x, y, z);
                        }
                        if(tempLight != null)
                        {
                            if(tempLight is SimplePointLight)
                                (tempLight as SimplePointLight).Transformation.Translate(x, y, z);
                            else if(tempLight is ProjectionLight)
                                (tempLight as ProjectionLight).camera.Transformation.Translate(x, y, z);
                        }
                        break;
                    }
                    case "scale":
                    {
                        if(tempMesh == null)
                            throw new ArgumentException("Invalid line in scene string: " + l);
                        string[] literals = data.Split(' ');
                        if(literals.Length != 3)
                            throw new ArgumentException("Invalid line in scene string: " + l);
                        float x, y, z;
                        if(!float.TryParse(literals[0], System.Globalization.NumberStyles.Float, System.Globalization.CultureInfo.InvariantCulture, out x))
                            throw new ArgumentException("Invalid line in scene string: " + l);
                        if(!float.TryParse(literals[1], System.Globalization.NumberStyles.Float, System.Globalization.CultureInfo.InvariantCulture, out y))
                            throw new ArgumentException("Invalid line in scene string: " + l);
                        if(!float.TryParse(literals[2], System.Globalization.NumberStyles.Float, System.Globalization.CultureInfo.InvariantCulture, out z))
                            throw new ArgumentException("Invalid line in scene string: " + l);
                        tempMesh.GetInstance(0).Transformation.Scale(x, y, z);
                        break;
                    }
                    case "rotate":
                    {
                        string[] literals = data.Split(' ');
                        if(literals.Length < 3 || literals.Length > 4)
                            throw new ArgumentException("Invalid line in scene string: " + l);
                        float x, y, z;
                        if(!float.TryParse(literals[0], System.Globalization.NumberStyles.Float, System.Globalization.CultureInfo.InvariantCulture, out x))
                            throw new ArgumentException("Invalid line in scene string: " + l);
                        if(!float.TryParse(literals[1], System.Globalization.NumberStyles.Float, System.Globalization.CultureInfo.InvariantCulture, out y))
                            throw new ArgumentException("Invalid line in scene string: " + l);
                        if(!float.TryParse(literals[2], System.Globalization.NumberStyles.Float, System.Globalization.CultureInfo.InvariantCulture, out z))
                            throw new ArgumentException("Invalid line in scene string: " + l);
                        Quaternion rot = Quaternion.Identity;
                        if(literals.Length == 3)
                        {
                            var rotx = Matrix3.CreateRotationX(MathHelper.DegreesToRadians(x));
                            var roty = Matrix3.CreateRotationY(MathHelper.DegreesToRadians(y));
                            var rotz = Matrix3.CreateRotationZ(MathHelper.DegreesToRadians(z));
                            rot = Quaternion.FromMatrix(rotx * roty * rotz);
                        }
                        if(literals.Length == 4)
                        {
                            float w;
                            if(!float.TryParse(literals[3], System.Globalization.NumberStyles.Float, System.Globalization.CultureInfo.InvariantCulture, out w))
                                throw new ArgumentException("Invalid line in scene string: " + l);
                            rot = new Quaternion(x, y, z, w);
                        }

                        if(tempMesh != null)
                        {
                            tempMesh.GetInstance(0).Transformation.Rotate(rot);
                        }
                        if(tempCamera != null)
                        {
                            tempCamera.Transformation.Rotate(rot);
                        }
                        if(tempLight != null)
                        {
                            if(tempLight is SimplePointLight)
                                (tempLight as SimplePointLight).Transformation.Rotate(rot);
                            else if(tempLight is ProjectionLight)
                                (tempLight as ProjectionLight).camera.Transformation.Rotate(rot);
                        }
                        break;
                    }

                    // Mesh3d end Material start
                    case "material":
                    {
                        flush();
                        tempMaterial = new GenericMaterial(Vector3.One);
                        tempMaterial.Name = data;
                        break;
                    }
                    case "type":
                    {
                        if(tempMaterial == null)
                            throw new ArgumentException("Invalid line in scene string: " + l);
                        tempMaterial.Type = (GenericMaterial.MaterialType)Enum.Parse(typeof(GenericMaterial.MaterialType), data, true);
                        break;
                    }
                    case "invertnormalmap":
                    {
                        if(tempMaterial == null)
                            throw new ArgumentException("Invalid line in scene string: " + l);
                        tempMaterial.InvertNormalMap = data == "true" ? true : false;
                        break;
                    }
                    case "transparency":
                    {
                        if(tempMaterial == null)
                            throw new ArgumentException("Invalid line in scene string: " + l);
                        tempMaterial.SupportTransparency = data == "true" ? true : false;
                        break;
                    }
                    case "normalmap":
                    {
                        if(tempMaterial == null)
                            throw new ArgumentException("Invalid line in scene string: " + l);
                        tempMaterial.NormalsTexture = new Texture(Media.Get(data));
                        break;
                    }
                    case "bumpmap":
                    {
                        if(tempMaterial == null)
                            throw new ArgumentException("Invalid line in scene string: " + l);
                        tempMaterial.BumpTexture = new Texture(Media.Get(data));
                        break;
                    }
                    case "roughnessmap":
                    {
                        if(tempMaterial == null)
                            throw new ArgumentException("Invalid line in scene string: " + l);
                        tempMaterial.RoughnessTexture = new Texture(Media.Get(data));
                        break;
                    }
                    case "metalnessmap":
                    {
                        if(tempMaterial == null)
                            throw new ArgumentException("Invalid line in scene string: " + l);
                       // tempMaterial.MetalnessMap = new Texture(Media.Get(data));
                        break;
                    }
                    case "roughness":
                    {
                        if(tempMaterial == null)
                            throw new ArgumentException("Invalid line in scene string: " + l);
                        float f;
                        if(!float.TryParse(data, System.Globalization.NumberStyles.Float, System.Globalization.CultureInfo.InvariantCulture, out f))
                            throw new ArgumentException("Invalid line in scene string: " + l);
                        tempMaterial.Roughness = f;
                        break;
                    }
                    case "metalness":
                    {
                        if(tempMaterial == null)
                            throw new ArgumentException("Invalid line in scene string: " + l);
                        float f;
                        if(!float.TryParse(data, System.Globalization.NumberStyles.Float, System.Globalization.CultureInfo.InvariantCulture, out f))
                            throw new ArgumentException("Invalid line in scene string: " + l);
                        //tempMaterial.Metalness = f;
                        break;
                    }
                    case "color":
                    {
                        if(tempMaterial == null)
                            throw new ArgumentException("Invalid line in scene string: " + l);
                        string[] literals = data.Split(' ');
                        if(literals.Length != 4)
                            throw new ArgumentException("Invalid line in scene string: " + l);
                        float x, y, z, a;
                        if(!float.TryParse(literals[0], System.Globalization.NumberStyles.Float, System.Globalization.CultureInfo.InvariantCulture, out x))
                            throw new ArgumentException("Invalid line in scene string: " + l);
                        if(!float.TryParse(literals[1], System.Globalization.NumberStyles.Float, System.Globalization.CultureInfo.InvariantCulture, out y))
                            throw new ArgumentException("Invalid line in scene string: " + l);
                        if(!float.TryParse(literals[2], System.Globalization.NumberStyles.Float, System.Globalization.CultureInfo.InvariantCulture, out z))
                            throw new ArgumentException("Invalid line in scene string: " + l);
                        if(!float.TryParse(literals[3], System.Globalization.NumberStyles.Float, System.Globalization.CultureInfo.InvariantCulture, out a))
                            throw new ArgumentException("Invalid line in scene string: " + l);
                        tempMaterial.DiffuseColor = new Vector3(x, y, z);
                        break;
                    }
                    case "texture":
                    {
                        if(tempMaterial == null)
                            throw new ArgumentException("Invalid line in scene string: " + l);
                        tempMaterial.DiffuseTexture = new Texture(Media.Get(data));
                        tempMaterial.SpecularTexture = tempMaterial.DiffuseTexture;
                        tempMaterial.Mode = GenericMaterial.DrawMode.TextureMultipleColor;
                        break;
                    }

                    // Material end
                    default:
                    throw new ArgumentException("Invalid line in scene string: " + l);
                }
            }
            flush();
        }
Пример #34
0
        protected override void Initialize()
        {
            base.Initialize();

            main_ui = new MainUI(this);
            main_ui.Initialize();

            scene = new Scene(this, 0, "test");
            Scm.AddScene(scene);
            Scm.SetActiveScene(scene);

            /*
            Vector3 camera_position = new Vector3(0, 10, 51);
            Vector3 camera_target = camera_position + Vector3.Forward;
            Camera cam = new Camera();
            cam.Initialize();
            cam.Fov = MathHelper.PiOver4;
            scene.ActiveCamera = cam;
             */
            camera = new Camera(this);
            scene.ActiveCamera = camera;

            //camera_ctrl = new FpsCameraController(this);
            //cam.AttachController(camera_ctrl);
            //camera_ctrl.Enabled = true;

            //////////////////////////////////////////////////////////////////////////
            Axis axis = new Axis(this);
            axis.Initialize();
            scene.AddEntity(axis);

            axis.Pose.SetScale(10, 10, 10);

            ////////////////////////////////��������ķָ���//////////////////////////////////////////
            Terrain terrain = new Terrain(this, "Textures\\flat_map", null);
            terrain.Initialize();
            scene.AddEntity(terrain);

            camera.Target = new Vector3(terrain.WidthInPixel / 2.0f, 0, -terrain.HeightInPixel / 2.0f);
            //camera.Target = new Vector3(0, 0, 0);

            //////////////////////////////////////////////////////////////////////////
            //Quad s1 = new Quad(this);
            //s1.Initialize();
            //scene.AddEntity(s1);

            //s1.Pose.SetPosition(0, 0, 0);
            //s1.Pose.SetScale(4, 4, 1);

            //////////////////////////////////////////////////////////////////////////
            //Avatar a1 = new Avatar(this, "Models\\p1_wedge");
            //a1.Initialize();
            //scene.AddEntity(a1);

            //a1.pose.SetPosition(10, 20, -10);
            //a1.pose.SetScale(0.01f, 0.01f, 0.01f);

            //////////////////////////////////////////////////////////////////////////
            Light l1 = new Light(this, new Vector3(0.0f, 1.0f, 0.0f), new Vector3(1.0f, -1.0f, -1.0f));
            scene.AddLight(l1);

            //////////////////////////////////////////////////////////////////////////
            Box b1 = new Box(this);
            b1.Initialize();
            b1.SetScale(10.0f, 10.0f, 10.0f);
            b1.SetPosition(100, 10, -100);
            scene.AddEntity(b1);

            Box b2 = new Box(this);
            b2.Initialize();
            b2.SetScale(10, 10, 10);
            b2.SetPosition(150, 10, -150);
            scene.AddEntity(b2);

            //////////////////////////////////////////////////////////////////////////
            float bx = 200;
            float by = 20;
            float bz = -200;

               // Sphere s1 = new Sphere(this);
               // s1.Initialize();
               // s1.SetScale(10, 10, 10);
               // s1.SetPosition(bx + 0.0442591f, by + 0.017970f, bz + 0.045182f);
               // s1.ModelFile = "Models\\SphereLowPoly";
               // scene.AddEntity(s1);

            Axis a1 = new Axis(this);
            a1.Initialize();
            a1.SetScale(10, 10, 10);
            Matrix rot = new Matrix();
            rot.M11 = 9.9994452992e-001f; rot.M12 = 2.8077475493e-003f; rot.M13 = 1.0151533926e-002f;
            rot.M21 = -2.8522679263e-003f; rot.M22 = 9.9998636727e-001f; rot.M23 = 4.3737664750e-003f;
            rot.M31 = -1.0139115101e-002f; rot.M32 = -4.4024787564e-003f; rot.M33 = 9.9993890640e-001f;
            a1.SetRotation(rot);
            a1.SetPosition(bx + 0.0442591f, by + 0.017970f, bz + 0.045182f);
            scene.AddEntity(a1);

            //Sphere s2 = new Sphere(this);
            //s2.Initialize();
            //s2.SetScale(10, 10, 10);
            //s2.SetPosition(bx -0.028148f, by -0.875799f, bz-0.436862f);
            //s2.ModelFile = "Models\\SphereLowPoly";
            //scene.AddEntity(s2);

            Axis a2 = new Axis(this);
            a2.Initialize();
            a2.SetScale(10, 10, 10);
            Matrix rot2 = new Matrix();
            rot2.M11 = 9.9927846095e-001f; rot2.M12 = -2.9406460387e-002f; rot2.M13 = -2.4037836360e-002f;
            rot2.M21 = 2.3013993637e-002f; rot2.M22 = 9.7227461380e-001f; rot2.M23 = -2.3270674991e-001f;
            rot2.M31 = 3.0214459887e-002f; rot2.M32 = 2.3198563629e-001f; rot2.M33 = 9.7224983979e-001f;
            a1.SetRotation(rot2);
            a2.SetPosition(bx-0.028148f, by-0.875799f, bz-0.436862f);
            scene.AddEntity(a2);

            //////////////////////////////////////////////////////////////////////////

            GenericMaterial mat_b2 = new GenericMaterial(this, "Effects\\generic");
            mat_b2.CurrentTechniqueName = "TGeneric";
            mat_b2.DiffuseTextureName = "Textures\\wood";
            mat_b2.EmissiveColor = new Vector3(0.1f, 0.1f, 0.1f);
            mat_b2.SpecularColor = new Vector3(0.0f, 1.0f, 0.0f);
            mat_b2.SpecularPower = 60.0f;
            mat_b2.FogEnabled = true;

            b2.Context.Material = mat_b2;
        }