Mercurial > hg > orthanc-stl
comparison Resources/Nexus/js/nexus_three.js @ 45:967f947014ac nexus
adding experimental support for nexus models
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Tue, 09 Apr 2024 22:13:01 +0200 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
44:111d952e7fa0 | 45:967f947014ac |
---|---|
1 function NexusObject(url, renderer, render, material) { | |
2 var gl = renderer.context; | |
3 var geometry = new THREE.BufferGeometry(); | |
4 var positions = new Float32Array(3); | |
5 | |
6 | |
7 geometry.addAttribute('position', new THREE.BufferAttribute(positions, 3)); | |
8 | |
9 | |
10 if(!material) | |
11 this.autoMaterial = true; | |
12 | |
13 THREE.Mesh.call( this, geometry, material); | |
14 this.frustumCulled = false; | |
15 | |
16 var mesh = this; | |
17 var instance = this.instance = new Nexus.Instance(gl); | |
18 instance.open(url); | |
19 instance.onLoad = function() { | |
20 var s = 1/instance.mesh.sphere.radius; | |
21 var pos = instance.mesh.sphere.center; | |
22 mesh.position.set(-pos[0]*s, -pos[1]*s, -pos[2]*s); | |
23 mesh.scale.set(s, s, s); | |
24 if(mesh.autoMaterial) | |
25 mesh.material = new THREE.MeshLambertMaterial( { color: 0xffffff } ); | |
26 | |
27 if(this.mesh.vertex.normal) { | |
28 var normals = new Float32Array(3); | |
29 geometry.addAttribute( 'normal', new THREE.BufferAttribute(normals, 3)); | |
30 } | |
31 if(this.mesh.vertex.color) { | |
32 var colors = new Float32Array(4); | |
33 geometry.addAttribute( 'color', new THREE.BufferAttribute(colors, 4)); | |
34 if(mesh.autoMaterial) | |
35 mesh.material = new THREE.MeshLambertMaterial({ vertexColors: THREE.VertexColors }); | |
36 } | |
37 | |
38 if(this.mesh.vertex.texCoord) { | |
39 var uv = new Float32Array(2); | |
40 geometry.addAttribute( 'uv', new THREE.BufferAttribute(uv, 2)); | |
41 if(mesh.autoMaterial) { | |
42 var texture = new THREE.DataTexture( new Uint8Array([1, 1, 1]), 1, 1, THREE.RGBFormat ); | |
43 texture.needsUpdate = true; | |
44 mesh.material = new THREE.MeshLambertMaterial( { color: 0xffffff, map: texture } ); | |
45 } | |
46 } | |
47 | |
48 if(this.mesh.face.index) { | |
49 var indices = new Uint32Array(3); | |
50 geometry.setIndex(new THREE.BufferAttribute( indices, 3) ); | |
51 } | |
52 render(); | |
53 }; | |
54 instance.onUpdate = function() { render(); } | |
55 | |
56 this.onAfterRender = function(renderer, scene, camera, geometry, material, group) { | |
57 if(!instance.isReady) return; | |
58 var s = renderer.getSize(); | |
59 instance.updateView([0, 0, s.width, s.height], | |
60 camera.projectionMatrix.elements, | |
61 mesh.modelViewMatrix.elements); | |
62 var program = renderer.context.getParameter(gl.CURRENT_PROGRAM); | |
63 instance.attributes['position'] = renderer.context.getAttribLocation(program, "position"); | |
64 instance.attributes['normal'] = renderer.context.getAttribLocation(program, "normal"); | |
65 instance.attributes['color'] = renderer.context.getAttribLocation(program, "color"); | |
66 instance.attributes['uv'] = renderer.context.getAttribLocation(program, "uv"); | |
67 | |
68 instance.render(); | |
69 } | |
70 } | |
71 | |
72 NexusObject.prototype = Object.create(THREE.Mesh.prototype); | |
73 | |
74 NexusObject.prototype.raycast = function(raycaster, intersects) { | |
75 var instance = this.instance; | |
76 var nexus = instance.mesh; | |
77 if(!nexus.sphere) return; | |
78 var sp = nexus.sphere; | |
79 var c = sp.center; | |
80 var center = new THREE.Vector3(c[0], c[1], c[2]); | |
81 var sphere = new THREE.Sphere(center, sp.radius); | |
82 sphere.applyMatrix4( this.matrixWorld ); | |
83 | |
84 if ( raycaster.ray.intersectsSphere( sphere ) === false ) return; | |
85 //just check the last level spheres. | |
86 if(!nexus.sink) return; | |
87 | |
88 var distance = -1.0; | |
89 for(var i = 0; i < nexus.sink; i++) { | |
90 var patch = nexus.nfirstpatch[i]; | |
91 if(nexus.patches[patch*3] != nexus.sink) | |
92 continue; | |
93 var x = nexus.nspheres[i*5]; | |
94 var y = nexus.nspheres[i*5+1]; | |
95 var z = nexus.nspheres[i*5+2]; | |
96 var r = nexus.nspheres[i*5+4]; //tight radius | |
97 var sphere = new THREE.Sphere(new THREE.Vector3(x, y, z), r); | |
98 sphere.applyMatrix4( this.matrixWorld ); | |
99 if ( raycaster.ray.intersectsSphere( sphere ) != false ) { | |
100 var d = sphere.center.lengthSq(); | |
101 if(distance == -1.0 || d < distance) | |
102 distance = d; | |
103 } | |
104 } | |
105 if(distance == -1.0) return; | |
106 | |
107 intersects.push({ distance: distance, object: this} ); | |
108 } |