Exemplo n.º 1
0
        public EntityHeadController(IAnimationManager animator, EntityAgent entity, Shape entityShape)
        {
            this.entity      = entity;
            this.animManager = animator;

            HeadElement = entityShape.GetElementByName("head");
            NeckElement = entityShape.GetElementByName("neck");

            HeadGlobalMatrix         = Mat4f.Create();
            HeadGlobalMatrixInverted = Mat4f.Create();
            HeadLocalMatrix          = Mat4f.Create();

            // Head
            List <ShapeElement> elems = HeadElement.GetParentPath();

            for (int i = 0; i < elems.Count; i++)
            {
                ShapeElement elem           = elems[i];
                float[]      localTransform = elem.GetLocalTransformMatrix();
                Mat4f.Mul(HeadGlobalMatrix, HeadGlobalMatrix, localTransform);
            }

            Mat4f.Mul(HeadGlobalMatrix, HeadGlobalMatrix, HeadElement.GetLocalTransformMatrix());
            Mat4f.Invert(HeadGlobalMatrixInverted, HeadGlobalMatrix);
        }
Exemplo n.º 2
0
        protected void GenerateFrame(int indexNumber, AnimationFrame[] resKeyFrames, ShapeElement[] elements, Dictionary <int, AnimationJoint> jointsById, float[] modelMatrix, List <ElementPose> transforms, bool recursive = true)
        {
            int frameNumber = resKeyFrames[indexNumber].FrameNumber;

            for (int i = 0; i < elements.Length; i++)
            {
                ShapeElement element = elements[i];

                ElementPose animTransform = new ElementPose();
                animTransform.ForElement = element;

                GenerateFrameForElement(frameNumber, element, ref animTransform);
                transforms.Add(animTransform);

                float[] animModelMatrix = Mat4f.CloneIt(modelMatrix);
                Mat4f.Mul(animModelMatrix, animModelMatrix, element.GetLocalTransformMatrix(null, animTransform));

                if (element.JointId > 0 && !jointsDone.Contains(element.JointId))
                {
                    resKeyFrames[indexNumber].SetTransform(element.JointId, animModelMatrix);
                    jointsDone.Add(element.JointId);
                }

                if (recursive && element.Children != null)
                {
                    GenerateFrame(indexNumber, resKeyFrames, element.Children, jointsById, animModelMatrix, animTransform.ChildElementPoses);
                }
            }
        }
Exemplo n.º 3
0
 public AnimationFrame()
 {
     for (int i = 0; i < animTransforms.Length; i++)
     {
         animTransforms[i] = Mat4f.Create();
     }
 }
        protected virtual void UpdateLightAndTransformMatrix(float[] values, int index, Vec3f distToCamera, Vec4f lightRgba, float rotX, float rotY, float rotZ)
        {
            Mat4f.Identity(tmpMat);

            Mat4f.Translate(tmpMat, tmpMat, distToCamera.X, distToCamera.Y, distToCamera.Z);

            Mat4f.Translate(tmpMat, tmpMat, 0.5f, 0.5f, 0.5f);

            quat[0] = 0;
            quat[1] = 0;
            quat[2] = 0;
            quat[3] = 1;
            Quaterniond.RotateX(quat, quat, rotX);
            Quaterniond.RotateY(quat, quat, rotY);
            Quaterniond.RotateZ(quat, quat, rotZ);

            for (int i = 0; i < quat.Length; i++)
            {
                qf[i] = (float)quat[i];
            }
            Mat4f.Mul(tmpMat, tmpMat, Mat4f.FromQuat(rotMat, qf));

            Mat4f.Translate(tmpMat, tmpMat, -0.5f, -0.5f, -0.5f);

            values[index * 20]     = lightRgba.R;
            values[index * 20 + 1] = lightRgba.G;
            values[index * 20 + 2] = lightRgba.B;
            values[index * 20 + 3] = lightRgba.A;

            for (int i = 0; i < 16; i++)
            {
                values[index * 20 + i + 4] = tmpMat[i];
            }
        }
Exemplo n.º 5
0
        public void loadModelMatrix(Entity entity)
        {
            IEntityPlayer entityPlayer = capi.World.Player.Entity;

            Mat4f.Identity(ModelMat);
            Mat4f.Translate(ModelMat, ModelMat, (float)(entity.Pos.X - entityPlayer.CameraPos.X), (float)(entity.Pos.Y - entityPlayer.CameraPos.Y), (float)(entity.Pos.Z - entityPlayer.CameraPos.Z));

            float rotX = entity.Properties.Client.Shape != null ? entity.Properties.Client.Shape.rotateX : 0;
            float rotY = entity.Properties.Client.Shape != null ? entity.Properties.Client.Shape.rotateY : 0;
            float rotZ = entity.Properties.Client.Shape != null ? entity.Properties.Client.Shape.rotateZ : 0;

            Mat4f.Translate(ModelMat, ModelMat, 0, entity.CollisionBox.Y2 / 2, 0);

            double[] quat = Quaterniond.Create();
            Quaterniond.RotateX(quat, quat, entity.Pos.Pitch + rotX * GameMath.DEG2RAD);
            Quaterniond.RotateY(quat, quat, entity.Pos.Yaw + (rotY + 90) * GameMath.DEG2RAD);
            Quaterniond.RotateZ(quat, quat, entity.Pos.Roll + rotZ * GameMath.DEG2RAD);

            float[] qf = new float[quat.Length];
            for (int i = 0; i < quat.Length; i++)
            {
                qf[i] = (float)quat[i];
            }
            Mat4f.Mul(ModelMat, ModelMat, Mat4f.FromQuat(Mat4f.Create(), qf));

            float scale = entity.Properties.Client.Size;

            Mat4f.Scale(ModelMat, ModelMat, new float[] { scale, scale, scale });
            Mat4f.Translate(ModelMat, ModelMat, -0.5f, -entity.CollisionBox.Y2 / 2, -0.5f);
        }
Exemplo n.º 6
0
        protected override void LoadPosesAndAttachmentPoints(ShapeElement[] elements, List <ElementPose> intoPoses)
        {
            // Only load root pose and only the ones that have attachment points
            if (loadFully)
            {
                base.LoadPosesAndAttachmentPoints(elements, intoPoses);
                return;
            }

            ElementPose pose;

            for (int i = 0; i < elements.Length; i++)
            {
                ShapeElement elem = elements[i];
                if (elem.AttachmentPoints == null)
                {
                    continue;
                }

                intoPoses.Add(pose   = new ElementPose());
                pose.AnimModelMatrix = Mat4f.Create();
                pose.ForElement      = elem;

                for (int j = 0; j < elem.AttachmentPoints.Length; j++)
                {
                    AttachmentPoint apoint = elem.AttachmentPoints[j];
                    AttachmentPointByCode[apoint.Code] = new AttachmentPointAndPose()
                    {
                        AttachPoint = apoint,
                        CachedPose  = pose
                    };
                }
            }
        }
Exemplo n.º 7
0
        public override void ApplyToTpPlayer(EntityPlayer entityPlr, float[] modelMatrix, float?playerIntensity = null)
        {
            float inten = playerIntensity == null ? Intensity : (float)playerIntensity;

            Mat4f.RotateX(modelMatrix, modelMatrix, GameMath.Sin(accum) / 6f * inten);
            Mat4f.RotateZ(modelMatrix, modelMatrix, GameMath.Sin(accum * 1.2f) / 6f * inten);
        }
Exemplo n.º 8
0
        public AnimatorBase(WalkSpeedSupplierDelegate WalkSpeedSupplier, Animation[] Animations, Action <string> onAnimationStoppedListener = null)
        {
            this.WalkSpeedSupplier          = WalkSpeedSupplier;
            this.onAnimationStoppedListener = onAnimationStoppedListener;

            anims = new RunningAnimation[Animations == null ? 0 : Animations.Length];

            for (int i = 0; i < anims.Length; i++)
            {
                Animations[i].Code = Animations[i].Code.ToLower();

                anims[i] = new RunningAnimation()
                {
                    Active       = false,
                    Running      = false,
                    Animation    = Animations[i],
                    CurrentFrame = 0
                };
            }

            float[] identMat = Mat4f.Create();
            for (int i = 0; i < TransformationMatricesDefaultPose.Length; i++)
            {
                TransformationMatricesDefaultPose[i] = identMat[i % 16];
            }
        }
Exemplo n.º 9
0
        protected virtual void LoadPosesAndAttachmentPoints(ShapeElement[] elements, List <ElementPose> intoPoses)
        {
            ElementPose pose;

            for (int i = 0; i < elements.Length; i++)
            {
                ShapeElement elem = elements[i];

                intoPoses.Add(pose   = new ElementPose());
                pose.AnimModelMatrix = Mat4f.Create();
                pose.ForElement      = elem;

                if (elem.AttachmentPoints != null)
                {
                    for (int j = 0; j < elem.AttachmentPoints.Length; j++)
                    {
                        AttachmentPoint apoint = elem.AttachmentPoints[j];
                        AttachmentPointByCode[apoint.Code] = new AttachmentPointAndPose()
                        {
                            AttachPoint = apoint,
                            CachedPose  = pose
                        };
                    }
                }

                if (elem.Children != null)
                {
                    pose.ChildElementPoses = new List <ElementPose>(elem.Children.Length);
                    LoadPosesAndAttachmentPoints(elem.Children, pose.ChildElementPoses);
                }
            }
        }
Exemplo n.º 10
0
 public Matrixf RotateDeg(Vec3f degrees)
 {
     Mat4f.RotateX(Values, Values, degrees.X * GameMath.DEG2RAD);
     Mat4f.RotateY(Values, Values, degrees.Y * GameMath.DEG2RAD);
     Mat4f.RotateZ(Values, Values, degrees.Z * GameMath.DEG2RAD);
     return(this);
 }
Exemplo n.º 11
0
 public Matrixf Rotate(Vec3f radians)
 {
     Mat4f.RotateX(Values, Values, radians.X);
     Mat4f.RotateY(Values, Values, radians.Y);
     Mat4f.RotateZ(Values, Values, radians.Z);
     return(this);
 }
Exemplo n.º 12
0
        public Vec4d TransformVector(Vec4d vec)
        {
            Vec4d outval = new Vec4d();

            Mat4f.MulWithVec4(Values, vec, outval);
            return(outval);
        }
Exemplo n.º 13
0
        private void LoadModelMatrix(ItemRenderInfo renderInfo, bool isShadowPass)
        {
            IRenderAPI    rapi         = capi.Render;
            IEntityPlayer entityPlayer = capi.World.Player.Entity;

            Mat4f.Identity(ModelMat);
            Mat4f.Translate(ModelMat, ModelMat,
                            (float)(entityitem.Pos.X - entityPlayer.CameraPos.X),
                            (float)(entityitem.Pos.Y - entityPlayer.CameraPos.Y),
                            (float)(entityitem.Pos.Z - entityPlayer.CameraPos.Z)
                            );

            float sizeX = 0.2f * renderInfo.Transform.ScaleXYZ.X;
            float sizeY = 0.2f * renderInfo.Transform.ScaleXYZ.Y;
            float sizeZ = 0.2f * renderInfo.Transform.ScaleXYZ.Z;



            long ellapseMs = capi.World.ElapsedMilliseconds;

            if (entity.Collided || entity.Swimming || capi.IsGamePaused)
            {
                touchGroundMS = ellapseMs;
            }

            float easeIn  = Math.Min(1, (ellapseMs - touchGroundMS) / 200);
            float angleMs = Math.Max(ellapseMs - touchGroundMS, 0) * easeIn;


            float yangle = angleMs / 7f;
            float xangle = angleMs / 7f;
            float zangle = angleMs / 7f;

            float dx = 0, dz = 0;

            if (entity.Swimming)
            {
                float diff = 1;
                if (entityitem.Itemstack.Collectible.MaterialDensity > 1000)
                {
                    dx   = GameMath.Sin((float)(ellapseMs / 1000.0)) / 50;
                    dz   = -GameMath.Sin((float)(ellapseMs / 3000.0)) / 50;
                    diff = 0.1f;
                }

                xangle = GameMath.Sin((float)(ellapseMs / 1000.0)) * 8 * diff;
                yangle = GameMath.Cos((float)(ellapseMs / 2000.0)) * 3 * diff;
                zangle = -GameMath.Sin((float)(ellapseMs / 3000.0)) * 8 * diff;
            }



            Mat4f.Translate(ModelMat, ModelMat, dx + renderInfo.Transform.Translation.X, renderInfo.Transform.Translation.Y, dz + renderInfo.Transform.Translation.Z);
            Mat4f.Scale(ModelMat, ModelMat, new float[] { sizeX + scaleRand, sizeY + scaleRand, sizeZ + scaleRand });
            Mat4f.RotateY(ModelMat, ModelMat, GameMath.DEG2RAD * (renderInfo.Transform.Rotation.Y + yangle) + (renderInfo.Transform.Rotate ? yRotRand : 0));
            Mat4f.RotateZ(ModelMat, ModelMat, GameMath.DEG2RAD * (renderInfo.Transform.Rotation.Z + zangle));
            Mat4f.RotateX(ModelMat, ModelMat, GameMath.DEG2RAD * (renderInfo.Transform.Rotation.X + xangle));
            Mat4f.Translate(ModelMat, ModelMat, -renderInfo.Transform.Origin.X, -renderInfo.Transform.Origin.Y, -renderInfo.Transform.Origin.Z);
        }
Exemplo n.º 14
0
        private float[] GetFirstPersonHandsMatrix(EntityAgent entity, float[] viewMat, float deltaTime)
        {
            var modelMat = Mat4f.Invert(Mat4f.Create(), viewMat);

            // If the hands haven't been rendered in the last 10 render ticks, reset wobble and such.
            if (_renderTick - _lastTickHandsRendered > 10)
            {
                _moveWobble    = 0;
                _lastYaw       = entity.Pos.Yaw;;
                _yawDifference = 0;
            }
            _lastTickHandsRendered = _renderTick;


            if (entity.Controls.TriesToMove)
            {
                var moveSpeed = entity.Controls.MovespeedMultiplier * (float)entity.GetWalkSpeedMultiplier();
                _moveWobble += moveSpeed * deltaTime * 5.0F;
            }
            else
            {
                var target = (float)(Math.Round(_moveWobble / Math.PI) * Math.PI);
                var speed  = deltaTime * (0.2F + Math.Abs(target - _moveWobble) * 4);
                if (Math.Abs(target - _moveWobble) < speed)
                {
                    _moveWobble = target;
                }
                else
                {
                    _moveWobble += Math.Sign(target - _moveWobble) * speed;
                }
            }
            _moveWobble = _moveWobble % (GameMath.PI * 2);

            var moveWobbleOffsetX = GameMath.Sin((_moveWobble + GameMath.PI)) * 0.03F;
            var moveWobbleOffsetY = GameMath.Sin(_moveWobble * 2) * 0.02F;


            _yawDifference += GameMath.AngleRadDistance(_lastYaw, entity.Pos.Yaw);
            _yawDifference *= (1 - 0.075F);
            _lastYaw        = entity.Pos.Yaw;

            var yawRotation   = -_yawDifference / 2;
            var pitchRotation = (entity.Pos.Pitch - GameMath.PI) / 4;


            Mat4f.RotateY(modelMat, modelMat, yawRotation);
            Mat4f.Translate(modelMat, modelMat, 0.0F, -0.35F, -0.20F);
            Mat4f.RotateY(modelMat, modelMat, -yawRotation);
            Mat4f.RotateX(modelMat, modelMat, pitchRotation / 2);
            Mat4f.Translate(modelMat, modelMat, 0.0F, 0.0F, -0.20F);
            Mat4f.RotateX(modelMat, modelMat, pitchRotation);
            Mat4f.RotateY(modelMat, modelMat, yawRotation);

            Mat4f.Translate(modelMat, modelMat, moveWobbleOffsetX, moveWobbleOffsetY, 0.0F);
            Mat4f.RotateY(modelMat, modelMat, 90.0F * GameMath.DEG2RAD);

            return(modelMat);
        }
        public override void DoRender3DOpaqueBatched(float dt, bool isShadowPass)
        {
            if (isSpectator || (meshRefOpaque == null && meshRefOit == null))
            {
                return;
            }

            if (isShadowPass)
            {
                Mat4f.Mul(tmpMvMat, capi.Render.CurrentModelviewMatrix, ModelMat);
                capi.Render.CurrentActiveShader.UniformMatrix("modelViewMatrix", tmpMvMat);
            }
            else
            {
                Vec4f lightrgbs = capi.World.BlockAccessor.GetLightRGBs((int)(entity.Pos.X + entity.CollisionBox.X1 - entity.OriginCollisionBox.X1), (int)entity.Pos.Y, (int)(entity.Pos.Z + entity.CollisionBox.Z1 - entity.OriginCollisionBox.Z1));

                capi.Render.CurrentActiveShader.Uniform("rgbaLightIn", lightrgbs);
                capi.Render.CurrentActiveShader.Uniform("extraGlow", entity.Properties.Client.GlowLevel);
                capi.Render.CurrentActiveShader.UniformMatrix("modelMatrix", ModelMat);
                capi.Render.CurrentActiveShader.UniformMatrix("viewMatrix", capi.Render.CurrentModelviewMatrix);
                capi.Render.CurrentActiveShader.Uniform("addRenderFlags", AddRenderFlags);
                capi.Render.CurrentActiveShader.Uniform("windWaveIntensity", (float)WindWaveIntensity);
                capi.Render.CurrentActiveShader.Uniform("skipRenderJointId", skipRenderJointId);
                capi.Render.CurrentActiveShader.Uniform("skipRenderJointId2", skipRenderJointId2);
                capi.Render.CurrentActiveShader.Uniform("entityId", (int)entity.EntityId);
                capi.Render.CurrentActiveShader.Uniform("waterWaveCounter", capi.Render.ShaderUniforms.WaterWaveCounter);

                color[0] = (entity.RenderColor >> 16 & 0xff) / 255f;
                color[1] = ((entity.RenderColor >> 8) & 0xff) / 255f;
                color[2] = ((entity.RenderColor >> 0) & 0xff) / 255f;
                color[3] = ((entity.RenderColor >> 24) & 0xff) / 255f;

                capi.Render.CurrentActiveShader.Uniform("renderColor", color);

                double stab    = entity.WatchedAttributes.GetDouble("temporalStability", 1);
                double plrStab = capi.World.Player.Entity.WatchedAttributes.GetDouble("temporalStability", 1);
                double stabMin = Math.Min(stab, plrStab);

                float strength = (float)(glitchAffected ? Math.Max(0, 1 - 1 / 0.4f * stabMin) : 0);
                capi.Render.CurrentActiveShader.Uniform("glitchEffectStrength", strength);
            }


            capi.Render.CurrentActiveShader.UniformMatrices(
                "elementTransforms",
                GlobalConstants.MaxAnimatedElements,
                entity.AnimManager.Animator.Matrices
                );

            if (meshRefOpaque != null)
            {
                capi.Render.RenderMesh(meshRefOpaque);
            }

            if (meshRefOit != null)
            {
                capi.Render.RenderMesh(meshRefOit);
            }
        }
        void render(byte[] buf, List <Sphere> spheres, int pixelWidth)
        {
            float tm = _timer.gameTime() * 2.0f;

            int stride      = (pixelWidth * 32) / 8;
            int pixelHeight = buf.Length / stride;

            int width  = pixelWidth;
            int height = pixelHeight;

            float invWidth    = 1.0f / (float)width;
            float invHeight   = 1.0f / (float)height;
            float fov         = 80.0f;
            float aspectratio = width / (float)height;
            float angle       = (float)Math.Tan(M_PI * 0.5f * fov / 180.0f);

            List <Task> tasks = new List <Task>();

            for (int i = 0; i < lineTos.Length; i++)
            {
                Task task = new Task(obj => {
                    // Trace rays
                    lineToRender toRender = lineTos[(int)obj];
                    for (int y = toRender.from; y < toRender.to; ++y)
                    {
                        for (int x = 0; x < width; ++x)
                        {
                            float xx     = (2.0f * (((float)x + 0.5f) * invWidth) - 1.0f) * angle * aspectratio;
                            float yy     = (1.0f - 2.0f * (((float)y + 0.5f) * invHeight)) * angle;
                            Vec3f raydir = new Vec3f(xx, yy, -1.0f);
                            raydir.normalize();

                            Vec3f raydir2 = new Vec3f(Mat4f.RotationYMatrix(-tm) * raydir);
                            //Vec3f raydir2 = raydir;

                            //Vec3f rayorig = new Vec3f(0.0f, 1.0f + (float)Math.Sin(tm) * 4.0f, -20.0f) + new Vec3f(Mat4f.RotationYMatrix(-tm) * new Vec3f(0.0f, 1.0f, 20.0f));
                            Vec3f rayorig = new Vec3f(0.0f, 5.0f, -25.0f) + new Vec3f(Mat4f.RotationYMatrix(-tm) * new Vec3f(0.0f, 5.0f, 25.0f));
                            //Vec3f rayorig = spheres[1].center + new Vec3f(Mat4f.RotationYMatrix(-tm) * new Vec3f(0.0f, 3.0f, 5.0f + tm));
                            //Vec3f rayorig = new Vec3f(0.0f);
                            //Vec3f rayorig = new Vec3f(0.0f, 10.0f, 0.0f);
                            Vec3f pixel = trace(rayorig, raydir2, spheres, 0);

                            printPixel(buf, x, y, new pBGRA((byte)(pixel.z * 255.0f), (byte)(pixel.y * 255.0f), (byte)(pixel.x * 255.0f), 255), pixelWidth);
                        }
                    }
                }, i);
                task.Start();
                tasks.Add(task);
            }

            foreach (Task t in tasks)
            {
                t.Wait();
            }
        }
Exemplo n.º 17
0
        /// <summary>
        /// The event fired when the game ticks.
        /// </summary>
        /// <param name="dt"></param>
        public virtual void OnFrame(float dt)
        {
            if (entity.HeadYaw != 0 || entity.HeadPitch != 0)
            {
                Mat4f.Identity(HeadLocalMatrix);
                Mat4f.RotateY(HeadLocalMatrix, HeadLocalMatrix, entity.HeadYaw);
                Mat4f.RotateZ(HeadLocalMatrix, HeadLocalMatrix, entity.HeadPitch);

                ApplyTransformToElement(HeadLocalMatrix, HeadGlobalMatrix, HeadGlobalMatrixInverted, HeadElement);
            }
        }
Exemplo n.º 18
0
        void updateLocalEyePosImmersiveFpMode()
        {
            AttachmentPointAndPose apap = AnimManager.Animator.GetAttachmentPointPose("Eyes");
            AttachmentPoint        ap   = apap.AttachPoint;

            float[] ModelMat    = Mat4f.Create();
            Matrixf tmpModelMat = new Matrixf();

            float bodyYaw   = BodyYaw;
            float rotX      = Properties.Client.Shape != null ? Properties.Client.Shape.rotateX : 0;
            float rotY      = Properties.Client.Shape != null ? Properties.Client.Shape.rotateY : 0;
            float rotZ      = Properties.Client.Shape != null ? Properties.Client.Shape.rotateZ : 0;
            float bodyPitch = WalkPitch;

            float lookOffset = (SidedPos.Pitch - GameMath.PI) / 9f;

            bool wasHoldPos = holdPosition;

            holdPosition = false;

            for (int i = 0; i < AnimManager.Animator.RunningAnimations.Length; i++)
            {
                RunningAnimation anim = AnimManager.Animator.RunningAnimations[i];
                if (anim.Running && anim.EasingFactor > anim.meta.HoldEyePosAfterEasein)
                {
                    if (!wasHoldPos)
                    {
                        prevAnimModelMatrix = (float[])apap.AnimModelMatrix.Clone();
                    }
                    holdPosition = true;
                    break;
                }
            }


            tmpModelMat
            .Set(ModelMat)
            .RotateX(SidedPos.Roll + rotX * GameMath.DEG2RAD)
            .RotateY(bodyYaw + (180 + rotY) * GameMath.DEG2RAD)
            .RotateZ(bodyPitch + rotZ * GameMath.DEG2RAD)
            .Mul(holdPosition ? prevAnimModelMatrix : apap.AnimModelMatrix)
            .Scale(Properties.Client.Size, Properties.Client.Size, Properties.Client.Size)
            .Translate(-0.5f, 0, -0.5f)
            .Translate(ap.PosX / 16f - lookOffset, ap.PosY / 16f - lookOffset / 1.3f, ap.PosZ / 16f)
            ;

            float[] pos = new float[4] {
                0, 0, 0, 1
            };
            float[] endVec = Mat4f.MulWithVec4(tmpModelMat.Values, pos);

            LocalEyePos.Set(endVec[0], endVec[1], endVec[2]);
        }
        public void InitializeArrays()
        {
            // If initialising on fresh creation, both arrays will be null  (otherwise fruitPoints was already initialised in FromTreeAttributes)

            if (fruitPoints == null)
            {
                // This code path is called on block placement (i.e. when previous stage crop grows to final stage)
                // This initialises each fruit point with a different germination time
                // (if called after FromTreeAttributes, fruitPoints will already have been populated, and GetGerminationDate() will be called in the next CheckForGrowth() call instead)

                fruitPoints = new FruitData[positionsCount];
                int randomSelector = Math.Abs(this.Blockentity.Pos.GetHashCode()) % fruitCodeBases.Length;

                for (int i = 0; i < positionsCount; i++)
                {
                    int fruitVariant = i;
                    if (i >= fruitCodeBases.Length) fruitVariant = randomSelector++ % fruitCodeBases.Length;
                    fruitPoints[i] = new FruitData(fruitVariant, GetGerminationDate(), this, null);
                }
            }

            positions = new Vec3d[positionsCount];

            Vec3f temp = new Vec3f();
            float[] matrix = null;
            if (Blockentity.Block.RandomizeRotations)
            {
                // For performance, only call the hash function once per blockEntity loaded
                int randomSelector = GameMath.MurmurHash3(-Blockentity.Pos.X, Blockentity.Block.RandomizeAxes == EnumRandomizeAxes.XYZ ? Blockentity.Pos.Y : 0, Blockentity.Pos.Z);
                matrix = randomRotMatrices[GameMath.Mod(randomSelector, randomRotations.Length)];
            }

            for (int i = 0; i < positionsCount; i++)
            {
                if (Api.Side == EnumAppSide.Client)
                {
                    positions[i] = new Vec3d(points[i * 3], points[i * 3 + 1], points[i * 3 + 2]);
                    if (matrix != null)
                    {
                        Mat4f.MulWithVec3_Position(matrix, (float)positions[i].X, (float)positions[i].Y, (float)positions[i].Z, temp);
                        positions[i].X = temp.X;
                        positions[i].Y = temp.Y;
                        positions[i].Z = temp.Z;
                    }
                }
                else
                {
                    positions[i] = new Vec3d((i + 1) / positionsCount, (i + 1) / positionsCount, (i + 1) / positionsCount);  //dummy vector for serverside - exact positions don't matter because they won't be rendered
                }

                positions[i].Add(Blockentity.Pos);
            }
        }
Exemplo n.º 20
0
        public float[] GetLocalTransformMatrix(float[] output = null, ElementPose tf = null)
        {
            if (tf == null)
            {
                tf = noTransform;
            }

            ShapeElement elem = this;

            if (output == null)
            {
                output = Mat4f.Create();
            }

            float[] origin = new float[] { 0f, 0f, 0f };

            if (elem.RotationOrigin != null)
            {
                origin[0] = (float)elem.RotationOrigin[0] / 16;
                origin[1] = (float)elem.RotationOrigin[1] / 16;
                origin[2] = (float)elem.RotationOrigin[2] / 16;
            }

            Mat4f.Translate(output, output, origin);

            if (elem.RotationX + tf.degX != 0)
            {
                Mat4f.RotateX(output, output, (float)(elem.RotationX + tf.degX) * GameMath.DEG2RAD);
            }
            if (elem.RotationY + tf.degY != 0)
            {
                Mat4f.RotateY(output, output, (float)(elem.RotationY + tf.degY) * GameMath.DEG2RAD);
            }
            if (elem.RotationZ + tf.degZ != 0)
            {
                Mat4f.RotateZ(output, output, (float)(elem.RotationZ + tf.degZ) * GameMath.DEG2RAD);
            }

            Mat4f.Scale(output, output, new float[] { (float)elem.ScaleX * tf.scaleX, (float)elem.ScaleY * tf.scaleY, (float)elem.ScaleZ * tf.scaleZ });

            Mat4f.Translate(output, output, new float[] { -origin[0], -origin[1], -origin[2] });

            Mat4f.Translate(output, output, new float[] {
                (float)elem.From[0] / 16 + tf.translateX,
                (float)elem.From[1] / 16 + tf.translateY,
                (float)elem.From[2] / 16 + tf.translateZ
            });



            return(output);
        }
        static BEBehaviorFruiting()
        {
            randomRotMatrices = new float[randomRotations.Length][];
            for (int i = 0; i < randomRotations.Length; i++)
            {
                float[] matrix = Mat4f.Create();
                Mat4f.Translate(matrix, matrix, 0.5f, 0.5f, 0.5f);
                Mat4f.RotateY(matrix, matrix, randomRotations[i] * GameMath.DEG2RAD);
                Mat4f.Translate(matrix, matrix, -0.5f, -0.5f, -0.5f);

                randomRotMatrices[i] = matrix;
            }
        }
Exemplo n.º 22
0
        public void OnRenderFrame(float deltaTime, EnumRenderStage stage)
        {
            // before rendering: update our values
            Mat4f.Invert(InvProjectionMatrix, _mod.CApi.Render.CurrentProjectionMatrix);
            Mat4f.Invert(InvModelViewMatrix, _mod.CApi.Render.CameraMatrixOriginf);

            _tempVec4f.Set(0, 0, 0, 1);
            Mat4f.MulWithVec4(InvModelViewMatrix, _tempVec4f, CameraWorldPosition);

            DayLight = 1.25f * GameMath.Max(
                _mod.CApi.World.Calendar.DayLightStrength -
                _mod.CApi.World.Calendar.MoonLightStrength / 2f, 0.05f);
        }
Exemplo n.º 23
0
        protected float[] UpdateLightAndTransformMatrix(float[] values, int index, Vec3f distToCamera, Vec4f lightRgba, float rotX, float rotY, float rotZ, Vec3f axis, float[] xtraTransform)
        {
            if (xtraTransform == null)
            {
                Mat4f.Identity(tmpMat);
                Mat4f.Translate(tmpMat, tmpMat, distToCamera.X + axis.X, distToCamera.Y + axis.Y, distToCamera.Z + axis.Z);
            }
            else
            {
                Mat4f.Translate(tmpMat, tmpMat, axis.X, axis.Y, axis.Z);
            }

            quat[0] = 0;
            quat[1] = 0;
            quat[2] = 0;
            quat[3] = 1;
            if (rotX != 0f)
            {
                Quaterniond.RotateX(quat, quat, rotX);
            }
            if (rotY != 0f)
            {
                Quaterniond.RotateY(quat, quat, rotY);
            }
            if (rotZ != 0f)
            {
                Quaterniond.RotateZ(quat, quat, rotZ);
            }

            for (int i = 0; i < quat.Length; i++)
            {
                qf[i] = (float)quat[i];
            }
            Mat4f.Mul(tmpMat, tmpMat, Mat4f.FromQuat(rotMat, qf));

            Mat4f.Translate(tmpMat, tmpMat, -axis.X, -axis.Y, -axis.Z);
            //if (xtraTransform != null) Mat4f.Mul(tmpMat, tmpMat, xtraTransform);

            int j = index * 20;

            values[j]   = lightRgba.R;
            values[++j] = lightRgba.G;
            values[++j] = lightRgba.B;
            values[++j] = lightRgba.A;

            for (int i = 0; i < 16; i++)
            {
                values[++j] = tmpMat[i];
            }
            return(tmpMat);
        }
Exemplo n.º 24
0
        public unsafe static void Write(this BinaryWriter writer, Mat4f mat)
        {
            float *ptr = &mat.c0.x;

            for (uint i = 0; i < 16; ++i)
            {
                writer.Write(ptr[i]);
            }

            //writer.Write(mat.c0);
            //writer.Write(mat.c1);
            //writer.Write(mat.c2);
            //writer.Write(mat.c3);
        }
Exemplo n.º 25
0
        protected virtual void UpdateLightAndTransformMatrix(float[] values, int index, Vec3f distToCamera, Vec4f lightRgba, float rotX, float rotY, float rotZ)
        {
            // This commented-out code could allow individual fruits to change size as they grow, in future: But ... Saraty asked not to implement because the pixel density will change

            //float scale = 1f;
            //Mat4f.Identity_Scaled(tmpMat, scale);
            //Mat4f.Translate(tmpMat, tmpMat, distToCamera.X / scale, distToCamera.Y / scale, distToCamera.Z / scale);

            Mat4f.Identity(tmpMat);
            Mat4f.Translate(tmpMat, tmpMat, distToCamera.X, distToCamera.Y, distToCamera.Z);

            quat[0] = 0;
            quat[1] = 0;
            quat[2] = 0;
            quat[3] = 1;
            if (rotX != 0f)
            {
                Quaterniond.RotateX(quat, quat, rotX);
            }
            if (rotY != 0f)
            {
                Quaterniond.RotateY(quat, quat, rotY);
            }
            if (rotZ != 0f)
            {
                Quaterniond.RotateZ(quat, quat, rotZ);
            }

            for (int i = 0; i < quat.Length; i++)
            {
                qf[i] = (float)quat[i];
            }
            Mat4f.Mul(tmpMat, tmpMat, Mat4f.FromQuat(rotMat, qf));

            int j = index * 20;

            values[j]   = lightRgba.R;
            values[++j] = lightRgba.G;
            values[++j] = lightRgba.B;
            values[++j] = lightRgba.A;

            for (int i = 0; i < 16; i++)
            {
                values[++j] = tmpMat[i];
            }
        }
Exemplo n.º 26
0
        /// <summary> Returns a model view matrix for rendering a carried block on the specified attachment point. </summary>
        private float[] GetAttachmentPointMatrix(EntityShapeRenderer renderer, AttachmentPointAndPose attachPointAndPose)
        {
            var modelMat     = Mat4f.CloneIt(renderer.ModelMat);
            var animModelMat = attachPointAndPose.AnimModelMatrix;

            Mat4f.Mul(modelMat, modelMat, animModelMat);

            // Apply attachment point transform.
            var attach = attachPointAndPose.AttachPoint;

            Mat4f.Translate(modelMat, modelMat, (float)(attach.PosX / 16), (float)(attach.PosY / 16), (float)(attach.PosZ / 16));
            Mat4f.RotateX(modelMat, modelMat, (float)attach.RotationX * GameMath.DEG2RAD);
            Mat4f.RotateY(modelMat, modelMat, (float)attach.RotationY * GameMath.DEG2RAD);
            Mat4f.RotateZ(modelMat, modelMat, (float)attach.RotationZ * GameMath.DEG2RAD);

            return(modelMat);
        }
Exemplo n.º 27
0
        public void loadModelMatrix(Entity entity, float dt, bool isShadowPass)
        {
            prevYAccum += dt;
            if (prevYAccum > 1f / 5f)
            {
                prevYAccum = 0;
                prevY      = entity.Pos.Y;
            }

            EntityPlayer entityPlayer = capi.World.Player.Entity;

            Mat4f.Identity(ModelMat);

            if (entity is IMountableSupplier ims && ims.IsMountedBy(entityPlayer))
            {
                var mountoffset = ims.GetMountOffset(entityPlayer);
                Mat4f.Translate(ModelMat, ModelMat, -mountoffset.X, -mountoffset.Y, -mountoffset.Z);
            }
Exemplo n.º 28
0
        protected void loadModelMatrixForGui(Entity entity, double posX, double posY, double posZ, double yawDelta, float size)
        {
            Mat4f.Identity(ModelMat);
            Mat4f.Translate(ModelMat, ModelMat, (float)posX, (float)posY, (float)posZ);

            Mat4f.Translate(ModelMat, ModelMat, size, 2 * size, 0);

            float rotX = entity.Properties.Client.Shape != null ? entity.Properties.Client.Shape.rotateX : 0;
            float rotY = entity.Properties.Client.Shape != null ? entity.Properties.Client.Shape.rotateY : 0;
            float rotZ = entity.Properties.Client.Shape != null ? entity.Properties.Client.Shape.rotateZ : 0;

            Mat4f.RotateX(ModelMat, ModelMat, GameMath.PI + rotX * GameMath.DEG2RAD);
            Mat4f.RotateY(ModelMat, ModelMat, (float)yawDelta + rotY * GameMath.DEG2RAD);
            Mat4f.RotateZ(ModelMat, ModelMat, rotZ * GameMath.DEG2RAD);

            float scale = entity.Properties.Client.Size * size;
            Mat4f.Scale(ModelMat, ModelMat, new float[] { scale, scale, scale });
            Mat4f.Translate(ModelMat, ModelMat, -0.5f, 0f, -0.5f);
        }
Exemplo n.º 29
0
        /// <summary>
        /// Returns the full inverse model matrix (includes all parent transforms)
        /// </summary>
        /// <returns></returns>
        public float[] GetInverseModelMatrix()
        {
            List <ShapeElement> elems = GetParentPath();

            float[] modelTransform = Mat4f.Create();

            for (int i = 0; i < elems.Count; i++)
            {
                ShapeElement elem           = elems[i];
                float[]      localTransform = elem.GetLocalTransformMatrix();
                Mat4f.Mul(modelTransform, modelTransform, localTransform);
            }

            Mat4f.Mul(modelTransform, modelTransform, GetLocalTransformMatrix());

            float[] inverseTransformMatrix = Mat4f.Invert(Mat4f.Create(), modelTransform);

            return(inverseTransformMatrix);
        }
Exemplo n.º 30
0
        /// <summary>
        /// Compiles the animation into a bunch of matrices, 31 matrices per frame.
        /// </summary>
        /// <param name="rootElements"></param>
        /// <param name="jointsById"></param>
        /// <param name="recursive">When false, will only do root elements</param>
        public void GenerateAllFrames(ShapeElement[] rootElements, Dictionary <int, AnimationJoint> jointsById, bool recursive = true)
        {
            AnimationFrame[] resolvedKeyFrames = new AnimationFrame[KeyFrames.Length];

            for (int i = 0; i < resolvedKeyFrames.Length; i++)
            {
                resolvedKeyFrames[i] = new AnimationFrame()
                {
                    FrameNumber = KeyFrames[i].Frame
                };
            }

            if (KeyFrames.Length == 0)
            {
                return;
            }

            if (jointsById.Count > GlobalConstants.MaxAnimatedElements)
            {
                throw new Exception("Max joint cap of " + GlobalConstants.MaxAnimatedElements + " reached. Sorry, you'll have to simplify your model if you want it to be animated. (until some programmer finds another solution to pass on more joint data through shader uniforms)");
            }

            for (int i = 0; i < resolvedKeyFrames.Length; i++)
            {
                jointsDone.Clear();
                GenerateFrame(i, resolvedKeyFrames, rootElements, jointsById, Mat4f.Create(), resolvedKeyFrames[i].RootElementTransforms, recursive);
            }

            for (int i = 0; i < resolvedKeyFrames.Length; i++)
            {
                resolvedKeyFrames[i].FinalizeMatrices(jointsById);
            }

            PrevNextKeyFrameByFrame = new AnimationFrame[QuantityFrames][];
            for (int i = 0; i < QuantityFrames; i++)
            {
                AnimationFrame left, right;
                getLeftRightResolvedFrame(i, resolvedKeyFrames, out left, out right);

                PrevNextKeyFrameByFrame[i] = new AnimationFrame[] { left, right };
            }
        }