[SOLVED] Terrain generator error

Hello, the terrain generator works well when the game start, when the game change the terrain (so it generate a new terrain removing the old one) it give an error (it worked fine with old scripts.


To replicate the error press M key and click on the town upper the shield Vigo
https://playcanvas.com/editor/scene/898756
Code:

Terrain.prototype.createTerrainFromHeightMap= function (img,subd) {
            var canvas = document.createElement("canvas");
            var context = canvas.getContext("2d");
            var bufferWidth = img.width;
            var bufferHeight = img.height;
            canvas.width = bufferWidth;
            canvas.height = bufferHeight;

            context.drawImage(img, 0, 0);  (line that give error)

            var buffer = context.getImageData(0, 0, bufferWidth, bufferHeight).data;
            var vertexData = this.createTerrainVertexData({
                width: this.width,
                height: this.depth,
                subdivisions: subd,
                minHeight: this.minHeight,
                maxHeight: this.maxHeight,
                buffer: buffer,
                bufferWidth: bufferWidth,
                bufferHeight: bufferHeight
            });

            var node = new pc.GraphNode();
            var material = this.material[this.lM].resource;

            var mesh = pc.createMesh(this.app.graphicsDevice, vertexData.positions, {
                normals: vertexData.normals,
                uvs: vertexData.uvs,
                indices: vertexData.indices
            });

            var meshInstance = new pc.MeshInstance(node, mesh, material);

            var model = new pc.Model();
            model.graph = node;
            model.meshInstances.push(meshInstance);
            
            return model;
        };

Hi @ayrin,

The message indicates that the img argument you are passing the 2nd time isn’t correct.

Can you debug that?

I’ve tried running/debugging your project but it’s huge, not easy. If the problem persists try creating a simpler project that replicates this issue and I will help you out.

Yes i know it’s huge, anyway found the bug it was missing getSource() in this line. Thanks for the assistance.

var img =this.heightMap[this.lM].resource.getSource();;

1 Like