I have a <circle>
element within an SVG file, where I've used a <radialGradient>
to create the appearance of it being a sphere:
<svg version="1.1" id="sphere_svg" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" width="640px" height="640px" viewBox="0 0 640 640" enable-background="new 0 0 640 640" xml:space="preserve">
<defs>
<radialGradient id="sphere_gradient" cx="292.3262" cy="287.4077" r="249.2454" fx="147.7949" fy="274.5532" gradientTransform="matrix(1.0729 0 0 1.0729 -23.3359 -23.3359)" gradientUnits="userSpaceOnUse">
<stop id="sphere_gradient_0" offset="0" style="stop-color:#F37D7F"/>
<stop id="sphere_gradient_1" offset="0.4847" style="stop-color:#ED1F24"/>
<stop id="sphere_gradient_2" offset="1" style="stop-color:#7E1416"/>
</radialGradient>
</defs>
<circle fill="url(#sphere_gradient)" cx="320" cy="320" r="320"/>
</svg>
The visual representation is showcased here:
To take this concept further into a three.js environment using a WebGLRenderer
, I rely on Gabe Lerner's canvg
library:
/* The svg asset containing the circle element */
var red_svg_html = new String($('#sphere_asset').html());
var red_svg_canvas = document.createElement("canvas");
canvg(red_svg_canvas, red_svg_html);
var red_svg_texture = new THREE.Texture(red_svg_canvas);
var red_particles = new THREE.Geometry();
var red_particle_material = new THREE.PointCloudMaterial({
map: red_svg_texture,
transparent: true,
size: 0.15,
alphaTest: 0.10
});
var red_particle_count = 25;
for (var p = 0; p < red_particle_count; p++) {
var pX = 0.9 * (Math.random() - 0.5),
pY = 0.9 * (Math.random() - 0.5),
pZ = 0.9 * (Math.random() - 0.5),
red_particle = new THREE.Vector3(pX, pY, pZ);
red_particles.vertices.push(red_particle);
}
var red_particle_system = new THREE.PointCloud(red_particles, red_particle_material);
scene.add(red_particle_system);
The transition from WebGLRenderer
to SVGRenderer
aims to offer users control over orientation for exporting vector images (SVG or PDF) suitable for high-quality publications.
In my attempts utilizing three.js' SVG sandbox example as a foundation, I experimented with multiple methods but faced challenges. Any advice from experienced three.js users would be greatly appreciated.
Initially, I tried rendering PNG images through canvg
and integrating them into <image>
nodes:
// Code snippet
No results were visible within the viewbox.
Another approach incorporated THREE.Sprite
objects along with canvg
and THREE.Texture
:
// Additional code segment
While there was slight progress, white opaque boxes replaced the spheres in the output.
A third method involved creating nested <svg>
elements linked to a referenced <radialGradient>
, failing to render any nodes successfully:
// More code examples
Changing the fill
attribute provided some insight but fell short compared to desired outcomes.
In conclusion, is there a feasible and efficient way to generate spherical entities or similar rounded particles within space using a SVGRenderer
in conjunction with three.js?