fenomas / noa

Experimental voxel game engine.
MIT License
616 stars 91 forks source link

Riddle for everyone using noa engine #64

Closed levlups closed 5 years ago

levlups commented 5 years ago

I added this code to add custom mesh form png , from the index.html file , it works but the colors are not replicated exactly and the colors are super dull anybody see a mistake in my code

var mesharrays=[];
var colors;

function supermesh(){

var objectContext = window.document.createElement('canvas').getContext('2d');
var Scale=1;

    var box = BABYLON.Mesh.CreateBox("box", 1, scene);
    scene.removeMesh(box);
            var mat = new BABYLON.StandardMaterial("mat", scene,false, true, BABYLON.Texture.NEAREST_SAMPLINGMODE);

                objectContext.drawImage(jQuery('#idEntityAtlas').get(0), 48, 0,16,16,0,0,16,16);

        {
            var bits=16; // 16 pixels
            for (var intFor1 = 0; intFor1 < bits; intFor1 += 1) {
                for (var intFor2 = 0; intFor2 < bits; intFor2 += 1) {

                    var intColor = objectContext.getImageData(intFor1, intFor2,1, 1).data

                    if (intColor[3] === 0) {
                        continue;
                    }
                      var mesh=box.clone('box1');
                      box.geometry.copy(BABYLON.Geometry.RandomId()).applyToMesh(mesh);

                    var x=  mesh.position.x += Scale * (8- intFor1);
                        var y=       mesh.position.y += Scale * (8- intFor2);
                        var z=       mesh.position.z += 0.0;

                                       colorVertices(mesh, new BABYLON.Color3(intColor[0], intColor[1],intColor[2] )) 

                                 mesharrays.push(mesh);
                      }

            }
        }

function colorVertices(mesh, color) {

    var positions = mesh.getVerticesData(BABYLON.VertexBuffer.PositionKind);

    colors = [];

 for (var i = 0; i<positions.length/3; i ++) {

     colors.push(color.r, color.g, color.b, 1);

    }

    mesh.setVerticesData(BABYLON.VertexBuffer.ColorKind, colors,false, false);

}

var supermesh = BABYLON.Mesh.MergeMeshes(mesharrays,true);

supermesh.material = new BABYLON.StandardMaterial('mat', scene);

return supermesh;

}

fenomas commented 5 years ago

Hi, this is more a Babylon.js question, so the forums there might be able to help. If the colors are coming out dull, my guess would be that the issue is lighting? You could try changing the materials emmissive / diffuse / specular colors - e.g. setting the emissive color to white should make the mesh draw at full brightness even if no light is affecting it.

material.emissiveColor = BABYLON.Color3.White()
levlups commented 5 years ago

it didnt do anything let me show you the issue ,and ill post the issue on babylonjs forum , Im just asking you Andy because you are a more experience babtlonjs user than me I started using babylonjs a week ago because of noa engine , I was a threejs guy pngissue

levlups commented 5 years ago

by the way Andy would you know how to load an .obj file or .glb gltf ,I have been trying for a week never worked

fenomas commented 5 years ago

Sorry, on Babylon I understand the internals but I really don't know anything about high-level stuff like reading in object formats.

However in my experience the easy way to get answers on babylon stuff is to try your code in the playground. If you can reproduce your issue there, just hit the save button and then post that link to the Babylon forum, and then the pros there can very easily see what you did and how to fix it.