hjoykim / THREE

c# port of Three.js
MIT License
135 stars 24 forks source link

Why Renderer not center screen ? #19

Closed Pathompongmc closed 1 year ago

Pathompongmc commented 1 year ago

Why Renderer not center screen ?

hjoykim commented 1 year ago

hello, please, check if camera.View.Enabled is true or no in your camera setting of source if camera.View.Enabled is true, the rendering screen will be offset by camera.View.OffsetX, OffsetY when executing camera.UpdateProjectionMatrix(), this may occure screen initializing and resizing the default value of camera.View.Enabled is false like as below

public Camera() { this.IsCamera = true; this.type = "Camera";

        View = new View()
        {
            Enabled = false,
            FullWidth = 1,
            FullHeight = 1,
            OffsetX = 0,
            OffsetY = 0,
            Width = 1,
            Height = 1
        };
    }

OrthographicCamera.UpdateProjectionMatrix

public override void UpdateProjectionMatrix() { var dx = (this.CameraRight - this.Left) / (2 this.Zoom); var dy = (this.Top - this.Bottom) / (2 this.Zoom); var cx = (this.CameraRight + this.Left) / 2; var cy = (this.Top + this.Bottom) / 2;

        var left = cx - dx;
        var right = cx + dx;
        var top = cy + dy;
        var bottom = cy - dy;

        if (this.View.Enabled ) {

            //var zoomW = this.Zoom / ( this.View.Width / this.View.FullWidth );
            //var zoomH = this.Zoom / ( this.View.Height / this.View.FullHeight );

            var scaleW = ( this.CameraRight - this.Left ) / this.View.FullWidth / this.Zoom;
            var scaleH = ( this.Top - this.Bottom ) / this.View.FullHeight / this.Zoom;

            left += scaleW *  this.View.OffsetX ;
            right = left + scaleW * this.View.Width;
            top -= scaleH * this.View.OffsetY;
            bottom = top - scaleH *this.View.Height;

        }

        this.ProjectionMatrix = Matrix4.Identity().MakeOrthographic(left,right,top,bottom, this.Near, this.Far);

        this.ProjectionMatrixInverse.GetInverse(this.ProjectionMatrix);
    }

PerspectiveCamera.UpdateProjectionMatrix public override void UpdateProjectionMatrix() { //base.UpdateProjectionMatrix();

        float near = this.Near,
        top = near * (float)Math.Tan(MathUtils.DEG2RAD * 0.5 * this.Fov) / this.Zoom,

        height = 2 * top,
        width = this.Aspect * height,
        left = -0.5f * width;

        if (this.View.Enabled)

        {              
            left += (float)View.OffsetX * width / (float)View.FullWidth;
            top -= (float)View.OffsetY * height / (float)View.FullHeight;
            width *= (float)View.Width / (float)View.FullWidth;
            height *= (float)View.Height / (float)View.FullHeight;
        }

        var skew = this.FilmOffset;
        if (skew != 0) left += near * skew / this.GetFilmWidth();

        this.ProjectionMatrix = this.ProjectionMatrix.MakePerspective(left, left + width, top, top - height, near, this.Far);

        this.ProjectionMatrixInverse.GetInverse(this.ProjectionMatrix);

    }