/// <summary>
        /// Sets per frame settings for this effect (this sets camera, lights and other per frame settings).
        /// </summary>
        /// <param name="camera">camera</param>
        /// <param name="lights">list of lights</param>
        /// <param name="renderingContext">RenderingContext</param>
        protected override void OnApplyPerFrameSettings(ICamera camera, IList <ILight> lights, RenderingContext renderingContext)
        {
            _renderingContext = renderingContext;

            // Do the calculation here to optimize calculating worldViewProjection in ApplyMaterial
            _frameViewProjection = camera.GetViewProjection();

            // _viewportWidthFactor and _viewportHeightFactor are used to convert LineThickness into normalized viewport units (1.0 = width; 0.5 = half width; etc)
            float viewportWidthFactor  = renderingContext.DXScene.DpiScaleX / renderingContext.CurrentViewport.Width;
            float viewportHeightFactor = renderingContext.DXScene.DpiScaleY / renderingContext.CurrentViewport.Height;

            float newHalfPixelSizeXPerViewport = PointSize * viewportWidthFactor;
            float newHalfPixelSizeYPerViewport = PointSize * viewportHeightFactor;

            if (_geometryShaderConstantBufferData.HalfPixelSizeXPerViewport != newHalfPixelSizeXPerViewport ||
                _geometryShaderConstantBufferData.HalfPixelSizeYPerViewport != newHalfPixelSizeYPerViewport)
            {
                _geometryShaderConstantBufferData.HalfPixelSizeXPerViewport = newHalfPixelSizeXPerViewport;
                _geometryShaderConstantBufferData.HalfPixelSizeYPerViewport = newHalfPixelSizeYPerViewport;

                _renderingContext.DeviceContext.UpdateSubresource(ref _geometryShaderConstantBufferData, _geometryShaderConstantBuffer);
            }


            _vertexShaderConstantBufferData.EyePos = camera.GetCameraPosition();


            // Update per frame Lights constant buffer
            _vertexShaderConstantBufferData.AmbientColor = CollectAmbientLight(lights);

            var firstDirectionalLight = lights.OfType <IDirectionalLight>().FirstOrDefault();

            if (firstDirectionalLight == null)
            {
                // No directional light
                _vertexShaderConstantBufferData.LightColor     = Color3.Black;
                _vertexShaderConstantBufferData.LightDirection = Vector3.Zero;
            }
            else
            {
                _vertexShaderConstantBufferData.LightColor = firstDirectionalLight.DiffuseColor;

                _vertexShaderConstantBufferData.LightDirection = firstDirectionalLight.Direction;
                _vertexShaderConstantBufferData.LightDirection.Normalize();
            }
        }
示例#2
0
        /// <summary>
        /// Sets per frame settings for this effect (this sets camera, lights and other per frame settings).
        /// </summary>
        /// <param name="camera">camera</param>
        /// <param name="lights">list of lights</param>
        /// <param name="renderingContext">RenderingContext</param>
        protected override void OnApplyPerFrameSettings(ICamera camera, IList <ILight> lights, RenderingContext renderingContext)
        {
            _renderingContext     = renderingContext;
            _contextStatesManager = renderingContext.ContextStatesManager;


            // Update per frame Camera constant buffer
            _perFrameCameraConstantsBufferData.ViewProjection = camera.GetViewProjection();
            _perFrameCameraConstantsBufferData.EyePosW        = camera.GetCameraPosition();

            renderingContext.DeviceContext.UpdateSubresource(ref _perFrameCameraConstantsBufferData, _perFrameCameraConstantsBuffer);


            // Update per frame Lights constant buffer
            _perFrameLightsConstantsBufferData.AmbientColor = SetupAmbientLight(lights);

            var firstDirectionalLight = lights.OfType <IDirectionalLight>().FirstOrDefault();

            if (firstDirectionalLight == null)
            {
                // No directional light
                _perFrameLightsConstantsBufferData.LightColor     = Color3.Black;
                _perFrameLightsConstantsBufferData.LightDirection = Vector3.Zero;
            }
            else
            {
                _perFrameLightsConstantsBufferData.LightColor = firstDirectionalLight.DiffuseColor;

                _perFrameLightsConstantsBufferData.LightDirection = firstDirectionalLight.Direction;
                _perFrameLightsConstantsBufferData.LightDirection.Normalize();
            }

            renderingContext.DeviceContext.UpdateSubresource(ref _perFrameLightsConstantsBufferData, _perFrameLightsConstantsBuffer);


            _perObjectConstantsBufferData.World = Matrix.Zero; // Set World so the next time the object's worldMatrix will be compared to this value it will not be threted as equal

            _isLastWorldMatrixIdentity = false;
            _isConstantBufferDirty     = true;
        }
        /// <summary>
        /// Sets per frame settings for this effect (this sets camera, lights and other per frame settings).
        /// </summary>
        /// <param name="camera">camera</param>
        /// <param name="lights">list of lights</param>
        /// <param name="renderingContext">RenderingContext</param>
        protected override void OnApplyPerFrameSettings(ICamera camera, IList <ILight> lights, RenderingContext renderingContext)
        {
            _renderingContext     = renderingContext;
            _contextStatesManager = renderingContext.ContextStatesManager;


            // Update per frame Camera constant buffer
            _perFrameCameraConstantsBufferData.ViewProjection = camera.GetViewProjection();
            _perFrameCameraConstantsBufferData.EyePosW        = camera.GetCameraPosition();

            renderingContext.DeviceContext.UpdateSubresource(ref _perFrameCameraConstantsBufferData, _perFrameCameraConstantsBuffer);


            // Update per frame Lights constant buffer
            _perFrameLightsConstantsBufferData.AmbientColor = SetupAmbientLight(lights);

            renderingContext.DeviceContext.UpdateSubresource(ref _perFrameLightsConstantsBufferData, _perFrameLightsConstantsBuffer);


            _perObjectConstantsBufferData.World = Matrix.Zero; // Set World so the next time the object's worldMatrix will be compared to this value it will not be threted as equal

            _isLastWorldMatrixIdentity = false;
            _isConstantBufferDirty     = true;
        }
		//also used by Camera2D
		internal static void ProjectFromCoordinate(ICamera camera, bool is2D, ref Vector2 screenPosition, float projectDepth, out Vector3 position, ref Vector2 targetSize)
		{
			Vector4 coordinate = new Vector4(0, 0, 0.5f, 1);
			if (targetSize.X != 0)
				coordinate.X = ((screenPosition.X / targetSize.X) - 0.5f) * 2;
			if (targetSize.Y != 0)
				coordinate.Y = ((screenPosition.Y / targetSize.Y) - 0.5f) * 2;

			//this is much slower for 3D Cameras than 2D, due to matrix inversion requirements.
			Matrix mat;

			if (is2D)
			{
				//projection is always identity, so only need the inverse of the view matrix...
				//which is the camera matrix
				camera.GetCameraMatrix(out mat);
			}
			else
			{
				//more complex.
				Matrix pm;
				camera.GetProjectionMatrix(out pm, ref targetSize);
				camera.GetViewMatrix(out mat);

				// OUCH
				Matrix.Multiply(ref mat, ref pm, out mat); 
				Matrix.Invert(ref mat, out mat); 
			}

			Vector4.Transform(ref coordinate, ref mat, out coordinate);

			if (coordinate.W != 0)
			{
				coordinate.W = 1.0f / coordinate.W;
				coordinate.X *= coordinate.W;
				coordinate.Y *= coordinate.W;
				coordinate.Z *= coordinate.W;
				coordinate.W = 1;
			}

			//this could probably be done better...
			Vector3 cameraPos;
			camera.GetCameraPosition(out cameraPos);

			Vector3 difference = new Vector3();
			difference.X = coordinate.X - cameraPos.X;
			difference.Y = coordinate.Y - cameraPos.Y;
			difference.Z = coordinate.Z - cameraPos.Z;

			if (difference.X != 0 || difference.Y != 0 || difference.Y != 0)
				difference.Normalize();

			difference.X *= projectDepth;
			difference.Y *= projectDepth;
			difference.Z *= projectDepth;

			position = new Vector3();
			position.X = difference.X + cameraPos.X;
			position.Y = difference.Y + cameraPos.Y;
			position.Z = difference.Z + cameraPos.Z;
		}
示例#5
0
        //also used by Camera2D
        internal static void ProjectFromCoordinate(ICamera camera, bool is2D, ref Vector2 screenPosition, float projectDepth, out Vector3 position, ref Vector2 targetSize)
        {
            Vector4 coordinate = new Vector4(0, 0, 0.5f, 1);

            if (targetSize.X != 0)
            {
                coordinate.X = ((screenPosition.X / targetSize.X) - 0.5f) * 2;
            }
            if (targetSize.Y != 0)
            {
                coordinate.Y = ((screenPosition.Y / targetSize.Y) - 0.5f) * 2;
            }

            //this is much slower for 3D Cameras than 2D, due to matrix inversion requirements.
            Matrix mat;

            if (is2D)
            {
                //projection is always identity, so only need the inverse of the view matrix...
                //which is the camera matrix
                camera.GetCameraMatrix(out mat);
            }
            else
            {
                //more complex.
                Matrix pm;
                camera.GetProjectionMatrix(out pm, ref targetSize);
                camera.GetViewMatrix(out mat);

                // OUCH
                Matrix.Multiply(ref mat, ref pm, out mat);
                Matrix.Invert(ref mat, out mat);
            }

            Vector4.Transform(ref coordinate, ref mat, out coordinate);

            if (coordinate.W != 0)
            {
                coordinate.W  = 1.0f / coordinate.W;
                coordinate.X *= coordinate.W;
                coordinate.Y *= coordinate.W;
                coordinate.Z *= coordinate.W;
                coordinate.W  = 1;
            }

            //this could probably be done better...
            Vector3 cameraPos;

            camera.GetCameraPosition(out cameraPos);

            Vector3 difference = new Vector3();

            difference.X = coordinate.X - cameraPos.X;
            difference.Y = coordinate.Y - cameraPos.Y;
            difference.Z = coordinate.Z - cameraPos.Z;

            if (difference.X != 0 || difference.Y != 0 || difference.Y != 0)
            {
                difference.Normalize();
            }

            difference.X *= projectDepth;
            difference.Y *= projectDepth;
            difference.Z *= projectDepth;

            position   = new Vector3();
            position.X = difference.X + cameraPos.X;
            position.Y = difference.Y + cameraPos.Y;
            position.Z = difference.Z + cameraPos.Z;
        }
示例#6
0
        internal static void ProjectFromCoordinate(ICamera camera, bool is2D, ref Vector2 screenPosition, float projectDepth, out Vector3 position, ref Vector2 targetSize)
        {
            Vector4 coordinate = new Vector4(0, 0, 0.5f, 1);
            if (targetSize.X != 0)
                coordinate.X = ((screenPosition.X / targetSize.X) - 0.5f) * 2;
            if (targetSize.Y != 0)
                coordinate.Y = ((screenPosition.Y / targetSize.Y) - 0.5f) * 2;

            Matrix mat;

            if (is2D)
            {
                camera.GetCameraMatrix(out mat);
            }
            else
            {
                Matrix pm;
                camera.GetProjectionMatrix(out pm, ref targetSize);
                camera.GetViewMatrix(out mat);

                Matrix.Multiply(ref mat, ref pm, out mat);
                Matrix.Invert(ref mat, out mat);
            }

            Vector4.Transform(ref coordinate, ref mat, out coordinate);

            if (coordinate.W != 0)
            {
                coordinate.W = 1.0f / coordinate.W;
                coordinate.X *= coordinate.W;
                coordinate.Y *= coordinate.W;
                coordinate.Z *= coordinate.W;
                coordinate.W = 1;
            }

            Vector3 cameraPos;
            camera.GetCameraPosition(out cameraPos);

            Vector3 difference = new Vector3();
            difference.X = coordinate.X - cameraPos.X;
            difference.Y = coordinate.Y - cameraPos.Y;
            difference.Z = coordinate.Z - cameraPos.Z;

            if (difference.X != 0 || difference.Y != 0 || difference.Y != 0)
                difference.Normalize();

            difference.X *= projectDepth;
            difference.Y *= projectDepth;
            difference.Z *= projectDepth;

            position = new Vector3();
            position.X = difference.X + cameraPos.X;
            position.Y = difference.Y + cameraPos.Y;
            position.Z = difference.Z + cameraPos.Z;
        }
示例#7
0
        internal static void ProjectFromCoordinate(ICamera camera, bool is2D, ref Vector2 screenPosition, float projectDepth, out Vector3 position, ref Vector2 targetSize)
        {
            Vector4 coordinate = new Vector4(0, 0, 0.5f, 1);

            if (targetSize.X != 0)
            {
                coordinate.X = ((screenPosition.X / targetSize.X) - 0.5f) * 2;
            }
            if (targetSize.Y != 0)
            {
                coordinate.Y = ((screenPosition.Y / targetSize.Y) - 0.5f) * 2;
            }

            Matrix mat;

            if (is2D)
            {
                camera.GetCameraMatrix(out mat);
            }
            else
            {
                Matrix pm;
                camera.GetProjectionMatrix(out pm, ref targetSize);
                camera.GetViewMatrix(out mat);

                Matrix.Multiply(ref mat, ref pm, out mat);
                Matrix.Invert(ref mat, out mat);
            }

            Vector4.Transform(ref coordinate, ref mat, out coordinate);

            if (coordinate.W != 0)
            {
                coordinate.W  = 1.0f / coordinate.W;
                coordinate.X *= coordinate.W;
                coordinate.Y *= coordinate.W;
                coordinate.Z *= coordinate.W;
                coordinate.W  = 1;
            }

            Vector3 cameraPos;

            camera.GetCameraPosition(out cameraPos);

            Vector3 difference = new Vector3();

            difference.X = coordinate.X - cameraPos.X;
            difference.Y = coordinate.Y - cameraPos.Y;
            difference.Z = coordinate.Z - cameraPos.Z;

            if (difference.X != 0 || difference.Y != 0 || difference.Y != 0)
            {
                difference.Normalize();
            }

            difference.X *= projectDepth;
            difference.Y *= projectDepth;
            difference.Z *= projectDepth;

            position   = new Vector3();
            position.X = difference.X + cameraPos.X;
            position.Y = difference.Y + cameraPos.Y;
            position.Z = difference.Z + cameraPos.Z;
        }