目录

ForceLayout示例

目录

示例 代码片段

CSS:

1
2
3
4
5
6
7
#three-canvas{
    position:relative
}

#three-canvas button{
    position:absolute
}

HTML:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
<script src="https://cdn.jsdelivr.net/npm/[email protected]/build/three.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/examples/js/controls/OrbitControls.js"></script>
<script src="https://d3js.org/d3.v6.min.js"></script>
<script
      type="text/javascript"
      src="https://cdn.jsdelivr.net/npm/[email protected]/dist/d3-force-3d.min.js"
></script>

<div id="three-canvas">

<button onclick="onRun()">运行布局</button>
</div>

</div>

JS:

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228

function onTick(){
    console.log(graph)
    
    let positions = pointsGeometry.attributes.position.array
    for(let i = 0;i< positions.length;i+=3){
        positions[i] = nodes[i/3].x/100
        positions[i+1] = nodes[i/3].y/100
        positions[i+2] = 0
    }
   pointsGeometry.attributes.position.needsUpdate = true

linePositions = lineGeometry.attributes.position.array
   graph.links.forEach((l,idx)=>{
    let startNode = l.source
    let endNode = l.target

    linePositions[idx *6] = startNode.x/100;
    linePositions[idx *6 +1] = startNode.y/100;
    linePositions[idx * 6+2] = 0;

    linePositions[idx *6 + 3] = endNode.x/100;
    linePositions[idx *6+ 4] = endNode.y/100;
    linePositions[idx *6+ 5] = 0;
   })
      lineGeometry.attributes.position.needsUpdate = true
}


function onRun(){
    nodes = graph.nodes
    links = graph.links
    simulation = d3.forceSimulation(nodes)
    .force("charge", d3.forceManyBody())
    .force("link", d3.forceLink(links).strength(0.05))
    // .force("center", d3.forceCenter())
    .force("tick", onTick);

    simulation.restart()
}

function lineCloud(graph){
  let points = [];
  let nodes = graph.nodes;
  let links = graph.links.forEach((link) => {
    let startNode = nodes.find((n) => n.id == link.sourceId);
    let endNode = nodes.find((n) => n.id == link.targetId);
    points.push(startNode.position);
    points.push(endNode.position);
  });
  lineGeometry = new THREE.BufferGeometry().setFromPoints(points);
  const material = new THREE.LineBasicMaterial({ color: "#3498db" });
  let lines = new THREE.LineSegments(lineGeometry, material);
  return lines;
}

function pointCloud(graph)
 {
  pointsGeometry = new THREE.BufferGeometry();
  let points = [];
  graph.nodes.forEach((n) => {
    let vertex = n.position;
    points.push(vertex);
  });
  pointsGeometry.setFromPoints(points);

  // let pointsMaterial = new THREE.PointsMaterial({
  //   size: 8,
  //   sizeAttenuation: false,
  //   color: "#92F22A",
  //   map: circle_sprite_aa,
  //   transparent: true,
  //   alphaTest: 0.5
  // });

  let pointsMaterial = new THREE.ShaderMaterial({
    vertexShader: nodeVertextShader,
    fragmentShader: nodeFragmentShader,
    transparent: true,
    alphaTest: 0.1
  });

  return new THREE.Points(pointsGeometry, pointsMaterial);
}


function parseGraph(data) {
        var id = 0;
        var nodes = [];
        var links = [];

        function traval(root, children) {
            if (!children) {
                return;
            }
            let node = {
                id: id,
                property: {
                    name: root.name
                }
            };
            node.position = new THREE.Vector3(
                Math.random() * 5 * 2 - 5,
                Math.random() * 5 * 2 - 5,
                Math.random() * 5 * 2 - 5
            );
            nodes.push(node);
            let neibours = root.children
                .map((c) => {
                    id++;
                    let n = {
                        id: id,
                        name: c.name
                    };
                    n.position = new THREE.Vector3(
                        Math.random() * 5 * 2 - 5,
                        Math.random() * 5 * 2 - 5,
                        Math.random() * 5 * 2 - 5
                    );
                    return {
                        node: n,
                        children: c.children
                    };
                })
                .forEach((c) => {
                    nodes.push(c.node);
                    let link = {
                        id: node.id + "-" + c.node.id,
                        source: node,
                        target: c.node,
                        sourceId: node.id,
                        targetId: c.node.id
                    };
                    links.push(link);
                    traval(c, c.children);
                });
        }

        traval(data, data.children);
        return {
            nodes,
            links
        };
}

var url = window.parent.dataUrl 
var data = fetch(url).then(d=>d.json()).then(d=>{
	graph = parseGraph(d)
	nodeCloud = pointCloud(graph)
	edgeCloud = lineCloud(graph)

	scene.add(nodeCloud);
        scene.add(edgeCloud);
})

var nodeVertextShader = `
precision highp float;
void main() {
    vec3 finalPosition = position;
    vec4 mvPosition = modelViewMatrix * vec4( finalPosition, 1.0 );
    float vSize = 3.0 * ( 50.0 / -mvPosition.z );
    gl_PointSize = vSize;
    gl_Position = projectionMatrix * mvPosition;
} 
`

var nodeFragmentShader = `
void antiAlia(){
  // anti-aliased support
  float len = length(gl_PointCoord- vec2(0.5, 0.5));
  float delta = 0.0, alpha = 1.0;
  delta = fwidth(len);
  alpha = smoothstep(.495-delta, 0.495 + delta, len);
  vec4 bColor = vec4(0.0);
  gl_FragColor = mix(gl_FragColor, bColor, alpha);
  gl_FragColor = gl_FragColor * (1.0 - alpha);
}

void renderCircle(vec4 color){
    float r = length(gl_PointCoord- vec2(0.5, 0.5)), delta = 0.0, alpha = 1.0;
    if (r > .5) {
        discard;
    }
    gl_FragColor = color;
}
void main() {
    vec4 color = vec4(231.0/255.0, 76.0/255.0, 60.0/255.0,1.0);
    float len = length(gl_PointCoord- vec2(0.5, 0.5));
    renderCircle(color);
    antiAlia();
}
`

var scene = new THREE.Scene();

// Create a basic perspective camera
var camera = new THREE.PerspectiveCamera( 75, window.innerWidth/window.innerHeight, 0.1, 1000 );
camera.position.z = 4;


// Create a renderer with Antialiasing
var renderer = new THREE.WebGLRenderer({antialias:true});

// Configure renderer clear color
renderer.setClearColor("#000000");

const controls = new THREE.OrbitControls( camera, renderer.domElement );


// Configure renderer size
let width = d3.select("#three-canvas").node().getBoundingClientRect().width
let height = 400;
 
renderer.setSize(width,height);

// Append Renderer to DOM
let canvas = document.getElementById("three-canvas")
canvas.appendChild( renderer.domElement );

// Render Loop
var render = function () {
  requestAnimationFrame( render );
  controls.update()
  // Render the scene
  renderer.render(scene, camera);
};

render();
运行结果: