-
-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #45 from rnd-team-dev/r0.16.0
R0.16.0
- Loading branch information
Showing
26 changed files
with
782 additions
and
106 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
""" | ||
Pytorch texture source - game of life. | ||
""" | ||
|
||
import torch | ||
import torch.nn.functional as F | ||
|
||
from plotoptix import TkOptiX | ||
from plotoptix.materials import m_flat | ||
|
||
|
||
class params(): | ||
if torch.cuda.is_available: | ||
device = torch.device('cuda') | ||
dtype = torch.float16 | ||
else: | ||
device = torch.device('cpu') | ||
dtype = torch.float32 | ||
w = torch.tensor( | ||
[[[[1.0,1.0,1.0], [1.0,0.0,1.0], [1.0,1.0,1.0]]]], | ||
dtype=dtype, device=device, requires_grad=False | ||
) | ||
cells = torch.rand((1,1,128,128), dtype=dtype, device=device, requires_grad=False) | ||
cells[cells > 0.995] = 1.0 | ||
cells[cells < 1.0] = 0.0 | ||
tex2D = torch.unsqueeze(cells[0, 0].type(torch.float32), -1).expand(-1, -1, 4).contiguous() | ||
|
||
|
||
# Update texture data with a simple "game of life" rules. | ||
def compute(rt, delta): | ||
params.cells = F.conv2d(params.cells, weight=params.w, stride=1, padding=1) | ||
params.cells[params.cells < 2.0] = 0.0 | ||
params.cells[params.cells > 3.0] = 0.0 | ||
params.cells[params.cells != 0.0] = 1.0 | ||
|
||
# Conversion to float32 and to contiguous memoty layout is ensured by plotoptix, | ||
# though you may wamt to make it explicit like here, eg for performance reasons. | ||
params.tex2D = torch.unsqueeze(params.cells[0, 0].type(torch.float32), -1).expand(-1, -1, 4).contiguous() | ||
|
||
|
||
# Copy texture data to plotoptix scene. | ||
def update_data(rt): | ||
rt.set_torch_texture_2d("tex2d", params.tex2D, refresh=True) | ||
|
||
|
||
def main(): | ||
rt = TkOptiX( | ||
on_scene_compute=compute, | ||
on_rt_completed=update_data | ||
) | ||
rt.set_param(min_accumulation_step=1) | ||
rt.set_background(0) | ||
rt.set_ambient(0) | ||
|
||
# NOTE: pytorch features are not enabled by default. You need | ||
# to call this method before using anything related to pytorch. | ||
rt.enable_torch() | ||
|
||
rt.set_torch_texture_2d("tex2d", params.tex2D, addr_mode="Clamp", filter_mode="Nearest") | ||
m_flat["ColorTextures"] = [ "tex2d" ] | ||
rt.setup_material("flat", m_flat) | ||
|
||
rt.setup_camera("cam1", eye=[0, 0, 3], target=[0, 0, 0], fov=35, glock=True) | ||
|
||
rt.set_data("plane", geom="Parallelograms", mat="flat", | ||
pos=[-1, -1, 0], u=[2, 0, 0], v=[0, 2, 0], c=0.9 | ||
) | ||
|
||
rt.start() | ||
|
||
if __name__ == '__main__': | ||
main() | ||
|
64 changes: 64 additions & 0 deletions
64
examples/2_animations_and_callbacks/6_direct_data_update.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
""" | ||
Direct update of geometry data on device. | ||
This example uses numpy arrays, but pytorch tensors are supported as well, | ||
just note that rt.enable_torch() call is required before using pytorch. | ||
""" | ||
|
||
import numpy as np | ||
|
||
from plotoptix import TkOptiX | ||
from plotoptix.utils import simplex | ||
|
||
|
||
class params(): | ||
n = 100 | ||
rx = (-20, 20) | ||
r = 0.85 * 0.5 * (rx[1] - rx[0]) / (n - 1) | ||
|
||
x = np.linspace(rx[0], rx[1], n) | ||
z = np.linspace(rx[0], rx[1], n) | ||
X, Z = np.meshgrid(x, z) | ||
|
||
data = np.stack([X.flatten(), np.zeros(n*n), Z.flatten()], axis=1) | ||
t = 0 | ||
|
||
# Compute updated geometry data. | ||
def compute(rt, delta): | ||
row = np.ones((params.data.shape[0], 1)) | ||
xn = simplex(np.concatenate([params.data, params.t * row], axis=1)) | ||
yn = simplex(np.concatenate([params.data, (params.t + 20) * row], axis=1)) | ||
zn = simplex(np.concatenate([params.data, (params.t - 20) * row], axis=1)) | ||
dv = np.stack([xn, yn, zn], axis=1) | ||
params.data += 0.02 * dv | ||
params.t += 0.05 | ||
|
||
# Fast copy to geometry buffer on device, without making a host copy. | ||
def update_data(rt): | ||
rt.update_raw_data("balls", pos=params.data) | ||
|
||
|
||
def main(): | ||
rt = TkOptiX( | ||
on_scene_compute=compute, | ||
on_rt_completed=update_data | ||
) | ||
rt.set_param(min_accumulation_step=10, max_accumulation_frames=16) | ||
rt.set_background(0) | ||
rt.set_ambient(0) | ||
|
||
rt.setup_camera("cam1", cam_type="ThinLens", eye=[3.5, 1.27, 3.5], target=[0, 0, 0], fov=30, glock=True) | ||
rt.setup_light("light1", pos=[4, 5, 5], color=18, radius=1.0) | ||
|
||
exposure = 1.0; gamma = 2.2 | ||
rt.set_float("tonemap_exposure", exposure) | ||
rt.set_float("tonemap_gamma", gamma) | ||
rt.add_postproc("Gamma") | ||
|
||
rt.set_data("balls", pos=params.data, c=0.82, r=params.r) | ||
|
||
rt.start() | ||
|
||
if __name__ == '__main__': | ||
main() | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,8 +12,8 @@ | |
|
||
__author__ = "Robert Sulej, R&D Team <[email protected]>" | ||
__status__ = "beta" | ||
__version__ = "0.15.1" | ||
__date__ = "16 March 2023" | ||
__version__ = "0.16.0" | ||
__date__ = "17 May 2023" | ||
|
||
import logging | ||
|
||
|
Oops, something went wrong.