ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Fri, 11 Nov 2016 23:28:50 +0100Best way to import Sage 3D functions to cubes in Unity3D?https://ask.sagemath.org/question/35512/best-way-to-import-sage-3d-functions-to-cubes-in-unity3d/Hello everyone. I'm having a bit of trouble figuring out how to take a 3D function, say something like:
`implicit_plot3d(sqrt(3*x^2+y^2)-z, (x, -30, 30), (y, -30,30), (z, -10,40))`
and have it translated and rendered inside of a transparent cube in Unity3D.
Is there a way to do this? If so, how? I have done some investigation by myself and I know that there is a Python interpreter asset for Unity but would Sage be able to run off of the interpreter?
I'm just very confused.Thu, 10 Nov 2016 22:58:52 +0100https://ask.sagemath.org/question/35512/best-way-to-import-sage-3d-functions-to-cubes-in-unity3d/Answer by FrédéricC for <p>Hello everyone. I'm having a bit of trouble figuring out how to take a 3D function, say something like:</p>
<p><code>implicit_plot3d(sqrt(3*x^2+y^2)-z, (x, -30, 30), (y, -30,30), (z, -10,40))</code> </p>
<p>and have it translated and rendered inside of a transparent cube in Unity3D.</p>
<p>Is there a way to do this? If so, how? I have done some investigation by myself and I know that there is a Python interpreter asset for Unity but would Sage be able to run off of the interpreter?</p>
<p>I'm just very confused.</p>
https://ask.sagemath.org/question/35512/best-way-to-import-sage-3d-functions-to-cubes-in-unity3d/?answer=35518#post-id-35518Did you look at the doc of save ?
sage: var('x,y,z')
sage: P=implicit_plot3d(sqrt(3*x^2+y^2)-z, (x, -30, 30), (y, -30,30), (z, -10,40))
sage: P.save?
This offers several formats, such as .x3d that could be possible inputs for other 3d software.Fri, 11 Nov 2016 08:51:55 +0100https://ask.sagemath.org/question/35512/best-way-to-import-sage-3d-functions-to-cubes-in-unity3d/?answer=35518#post-id-35518Answer by paulmasson for <p>Hello everyone. I'm having a bit of trouble figuring out how to take a 3D function, say something like:</p>
<p><code>implicit_plot3d(sqrt(3*x^2+y^2)-z, (x, -30, 30), (y, -30,30), (z, -10,40))</code> </p>
<p>and have it translated and rendered inside of a transparent cube in Unity3D.</p>
<p>Is there a way to do this? If so, how? I have done some investigation by myself and I know that there is a Python interpreter asset for Unity but would Sage be able to run off of the interpreter?</p>
<p>I'm just very confused.</p>
https://ask.sagemath.org/question/35512/best-way-to-import-sage-3d-functions-to-cubes-in-unity3d/?answer=35535#post-id-35535If you're looking for a WebGL-oriented approach, then use the `json_repr` of a 3D surface. This code
var('x y z')
p = implicit_plot3d(sqrt(3*x^2+y^2)-z, (x, -30, 30), (y, -30,30), (z, -10,40))
p.json_repr(p.default_render_params())
returns an array of vertices, faces and color as can be seen in this [live example](http://sagecell.sagemath.org/?z=eJwlzEEOwiAQQNG9iXeYXWcUGixuvUoJEUwwtJ0CGtrTi-ny5Sf_axN2FTbYOzqfGB4QJo7hGYrhuBTtMK-poL7Ucbhu40ByF4BVgNRKgFbUtB060LK8KXFX9P_177zMJnlOyL3zL_uJpXF2Phm2yU4ZiX68SyZk&lang=sage). The array can be saved to file using Python
f = open('data.json', 'w')
f.write(str(p.json_repr(p.default_render_params())))
f.close()
and imported into Unity. This will work for any surface composed of faces, but not for 3D lines and points.
One catch is that by default Sage returns quadrilateral faces instead of triangles. If Unity can't import quadrilateral faces you'll need to split the quads into triangles beforehand. That's pretty straightforward in either Python or JavaScript.Fri, 11 Nov 2016 23:28:50 +0100https://ask.sagemath.org/question/35512/best-way-to-import-sage-3d-functions-to-cubes-in-unity3d/?answer=35535#post-id-35535