Beispiel #1
0
 /// <summary>
 /// Initializes a new instance of the <see cref="LightComponent"/> class.
 /// </summary>
 public LightComponent()
 {
     Type        = new LightDirectional();
     Intensity   = 1.0f;
     CullingMask = EntityGroupMask.All;
     Id          = Interlocked.Increment(ref LightComponentIds);
 }
        public static Scene Lighting1()
        {
            Scene scene = new Scene(new Color(255, 255, 255));

            Light      sun        = new LightDirectional(new Color(255, 255, 255), new Vector3(1, -1, 1), scene);
            LightGroup lightGroup = new LightGroup();

            lightGroup.addLight(sun);

            SurfaceMaterial diffuseRed   = new SurfaceDiffuse(new Color(255, 50, 50), lightGroup);
            SurfaceMaterial diffuseGreen = new SurfaceDiffuse(new Color(50, 255, 50), lightGroup);
            SurfaceMaterial diffuseBlue  = new SurfaceDiffuse(new Color(50, 50, 255), lightGroup);
            SurfaceMaterial diffuseWhite = new SurfaceDiffuse(new Color(255, 255, 255), lightGroup);

            SurfaceMaterial specular = new SurfaceSpecular(new Color(100, 100, 100), 50, lightGroup);

            SurfaceMaterial red   = new SurfaceMaterialSum(specular, diffuseRed);
            SurfaceMaterial blue  = new SurfaceMaterialSum(specular, diffuseBlue);
            SurfaceMaterial green = new SurfaceMaterialSum(specular, diffuseGreen);
            SurfaceMaterial white = new SurfaceMaterialSum(specular, diffuseWhite);

            scene.addSurface(new Surface(new SurfacePlane(-25 * Vector3.Up, Vector3.Forward, Vector3.Right), white));
            scene.addSurface(new Surface(new SurfaceSphere(new Vector3(-40, 0, 80), 10), red));
            scene.addSurface(new Surface(new SurfaceSphere(new Vector3(0, 0, 80), 10), green));
            scene.addSurface(new Surface(new SurfaceSphere(new Vector3(40, 0, 80), 10), blue));

            return(scene);
        }
Beispiel #3
0
        public void Apply(LightDirectional light)
        {
            string techniqueName = "Visualize" + VisualizeCascades + "Filter" + FilterAcrossCascades + "FilterSize" + FilterSize;

            CurrentTechnique = Techniques[techniqueName];

            p_cameraPosWS.SetValue(CameraPosWS);
            p_shadowMatrix.SetValue(ShadowMatrix);
            p_cascadeSplits.SetValue(new Vector4(light.ShadowMapper.CascadeSplits[0],
                                                 light.ShadowMapper.CascadeSplits[1],
                                                 light.ShadowMapper.CascadeSplits[2],
                                                 light.ShadowMapper.CascadeSplits[3]));
            p_cascadeOffsets.SetValue(light.ShadowMapper.CascadeOffsets);
            p_cascadeScales.SetValue(light.ShadowMapper.CascadeScales);
            p_bias.SetValue(Bias);
            p_offsetScale.SetValue(OffsetScale);
            p_lightDirection.SetValue(light.DirectionToLight);
            p_lightColor.SetValue(light.ColorIntensity);
            p_world.SetValue(World);
            p_viewProjection.SetValue(ViewProjection);
            p_shadowMapSize.SetValue(new Vector2(light.ShadowMapper.ShadowMaps.Width,
                                                 light.ShadowMapper.ShadowMaps.Height));

            p_shadowMaps.SetValue(light.ShadowMapper.ShadowMaps);
        }
        public static Scene Boolean(bool outlines)
        {
            Scene scene = new Scene(new Color(255, 255, 255));

            Light      sun        = new LightDirectional(new Color(255, 255, 255), new Vector3(0, -1, 1), scene, false);
            LightGroup lightGroup = new LightGroup();

            lightGroup.addLight(sun);

            Vector3 center1 = new Vector3(-12, 0, 30);
            Vector3 center2 = new Vector3(-6, 0, 30);
            Vector3 center3 = new Vector3(9, 0, 30);
            Vector3 center4 = new Vector3(9, 1, 27);
            double  radius  = 5;
            double  radius2 = 3;

            SurfaceGeometry surfaceIntersection = new SurfaceIntersection(new VolumeSphere(center1, radius), new VolumeSphere(center2, radius));
            SurfaceGeometry surfaceDifference   = new SurfaceDifference(new VolumeSphere(center3, radius), new VolumeSphere(center4, radius2));

            SurfaceMaterial diffuse    = new SurfaceDiffuse(new Color(255, 255, 255), lightGroup);
            SurfaceMaterial semiopaque = new SurfaceMaterialBlend(new SurfaceTransparent(scene), diffuse, 0.2);

            scene.addSurface(new Surface(surfaceIntersection, diffuse));
            scene.addSurface(new Surface(surfaceDifference, diffuse));
            if (outlines)
            {
                scene.addSurface(new Surface(new SurfaceSphere(center1, radius), semiopaque));
                scene.addSurface(new Surface(new SurfaceSphere(center2, radius), semiopaque));
                scene.addSurface(new Surface(new SurfaceSphere(center3, radius), semiopaque));
                scene.addSurface(new Surface(new SurfaceSphere(center4, radius2), semiopaque));
            }

            return(scene);
        }
        public static Scene Lighting2()
        {
            Scene      scene      = new Scene(new Color(255, 255, 255));
            Light      lightRed   = new LightDirectional(new Color(255, 0, 0), new Vector3(1, -3, 0), scene, true);
            Light      lightGreen = new LightDirectional(new Color(0, 255, 0), new Vector3(-1, -3, 0), scene, true);
            Light      lightBlue  = new LightDirectional(new Color(0, 0, 255), new Vector3(0, -3, 1), scene, true);
            LightGroup lightGroup = new LightGroup();

            lightGroup.addLight(lightRed);
            lightGroup.addLight(lightGreen);
            lightGroup.addLight(lightBlue);

            SurfaceMaterial diffuseWhite = new SurfaceDiffuse(new Color(255, 255, 255), lightGroup);
            SurfaceMaterial specular     = new SurfaceSpecular(new Color(100, 100, 100), 50, lightGroup);
            SurfaceMaterial material     = new SurfaceMaterialSum(specular, diffuseWhite);

            scene.addSurface(new Surface(new SurfacePlane(-25 * Vector3.Up, Vector3.Forward, Vector3.Right), diffuseWhite));
            scene.addSurface(new Surface(new SurfaceSphere(new Vector3(0, 0, 80), 10), material));

            return(scene);
        }
        /// <summary>
        /// Creates shadow maps for the specified directional light.
        /// </summary>
        /// <param name="sunLight"></param>
        /// <param name="drawGeometry"></param>
        public void ShadowMap(LightDirectional sunLight, Action <DrawStep> drawGeometry)
        {
            if (!sunLight.EnableShadows)
            {
                return;
            }

            if (sunLight.ShadowMapper == null)
            {
                sunLight.ShadowMapper = new CascadedShadowMapper(graphics, new ShadowSettings(), content);
            }

            drawStep.ShadowCastersOnly = true;

            sunLight.ShadowMapper.RenderShadowMap(graphics, sunLight, Camera, (c, effect) =>
            {
                drawStep.Camera = c;
                drawStep.Effect = effect;

                drawGeometry(drawStep);
            });
        }
        /// <summary>
        /// Makes the "global" shadow matrix used as the reference point for the cascades.
        /// </summary>
        private Matrix MakeGlobalShadowMatrix(Camera camera, LightDirectional sunLight)
        {
            // Get the 8 points of the view frustum in world space
            ResetViewFrustumCorners();

            var invViewProj   = Matrix.Invert(camera.ViewProjection);
            var frustumCenter = Vector3.Zero;

            for (var i = 0; i < 8; i++)
            {
                _frustumCorners[i] = Vector4.Transform(_frustumCorners[i], invViewProj).ToVector3();
                frustumCenter     += _frustumCorners[i];
            }

            frustumCenter /= 8.0f;

            // Pick the up vector to use for the light camera
            var upDir = camera.Right;

            // This needs to be constant for it to be stable
            if (_settings.StabilizeCascades)
            {
                upDir = Vector3.Up;
            }

            // Get position of the shadow camera
            var shadowCameraPos = frustumCenter + sunLight.DirectionToLight * -0.5f;

            // Come up with a new orthographic camera for the shadow caster
            var shadowCamera = new OrthographicCamera(-0.5f, -0.5f, 0.5f, 0.5f, 0.0f, 1.0f);

            shadowCamera.SetLookAt(shadowCameraPos, frustumCenter, upDir);

            var texScaleBias = Matrix.CreateScale(0.5f, -0.5f, 1.0f);

            texScaleBias.Translation = new Vector3(0.5f, 0.5f, 0.0f);
            return(shadowCamera.ViewProjection * texScaleBias);
        }
Beispiel #8
0
 /// <summary>
 /// Initializes a new instance of the <see cref="LightComponent"/> class.
 /// </summary>
 public LightComponent()
 {
     Type      = new LightDirectional();
     Intensity = 1.0f;
 }
Beispiel #9
0
 /// <summary>
 /// Initializes a new instance of the <see cref="LightComponent"/> class.
 /// </summary>
 public LightComponent()
 {
     Type        = new LightDirectional();
     Intensity   = 1.0f;
     CullingMask = EntityGroupMask.All;
 }
Beispiel #10
0
        public override void Load(BinaryReader reader, int size)
        {
            //long start_pos = reader.BaseStream.Position;

            HeaderUnk1 = reader.ReadUInt32();
            uint chunkNameLength = reader.ReadUInt32();

            ChunkName  = new string(reader.ReadChars((int)chunkNameLength));
            HeaderUnk2 = reader.ReadUInt32();
            HeaderUnk3 = reader.ReadUInt32();
            HeaderUnk4 = reader.ReadByte();
            if ((HeaderUnk1 & 0x10000) != 0)
            {
                SkydomeID = reader.ReadUInt32();
            }

            LightsAmbient     = new List <LightAmbient>();
            LightsDirectional = new List <LightDirectional>();
            LightsPoint       = new List <LightPoint>();
            LightsNegative    = new List <LightNegative>();

            if ((HeaderUnk1 & 0x20000) != 0)
            {
                HeaderBuffer = reader.ReadBytes(0x400);

                uint LightsNum = reader.ReadUInt32();

                uint LightAmbientNum     = reader.ReadUInt32();
                uint LightDirectionalNum = reader.ReadUInt32();
                uint LightPointNum       = reader.ReadUInt32();
                uint LightNegativeNum    = reader.ReadUInt32();

                if (LightAmbientNum > 0)
                {
                    for (int i = 0; i < LightAmbientNum; i++)
                    {
                        LightAmbient light = new LightAmbient();

                        light.Flags     = reader.ReadBytes(4);
                        light.Radius    = reader.ReadSingle();
                        light.Color_R   = reader.ReadSingle();
                        light.Color_G   = reader.ReadSingle();
                        light.Color_B   = reader.ReadSingle();
                        light.Color_Unk = reader.ReadSingle();
                        light.Position  = new Pos(reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle());
                        light.Vector1   = new Pos(reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle());
                        light.Vector2   = new Pos(reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle());

                        LightsAmbient.Add(light);
                    }
                }
                if (LightDirectionalNum > 0)
                {
                    for (int i = 0; i < LightDirectionalNum; i++)
                    {
                        LightDirectional light = new LightDirectional();

                        light.Flags     = reader.ReadBytes(4);
                        light.Radius    = reader.ReadSingle();
                        light.Color_R   = reader.ReadSingle();
                        light.Color_G   = reader.ReadSingle();
                        light.Color_B   = reader.ReadSingle();
                        light.Color_Unk = reader.ReadSingle();
                        light.Position  = new Pos(reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle());
                        light.Vector1   = new Pos(reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle());
                        light.Vector2   = new Pos(reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle());

                        light.Vector3  = new Pos(reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle());
                        light.unkShort = reader.ReadUInt16();

                        LightsDirectional.Add(light);
                    }
                }
                if (LightPointNum > 0)
                {
                    for (int i = 0; i < LightPointNum; i++)
                    {
                        LightPoint light = new LightPoint();

                        light.Flags     = reader.ReadBytes(4);
                        light.Radius    = reader.ReadSingle();
                        light.Color_R   = reader.ReadSingle();
                        light.Color_G   = reader.ReadSingle();
                        light.Color_B   = reader.ReadSingle();
                        light.Color_Unk = reader.ReadSingle();
                        light.Position  = new Pos(reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle());
                        light.Vector1   = new Pos(reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle());
                        light.Vector2   = new Pos(reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle());

                        light.unkShort = reader.ReadUInt16();

                        LightsPoint.Add(light);
                    }
                }
                if (LightNegativeNum > 0)
                {
                    for (int i = 0; i < LightNegativeNum; i++)
                    {
                        LightNegative light = new LightNegative();

                        light.Flags     = reader.ReadBytes(4);
                        light.Radius    = reader.ReadSingle();
                        light.Color_R   = reader.ReadSingle();
                        light.Color_G   = reader.ReadSingle();
                        light.Color_B   = reader.ReadSingle();
                        light.Color_Unk = reader.ReadSingle();
                        light.Position  = new Pos(reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle());
                        light.Vector1   = new Pos(reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle());
                        light.Vector2   = new Pos(reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle());

                        light.Vector3    = new Pos(reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle());
                        light.unkFloat1  = reader.ReadSingle();
                        light.unkFloat2  = reader.ReadSingle();
                        light.unkUInt1   = reader.ReadUInt32();
                        light.unkUInt2   = reader.ReadUInt32();
                        light.unkUShort1 = reader.ReadUInt16();
                        light.unkUShort2 = reader.ReadUInt16();

                        LightsNegative.Add(light);
                    }
                }
            }

            SceneryRoot = null;
            if (HeaderUnk3 == 0x160A)
            {
                unkVar5     = reader.ReadUInt32();
                SceneryRoot = LoadScenery(reader);
            }
            else
            {
                //Console.WriteLine("no scenery!! bug?");
            }

            //Console.WriteLine("end pos: " + (reader.BaseStream.Position - start_pos) + " target: " + size);
        }
Beispiel #11
0
 /// <summary>
 /// Initializes a new instance of the <see cref="LightComponent"/> class.
 /// </summary>
 public LightComponent()
 {
     Type      = new LightDirectional();
     Intensity = 1.0f;
     Id        = Interlocked.Increment(ref LightComponentIds);
 }
        public void RenderShadowMap(GraphicsDevice graphicsDevice, LightDirectional sunLight, Camera camera, Action <Camera, ShadowMapEffect> renderScene)
        {
            // Set cascade split ratios.
            _cascadeSplits[0] = _settings.SplitDistance0;
            _cascadeSplits[1] = _settings.SplitDistance1;
            _cascadeSplits[2] = _settings.SplitDistance2;
            _cascadeSplits[3] = _settings.SplitDistance3;

            var globalShadowMatrix = MakeGlobalShadowMatrix(camera, sunLight);

            ShadowMatrix = globalShadowMatrix;

            // Render the meshes to each cascade.
            for (var cascadeIdx = 0; cascadeIdx < NumCascades; ++cascadeIdx)
            {
                // Set the shadow map as the render target
                graphicsDevice.SetRenderTarget(_shadowMaps, cascadeIdx);
                graphicsDevice.Clear(ClearOptions.Target | ClearOptions.DepthBuffer, Color.White, 1.0f, 0);

                // Get the 8 points of the view frustum in world space
                ResetViewFrustumCorners();

                var prevSplitDist = cascadeIdx == 0 ? 0.0f : _cascadeSplits[cascadeIdx - 1];
                var splitDist     = _cascadeSplits[cascadeIdx];

                var invViewProj = Matrix.Invert(camera.ViewProjection);
                for (var i = 0; i < 8; ++i)
                {
                    _frustumCorners[i] = Vector4.Transform(_frustumCorners[i], invViewProj).ToVector3();
                }

                // Get the corners of the current cascade slice of the view frustum
                for (var i = 0; i < 4; ++i)
                {
                    var cornerRay     = _frustumCorners[i + 4] - _frustumCorners[i];
                    var nearCornerRay = cornerRay * prevSplitDist;
                    var farCornerRay  = cornerRay * splitDist;
                    _frustumCorners[i + 4] = _frustumCorners[i] + farCornerRay;
                    _frustumCorners[i]     = _frustumCorners[i] + nearCornerRay;
                }

                // Calculate the centroid of the view frustum slice
                var frustumCenter = Vector3.Zero;
                for (var i = 0; i < 8; ++i)
                {
                    frustumCenter = frustumCenter + _frustumCorners[i];
                }
                frustumCenter /= 8.0f;

                // Pick the up vector to use for the light camera
                var upDir = camera.Right;

                Vector3 minExtents;
                Vector3 maxExtents;

                if (_settings.StabilizeCascades)
                {
                    // This needs to be constant for it to be stable
                    upDir = Vector3.Up;

                    // Calculate the radius of a bounding sphere surrounding the frustum corners
                    var sphereRadius = 0.0f;
                    for (var i = 0; i < 8; ++i)
                    {
                        var dist = (_frustumCorners[i] - frustumCenter).Length();
                        sphereRadius = Math.Max(sphereRadius, dist);
                    }

                    sphereRadius = (float)Math.Ceiling(sphereRadius * 16.0f) / 16.0f;

                    maxExtents = new Vector3(sphereRadius);
                    minExtents = -maxExtents;
                }
                else
                {
                    // Create a temporary view matrix for the light
                    var lightCameraPos = frustumCenter;
                    var lookAt         = frustumCenter - sunLight.DirectionToLight;
                    var lightView      = Matrix.CreateLookAt(lightCameraPos, lookAt, upDir);

                    // Calculate an AABB around the frustum corners
                    var mins  = new Vector3(float.MaxValue);
                    var maxes = new Vector3(float.MinValue);
                    for (var i = 0; i < 8; ++i)
                    {
                        var corner = Vector4.Transform(_frustumCorners[i], lightView).ToVector3();
                        mins  = Vector3.Min(mins, corner);
                        maxes = Vector3.Max(maxes, corner);
                    }

                    minExtents = mins;
                    maxExtents = maxes;

                    // Adjust the min/max to accommodate the filtering size
                    var scale = (ShadowMapSize + _settings.FixedFilterKernelSize) / (float)ShadowMapSize;
                    minExtents.X *= scale;
                    minExtents.Y *= scale;
                    maxExtents.X *= scale;
                    maxExtents.Y *= scale;
                }

                var cascadeExtents = maxExtents - minExtents;

                // Get position of the shadow camera
                var shadowCameraPos = frustumCenter + sunLight.DirectionToLight * -minExtents.Z;

                // Come up with a new orthographic camera for the shadow caster
                var shadowCamera = new OrthographicCamera(
                    minExtents.X, minExtents.Y, maxExtents.X, maxExtents.Y,
                    0.0f, cascadeExtents.Z);
                shadowCamera.SetLookAt(shadowCameraPos, frustumCenter, upDir);

                if (_settings.StabilizeCascades)
                {
                    // Create the rounding matrix, by projecting the world-space origin and determining
                    // the fractional offset in texel space
                    var shadowMatrixTemp = shadowCamera.ViewProjection;
                    var shadowOrigin     = new Vector4(0.0f, 0.0f, 0.0f, 1.0f);
                    shadowOrigin = Vector4.Transform(shadowOrigin, shadowMatrixTemp);
                    shadowOrigin = shadowOrigin * (ShadowMapSize / 2.0f);

                    var roundedOrigin = Vector4Utility.Round(shadowOrigin);
                    var roundOffset   = roundedOrigin - shadowOrigin;
                    roundOffset   = roundOffset * (2.0f / ShadowMapSize);
                    roundOffset.Z = 0.0f;
                    roundOffset.W = 0.0f;

                    var shadowProj = shadowCamera.Projection;
                    //shadowProj.r[3] = shadowProj.r[3] + roundOffset;
                    shadowProj.M41         += roundOffset.X;
                    shadowProj.M42         += roundOffset.Y;
                    shadowProj.M43         += roundOffset.Z;
                    shadowProj.M44         += roundOffset.W;
                    shadowCamera.Projection = shadowProj;
                }

                // Draw the mesh with depth only, using the new shadow camera
                graphicsDevice.BlendState        = BlendState.Opaque;
                graphicsDevice.DepthStencilState = DepthStencilState.Default;
                graphicsDevice.RasterizerState   = CreateShadowMapRasterizerState;

                _shadowMapEffect.View       = shadowCamera.View;
                _shadowMapEffect.Projection = shadowCamera.Projection;
                renderScene(shadowCamera, _shadowMapEffect);

                // Apply the scale/offset matrix, which transforms from [-1,1]
                // post-projection space to [0,1] UV space
                var texScaleBias = Matrix.CreateScale(0.5f, -0.5f, 1.0f)
                                   * Matrix.CreateTranslation(0.5f, 0.5f, 0.0f);
                var shadowMatrix = shadowCamera.ViewProjection;
                shadowMatrix = shadowMatrix * texScaleBias;

                // Store the split distance in terms of view space depth
                var clipDist = camera.FarZ - camera.NearZ;

                CascadeSplits[cascadeIdx] = camera.NearZ + splitDist * clipDist;

                // Calculate the position of the lower corner of the cascade partition, in the UV space
                // of the first cascade partition
                var invCascadeMat = Matrix.Invert(shadowMatrix);
                var cascadeCorner = Vector4.Transform(Vector3.Zero, invCascadeMat).ToVector3();
                cascadeCorner = Vector4.Transform(cascadeCorner, globalShadowMatrix).ToVector3();

                // Do the same for the upper corner
                var otherCorner = Vector4.Transform(Vector3.One, invCascadeMat).ToVector3();
                otherCorner = Vector4.Transform(otherCorner, globalShadowMatrix).ToVector3();

                // Calculate the scale and offset
                var cascadeScale = Vector3.One / (otherCorner - cascadeCorner);
                CascadeOffsets[cascadeIdx] = new Vector4(-cascadeCorner, 0.0f);
                CascadeScales[cascadeIdx]  = new Vector4(cascadeScale, 1.0f);
            }
        }
Beispiel #13
0
        private void LoadWorld()
        {
            world3D = new World3D();
            world3D.LoadContent();

            camera1 = new Camera();
            camera1.LoadContent();
            camera1.position = new Vector3(0, 27, -22);
            camera1.Face(new Vector3(0, 26, -21));

            camera2 = new Camera();
            camera2.LoadContent();
            camera2.position = new Vector3(10, 10, -10);
            camera2.Face(new Vector3(0, 0, 0));
            camera2.minDepth = 1f;
            camera2.maxDepth = 50f;

            dirLight1 = new LightDirectional();
            dirLight1.LoadContent();

            zombie = new GenericModelObject();
            zombie.LoadContent();
            zombie.model     = Global.zombieSceneModel;
            zombie.ColorMap  = Global.zombieDiffuse;
            zombie.NormalMap = Global.zombieNormal;
            zombie.position  = new Vector3(0, 0.5f, -20);
            zombie.angle.Y   = MathHelper.ToRadians(-90);

            map = new Map();
            map.LoadContent();
            map.length = 10;
            map.width  = 10;

            Cwing1 = new GenericModelObject();
            Cwing1.LoadContent();
            Cwing1.model       = Global.ship1Model;
            Cwing1.ColorMap    = Global.ship1Diffuse;
            Cwing1.NormalMap   = Global.ship1Normal;
            Cwing1.SpecularMap = Global.ship1Specular;
            Cwing1.position    = new Vector3(-52, 5f, 60);

            Acolyte1 = new GenericModelObject();
            Acolyte1.LoadContent();
            Acolyte1.model       = Global.ship2Model;
            Acolyte1.ColorMap    = Global.ship2Diffuse;
            Acolyte1.NormalMap   = Global.ship2Normal;
            Acolyte1.SpecularMap = Global.ship2Specular;
            Acolyte1.position    = new Vector3(-32, 5f, 60);

            Orbiter = new Orbiter();
            Orbiter.LoadContent();
            Orbiter.size = Vector3.One * 0.4f;

            Orbiter2 = new Orbiter();
            Orbiter2.LoadContent();
            Orbiter2.size = Vector3.One * 0.4f;

            Orbiter3 = new Orbiter();
            Orbiter3.LoadContent();
            Orbiter3.size = Vector3.One * 0.4f;

            pointLight1 = new PointLight();
            pointLight1.LoadContent();
            pointLight1.intensity = 1.5f;
            pointLight1.position  = new Vector3(40, 5, 45);
            pointLight1.color     = Color.Red;

            spotLight1 = new SpotLight();
            spotLight1.LoadContent();
            spotLight1.intensity = 2f;
            spotLight1.color     = Color.White;
            spotLight1.intensity = 1.5f;
            spotLight1.position  = new Vector3(10, 40, 30);
            spotLight1.Face(new Vector3(0f, -1, -0.7f));

            spotLight2 = new SpotLight();
            spotLight2.LoadContent();
            spotLight2.intensity = 2f;
            spotLight2.color     = Color.Red;
            spotLight2.intensity = 1.5f;
            spotLight2.position  = new Vector3(-60f, 35f, 75f);
            spotLight2.Face(new Vector3(-40f, 0f, 55f));

            spotLight3 = new SpotLight();
            spotLight3.LoadContent();
            spotLight3.intensity = 2f;
            spotLight3.color     = Color.Blue;
            spotLight3.intensity = 1.5f;
            spotLight3.position  = new Vector3(-20f, 35f, 75f);
            spotLight3.Face(new Vector3(-40f, 0f, 55f));

            spotLight4 = new SpotLight();
            spotLight4.LoadContent();
            spotLight4.intensity = 2f;
            spotLight4.color     = Color.Green;
            spotLight4.intensity = 1.5f;
            spotLight4.position  = new Vector3(-40f, 35f, 75f);
            spotLight4.Face(new Vector3(-40f, 0f, 45f));

            blueBall = new Ball();
            blueBall.LoadContent();
            blueBall.position = new Vector3(10, 15, 5);
            blueBall.friction = 1f;
            blueBall.Material.diffuseColor = Color.Blue.ToVector4();
            blueBall.Material.matteColor   = Color.AliceBlue.ToVector4();
            blueBall.Material.shininess    = 1f;
            blueBall.Material.ambient      = 0.4f;

            ball2 = new Ball();
            ball2.LoadContent();
            ball2.position = new Vector3(0f, 1f, 0f);
            ball2.Material.diffuseColor = Color.DarkMagenta.ToVector4();
            ball2.Material.matteColor   = Color.Magenta.ToVector4();
            ball2.Material.shininess    = 1f;
            ball2.Material.ambient      = 0.2f;

            ball4 = new Ball();
            ball4.LoadContent();
            ball4.position = new Vector3(0f, 3f, 0f);
            ball4.Material.diffuseColor = Color.Green.ToVector4();
            ball4.Material.matteColor   = Color.GreenYellow.ToVector4();
            ball4.Material.shininess    = 1f;
            ball4.Material.ambient      = 0.2f;

            goldBall = new Ball();
            goldBall.LoadContent();
            goldBall.position = new Vector3(50f, 10f, 50f);
            goldBall.Material.diffuseColor = Color.Goldenrod.ToVector4();
            goldBall.Material.matteColor   = Color.Gold.ToVector4();
            goldBall.Material.shininess    = 2f;
            goldBall.size = Vector3.One * 10;

            orientAid = new Orientaid();
            orientAid.LoadContent();
            orientAid2 = new Orientaid();
            orientAid2.LoadContent();
            orientAid3 = new Orientaid();
            orientAid3.LoadContent();
            orientAid4 = new Orientaid();
            orientAid4.LoadContent();

            a1 = new Arrow();
            a1.LoadContent();
            a2 = new Arrow();
            a2.LoadContent();
            a3 = new Arrow();
            a3.LoadContent();
            a4 = new Arrow();
            a4.LoadContent();
            a5 = new Arrow();
            a5.LoadContent();
            a6 = new Arrow();
            a6.LoadContent();
            a7 = new Arrow();
            a7.LoadContent();
            a8 = new Arrow();
            a8.LoadContent();
            a9 = new Arrow();
            a9.LoadContent();
            a10 = new Arrow();
            a10.LoadContent();
        }
Beispiel #14
0
        public override void Initialize()
        {
            if (TextureNames == null)
            {
                throw new ArgumentNullException("DaytimeChange: Textures not set!");
            }
            if (TextureNames.Count() < TEXTURE_COUNT)
            {
                throw new ArgumentException("DaytimeChange: Texture count less than " + TEXTURE_COUNT.ToString() + "!");
            }

            sun        = ResourceManager.Instance.CurrentScene.GetObject(SunID);
            lightDay   = ResourceManager.Instance.CurrentScene.DirectionalLights[LightDayID];
            lightNight = ResourceManager.Instance.CurrentScene.DirectionalLights[LightNightID];
            ambient    = ResourceManager.Instance.CurrentScene.AmbientLight;

            foreach (ObjectComponent comp in this.MyObject.Components)
            {
                if (comp.GetType() == typeof(CustomModel))
                {
                    myModel = (CustomModel)comp;
                    if (myModel == null)
                    {
                        break;
                    }
                    if (myModel.Mat[0].GetType() != typeof(SkyboxMaterial))
                    {
                        throw new InvalidOperationException("DaytimeChange: Skybox's material is not SkyboxMaterial!");
                    }
                    myMaterial = (SkyboxMaterial)myModel.Mat[0];
                    break;
                }
            }

            if (sun == null || lightDay == null || lightNight == null || myModel == null || myMaterial == null || ambient == null)
            {
                throw new ArgumentNullException("DaytimeChange: Some of the objects do not exist!");
            }

            foreach (ObjectComponent comp in sun.Components)
            {
                if (comp.GetType() == typeof(Billboard))
                {
                    if (((Billboard)comp).Mat != null)
                    {
                        sunMaterial = ((Billboard)comp).Mat;
                    }
                }
            }

            startDaylightColor    = lightDay.LightColor;
            startDaylightSpecular = lightDay.LightSpecularColor;
            startNightColor       = lightNight.LightColor;
            startNightSpecular    = lightNight.LightSpecularColor;
            startAmbientColor     = ambient.LightColor;

            for (int i = 0; i < TEXTURE_COUNT; ++i)
            {
                if (TextureNames[i] != null)
                {
                    textures[i] = ResourceManager.Instance.LoadTextureCube(TextureNames[i]);
                }
            }

            if (textures[0] != null)
            {
                myMaterial.CubeMap = textures[0];
            }
            if (textures[1] != null)
            {
                myMaterial.CubeMap1 = textures[1];
            }
            if (textures[2] != null)
            {
                myMaterial.CubeMap2 = textures[2];
            }
            if (textures[3] != null)
            {
                myMaterial.CubeMap3 = textures[3];
            }

            GameObject pt = ResourceManager.Instance.CurrentScene.GetObject("PlayerTime");

            if (pt == null)
            {
                throw new ArgumentNullException("DaytimeChange: PlayerTime object does not exist!");
            }

            foreach (ObjectComponent comp in pt.Components)
            {
                if (comp.GetType() == typeof(PlayerTime))
                {
                    cTime = (PlayerTime)comp;
                }
            }

            if (cTime == null)
            {
                throw new ArgumentNullException("DaytimeChange: PlayerTime object has no PlayerTime component!");
            }

            long t = cTime.TotalMilliseconds;

            if (t >= SunriseMS - StateChangeMS && t <= SunsetMS + StateChangeMS)
            {
                switched = true;
            }
            else
            {
                switched = false;
            }

            startSunDiffuse = myMaterial.DiffuseColor;

            base.Initialize();
        }
Beispiel #15
0
        public override void Update(Microsoft.Xna.Framework.GameTime gameTime)
        {
            if (!TrashSoupGame.Instance.EditorMode)
            {
                prevTime = time;
                //time = 60 * cTime.Hours + cTime.Minutes;
                time = cTime.TotalMilliseconds % MS_MAX;
                if (time < 0 || time > MS_MAX)
                {
                    Debug.Log("DaytimeChange: Time is invalid. Clamping.");
                    time = (int)MathHelper.Clamp((float)time, 0.0f, (float)MS_MAX);
                }

                //if (time != prevTime)
                //{
                Vector4 probes;
                ConvertTimeToProbes(time, out probes);
                myMaterial.Probes = probes;

                Vector3 lightDir;
                ConvertTimeToLightDirection(time, out lightDir);
                lightDay.LightDirection   = lightDir;
                lightNight.LightDirection = -lightDir;

                SetupSun(lightDir);

                Vector3 lightCol, lightSpec, nCol, nSpec, ambCol, sunDiff;
                ConvertTimeToDaylightColor(time, out lightCol, out lightSpec, out nCol, out nSpec, out ambCol, out sunDiff);
                lightDay.LightColor           = lightCol;
                lightDay.LightSpecularColor   = lightSpec;
                lightNight.LightColor         = nCol;
                lightNight.LightSpecularColor = nSpec;
                ambient.LightColor            = ambCol;
                sunMaterial.DiffuseColor      = sunDiff;

                //Debug.Log(lightCol.ToString());

                if (time >= SunriseMS - StateChangeMS && time <= SunsetMS + StateChangeMS)
                {
                    if (!switched)
                    {
                        lightTemp = ResourceManager.Instance.CurrentScene.DirectionalLights[0];
                        ResourceManager.Instance.CurrentScene.DirectionalLights[0] = ResourceManager.Instance.CurrentScene.DirectionalLights[1];
                        ResourceManager.Instance.CurrentScene.DirectionalLights[1] = lightTemp;
                        lightDay.Enabled   = true;
                        lightNight.Enabled = false;
                        switched           = true;
                    }
                }
                else
                {
                    if (switched)
                    {
                        lightTemp = ResourceManager.Instance.CurrentScene.DirectionalLights[0];
                        ResourceManager.Instance.CurrentScene.DirectionalLights[0] = ResourceManager.Instance.CurrentScene.DirectionalLights[1];
                        ResourceManager.Instance.CurrentScene.DirectionalLights[1] = lightTemp;
                        lightDay.Enabled   = false;
                        lightNight.Enabled = true;
                        switched           = false;
                    }
                }
                //}
            }
        }