Ejemplo n.º 1
0
        /// <summary>
        /// Is called when an entity is created for this item (e.g. if dropped).
        /// This function is supposed to add renderjobs and physicscomponents.
        /// Don't forget to add components to the item entity!
        /// </summary>
        public virtual void SetupItemEntity(ItemEntity itemEntity, Entity entity)
        {
            // Create the physical representation of the item.
            RigidBody body = new RigidBody(
                50f,
                entity.Transform,
                new BoxShape(new vec3(1))
                );

            itemEntity.ContainingWorld.Physics.AddRigidBody(body);

            itemEntity.AddPhysicsComponent(new PhysicsComponent(body, mat4.Identity));

            // Create the graphical representation of the item.
            MeshRenderJob renderJob = new MeshRenderJob(
                Renderer.Opaque.Mesh,
                Resources.UseMaterial("Items/Dummy", UpvoidMiner.ModDomain),
                Resources.UseMesh("::Debug/Box", UpvoidMiner.ModDomain),
                mat4.Identity
                );

            itemEntity.AddRenderComponent(new RenderComponent(renderJob, mat4.Identity, true));

            MeshRenderJob renderJobShadow = new MeshRenderJob(
                Renderer.Shadow.Mesh,
                Resources.UseMaterial("::Shadow", UpvoidMiner.ModDomain),
                Resources.UseMesh("::Debug/Box", UpvoidMiner.ModDomain),
                mat4.Identity
                );

            itemEntity.AddRenderComponent(new RenderComponent(renderJobShadow, mat4.Identity, true));
        }
Ejemplo n.º 2
0
        public override void OnSelect(Player player)
        {
            // Use correct preview mesh
            MeshResource     shapeMesh = null;
            MaterialResource shapeMat  = null;

            switch (player.CurrentDiggingShape)
            {
            case Player.DiggingShape.Box:
                shapeMesh = Resources.UseMesh("::Debug/Box", null);
                shapeMat  = Resources.UseMaterial("Items/DigPreviewBox", UpvoidMiner.ModDomain);
                break;

            case Player.DiggingShape.Cylinder:
                shapeMesh = Resources.UseMesh("::Debug/Cylinder", null);
                shapeMat  = Resources.UseMaterial("Items/DigPreviewCylinder", UpvoidMiner.ModDomain);
                break;

            case Player.DiggingShape.Sphere:
                shapeMesh = Resources.UseMesh("::Debug/Sphere", null);
                shapeMat  = Resources.UseMaterial("Items/DigPreviewSphere", UpvoidMiner.ModDomain);
                break;

            default:
                throw new InvalidOperationException("Unknown digging shape");
            }

            // Create a transparent sphere as 'fill-indicator'.
            previewShape = new MeshRenderJob(Renderer.Transparent.Mesh, shapeMat, shapeMesh, mat4.Scale(0f));
            LocalScript.world.AddRenderJob(previewShape);
            // And a second one for indicating the center.
            previewShapeIndicator = new MeshRenderJob(Renderer.Transparent.Mesh, Resources.UseMaterial("Items/ResourcePreviewIndicator", UpvoidMiner.ModDomain), shapeMesh, mat4.Scale(0f));
            LocalScript.world.AddRenderJob(previewShapeIndicator);
        }
Ejemplo n.º 3
0
        public static Tree OldTree(Random random, mat4 transform1, mat4 transform2, World world)
        {
            bool type0 = random.NextDouble() > 0.5;

            MeshRenderJob leavesOpaque = new MeshRenderJob(
                Renderer.Opaque.Mesh,
                Resources.UseMaterial("TreeLeaves01", UpvoidMiner.ModDomain),
                Resources.UseMesh(type0 ? "Vegetation/Tree01/Leaves_high" : "Vegetation/Tree03/Leaves_high", UpvoidMiner.ModDomain),
                transform2);

            MeshRenderJob leavesZPre = new MeshRenderJob(
                Renderer.zPre.Mesh,
                Resources.UseMaterial("TreeLeaves01.zPre", UpvoidMiner.ModDomain),
                Resources.UseMesh(type0 ? "Vegetation/Tree01/Leaves_high" : "Vegetation/Tree03/Leaves_high", UpvoidMiner.ModDomain),
                transform2);

            MeshRenderJob leavesShadow = new MeshRenderJob(
                Renderer.Shadow.Mesh,
                Resources.UseMaterial("TreeLeaves01.Shadow", UpvoidMiner.ModDomain),
                Resources.UseMesh(type0 ? "Vegetation/Tree01/Leaves_high" : "Vegetation/Tree03/Leaves_high", UpvoidMiner.ModDomain),
                transform2);

            MeshRenderJob trunkOpaque = new MeshRenderJob(
                Renderer.Opaque.Mesh,
                Resources.UseMaterial("TreeTrunk", UpvoidMiner.ModDomain),
                Resources.UseMesh(type0 ? "Vegetation/Tree01/Trunk" : "Vegetation/Tree03/Trunk", UpvoidMiner.ModDomain),
                transform2);

            MeshRenderJob trunkShadow = new MeshRenderJob(
                Renderer.Shadow.Mesh,
                Resources.UseMaterial("::Shadow", UpvoidMiner.ModDomain),
                Resources.UseMesh(type0 ? "Vegetation/Tree01/Trunk" : "Vegetation/Tree03/Trunk", UpvoidMiner.ModDomain),
                transform2);


            // Add some color variance to trees
            vec4 colorModulation = new vec4(0.7f + (float)random.NextDouble() * 0.5f, 0.7f + (float)random.NextDouble() * 0.5f, 1, 1);

            leavesOpaque.SetColor("uColorModulation", colorModulation);

            // Amount of wood depends on tree type (thicker/thinner trunk) and tree height scale factor.
            Tree t = new Tree((type0 ? 0.5f : 1.0f) * transform2.col1.y, Tree.TreeType.Birch);

            Tree.Log  l = new Tree.Log();
            RigidBody b = new RigidBody(0f, transform1 * mat4.Translate(new vec3(0, 5, 0)), new CylinderShape(.5f, 10));

            world.Physics.AddRigidBody(b);
            l.PhysicsComps.Add(new PhysicsComponent(b, mat4.Translate(new vec3(0, -5, 0))));

            t.RjLeaves0.Add(new RenderComponent(leavesOpaque, transform2));
            t.RjLeaves0.Add(new RenderComponent(leavesZPre, transform2));
            t.RjLeaves0.Add(new RenderComponent(leavesShadow, transform2));
            t.RjTrunk.Add(new RenderComponent(trunkOpaque, transform2));
            t.RjTrunk.Add(new RenderComponent(trunkShadow, transform2));

            t.Logs.Add(l);

            return(t);
        }
Ejemplo n.º 4
0
 public override void OnDeselect(Player player)
 {
     // Remove and delete it on deselect.
     LocalScript.world.RemoveRenderJob(previewShape);
     LocalScript.world.RemoveRenderJob(previewShapeIndicator);
     previewShape          = null;
     previewShapeIndicator = null;
 }
Ejemplo n.º 5
0
 public override void OnDeselect(Player player)
 {
     // Remove and delete it on deselect.
     LocalScript.world.RemoveRenderJob(previewMaterial);
     LocalScript.world.RemoveRenderJob(previewMaterialPlaced);
     LocalScript.world.RemoveRenderJob(previewMaterialPlacedIndicator);
     previewMaterial                = null;
     previewMaterialPlaced          = null;
     previewMaterialPlacedIndicator = null;
 }
Ejemplo n.º 6
0
        public static Tree Cactus(Random random, mat4 transform1, mat4 transform2, World world)
        {
            // Compute random cactus type \in 0..5
            int type = (int)(random.NextDouble() * 6.0);

            // Circumvent the unlikely case of NextDouble() returning 1.0
            if (type > 5)
            {
                type = 5;
            }

            // 0..5 -> 1..6
            ++type;

            string meshString = "Vegetation/Cactus/Cactus" + type.ToString();

            MeshRenderJob cactus = new MeshRenderJob(
                Renderer.Opaque.Mesh,
                Resources.UseMaterial("Cactus", UpvoidMiner.ModDomain),
                Resources.UseMesh(meshString, UpvoidMiner.ModDomain),
                transform2);

            MeshRenderJob cactusShadow = new MeshRenderJob(
                Renderer.Shadow.Mesh,
                Resources.UseMaterial("Cactus.Shadow", UpvoidMiner.ModDomain),
                Resources.UseMesh(meshString, UpvoidMiner.ModDomain),
                transform2);

            // Add some color variance to cacti
            vec4 colorModulation = new vec4(0.7f + (float)random.NextDouble() * 0.6f, 0.9f + (float)random.NextDouble() * 0.2f, 1, 1);

            cactus.SetColor("uColorModulation", colorModulation);

            // Create new Tree of type Cactus with 0 wood to gather.
            Tree t = new Tree(0, Tree.TreeType.Cactus);

            Tree.Log  l = new Tree.Log();
            RigidBody b = new RigidBody(0f, transform1 * mat4.Translate(new vec3(0, 5, 0)), new CylinderShape(.5f, 10));

            world.Physics.AddRigidBody(b);
            l.PhysicsComps.Add(new PhysicsComponent(b, mat4.Translate(new vec3(0, -5, 0))));

            t.RjLeaves0.Add(new RenderComponent(cactus, transform2));
            t.RjLeaves0.Add(new RenderComponent(cactusShadow, transform2));

            t.Logs.Add(l);

            return(t);
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Configures a renderjob for a vertical constraint between two drones.
        /// </summary>
        private void configureVerticalConstraint(Drone first, Drone second, MeshRenderJob job1, MeshRenderJob job2)
        {
            vec3 startPos = first.CurrentPosition;
            vec3 endPos   = second.CurrentPosition;

            vec3 up  = new vec3(0, 14 * 2 * 100, 0);
            vec3 dir = endPos - startPos;

            // This transforms x from start to end and y from -up to up.
            mat4 transform = new mat4(
                dir,
                up,
                new vec3(0, 0, 1),
                new vec3()
                );

            job1.ModelMatrix = job2.ModelMatrix = mat4.Translate(startPos) * transform * mat4.Scale(.5f) * mat4.Translate(new vec3(1, 0, 0));
        }
Ejemplo n.º 8
0
        public override void OnSelect(Player player)
        {
            MeshResource mesh;

            switch (Shape)
            {
            case MaterialShape.Cube: mesh = Resources.UseMesh("::Debug/Box", null); break;

            case MaterialShape.Sphere: mesh = Resources.UseMesh("::Debug/Sphere", null); break;

            case MaterialShape.Cylinder: mesh = Resources.UseMesh("::Debug/Cylinder", null); break;

            default: throw new NotImplementedException("Invalid shape");
            }

            MaterialResource material;

            if (Material is SolidTerrainResource)
            {
                material = (Material as SolidTerrainResource).RenderMaterial;
            }
            else
            {
                throw new NotImplementedException("Unknown terrain resource");
            }

            // Create a solid object for 'holding'.
            previewMaterial = new MeshRenderJob(Renderer.Opaque.Mesh, material, mesh, mat4.Scale(0f));
            LocalScript.world.AddRenderJob(previewMaterial);

            // Create a transparent object as 'placement-indicator'.
            previewMaterialPlaced = new MeshRenderJob(Renderer.Transparent.Mesh, Resources.UseMaterial("Items/ResourcePreview", UpvoidMiner.ModDomain), mesh, mat4.Scale(0f));
            LocalScript.world.AddRenderJob(previewMaterialPlaced);
            // And a second one for indicating the center.
            previewMaterialPlacedIndicator = new MeshRenderJob(Renderer.Transparent.Mesh, Resources.UseMaterial("Items/ResourcePreviewIndicator", UpvoidMiner.ModDomain), Resources.UseMesh("::Debug/Sphere", null), mat4.Scale(0f));
            LocalScript.world.AddRenderJob(previewMaterialPlacedIndicator);
        }
Ejemplo n.º 9
0
        /// <summary>
        /// Updates this constraint, i.e. renderjobs.
        /// </summary>
        public void Update(float _elapsedSeconds)
        {
            if (drones.Count > 0)
            {
                Drone refDrone = ReferenceDrone;

                switch (refDrone.Type)
                {
                case DroneType.Chain:
                    for (int i = 0; i < drones.Count - 1; ++i)
                    {
                        Drone first  = drones[i];
                        Drone second = drones[i + 1];

                        // Swap every other drone.
                        if (i % 2 == 1)
                        {
                            Drone tmp = first;
                            first  = second;
                            second = tmp;
                        }

                        bool addJob = false;
                        if (boundaryIndicators.Count <= i)
                        {
                            boundaryIndicators.Add(new MeshRenderJob(Renderer.Transparent.Mesh,
                                                                     Resources.UseMaterial("Miner/DroneConstraintVertical", UpvoidMiner.ModDomain),
                                                                     Resources.UseMesh("::Debug/Quad", UpvoidMiner.ModDomain),
                                                                     mat4.Identity));
                            boundaryIndicatorsDistort.Add(new MeshRenderJob(Renderer.Distortion.Mesh,
                                                                            Resources.UseMaterial("Miner/DroneConstraintVerticalDistort", UpvoidMiner.ModDomain),
                                                                            Resources.UseMesh("::Debug/Quad", UpvoidMiner.ModDomain),
                                                                            mat4.Identity));

                            // Vertical drones cause a constraint by the intersection of the planes (i.e. the plane between two drones and the two shadow-planes).
                            constraintExpression.Add(new CsgExpression(1, "max((dot(plane1Normal, vec3(x, y, z)) - plane1Dis), max( (dot(plane2Normal, vec3(x, y, z)) - plane2Dis), (dot(plane3Normal, vec3(x, y, z)) - plane3Dis)) )",
                                                                       UpvoidMiner.ModDomain,
                                                                       "plane1Normal:vec3, plane1Dis:float, plane2Normal:vec3, plane2Dis:float, plane3Normal:vec3, plane3Dis:float"));
                            addJob = true;
                        }

                        MeshRenderJob job1 = boundaryIndicators[i];
                        MeshRenderJob job2 = boundaryIndicatorsDistort[i];

                        configureVerticalConstraint(first, second, job1, job2);

                        if (addJob)
                        {
                            LocalScript.world.AddRenderJob(job1);
                            LocalScript.world.AddRenderJob(job2);
                        }
                    }


                    break;

                default:
                    Debug.Fail("Not implemented/Invalid");
                    break;
                }
            }
            // Remove old ones.
            while (boundaryIndicators.Count > Math.Max(0, drones.Count - 1))
            {
                LocalScript.world.RemoveRenderJob(boundaryIndicators[boundaryIndicators.Count - 1]);
                LocalScript.world.RemoveRenderJob(boundaryIndicatorsDistort[boundaryIndicators.Count - 1]);
                boundaryIndicators.RemoveAt(boundaryIndicators.Count - 1);
                boundaryIndicatorsDistort.RemoveAt(boundaryIndicatorsDistort.Count - 1);
                constraintExpression.RemoveAt(constraintExpression.Count - 1);
            }
        }
Ejemplo n.º 10
0
        /// <summary>
        /// Is called when an entity is created for this item (e.g. if dropped).
        /// This function is supposed to add renderjobs and physicscomponents.
        /// Don't forget to add components to the item entity!
        /// </summary>
        public override void SetupItemEntity(ItemEntity itemEntity, Entity entity)
        {
            // Create an appropriate physics shape.
            CollisionShape collShape;
            MeshResource   mesh;
            mat4           scaling;

            switch (Shape)
            {
            case MaterialShape.Cube:
                collShape = new BoxShape(Size / 2f);
                scaling   = mat4.Scale(Size / 2f);
                mesh      = Resources.UseMesh("Box", UpvoidMiner.ModDomain);
                break;

            case MaterialShape.Sphere:
                collShape = new SphereShape(Size.x);
                scaling   = mat4.Scale(Size);
                mesh      = Resources.UseMesh("Sphere", UpvoidMiner.ModDomain);
                break;

            case MaterialShape.Cylinder:
                collShape = new CylinderShape(Size.x, Size.y);
                mesh      = Resources.UseMesh("Cylinder", UpvoidMiner.ModDomain);
                scaling   = mat4.Scale(new vec3(Size.x, Size.y / 2f, Size.z));
                break;

            default: throw new NotImplementedException("Invalid Shape");
            }

            // Create the physical representation of the item.
            RigidBody body = new RigidBody(
                50f,
                entity.Transform,
                collShape
                );

            itemEntity.ContainingWorld.Physics.AddRigidBody(body);

            itemEntity.AddPhysicsComponent(new PhysicsComponent(body, mat4.Identity));

            MaterialResource material;

            if (Material is SolidTerrainResource)
            {
                material = (Material as SolidTerrainResource).RenderMaterial;
            }
            else
            {
                throw new NotImplementedException("Unknown terrain resource");
            }

            // Create the graphical representation of the item.
            MeshRenderJob renderJob = new MeshRenderJob(
                Renderer.Opaque.Mesh,
                material,
                mesh,
                mat4.Identity
                );

            itemEntity.AddRenderComponent(new RenderComponent(renderJob, scaling, true));

            MeshRenderJob renderJobShadow = new MeshRenderJob(
                Renderer.Shadow.Mesh,
                Resources.UseMaterial("::Shadow", UpvoidMiner.ModDomain),
                mesh,
                mat4.Identity
                );

            itemEntity.AddRenderComponent(new RenderComponent(renderJobShadow, scaling, true));
        }