Giter Site home page Giter Site logo

Scaling mesh about pb_csg HOT 3 OPEN

karl- avatar karl- commented on July 22, 2024
Scaling mesh

from pb_csg.

Comments (3)

tuseroni avatar tuseroni commented on July 22, 2024 7

i had this same problem, what's happening, as near as i can tell, is that the mesh is taking the scale set by the scale of the gameobject, so if you had a cube of 1x1x1 and you scaled it to 10x10x10 the newly created mesh would be 10x10x10 when you set the mesh of your gameobject this 10x10x10 cube gets scaled up by 10x10x10 and be 100x100x100.

in my case i just scaled it back down before adding it to the mesh:

Mesh scaleMesh(Mesh mesh, Vector3 scale)
{
    var baseVertices = mesh.vertices;
    var vertices = new Vector3[baseVertices.Length];

    for (var i = 0; i < vertices.Length; i++)
    {
        var vertex = baseVertices[i];
        vertex.x = vertex.x * scale.x;
        vertex.y = vertex.y * scale.y;
        vertex.z = vertex.z * scale.z;

        vertices[i] = vertex;
    }

    mesh.vertices = vertices;
    mesh.RecalculateNormals();
    mesh.RecalculateBounds();
    return mesh;
}
Mesh centreMesh(Mesh mesh, Vector3 centre)
{
    var baseVertices = mesh.vertices;
    var vertices = new Vector3[baseVertices.Length];

    for (var i = 0; i < vertices.Length; i++)
    {
        var vertex = baseVertices[i];
        vertex.x = vertex.x + centre.x;
        vertex.y = vertex.y + centre.y;
        vertex.z = vertex.z + centre.z;

        vertices[i] = vertex;
    }

    mesh.vertices = vertices;
    mesh.RecalculateNormals();
    mesh.RecalculateBounds();
    return mesh;
}
Vector3 getScale(Vector3 orig, Vector3 newVec)
{
    return new Vector3(orig.x / newVec.x, orig.y / newVec.y, orig.z / newVec.z);
}

those are the functions i made to do this, then i do this:

            CSG_Model result = Boolean.Subtract(me, cutter);
            var factor=getScale(MFer.mesh.bounds.extents * 2.0f, result.mesh.bounds.extents * 2.0f);
            var newMesh=scaleMesh(result.mesh, factor);
            Vector3 CentreFactor = MFer.mesh.bounds.center - newMesh.bounds.center;
            newMesh = centreMesh(newMesh, CentreFactor);
            MFer.mesh = newMesh;

where MFer is the meshfilter for the object "me" in the subtract function.

from pb_csg.

strangeways-dev avatar strangeways-dev commented on July 22, 2024

i had this same problem, what's happening, as near as i can tell, is that the mesh is taking the scale set by the scale of the gameobject, so if you had a cube of 1x1x1 and you scaled it to 10x10x10 the newly created mesh would be 10x10x10 when you set the mesh of your gameobject this 10x10x10 cube gets scaled up by 10x10x10 and be 100x100x100.

in my case i just scaled it back down before adding it to the mesh:

Mesh scaleMesh(Mesh mesh, Vector3 scale)
{
    var baseVertices = mesh.vertices;
    var vertices = new Vector3[baseVertices.Length];

    for (var i = 0; i < vertices.Length; i++)
    {
        var vertex = baseVertices[i];
        vertex.x = vertex.x * scale.x;
        vertex.y = vertex.y * scale.y;
        vertex.z = vertex.z * scale.z;

        vertices[i] = vertex;
    }

    mesh.vertices = vertices;
    mesh.RecalculateNormals();
    mesh.RecalculateBounds();
    return mesh;
}
Mesh centreMesh(Mesh mesh, Vector3 centre)
{
    var baseVertices = mesh.vertices;
    var vertices = new Vector3[baseVertices.Length];

    for (var i = 0; i < vertices.Length; i++)
    {
        var vertex = baseVertices[i];
        vertex.x = vertex.x + centre.x;
        vertex.y = vertex.y + centre.y;
        vertex.z = vertex.z + centre.z;

        vertices[i] = vertex;
    }

    mesh.vertices = vertices;
    mesh.RecalculateNormals();
    mesh.RecalculateBounds();
    return mesh;
}
Vector3 getScale(Vector3 orig, Vector3 newVec)
{
    return new Vector3(orig.x / newVec.x, orig.y / newVec.y, orig.z / newVec.z);
}

those are the functions i made to do this, then i do this:

            CSG_Model result = Boolean.Subtract(me, cutter);
            var factor=getScale(MFer.mesh.bounds.extents * 2.0f, result.mesh.bounds.extents * 2.0f);
            var newMesh=scaleMesh(result.mesh, factor);
            Vector3 CentreFactor = MFer.mesh.bounds.center - newMesh.bounds.center;
            newMesh = centreMesh(newMesh, CentreFactor);
            MFer.mesh = newMesh;

where MFer is the meshfilter for the object "me" in the subtract function.

Did you ever figure out how to make the object keep it's proportions when rotating?

from pb_csg.

strangeways-dev avatar strangeways-dev commented on July 22, 2024

I solved my issue #28

from pb_csg.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.