예제 #1
0
        private void UpdateShader()
        {
            // Always apply the shader
            SurfaceEffect = SurfaceEffect.Shader;
            SurfaceShader = _particleShader;

            _particleShader.SpawnRadius    = Preset.SpawnRadius;
            _particleShader.SystemHeight   = Preset.SystemHeight;
            _particleShader.ParticleSpeed  = Preset.Movement.Length();
            _particleShader.ParticleSpread = Preset.ParticleSpread;
            _particleShader.ParticleSize   = Preset.ParticleSize;
            _particleShader.ParticleShape  = Preset.ParticleShape;

            if (Preset.TextureDirty)
            {
                // Release the previous texture
                _particleShader.ParticleTexture?.ReleaseReference();

                string id = Path.Combine("Shaders", Preset.ParticleTexture);
                _particleShader.ParticleTexture =
                    // Check the new texture is available/exists
                    (!string.IsNullOrEmpty(Preset.ParticleTexture) && ContentManager.FileExists("Textures", Path.Combine("Shaders", Preset.ParticleTexture)))
                        ? XTexture.Get(Engine, id)
                        : null;

                Preset.TextureDirty = false;
            }
        }
예제 #2
0
        //--------------------//

        #region Engine
        protected override void OnEngineSet()
        {
            base.OnEngineSet();

            // Load particle mesh
            using (var stream = ContentManager.GetFileStream("Meshes", "Engine/Particles.x"))
                _particleMesh = Mesh.FromStream(Engine.Device, stream, MeshFlags.Managed);

            // Load particle shader
            string id = Path.Combine("Shaders", Preset.ParticleTexture);

            _particleShader = new ParticleShader(XTexture.Get(Engine, id));
        }
예제 #3
0
        /// <summary>
        /// Creates a new water plane.
        /// </summary>
        /// <param name="engine">The <see cref="Engine"/> to use for rendering.</param>
        /// <param name="size">The size of the water plane.</param>
        public Water(Engine engine, SizeF size) : base(BuildMesh(engine, size), XMaterial.DefaultMaterial)
        {
            Engine = engine;
            Size   = size;

            RenderIn              = ViewType.NormalOnly;
            Pickable              = false;
            SurfaceEffect         = SurfaceEffect.Shader;
            Materials[0].Emissive = Color.LightBlue;

            BoundingBox   = new BoundingBox(new Vector3(0, 0, 0), new Vector3(size.Width, 0, -size.Height));
            _waterTexture = XTexture.Get(Engine, @"Water\surface.png");
            _waterTexture.HoldReference();
        }
        /// <inheritdoc/>
        protected override void OnEngineSet()
        {
            if (MinShaderModel > Engine.Capabilities.MaxShaderModel)
            {
                throw new NotSupportedException(Resources.NotSupportedShader);
            }
            LoadShaderFile("Post_ScratchedFilm.fxo");

            // Load noise texture
            _noiseTexture = XTexture.Get(Engine, "Shaders/Noise128.dds");
            _noiseTexture.HoldReference();
            Effect.SetTexture("Noise2DTex", _noiseTexture);

            base.OnEngineSet();
        }
예제 #5
0
        /// <summary>
        /// Creates a new skybox using a cached <see cref="XTexture"/>s (loading new ones if they aren't cached).
        /// </summary>
        /// <param name="engine">The <see cref="Engine"/> providing the cache and rendering capabilities.</param>
        /// <param name="rt">The ID of the "right" texture.</param>
        /// <param name="lf">The ID of the "left" texture</param>
        /// <param name="up">The ID of the "up" texture.</param>
        /// <param name="dn">The ID of the "down" texture.</param>
        /// <param name="ft">The ID of the "front" texture.</param>
        /// <param name="bk">The ID of the "back" texture.</param>
        /// <returns>The skybox that was created.</returns>
        /// <exception cref="FileNotFoundException">On of the specified texture files could not be found.</exception>
        /// <exception cref="IOException">There was an error reading one of the texture files.</exception>
        /// <exception cref="UnauthorizedAccessException">Read access to one of the texture files is not permitted.</exception>
        /// <exception cref="InvalidDataException">One of the texture files does not contain a valid texture.</exception>
        public static SimpleSkybox FromAssets(Engine engine, string rt, string lf, string up, string dn, string ft, string bk)
        {
            #region Sanity checks
            if (engine == null)
            {
                throw new ArgumentNullException(nameof(engine));
            }
            #endregion

            var textures = new[]
            {
                XTexture.Get(engine, rt), XTexture.Get(engine, lf), XTexture.Get(engine, up),
                XTexture.Get(engine, dn), XTexture.Get(engine, ft), XTexture.Get(engine, bk)
            };
            return(new SimpleSkybox(textures));
        }
예제 #6
0
        private void InitializeScene()
        {
            var scene = new Scene
            {
                Positionables = { Model.Sphere(Engine, XTexture.Get(Engine, "flag.png"), slices: 50, stacks: 50) }
            };

            _camera = new TrackCamera {
                VerticalRotation = 20
            };
            var view = new View(scene, _camera)
            {
                BackgroundColor = Color.CornflowerBlue
            };

            Engine.Views.Add(view);
        }
예제 #7
0
        private static XData _Export(string n, params GameObject[] go)
        {
            XData            d    = new XData(n, FileType.Unity);
            List <Material>  mats = new List <Material>();
            List <Texture2D> texs = new List <Texture2D>();

            for (int i = 0; i < go.Length; i++)
            {
                d.Objects.Add(CreateElement(go[i], mats));
            }
            for (int i = 0; i < mats.Count; i++)
            {
                Material  m  = mats[i];
                XMaterial xm = new XMaterial();
                xm.Color = m.GetColor(_ColorId).ToXColor();
                Texture2D t = m.GetTexture(_MainTexId) as Texture2D;
                if (t != null)
                {
                    XMapping xp = new XMapping();
                    xp.ID = texs.IndexOf(t);
                    if (xp.ID < 0)
                    {
                        xp.ID = texs.Count;
                        texs.Add(t);
                    }
                    xp.Type   = TextureType.Diffuse;
                    xp.Offset = m.GetTextureOffset(_MainTexId).ToXVec2();
                    xp.Scale  = m.GetTextureScale(_MainTexId).ToXVec2();
                    xm.Maps.Add(xp);
                }
                d.Materials.Add(xm);
            }
            for (int i = 0; i < texs.Count; i++)
            {
                Texture2D t  = texs[i];
                Texture2D _t = new Texture2D(t.width, t.height);
                _t.SetPixels(t.GetPixels());
                XTexture xt = new XTexture();
                xt.Data = _t.EncodeToPNG();
                d.Textures.Add(xt);
            }
            return(d);
        }
예제 #8
0
        internal void OnTextureLoaded(XTexture tex)
        {
            if (_textureLoadHook == null)
            {
                return;
            }

            foreach (Delegate toInvoke in _textureLoadHook.GetInvocationList())
            {
                try
                {
                    toInvoke.DynamicInvoke(tex);
                }
                catch (Exception e)
                {
                    Logger.LogError($"[{GetModNameFromDelegate(toInvoke)}] Error running TextureLoaded\n{e}");
                }
            }
        }
예제 #9
0
        /// <inheritdoc/>
        protected override void RegisterRenderablesSync()
        {
            base.RegisterRenderablesSync();

            RenderablesSync.Register(
                (Waypoint waypoint) => new Model(XMesh.Get(Engine, "Engine/Waypoint.x"))
            {
                Scale = new Vector3(100)
            },
                UpdateRepresentation);
            RenderablesSync.Register(
                (Trigger trigger) =>
            {
                var area      = Model.Cylinder(Engine, XTexture.Get(Engine, "flag.png"), radiusBottom: trigger.Range, radiusTop: trigger.Range, length: 150);
                area.Rotation = Quaternion.RotationYawPitchRoll(0, (float)Math.PI / 2, 0);
                area.Alpha    = 160;
                return(area);
            },
                UpdateRepresentation);
        }
예제 #10
0
        //--------------------//

        #region Texture helper
        /// <summary>
        /// Loads the textures for the <see cref="CpuParticle"/> sprites
        /// </summary>
        private void UpdateSpriteTextures()
        {
            // Release previous textures
            _material1.ReleaseReference();
            _material2.ReleaseReference();

            #region First-life material
            // Start with empty material
            _material1 = XMaterial.DefaultMaterial;
            if (!string.IsNullOrEmpty(Preset.Particle1Texture))
            {
                // Get texture path
                string texture = Path.Combine("Particles", Preset.Particle1Texture);

                // Check if texture path is valid
                if (ContentManager.FileExists("Textures", texture))
                {
                    _material1 = new XMaterial(XTexture.Get(Engine, texture));
                    _material1.HoldReference();
                }
            }
            #endregion

            #region Second-life material
            _material2 = XMaterial.DefaultMaterial;
            if (!string.IsNullOrEmpty(Preset.Particle2Texture))
            {
                // Get texture path
                string texture = Path.Combine("Particles", Preset.Particle2Texture);

                // Check if texture path is valid
                if (ContentManager.FileExists("Textures", texture))
                {
                    _material2 = new XMaterial(XTexture.Get(Engine, texture));
                    _material2.HoldReference();
                }
            }
            #endregion

            Preset.TexturesDirty = false;
        }
예제 #11
0
        //--------------------//

        #region Engine
        /// <inheritdoc/>
        protected override void OnEngineSet()
        {
            if (MinShaderModel > Engine.Capabilities.MaxShaderModel)
            {
                throw new NotSupportedException(Resources.NotSupportedShader);
            }

            LoadShaderFile("Water.fxo");

            _reflectionMapHandle = Effect.GetParameter(null, "ReflectionMap");
            _refractionMapHandle = Effect.GetParameter(null, "RefractionMap");
            _normalTextureHandle = Effect.GetParameter(null, "NormalTexture");

            if (_refractionView == null)
            {
                _waterTexture = XTexture.Get(Engine, @"Water\surface.png");
                _waterTexture.HoldReference();
                Effect.Technique = "Simple";
            }
            else
            {
                _normalTexture = XTexture.Get(Engine, @"Water\normal.png");
                _normalTexture.HoldReference();

                if (_reflectionView == null)
                {
                    _waterTexture = XTexture.Get(Engine, @"Water\surface.png");
                    _waterTexture.HoldReference();
                    Effect.Technique = "Refraction";
                }
                else
                {
                    Effect.Technique = "RefractionReflection";
                }
            }

            base.OnEngineSet();
        }
예제 #12
0
        private static GameObject _Import(XData d)
        {
            GameObject g = new GameObject(d.Name);

            List <Texture2D> t = new List <Texture2D>();

            for (int i = 0; i < d.Textures.Count; i++)
            {
                XTexture  t0 = d.Textures[i];
                Texture2D t1 = new Texture2D(0, 0);
                t1.name = t0.Name;
                if (t1.LoadImage(t0.Data))
                {
                    t.Add(t1);
                }
                else
                {
                    Debug.LogError("XRobject Texture Load Error!");
                }
            }
            List <Material> m = new List <Material>();

            for (int i = 0; i < d.Materials.Count; i++)
            {
                XMaterial xm = d.Materials[i];
                Material  _m;
                if (xm.Color.a > 0.0 && xm.Color.a < 1.0)
                {
                    _m = new Material(mat_transparent);
                }
                else
                {
                    _m = new Material(mat_diffuse);
                }
                _m.name  = xm.Name;
                _m.color = new Color(xm.Color.r, xm.Color.g, xm.Color.b, xm.Color.a);
                //mat.SetColor("_SpecColor", m.Specular.ToColor());
                //mat.SetFloat("_Shininess", m.Shininess);
                foreach (XMapping xp in xm.Maps)
                {
                    string ts = null;
                    switch (xp.Type)
                    {
                    case TextureType.Diffuse:
                        ts = "_MainTex";
                        break;

                    case TextureType.Normal:
                        ts = "_BumpMap";
                        break;

                    case TextureType.Reflection:
                        break;
                    }
                    _m.SetTexture(ts, t[xp.ID]);
                    _m.SetTextureScale(ts, xp.Scale.ToVector2());
                    _m.SetTextureOffset(ts, xp.Offset.ToVector2());
                }
                m.Add(_m);
            }
            Vector3 min = Vector3.one * float.MaxValue;
            Vector3 max = Vector3.one * float.MinValue;

            CreateGameObject(d.Type, d.Objects, g.transform, m.ToArray(), ref min, ref max);

            switch (d.Type)
            {
            case FileType.Maya:
            case FileType.Unity:
                break;

            case FileType.D3Max:
            case FileType.Revit:
                g.transform.localScale  = new Vector3(-1f, 1f, 1f);
                g.transform.eulerAngles = new Vector3(-90f, 0.0f, 0.0f);
                break;
            }
            BoxCollider b = g.AddComponent <BoxCollider>();

            b.size   = max - min;
            b.center = (max + min) * 0.5f;
            return(g);
        }
예제 #13
0
        private XMapping ReadTexture(AssetProperty asset)
        {
            switch (asset.Type)
            {
            case AssetPropertyType.APT_DoubleArray4d:
                TextureType t = TextureType.None;
                switch (asset.Name)
                {
                case "generic_diffuse":
                case "ceramic_color":
                    t = TextureType.Diffuse;
                    break;

                case "generic_bump_map":
                case "ceramic_bump_map":
                    t = TextureType.Normal;
                    break;
                }
                if (t != TextureType.None)
                {
                    IList <AssetProperty> la = asset.GetAllConnectedProperties();
                    if (la.Count > 0)
                    {
                        Asset    a = la[0] as Asset;
                        XMapping m = new XMapping();
                        m.Type = t;
                        XVec2 s = new XVec2();
                        XVec2 o = new XVec2();
                        for (int i = 0; i < a.Size; i++)
                        {
                            AssetProperty _a = a[i];
                            switch (_a.Name)
                            {
                            case "unifiedbitmap_Bitmap":
                                AssetPropertyString aps = _a as AssetPropertyString;
                                string p = aps.Value.Split('|')[0];
                                if (!p.Contains(":"))
                                {
                                    if (p.Contains("/") || p.Contains("\\"))
                                    {
                                        p = Path.Combine(localMap, p);
                                    }
                                    else
                                    {
                                        p = Path.Combine(localMap, localMap1, p);
                                    }
                                }
                                if (texLink.ContainsKey(p))
                                {
                                    m.ID = texLink[p];
                                }
                                else
                                {
                                    if (File.Exists(p))
                                    {
                                        using (FileStream fs = new FileStream(p, FileMode.Open, FileAccess.Read))
                                        {
                                            XTexture tex = new XTexture();
                                            tex.Name = fs.Name;
                                            tex.Data = new byte[fs.Length];
                                            fs.Read(tex.Data, 0, (int)fs.Length);
                                            m.ID = XData.Textures.Count;
                                            texLink.Add(p, m.ID);
                                            XData.Textures.Add(tex);
                                        }
                                    }
                                    else
                                    {
                                        return(null);
                                    }
                                }
                                break;

                            case "texture_RealWorldScaleX":
                                AssetPropertyDistance asx = _a as AssetPropertyDistance;
                                s.x = 1 / (float)UnitUtils.Convert(asx.Value, asx.DisplayUnitType, DisplayUnitType.DUT_DECIMAL_FEET);
                                break;

                            case "texture_RealWorldScaleY":
                                AssetPropertyDistance asy = _a as AssetPropertyDistance;
                                s.y = 1 / (float)UnitUtils.Convert(asy.Value, asy.DisplayUnitType, DisplayUnitType.DUT_DECIMAL_FEET);
                                break;

                            case "texture_RealWorldOffsetX":
                                AssetPropertyDistance aox = _a as AssetPropertyDistance;
                                o.x = s.x * (float)UnitUtils.Convert(aox.Value, aox.DisplayUnitType, DisplayUnitType.DUT_DECIMAL_FEET);
                                break;

                            case "texture_RealWorldOffsetY":
                                AssetPropertyDistance aoy = _a as AssetPropertyDistance;
                                o.y = s.y * (float)UnitUtils.Convert(aoy.Value, aoy.DisplayUnitType, DisplayUnitType.DUT_DECIMAL_FEET);
                                break;
                            }
                        }
                        m.Scale  = s;
                        m.Offset = o;
                        return(m);
                    }
                }
                break;
            }
            return(null);
        }
예제 #14
0
        /// <inheritdoc/>
        protected override void RegisterRenderablesSync()
        {
            RegisterWater();

            RegisterRenderComponentLight();

            RegisterRenderComponent <StaticMesh>((entity, component) =>
            {
                if (string.IsNullOrEmpty(component.Filename))
                {
                    return(null);
                }

                var model = new Model(XMesh.Get(Engine, component.Filename))
                {
                    Name = entity.Name
                };
                ConfigureModel(model, component);
                return(model);
            });
            RegisterRenderComponent <AnimatedMesh>((entity, component) =>
            {
                if (string.IsNullOrEmpty(component.Filename))
                {
                    return(null);
                }

                var model = new AnimatedModel(XAnimatedMesh.Get(Engine, component.Filename))
                {
                    Name = entity.Name
                };
                ConfigureModel(model, component);
                return(model);
            });
            RegisterRenderComponent <TestSphere>((entity, component) =>
            {
                var model  = Model.Sphere(Engine, XTexture.Get(Engine, component.Texture), component.Radius, component.Slices, component.Stacks);
                model.Name = entity.Name;
                ConfigureModel(model, component);
                return(model);
            });
            RegisterRenderComponent <CpuParticleSystem>((entity, component) =>
            {
                if (string.IsNullOrEmpty(component.Filename))
                {
                    return(null);
                }

                var particleSystem = new OmegaEngine.Graphics.Renderables.CpuParticleSystem(CpuParticlePreset.FromContent(component.Filename));
                ConfigureParticleSystem(entity, particleSystem, component);
                return(particleSystem);
            });
            RegisterRenderComponent <GpuParticleSystem>((entity, component) =>
            {
                if (string.IsNullOrEmpty(component.Filename))
                {
                    return(null);
                }

                var particleSystem = new OmegaEngine.Graphics.Renderables.GpuParticleSystem(GpuParticlePreset.FromContent(component.Filename));
                ConfigureParticleSystem(entity, particleSystem, component);
                return(particleSystem);
            });
        }