Jump to content

All my viewport cordinates are upside down


PowerKuz
 Share

Recommended Posts

Im new to pixi and im trying to create a chunk system in pixix js. But for some reason is all my cordinates upside down so x is -x and y is -y. I have no idea of whats happening so how can i fix this. 

 

<script src="https://pixijs.download/v6.1.1/pixi.min.js"></script>
<script src="https://github.com/davidfig/pixi-cull/releases/download/2.0.1/pixi-cull.min.js"></script>
<script src="https://github.com/davidfig/pixi-viewport/releases/download/4.31.0/viewport.min.js"></script>

const app = new PIXI.Application({autoResize: true, resolution: devicePixelRatio})
document.body.appendChild(app.view)

// create viewport
const viewport = new pixi_viewport.Viewport({
  screenWidth: window.innerWidth,
  screenHeight: window.innerHeight,
  worldWidth: 3000000,
  worldHeight: 3000000,
  interaction: app.renderer.plugins.interaction
})

app.stage.addChild(viewport)
viewport.drag().pinch().wheel().decelerate().moveCenter(0, 0)

const shader = PIXI.Shader.from(`
    precision mediump float;
    attribute vec2 aVPos;
    attribute vec2 aIPos;
    attribute vec3 aICol;

    uniform mat3 translationMatrix;
    uniform mat3 projectionMatrix;

    varying vec3 vCol;

    void main() {
        vCol = aICol;

        gl_Position = vec4((projectionMatrix * translationMatrix * vec3(aVPos + aIPos, 1.0)).xy, 0.0, 1.0);
    }`,

`precision mediump float;

    varying vec3 vCol;

    void main() {
        gl_FragColor = vec4(vCol, 1.0);
    }

`);

const chunk_size = 16
const block_size = 100
var chunks = {}

function hexToRgb(hex) {
    var result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
    return result ? {
      r: parseInt(result[1], 16),
      g: parseInt(result[2], 16),
      b: parseInt(result[3], 16)
    } : null;
}
function pos_string(x,y){
    return String(x) + ':' + String(y)
}

function append_chunk(mesh, x, y){
    chunks[pos_string(x,y)] = mesh
}

function add_chunk(x,y, blocks = new Array(chunk_size * chunk_size)){
    const geometry = new PIXI.Geometry()
        .addAttribute('aVPos', [block_size, 0, 0, 0, 0, block_size,
                           block_size, 0, block_size, block_size, 0, block_size]);

    geometry.instanced = true;
    geometry.instanceCount = chunk_size * chunk_size;

    const positionSize = 2;
    const colorSize = 3;
    const buffer = new PIXI.Buffer(new Float32Array(geometry.instanceCount * (positionSize + colorSize)));
    geometry.addAttribute('aIPos', buffer, positionSize, false, PIXI.TYPES.FLOAT, 4 * (positionSize + colorSize), 0, true);
    geometry.addAttribute('aICol', buffer, colorSize, false, PIXI.TYPES.FLOAT, 4 * (positionSize + colorSize), 4 * positionSize, true);

    
    const ex_chunk = chunks[pos_string(x,y)]
    if (ex_chunk){
        buffer.data = ex_chunk[1].data
        viewport.removeChild(ex_chunk[0])
    }

    for (const block of blocks) {
        const x = block.position.x + 1
        const y = block.position.y
        const instanceOffset = (x * y) * (positionSize + colorSize);
        const block_rgb = hexToRgb(block.color)

        buffer.data[instanceOffset + 0] = x * block_size;
        buffer.data[instanceOffset + 1] = y * block_size;

        buffer.data[instanceOffset + 2] = block_rgb ? block_rgb.r : 0;
        buffer.data[instanceOffset + 3] = block_rgb ? block_rgb.g : 0;
        buffer.data[instanceOffset + 4] = block_rgb ? block_rgb.b : 0;
    }
    const mesh = new PIXI.Mesh(geometry, shader);

    mesh.position.set(x * chunk_size * block_size, y * chunk_size * block_size);

    chunks[pos_string(x,y)] = [mesh, buffer]
    viewport.addChild(mesh)
}


// her is where i add and its inverted
add_chunk(-1,0, [{position: {x: 0, y: 0}, color: '#ff00ff'}])
add_chunk(0,0, [{position: {x: 5, y: 0}, color: '#0000ff'}])

setTimeout(t, 1000)

const cull = new Cull.Simple()
cull.addList(viewport.children)
cull.cull(viewport.getVisibleBounds())

PIXI.Ticker.shared.add(() => {
  if (viewport.dirty) {
    cull.cull(viewport.getVisibleBounds())
    viewport.dirty = false
  }
})
Link to comment
Share on other sites

    for (const block of blocks) {
        const x = block.position.x + 1
        const y = block.position.y
        const instanceOffset = (x * y) * (positionSize + colorSize);
        const block_rgb = hexToRgb(block.color)

        buffer.data[instanceOffset + 0] = x * block_size;
        buffer.data[instanceOffset + 1] = y * block_size;

        buffer.data[instanceOffset + 2] = block_rgb ? block_rgb.r : 0;
        buffer.data[instanceOffset + 3] = block_rgb ? block_rgb.g : 0;
        buffer.data[instanceOffset + 4] = block_rgb ? block_rgb.b : 0;
    }

If you analyze the system a bit you may notice an error with " x * y * (positionSize + colorSize) "

Example:

blocks =  [
   {
     position: {x: 0, y: 1}, 
     color: "#000"
   }, 
   {
     position: {x: 1, y: 0}, 
     color: "#000"
   }
 ] 

With the first block the instanceOffset will be equal to: 0 * 1 * (2 + 3) = 0

and with the second block the instanceOffeset will be equal to: 1 * 0 * (2 + 3) = 0

So when you add the value of the second block to the buffer.data you will be replacing the data of the first block.

I don't have idea what you are trying to do but by analyzing that formula you can realize that problem

Link to comment
Share on other sites

If you want to add it one after the other (Consecutively) you can use the following:

let sz = 0
for (const block of blocks) {
    const x = block.position.x + 1
    const y = block.position.y
    const block_rgb = hexToRgb(block.color)

    buffer.data[sz++] = x * block_size;
    buffer.data[sz++] = y * block_size;

    buffer.data[sz++] = block_rgb ? block_rgb.r : 0;
    buffer.data[sz++] = block_rgb ? block_rgb.g : 0;
    buffer.data[sz++] = block_rgb ? block_rgb.b : 0;
}

 

Link to comment
Share on other sites

True, so you should use (x + y * chunk_size) * (positionSize + colorSize);

for (const block of blocks) {
    const x = block.position.x + 1
    const y = block.position.y
    const instanceOffset = (x + y * chunk_size) * (positionSize + colorSize);
    const block_rgb = hexToRgb(block.color)

    buffer.data[instanceOffset + 0] = x * block_size;
    buffer.data[instanceOffset + 1] = y * block_size;

    buffer.data[instanceOffset + 2] = block_rgb ? block_rgb.r : 0;
    buffer.data[instanceOffset + 3] = block_rgb ? block_rgb.g : 0;
    buffer.data[instanceOffset + 4] = block_rgb ? block_rgb.b : 0;
}

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...