3D EngineHello everyone,
I am excited to share with you the first 3D engine in Pine Script! This is a remarkable achievement that will enable traders to explore the possibilities of 3D objects in their technical analysis.
To use this engine, you will need to use a higher time frame (1W or greater) to load the 3D objects in time. The code is straightforward and easy to use, with various input options to customize the object's position, rotation, and scale. You can select between two default 3D objects, Pin or Ball, by changing the 'list' input string.
The 3D engine is based on the vector and mesh data structures, and it includes several utility functions to perform operations such as rotation, translation, and scaling on these structures. It also includes functions to calculate perspective transformation for projecting 3D objects onto a 2D surface, which is the essential step in rendering the object in a 2D chart.
The 'process_scene' function is the main function that converts the 3D object data into 2D polygon data, which can be rendered on the chart. This function uses the 'process_object' function, which applies the object's transformation (position, rotation, and scale) and returns a scaled and rotated mesh that is ready for projection.
At the end of the code, there is a camera object that defines the camera's position, target, and field of view. You can customize this camera object to change the perspective of the rendered object.
To make your own 3D object, you can use the included Python program to convert an OBJ file into the format used by the engine. This program converts the OBJ file into a list of vector4 objects, which can be copy-pasted into the 'pin_vertex_list' array or a similar array. Please note that this process requires some knowledge of Python and the OBJ file format.
Overall, this is a fantastic achievement that opens up new possibilities for technical analysis in Pine Script. I encourage you to try it out and see what you can create with it.
In this code, there are several functions and data structures used to represent and manipulate 3D objects in a simple 3D engine. I will explain each function in detail below:
radians(degree): Converts an angle in degrees to radians.
quat_identity(): Returns a quaternion representing the identity rotation.
quat_from_axis_angle(axis, angle): Creates a quaternion from an axis of rotation and an angle.
quat_multiply(q1, q2): Multiplies two quaternions, q1 and q2, and returns the result.
quat_conjugate(q): Returns the conjugate of a quaternion, q.
quat_rotate(q, v): Rotates a 3D vector, v, by a quaternion, q, and returns the result.
rotate_vector_by_quaternion(v, q): Rotates a 3D vector, v, by a quaternion, q, and returns the result.
scale_vector(vector, constant): Scales a 3D vector by a constant and returns the result.
scale_mesh(mesh_obj, constant): Scales a mesh by a constant and returns the new mesh.
rotate_mesh(mesh_obj, axis, angle): Rotates a mesh around a specified axis by a given angle and returns the new mesh.
quat_translate(src, offset): Translates a 3D vector, src, by an offset and returns the result.
translate_mesh(mesh_obj, offset): Translates a mesh by an offset and returns the new mesh.
perspective_transform(msh, cam): Transforms a 3D mesh into a 2D mesh using a camera.
process_object(obj, cam): Processes a 3D object with a camera and returns a 2D mesh.
size(scene): Calculates the total number of vertices in a scene.
make_poly(scene): Converts a scene of 2D meshes into an array of polygons.
process_scene(scn, cam): Processes an entire scene of 3D objects with a camera and returns an array of polygons.
These functions work together to create a simple 3D engine that can manipulate, transform, and render 3D objects as 2D polygons. The main part of the script defines the camera, vertices, and faces for a pin object, and then uses the functions to process and render the object.
Note that some types are also defined in the code, including vector2, vector3, vector4, face, mesh, mesh2d, location, object, poly4, polyg, and camera. These types represent various mathematical structures and entities used in 3D graphics, such as vectors, faces, meshes, and cameras.
Here is an explanation of the inputs for all the functions in the given code:
radians(degree):
degree: Angle in degrees to be converted to radians.
quat_from_axis_angle(axis, angle):
axis: A vector3 object representing the axis of rotation.
angle: The angle in radians to rotate about the axis.
quat_multiply(q1, q2):
q1, q2: Two quaternion objects to be multiplied.
quat_conjugate(q):
q: A quaternion object whose conjugate is to be calculated.
quat_rotate(q, v):
q: The quaternion representing the rotation.
v: The vector3 object to be rotated.
rotate_vector_by_quaternion(v, q):
v: The vector3 object to be rotated.
q: The quaternion representing the rotation.
scale_vector(vector, constant):
vector: A vector4 object to be scaled.
constant: The scaling factor.
scale_mesh(mesh_obj, constant):
mesh_obj: A mesh object to be scaled.
constant: The scaling factor.
rotate_mesh(mesh_obj, axis, angle):
mesh_obj: A mesh object to be rotated.
axis: A vector3 object representing the axis of rotation.
angle: The angle in radians to rotate the mesh.
quat_translate(src, offset):
src: A vector4 object representing the source position.
offset: A vector3 object representing the translation offset.
translate_mesh(mesh_obj, offset):
mesh_obj: A mesh object to be translated.
offset: A vector3 object representing the translation offset.
perspective_transform(msh, cam):
msh: A mesh object to be transformed.
cam: A camera object used for the transformation.
process_object(obj, cam):
obj: An object containing a mesh and its location.
cam: A camera object used for processing the object.
make_poly(scene):
scene: An array of mesh2d objects to generate polygons from.
process_scene(scn, cam):
scn: An array of objects containing meshes and their locations.
cam: A camera object used for processing the scene.
Note that some of these functions are utility functions or internal functions, and their inputs are derived from other functions or user inputs.
I will make a library eventually.
Graphics
CommonTypesMathLibrary "CommonTypesMath"
Provides a common library source for common types of useful mathematical structures.
Includes: `complex, Vector2, Vector3, Vector4, Quaternion, Segment2, Segment3, Pole, Plane, M32, M44`
complex
Representation of a Complex Number, a complex number `z` is a number in the form `z = x + yi`,
Fields:
re : Real part of the complex number.
im : Imaginary part of the complex number.
Vector2
Representation of a two dimentional vector with components `(x:float,y:float)`.
Fields:
x : Coordinate `x` of the vector.
y : Coordinate `y` of the vector.
Vector3
Representation of a three dimentional vector with components `(x:float,y:float,z:float)`.
Fields:
x : Coordinate `x` of the vector.
y : Coordinate `y` of the vector.
z : Coordinate `z` of the vector.
Vector4
Representation of a four dimentional vector with components `(x:float,y:float,z:float,w:float)`.
Fields:
x : Coordinate `x` of the vector.
y : Coordinate `y` of the vector.
z : Coordinate `z` of the vector.
w : Coordinate `w` of the vector.
Quaternion
Representation of a four dimentional vector with components `(x:float,y:float,z:float,w:float)`.
Fields:
x : Coordinate `x` of the vector.
y : Coordinate `y` of the vector.
z : Coordinate `z` of the vector.
w : Coordinate `w` of the vector, specifies the rotation component.
Segment2
Representation of a line in two dimentional space.
Fields:
origin : Origin coordinates.
target : Target coordinates.
Segment3
Representation of a line in three dimentional space.
Fields:
origin : Origin coordinates.
target : Target coordinates.
Pole
Representation of polar coordinates `(radius:float,angle:float)`.
Fields:
radius : Radius of the pole.
angle : Angle in radians of the pole.
Plane
Representation of a 3D plane.
Fields:
normal : Normal vector of the plane.
distance : Distance of the plane along its normal from the origin.
M32
Representation of a 3x2 matrix.
Fields:
m11 : First element of the first row.
m12 : Second element of the first row.
m21 : First element of the second row.
m22 : Second element of the second row.
m31 : First element of the third row.
m32 : Second element of the third row.
M44
Representation of a 4x4 matrix.
Fields:
m11 : First element of the first row.
m12 : Second element of the first row.
m13 : Third element of the first row.
m14 : fourth element of the first row.
m21 : First element of the second row.
m22 : Second element of the second row.
m23 : Third element of the second row.
m24 : fourth element of the second row.
m31 : First element of the third row.
m32 : Second element of the third row.
m33 : Third element of the third row.
m34 : fourth element of the third row.
m41 : First element of the fourth row.
m42 : Second element of the fourth row.
m43 : Third element of the fourth row.
m44 : fourth element of the fourth row.
watermark_asciiJust for fun... You can make an ascii watermark! The steps are:
1. Convert an image into ascii, e.g. using an online generator.
2. Split the ascii into strings that are no more than 4096 characters (pinescript limit).
3. Stack them together as cells on a table (as shown in the script).
I use code generation for step 2, by splitting the ascii file into chunks, then joining each line with a plus sign to make a large string concatenation statement, which I manually copy/paste into pine. Ideally, you could make this whole script into a template with a couple parameters.
Unfortunately, Pinescript doesn't use a monospaced font, so there will be some distortion. Feature request!