Exemplo n.º 1
0
        void CompositionTarget_Rendering(object sender, EventArgs e)
        {
#if DEBUG
            if (renderTimer.IsRunning)
            {
                renderTimer.Stop();
                Debug.WriteLine(string.Format("RENDER: {0} ellapsed for setting properties and rendering.", renderTimer.Elapsed));
                renderTimer.Reset();
            }
#endif
            if (directionalLight == null)
            {
                return;
            }

            var cf    = new Vector3((float)camera.LookDirection.X, (float)camera.LookDirection.Y, (float)camera.LookDirection.Z).Normalized();
            var cu    = new Vector3((float)camera.UpDirection.X, (float)camera.UpDirection.Y, (float)camera.UpDirection.Z).Normalized();
            var right = Vector3.Cross(cf, cu);

            var qel = Quaternion.RotationAxis(right, (float)((-LightElevationDegrees * Math.PI) / 180));
            var qaz = Quaternion.RotationAxis(cu, (float)((LightAzimuthDegrees * Math.PI) / 180));
            var v   = Vector3.Transform(cf, qaz * qel);
            directionalLightDirection = v;

            if (!directionalLight.Direction.Equals(directionalLightDirection))
            {
                directionalLight.Direction = v;
            }

            UpdateNearClipPlaneForSceneBounds();
        }
Exemplo n.º 2
0
        private void ProcessRotationDrag()
        {
            if (m_activeAxis == EGizmoAxis.None)
            {
                return;
            }

            if (m_activeAxisList.Count <= 0)
            {
                return;
            }

            Matrix viewProj = m_frameViewInfo.ViewMatrix * m_frameViewInfo.ProjectionMatrix;

            SharpDX.Vector2 mouseDelta = new SharpDX.Vector2(Input.GetNativeAxisValue(EInputAxis.MouseX), -Input.GetNativeAxisValue(EInputAxis.MouseY));
            SharpDX.Vector2 screenAxis = (SharpDX.Vector2)Vector3.TransformNormal(m_rotationDragAxis, viewProj);
            screenAxis.Normalize();

            float deltaMove = -SharpDX.Vector2.Dot(mouseDelta, screenAxis) * RotationSpeed;

            m_totalAngleDelta += deltaMove;

            float snappedAngle = m_totalAngleDelta;

            if (AngleSnap > 0.0f)
            {
                snappedAngle = m_totalAngleDelta - m_totalAngleDelta % AngleSnap;
            }

            Quaternion deltaRotation = Quaternion.RotationAxis(m_activeAxisList[0], snappedAngle);

            m_controlledTransform.SetWorldRotation(deltaRotation * m_originalRotation);
        }
Exemplo n.º 3
0
        private MeshGeometry3D CreateTube(Vector pos, out Vector3 endPoint, bool slim = false)
        {
            var m = pos.Length;

            if (double.IsNaN(m))
            {
                endPoint = default;
                return(new MeshGeometry3D());
            }

            float tubeLength = 100;
            float tubeRadius = 6;

            if (slim)
            {
                tubeRadius *= 0.98f;
            }

            float cableToCenter = 8;
            float wheelRadius   = 20;
            float cableDist     = (float)(m * Math.PI / 180 * wheelRadius);
            float curvature     = cableDist / (tubeLength * cableToCenter);

            var     r         = Quaternion.RotationAxis(Vector3.BackwardRH, (float)Math.Atan2(pos.Y, pos.X));
            var     points    = new List <Vector3>();
            int     divisions = 100;
            Vector3 p;

            points.Add(p = Vector3.Zero);
            for (int i = 1; i <= divisions; i++)
            {
                float a = curvature * tubeLength * i / divisions;
                var   t = new Vector3((float)Math.Sin(a), 0, (float)Math.Cos(a));
                p += t * tubeLength / divisions;
                points.Add(Vector3.Transform(p, r));
            }

            var mb = new MeshBuilder();

            mb.AddTube(points, tubeRadius * 2, 36, false, true, true);

            endPoint = points.Last();
            return(mb.ToMesh());
        }
Exemplo n.º 4
0
        public void Init(CInitializer initializer)
        {
            UpdateScheduler = new CUpdateScheduler();

            CreateLevel();
            GameConsole.Init();
            PhysicsWorld.Init(this);


            #region Sponza
            CModelAsset sponzaAsset = CImportManager.Instance.MeshImporter.LoadModelAsync("TestResources/SponzaAtrium/sponza.obj");
            m_sponzaEntity = SpawnEntity <CEntity>();
            CModelComponent modelComponent = m_sponzaEntity.AddComponent <CModelComponent>(true, true);
            m_sponzaEntity.SetWorldPosition(new Vector3(0, -5, -5));
            m_sponzaEntity.SetWorldRotation(Quaternion.RotationAxis(Axis.Up, MathUtil.DegreesToRadians(90)));
            m_sponzaEntity.SetWorldScale(new Vector3(0.03f));
            CStaticModelColliderComponent staticModelCollider = m_sponzaEntity.AddComponent <CStaticModelColliderComponent>(true, true);
            staticModelCollider.ModelAsset  = sponzaAsset;
            modelComponent.Model            = sponzaAsset;
            m_sponzaEntity.IsPhysicsStatic  = true;
            m_sponzaEntity.IsPhysicsEnabled = true;
            #endregion

            CMeshAsset cubeAsset = CImportManager.Instance.MeshImporter.LoadMeshAsync("EngineResources/DefaultMeshes/DefaultCube.obj");

            CEntity floorEntity = SpawnEntity <CEntity>();
            floorEntity.AddComponent <CSceneComponent>(true, true);
            CMeshComponent floorMesh = floorEntity.AddComponent <CMeshComponent>(true, true);
            floorMesh.Mesh       = cubeAsset;
            floorMesh.LocalScale = new Vector3(500, 1, 500);
            CBoxColliderComponent floorCollider = floorEntity.AddComponent <CBoxColliderComponent>(true, true);
            floorCollider.Height = 1;
            floorCollider.Width  = 500;
            floorCollider.Length = 500;
            floorEntity.SetLocalPosition(new Vector3(0, -15, 0));
            floorEntity.IsPhysicsStatic         = true;
            floorEntity.IsPhysicsEnabled        = true;
            floorEntity.PhysicalStatic.Material = new Material(1, 0.8f, 1f);

            #region LightSetup
            m_lightEntity = SpawnEntity <CEntity>();
            m_lightEntity.AddComponent <CSceneComponent>(true, true);

            CDirectionalLightComponent directionalLight = m_lightEntity.AddComponent <CDirectionalLightComponent>(true, true);
            directionalLight.LocalRotation = MathUtilities.CreateLookAtQuaternion(Vector3.Normalize(new Vector3(0.2f, -0.5f, 0.2f)), Axis.Up);
            directionalLight.LightColor    = Color4.White * 0.8f;

            CSpotLightComponent spotLight = m_lightEntity.AddComponent <CSpotLightComponent>(true, true);
            spotLight.ConstantAttenuation  = 0.01f;
            spotLight.LinearAttenuation    = 0.3f;
            spotLight.QuadraticAttenuation = 0.0f;
            spotLight.Enabled    = true;
            spotLight.SpotAngle  = MathUtil.DegreesToRadians(30.0f);
            spotLight.LightColor = new Color4(0.1f, 0.8f, 0.1f, 1.0f);
            spotLight.Range      = 100.0f;
            Quaternion deltaRotation = Quaternion.RotationAxis(Axis.Right, MathUtil.DegreesToRadians(10));
            spotLight.LocalRotation = deltaRotation;
            spotLight.LocalPosition = new Vector3(0, 1, -4);

            CPointLightComponent pointLight1 = m_lightEntity.AddComponent <CPointLightComponent>(true, true);
            pointLight1.ConstantAttenuation = 1;
            pointLight1.LinearAttenuation   = 0.2f;
            pointLight1.Enabled             = true;
            pointLight1.Range         = 100.0f;
            pointLight1.LightColor    = new Color4(0.8f, 0.1f, 0.1f, 1.0f);
            pointLight1.LocalPosition = new Vector3(0, 0, 3.0f);

            CPointLightComponent pointLight2 = m_lightEntity.AddComponent <CPointLightComponent>(true, true);
            pointLight2.ConstantAttenuation = 1;
            pointLight2.LinearAttenuation   = 0.4f;
            pointLight2.Enabled             = true;
            pointLight2.Range         = 100.0f;
            pointLight2.LightColor    = new Color4(0.1f, 0.1f, 0.8f, 1.0f);
            pointLight2.LocalPosition = new Vector3(0, -3, -8.0f);

            CAmbientLightComponent ambientLight = m_lightEntity.AddComponent <CAmbientLightComponent>(true, true);
            ambientLight.LightColor = Color4.White * 0.15f;
            #endregion
        }