Cesium教程番外篇--自定义Geometry（一）
Cesium教程番外篇--自定义Geometry（一）

Cesium教程番外篇--自定义Geometry（一）
• 发表于 4年前
• 阅读 1175
• 收藏 0
• 评论 0

Cesium交流群：115883691，欢迎大家一起来讨论Cesium相关技

``````var TetrahedronGeometry = function(){
var negativeRootTwoOverThree = -Math.sqrt(2.0) / 3.0;
var negativeOneThird = -1.0 / 3.0;
var rootSixOverThree = Math.sqrt(6.0) / 3.0;

//四面体的四个顶点
var positions = new Float64Array(4 * 3);

// position 0
positions[0] = 0.0;
positions[1] = 0.0;
positions[2] = 1.0;

// position 1
positions[3] = 0.0;
positions[4] = (2.0 * Math.sqrt(2.0)) / 3.0;
positions[5] = negativeOneThird;

// position 2
positions[6] = -rootSixOverThree;
positions[7] = negativeRootTwoOverThree;
positions[8] = negativeOneThird;

// position 3
positions[9] = rootSixOverThree;
positions[10] = negativeRootTwoOverThree;
positions[11] = negativeOneThird;

var attributes = new Cesium.GeometryAttributes({
position : new Cesium.GeometryAttribute({
componentDatatype : Cesium.ComponentDatatype.DOUBLE,
componentsPerAttribute : 3,
values : positions
})
});

//顶点索引
var indices = new Uint16Array(4 * 3);

// back triangle
indices[0] = 0;
indices[1] = 1;
indices[2] = 2;

// left triangle
indices[3] = 0;
indices[4] = 2;
indices[5] = 3;

// right triangle
indices[6] = 0;
indices[7] = 3;
indices[8] = 1;

// bottom triangle
indices[9] = 2;
indices[10] = 1;
indices[11] = 3;

//包围球
var boundingSphere = new Cesium.BoundingSphere(new Cesium.Cartesian3(0.0,0.0,0.0),1.0);

var geometry = Cesium.GeometryPipeline.computeNormal(new Cesium.Geometry({
attributes: attributes,
indices: indices,
primitiveType: Cesium.PrimitiveType.TRIANGLES,
boundingSphere: boundingSphere
}));

this.attributes = geometry.attributes;
this.indices = geometry.indices;
this.primitiveType = geometry.primitiveType;
this.boundingSphere = geometry.boundingSphere;
//this.boundingSphere = Cesium.BoundingSphere.fromVertices(positions);
};``````

``````var cesiumWidget = new Cesium.CesiumWidget('cesiumContainer');
var scene = cesiumWidget.scene;
var ellipsoid = cesiumWidget.scene.globe.ellipsoid;

//模型矩阵
var modelMatrix = Cesium.Matrix4.multiplyByUniformScale(
Cesium.Matrix4.multiplyByTranslation(
Cesium.Transforms.eastNorthUpToFixedFrame(ellipsoid.cartographicToCartesian(
Cesium.Cartographic.fromDegrees(-100.0, 40.0))),
new Cesium.Cartesian3(0.0, 0.0, 200000.0)),
500000.0);

//四面体的实例
var instance = new Cesium.GeometryInstance({
geometry : new TetrahedronGeometry(),
modelMatrix : modelMatrix,
attributes : {
color : Cesium.ColorGeometryInstanceAttribute.fromColor(Cesium.Color.WHITE)    //白色
}
});

//加入场景
geometryInstances : instance,
appearance : new Cesium.PerInstanceColorAppearance({
flat : true,
translucent : false
})
}));``````

``````var instance = new Cesium.GeometryInstance({
geometry : Cesium.GeometryPipeline.toWireframe(new TetrahedronGeometry()),
modelMatrix : modelMatrix,
attributes : {
color : Cesium.ColorGeometryInstanceAttribute.fromColor(Cesium.Color.WHITE)
}
});``````

×