1 基础-圣诞树

2021-12-20  本文已影响0人  skoll

简单的,低多边形的松树

1 .即用来熟悉生成器的基础知识,也用来给SLG世界添加东西,这个用来做木头,1-10级地的材料
2 .原文:https://www.html5gamedevs.com/topic/21484-simple-pine-trees/

截屏2021-12-12 下午10.47.46.png

3 .问题

1 .顶点数太多,可以优化
2 .可以描边然后旋转,不用这么麻烦
3 .克隆的时候更麻烦

  var spot = new BABYLON.SpotLight("spot", new BABYLON.Vector3(25, 15, -10), new BABYLON.Vector3(-1, -0.8, 1), 15, 1, scene);
  spot.diffuse = new BABYLON.Color3(1, 1, 1);
  spot.specular = new BABYLON.Color3(0, 0, 0);
  spot.intensity = 0.8;

  var mat = new BABYLON.StandardMaterial("mat1", scene);
  mat.alpha = 1.0;
  mat.diffuseColor = new BABYLON.Color3(0.5, 0.5, 1.0);
  mat.backFaceCulling = false;
  mat.wireframe = false;

  var curvePoints = function(l, t) {
    var path = [];
    var step = l / t;
    var a = 5;
    for (var i = -l/2; i < l/2; i += step ) {
      var t = i  / Math.PI * 2;
      var x =   Math.sin(t) + i;
      path.push(new BABYLON.Vector3(x, 0, 0 ));
     }
    return path;
  };

  var curve = curvePoints(40, 400);
  
  var radiusFunction = function(i, distance) {
    var t = i / Math.PI * 2 / 8;
    var radius =  Math.sin(t) + i / 25;
    return radius;
  };  
  
  var tube = BABYLON.Mesh.CreateTube("tube", curve, 2, 60, radiusFunction, 0, scene, false, BABYLON.Mesh.FRONTSIDE);
  tube.material = mat; 
//这种方式实现

4 .解决了merge的问题,可以显示了,怎么又出来了问题,自己定义的可以显示材质,但是放在粒子系统里面就不行了

const createScene = () => {
    const scene = new BABYLON.Scene(engine);

    const camera = new BABYLON.ArcRotateCamera("ArcRotateCamera", -Math.PI / 2, Math.PI / 2.2, 50, new BABYLON.Vector3(0, 0, 0), scene);
    camera.attachControl(canvas, true);
    const light = new BABYLON.HemisphericLight("light", new BABYLON.Vector3(0, 1, 0), scene);

    const SPS = new BABYLON.SolidParticleSystem("SPS", scene,{useModelMaterial:true});


    var leafMaterial = new BABYLON.StandardMaterial("leafMaterial", scene);
    leafMaterial.diffuseColor = new BABYLON.Color3(0.5, 1, 0.5);
  
    var woodMaterial = new BABYLON.StandardMaterial('wood',scene);
    var woodTexture = new BABYLON.WoodProceduralTexture("text", 512, scene);
    woodTexture.ampScale = 50;
    woodMaterial.diffuseTexture = woodTexture;
  
    var simplePineGenerator = function(canopies, height, woodMaterial, leafMaterial) {
        var curvePoints = function(l, t) {
        var path = [];
        var step = l / t;
        for (var i = 0; i < l; i += step ) {
            path.push(new BABYLON.Vector3(0, i, 0));
            path.push(new BABYLON.Vector3(0, i, 0 ));
         }
        return path;
        };
        
        var nbL = canopies + 1;
        var nbS = height;
        var curve = curvePoints(nbS, nbL);

        var radiusFunction = function (i, distance) {
            var fact = 1;
            if (i % 2 == 0) { fact = .5; }
            var radius =  (nbL * 2 - i - 1) * fact; 
            return radius;
        };  
  
        var leaves = BABYLON.Mesh.CreateTube("tube", curve, 0, 10, radiusFunction, 1, scene);
        var trunk = BABYLON.Mesh.CreateCylinder("trunk", nbS/nbL, nbL*1.5 - nbL/2 - 1, nbL*1.5 - nbL/2 - 1, 12, 1, scene);

        let multiMat=new BABYLON.MultiMaterial('multi',scene)
        multiMat.subMaterials.push(leafMaterial)
        multiMat.subMaterials.push(woodMaterial)

        // leaves.material = leafMaterial;
        // trunk.material = woodMaterial; 
        //   merge之后材料好像也一起合并了

        let mesh=BABYLON.Mesh.MergeMeshes([leaves, trunk],true,true,undefined,true);
        mesh.material=multiMat
        mesh.subMeshes[1].materialIndex=1
        return mesh;
    }

    var tree = simplePineGenerator(7, 30, woodMaterial, leafMaterial);
    SPS.addShape(tree, 10); 


    const mesh = SPS.buildMesh(); // finally builds and displays the SPS mesh
    
    // initiate particles function
    SPS.initParticles = () => {
        for (let p = 0; p < SPS.nbParticles; p++) {
            const particle = SPS.particles[p];
            particle.position.x = BABYLON.Scalar.RandomRange(-50, 50);
            particle.position.y = BABYLON.Scalar.RandomRange(-50, 50);
            particle.position.z = BABYLON.Scalar.RandomRange(-50, 50);
        }
    };

    //Update SPS mesh
    SPS.initParticles();
    SPS.setParticles();

    return scene;
}

解决方式1

1 .把创建的mesh能不能导出,完全整合,或者粒子addSahep的时候不传material,因为一个单独的粒子是可以使用uv,这个时候给树使用uv是可以的
2 .直接导入一个mesh,外部的🌲试试
3 .

上一篇下一篇

猜你喜欢

热点阅读